Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow existing namespaces to be used #49

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions internal/clients/kube/apply_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import (
"context"
"encoding/json"
"fmt"

Check failure on line 6 in internal/clients/kube/apply_client.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed with -local github.com/crossplane/provider-template (goimports)
"github.com/crossplane/crossplane-runtime/pkg/logging"
"k8s.io/apimachinery/pkg/types"
"strings"

"k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -21,6 +24,7 @@
dynamicClient dynamic.Interface
clientset kubernetes.Interface
discoveryRestMapper meta.RESTMapper
logger logging.Logger
}

type applyObject struct {
Expand All @@ -29,7 +33,7 @@
unstructuredObj map[string]interface{}
}

func NewApplyClient(dynamicClient dynamic.Interface, clientset kubernetes.Interface) (*ApplyClient, error) {
func NewApplyClient(dynamicClient dynamic.Interface, clientset kubernetes.Interface, logger logging.Logger) (*ApplyClient, error) {
groupResources, err := restmapper.GetAPIGroupResources(clientset.Discovery())
if err != nil {
return &ApplyClient{}, fmt.Errorf("error setting up API discovery for dynamic client: %w", err)
Expand All @@ -38,6 +42,7 @@
return &ApplyClient{
dynamicClient: dynamicClient,
clientset: clientset,
logger: logger,
discoveryRestMapper: restmapper.NewDiscoveryRESTMapper(groupResources),
}, nil
}
Expand All @@ -62,6 +67,8 @@
return err
}

a.logger.Debug("Applying k8s object", "name", applyObject.name, "gvk", gvk)

if delete {
err = a.deleteObject(ctx, mapping, applyObject)
if errors.IsNotFound(err) {
Expand Down Expand Up @@ -132,19 +139,37 @@

func (a ApplyClient) applyObject(ctx context.Context, mapping *meta.RESTMapping, applyObject applyObject) error {
if isClusterScopedResource(mapping.Resource.Resource) {
// Don't risk overwriting a namespace if it already exists
if mapping.Resource.Resource == "namespaces" {
_, err := a.dynamicClient.Resource(mapping.Resource).Get(ctx, applyObject.name, v1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
_, err = a.dynamicClient.Resource(mapping.Resource).Apply(ctx, applyObject.name, &unstructured.Unstructured{Object: applyObject.unstructuredObj}, v1.ApplyOptions{FieldManager: "application/apply-patch"})
return err
}

return err
}

// Object already exists, just patch the namespace metadata.
metadata := applyObject.unstructuredObj["metadata"]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a logger.info for this case to be able to identify this in "normal" logs?

patchBytes, err := json.Marshal(metadata)
if err != nil {
return err
}

_, err = a.dynamicClient.Resource(mapping.Resource).Patch(ctx, applyObject.name, types.StrategicMergePatchType, patchBytes, v1.PatchOptions{FieldManager: "application/apply-patch"})
return err
}

_, err := a.dynamicClient.Resource(mapping.Resource).Apply(ctx, applyObject.name, &unstructured.Unstructured{Object: applyObject.unstructuredObj}, v1.ApplyOptions{FieldManager: "application/apply-patch"})
return err
}

_, err := a.dynamicClient.Resource(mapping.Resource).Namespace(applyObject.namespace).Apply(ctx, applyObject.name, &unstructured.Unstructured{Object: applyObject.unstructuredObj}, v1.ApplyOptions{FieldManager: "application/apply-patch"})

return err
}

func isClusterScopedResource(resource string) bool {
if resource == "namespaces" || resource == "clusterroles" || resource == "clusterrolebindings" {
return true
}

return false
return resource == "namespaces" || resource == "clusterroles" || resource == "clusterrolebindings"
}
5 changes: 3 additions & 2 deletions internal/clients/kube/apply_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"context"
"github.com/crossplane/crossplane-runtime/pkg/logging"

Check failure on line 5 in internal/clients/kube/apply_client_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed with -local github.com/crossplane/provider-template (goimports)
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -17,15 +18,15 @@
)

func TestApplyClient_EmptyManifests(t *testing.T) {
applyClient, err := kube.NewApplyClient(dynamic.NewSimpleDynamicClient(scheme.Scheme), fake.NewSimpleClientset())
applyClient, err := kube.NewApplyClient(dynamic.NewSimpleDynamicClient(scheme.Scheme), fake.NewSimpleClientset(), logging.NewNopLogger())
require.NoError(t, err)

err = applyClient.ApplyManifests(ctx, "", false)
require.NoError(t, err)
}

func TestApplyClient_ApplyManifestsInvalidKindErr(t *testing.T) {
applyClient, err := kube.NewApplyClient(dynamic.NewSimpleDynamicClient(scheme.Scheme), fake.NewSimpleClientset())
applyClient, err := kube.NewApplyClient(dynamic.NewSimpleDynamicClient(scheme.Scheme), fake.NewSimpleClientset(), logging.NewNopLogger())
require.NoError(t, err)

err = applyClient.ApplyManifests(ctx, "apiVersion: v1\nkind: InvalidKind\nmetadata:\n name: test-pod", false)
Expand Down
8 changes: 7 additions & 1 deletion internal/controller/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,17 @@ func (c *External) Create(ctx context.Context, mg resource.Managed) (managed.Ext
}

if managedCluster.Spec.ForProvider.EnableInClusterKubeConfig || managedCluster.Spec.ForProvider.KubeConfigSecretRef.Name != "" {
c.logger.Debug("Retrieving cluster manifests....")
clusterManifests, err := c.client.GetClusterManifests(ctx, managedCluster.Spec.ForProvider.InstanceID, managedCluster.Spec.ForProvider.Name)
if err != nil {
return managed.ExternalCreation{}, fmt.Errorf("could not get cluster manifests to apply: %w", err)
}

c.logger.Debug("Applying cluster manifests",
"clusterName", managedCluster.Name,
"instanceID", managedCluster.Spec.ForProvider.InstanceID,
)
c.logger.Debug(clusterManifests)
err = c.applyClusterManifests(ctx, *managedCluster, clusterManifests, false)
if err != nil {
return managed.ExternalCreation{}, fmt.Errorf("could not apply cluster manifests: %w", err)
Expand Down Expand Up @@ -358,7 +364,7 @@ func (c *External) applyClusterManifests(ctx context.Context, managedCluster v1a
return fmt.Errorf("error creating typed client: %w", err)
}

applyClient, err := kube.NewApplyClient(dynamicClient, clientset)
applyClient, err := kube.NewApplyClient(dynamicClient, clientset, c.logger)
if err != nil {
return fmt.Errorf("error creating apply client: %w", err)
}
Expand Down
Loading