From 5cc8a706501a0ea3f57099c25d785c991b579092 Mon Sep 17 00:00:00 2001 From: Tanvir Tatla Date: Thu, 15 Feb 2024 11:43:44 -0800 Subject: [PATCH] set clusterscoped option when namespace is empty --- pkg/clients/kubernetes/unauth.go | 5 +++++ pkg/clients/kubernetes/unauth_test.go | 9 +++++++++ pkg/workflows/create_prep.go | 15 +++------------ pkg/workflows/create_prep_test.go | 14 ++++++++------ pkg/workflows/management/create_test.go | 4 +++- pkg/workflows/workload/create_test.go | 4 +++- 6 files changed, 31 insertions(+), 20 deletions(-) diff --git a/pkg/clients/kubernetes/unauth.go b/pkg/clients/kubernetes/unauth.go index eebaaf80da03d..1c38d9635a5af 100644 --- a/pkg/clients/kubernetes/unauth.go +++ b/pkg/clients/kubernetes/unauth.go @@ -8,6 +8,7 @@ import ( "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/utils/pointer" "sigs.k8s.io/controller-runtime/pkg/client/apiutil" ) @@ -41,6 +42,10 @@ func (c *UnAuthClient) Get(ctx context.Context, name, namespace, kubeconfig stri return fmt.Errorf("getting kubernetes resource: %v", err) } + if namespace == "" { + return c.kubectl.Get(ctx, resourceType, kubeconfig, obj, &KubectlGetOptions{Name: name, ClusterScoped: pointer.Bool(true)}) + } + return c.kubectl.Get(ctx, resourceType, kubeconfig, obj, &KubectlGetOptions{Name: name, Namespace: namespace}) } diff --git a/pkg/clients/kubernetes/unauth_test.go b/pkg/clients/kubernetes/unauth_test.go index 96b31a77e65af..0e271da6ed355 100644 --- a/pkg/clients/kubernetes/unauth_test.go +++ b/pkg/clients/kubernetes/unauth_test.go @@ -9,6 +9,7 @@ import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/utils/pointer" clusterapiv1 "sigs.k8s.io/cluster-api/api/v1beta1" controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1" "sigs.k8s.io/controller-runtime/pkg/client" @@ -62,6 +63,14 @@ func TestUnAuthClientGetSuccess(t *testing.T) { Name: tt.name, Namespace: tt.namespace, } + + if tt.namespace == "" { + o = &kubernetes.KubectlGetOptions{ + Name: tt.name, + ClusterScoped: pointer.Bool(true), + } + } + kubectl.EXPECT().Get(ctx, tt.wantResourceType, kubeconfig, tt.obj, o) c := kubernetes.NewUnAuthClient(kubectl) diff --git a/pkg/workflows/create_prep.go b/pkg/workflows/create_prep.go index 1d55200a37597..bba782c315137 100644 --- a/pkg/workflows/create_prep.go +++ b/pkg/workflows/create_prep.go @@ -4,6 +4,7 @@ import ( "context" corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "github.com/aws/eks-anywhere/pkg/workflows/interfaces" @@ -16,19 +17,9 @@ func CreateNamespaceIfNotPresent(ctx context.Context, namespace, kubeconfig stri return err } - namespaces := &corev1.NamespaceList{Items: []corev1.Namespace{{ObjectMeta: metav1.ObjectMeta{Name: "default"}}}} - if err := client.List(ctx, namespaces); err != nil { + if err := client.Get(ctx, namespace, "", &corev1.Namespace{}); err != nil && !errors.IsNotFound(err) { return err - } - - found := false - for _, ns := range namespaces.Items { - if ns.Name == namespace { - found = true - } - } - - if found { + } else if err == nil { return nil } diff --git a/pkg/workflows/create_prep_test.go b/pkg/workflows/create_prep_test.go index da6f315eba769..a5df25c1bc921 100644 --- a/pkg/workflows/create_prep_test.go +++ b/pkg/workflows/create_prep_test.go @@ -7,7 +7,9 @@ import ( "github.com/golang/mock/gomock" corev1 "k8s.io/api/core/v1" + apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" clientmocks "github.com/aws/eks-anywhere/pkg/clients/kubernetes/mocks" "github.com/aws/eks-anywhere/pkg/workflows" @@ -46,13 +48,13 @@ func newNamespace(name string) *corev1.Namespace { } } -func TestCreateNamespaceSuccess(t *testing.T) { +func TestCreateNamespaceNotExistsSuccess(t *testing.T) { test := newCreatePrepTest(t) kubeconfig := "testpath" namespace := "test-ns" test.clientFactory.EXPECT().BuildClientFromKubeconfig(kubeconfig).Return(test.client, nil) - test.client.EXPECT().List(test.ctx, gomock.AssignableToTypeOf(&corev1.NamespaceList{})).Return(nil) + test.client.EXPECT().Get(test.ctx, namespace, "", &corev1.Namespace{}).Return(apierrors.NewNotFound(schema.GroupResource{Group: "", Resource: ""}, "")) test.client.EXPECT().Create(test.ctx, newNamespace(namespace)).Return(nil) err := workflows.CreateNamespaceIfNotPresent(test.ctx, namespace, kubeconfig, test.clientFactory) @@ -61,13 +63,13 @@ func TestCreateNamespaceSuccess(t *testing.T) { } } -func TestCreateNamespaceAlreadyExists(t *testing.T) { +func TestCreateNamespaceAlreadyExistsSuccess(t *testing.T) { test := newCreatePrepTest(t) kubeconfig := "testpath" namespace := "default" test.clientFactory.EXPECT().BuildClientFromKubeconfig(kubeconfig).Return(test.client, nil) - test.client.EXPECT().List(test.ctx, gomock.AssignableToTypeOf(&corev1.NamespaceList{})).Return(nil) + test.client.EXPECT().Get(test.ctx, namespace, "", &corev1.Namespace{}).Return(nil) err := workflows.CreateNamespaceIfNotPresent(test.ctx, namespace, kubeconfig, test.clientFactory) if err != nil { @@ -95,7 +97,7 @@ func TestCreateNamespaceGetNamespaceFail(t *testing.T) { namespace := "test-ns" test.clientFactory.EXPECT().BuildClientFromKubeconfig(kubeconfig).Return(test.client, nil) - test.client.EXPECT().List(test.ctx, gomock.AssignableToTypeOf(&corev1.NamespaceList{})).Return(fmt.Errorf("")) + test.client.EXPECT().Get(test.ctx, namespace, "", &corev1.Namespace{}).Return(fmt.Errorf("")) err := workflows.CreateNamespaceIfNotPresent(test.ctx, namespace, kubeconfig, test.clientFactory) @@ -110,7 +112,7 @@ func TestCreateNamespaceFail(t *testing.T) { namespace := "test-ns" test.clientFactory.EXPECT().BuildClientFromKubeconfig(kubeconfig).Return(test.client, nil) - test.client.EXPECT().List(test.ctx, gomock.AssignableToTypeOf(&corev1.NamespaceList{})).Return(nil) + test.client.EXPECT().Get(test.ctx, namespace, "", &corev1.Namespace{}).Return(apierrors.NewNotFound(schema.GroupResource{Group: "", Resource: ""}, "")) test.client.EXPECT().Create(test.ctx, newNamespace(namespace)).Return(fmt.Errorf("")) err := workflows.CreateNamespaceIfNotPresent(test.ctx, namespace, kubeconfig, test.clientFactory) diff --git a/pkg/workflows/management/create_test.go b/pkg/workflows/management/create_test.go index a63915b648f3c..189966fb93465 100644 --- a/pkg/workflows/management/create_test.go +++ b/pkg/workflows/management/create_test.go @@ -8,7 +8,9 @@ import ( "github.com/golang/mock/gomock" corev1 "k8s.io/api/core/v1" + apierrors "k8s.io/apimachinery/pkg/api/errors" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" "github.com/aws/eks-anywhere/internal/test" "github.com/aws/eks-anywhere/pkg/api/v1alpha1" @@ -270,7 +272,7 @@ func (c *createTestSetup) expectCreateNamespace() { }, ObjectMeta: v1.ObjectMeta{Name: n}, } - c.client.EXPECT().List(c.ctx, gomock.AssignableToTypeOf(&corev1.NamespaceList{})).MaxTimes(2) + c.client.EXPECT().Get(c.ctx, n, "", &corev1.Namespace{}).Return(apierrors.NewNotFound(schema.GroupResource{Group: "", Resource: ""}, "")).MaxTimes(2) c.client.EXPECT().Create(c.ctx, ns).MaxTimes(2) } diff --git a/pkg/workflows/workload/create_test.go b/pkg/workflows/workload/create_test.go index fd03448aa3a7e..ee0c145a6ac21 100644 --- a/pkg/workflows/workload/create_test.go +++ b/pkg/workflows/workload/create_test.go @@ -9,7 +9,9 @@ import ( "github.com/golang/mock/gomock" corev1 "k8s.io/api/core/v1" + apierrors "k8s.io/apimachinery/pkg/api/errors" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" "github.com/aws/eks-anywhere/internal/test" "github.com/aws/eks-anywhere/pkg/api/v1alpha1" @@ -126,7 +128,7 @@ func (c *createTestSetup) expectCreateNamespace() { }, ObjectMeta: v1.ObjectMeta{Name: n}, } - c.client.EXPECT().List(c.ctx, gomock.AssignableToTypeOf(&corev1.NamespaceList{})).MaxTimes(2) + c.client.EXPECT().Get(c.ctx, n, "", &corev1.Namespace{}).Return(apierrors.NewNotFound(schema.GroupResource{Group: "", Resource: ""}, "")).MaxTimes(2) c.client.EXPECT().Create(c.ctx, ns).MaxTimes(2) }