Skip to content

Commit

Permalink
set clusterscoped option when namespace is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
tatlat committed Feb 15, 2024
1 parent 4307954 commit 5cc8a70
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 20 deletions.
5 changes: 5 additions & 0 deletions pkg/clients/kubernetes/unauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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})
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/clients/kubernetes/unauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 3 additions & 12 deletions pkg/workflows/create_prep.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}

Expand Down
14 changes: 8 additions & 6 deletions pkg/workflows/create_prep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion pkg/workflows/management/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/workflows/workload/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 5cc8a70

Please sign in to comment.