Skip to content

Commit

Permalink
Add support for creating non namespaced resources
Browse files Browse the repository at this point in the history
This change will ensure we find out whether a resource
is namespaced or not before attempting to create it
  • Loading branch information
michaelhtm committed Oct 7, 2024
1 parent 8b899c1 commit 5b59284
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
12 changes: 10 additions & 2 deletions internal/controller/instance/controller_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,16 @@ func (igr *InstanceGraphReconciler) reconcileResource(ctx context.Context, resou
rUnstructured := igr.runtime.Resources[resourceID]

gvr := k8smetadata.GVKtoGVR(resourceMeta.GroupVersionKind)
namespace := igr.getResourceNamespace(resourceID)
rc := igr.client.Resource(gvr).Namespace(namespace)

var rc dynamic.ResourceInterface
var namespace string
if igr.rg.Resources[resourceID].Namespaced {
namespace = igr.getResourceNamespace(resourceID)
rc = igr.client.Resource(gvr).Namespace(namespace)
} else {
rc = igr.client.Resource(gvr)
namespace = "non-namespaced"
}

log.V(1).Info("Checking resource existence", "namespace", namespace, "name", rUnstructured.GetName())
observed, err := rc.Get(ctx, rUnstructured.GetName(), metav1.GetOptions{})
Expand Down
23 changes: 22 additions & 1 deletion internal/resourcegroup/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ import (
extv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
k8sSchema "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/apiserver/pkg/cel/openapi/resolver"
"k8s.io/client-go/discovery"
"k8s.io/client-go/rest"

"github.com/aws-controllers-k8s/symphony/api/v1alpha1"
Expand All @@ -40,7 +42,7 @@ import (
func NewResourceGroupBuilder(
clientConfig *rest.Config,
) (*GraphBuilder, error) {
schemaResolver, err := schema.NewCombinedResolver(clientConfig)
schemaResolver, dc, err := schema.NewCombinedResolver(clientConfig)
if err != nil {
return nil, fmt.Errorf("failed to create schema resolver: %w", err)
}
Expand All @@ -50,13 +52,15 @@ func NewResourceGroupBuilder(
rgBuilder := &GraphBuilder{
resourceEmulator: resourceEmulator,
schemaResolver: schemaResolver,
discoveryClient: dc,
}
return rgBuilder, nil
}

type GraphBuilder struct {
schemaResolver resolver.SchemaResolver
resourceEmulator *emulator.Emulator
discoveryClient *discovery.DiscoveryClient
}

func (b *GraphBuilder) NewResourceGroup(rg *v1alpha1.ResourceGroup) (*ResourceGroup, error) {
Expand Down Expand Up @@ -91,6 +95,20 @@ func (b *GraphBuilder) NewResourceGroup(rg *v1alpha1.ResourceGroup) (*ResourceGr
// 3. Extract CEL expressions from the schema.
// 4. Build the resource object.

namespacedResources := map[k8sSchema.GroupVersionKind]bool{}

apiResourceList, err := b.discoveryClient.ServerPreferredNamespacedResources()
if err != nil {
return nil, fmt.Errorf("failed to retrieve Kubernetes namespaced resources: %w", err)
}

for _, resourceList := range apiResourceList {
for _, r := range resourceList.APIResources {
gvk := k8sSchema.FromAPIVersionAndKind(resourceList.GroupVersion, r.Kind)
namespacedResources[gvk] = r.Namespaced
}
}

// we'll also store the resources in a map for easy access later.
resources := make(map[string]*Resource)
for _, rgResource := range resourceGroupCR.Spec.Resources {
Expand Down Expand Up @@ -157,13 +175,16 @@ func (b *GraphBuilder) NewResourceGroup(rg *v1alpha1.ResourceGroup) (*ResourceGr
}
}

_, isNamespaced := namespacedResources[gvk]

resources[rgResource.Name] = &Resource{
ID: rgResource.Name,
GroupVersionKind: gvk,
Schema: resourceSchema,
EmulatedObject: emulatedResource,
OriginalObject: unstructuredResource,
Variables: resourceVariables,
Namespaced: isNamespaced,
}
}

Expand Down
1 change: 1 addition & 0 deletions internal/resourcegroup/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Resource struct {
Variables []*ResourceVariable

Dependencies []string
Namespaced bool
}

func (r *Resource) HasDependency(dep string) bool {
Expand Down
6 changes: 3 additions & 3 deletions internal/resourcegroup/schema/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (
)

// NewCombinedResolver creates a new schema resolver that can resolve both core and client types.
func NewCombinedResolver(clientConfig *rest.Config) (resolver.SchemaResolver, error) {
func NewCombinedResolver(clientConfig *rest.Config) (resolver.SchemaResolver, *discovery.DiscoveryClient, error) {
discoveryClient, err := discovery.NewDiscoveryClientForConfig(clientConfig)
if err != nil {
return nil, err
return nil, nil, err
}

// ClientResolver is a resolver that uses the discovery client to resolve
Expand All @@ -46,5 +46,5 @@ func NewCombinedResolver(clientConfig *rest.Config) (resolver.SchemaResolver, er
// Combine the two resolvers to create a single resolver that can resolve
// both core and client types.
combinedResolver := coreResolver.Combine(clientResolver)
return combinedResolver, nil
return combinedResolver, discoveryClient, nil
}

0 comments on commit 5b59284

Please sign in to comment.