Skip to content

Commit

Permalink
List namespaces instead of get due to scope
Browse files Browse the repository at this point in the history
  • Loading branch information
tatlat committed Feb 15, 2024
1 parent 379dbca commit 5b16bc2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmd/eksctl-anywhere/cmd/createcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func (cc *createClusterOptions) createCluster(cmd *cobra.Command, _ []string) er
deps.EksdInstaller,
deps.PackageInstaller,
deps.ClusterCreator,
deps.UnAuthKubeClient,
deps.UnAuthKubectlClient,
)
err = createWorkloadCluster.Run(ctx, clusterSpec, createValidations)

Expand Down
19 changes: 17 additions & 2 deletions pkg/workflows/create_prep.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ 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 @@ -17,11 +16,27 @@ func CreateNamespaceIfNotPresent(ctx context.Context, namespace, kubeconfig stri
return err
}

if err := client.Get(ctx, namespace, "", &corev1.Namespace{}); err != nil && !errors.IsNotFound(err) {
namespaces := &corev1.NamespaceList{Items: []corev1.Namespace{{ObjectMeta: metav1.ObjectMeta{Name: "default"}}}}
if err := client.List(ctx, namespaces); err != nil {
return err
}

var found bool = false

Check warning on line 24 in pkg/workflows/create_prep.go

View workflow job for this annotation

GitHub Actions / lint

var-declaration: should omit type bool from declaration of var found; it will be inferred from the right-hand side (revive)
for _, ns := range namespaces.Items {
if ns.Name == namespace {
found = true
}
}

if found {
return nil
}

ns := &corev1.Namespace{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Namespace",
},
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
},
Expand Down
32 changes: 24 additions & 8 deletions pkg/workflows/create_prep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import (

"github.com/golang/mock/gomock"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

clientmocks "github.com/aws/eks-anywhere/pkg/clients/kubernetes/mocks"
"github.com/aws/eks-anywhere/pkg/workflows"
Expand Down Expand Up @@ -38,19 +36,23 @@ func newCreatePrepTest(t *testing.T) *createPrepTestSetup {

func newNamespace(name string) *corev1.Namespace {
return &corev1.Namespace{
ObjectMeta: v1.ObjectMeta{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Namespace",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}
}

func TestCreateNamespaceNotExists(t *testing.T) {
func TestCreateNamespaceSuccess(t *testing.T) {
test := newCreatePrepTest(t)
kubeconfig := "testpath"
namespace := "test-ns"

test.clientFactory.EXPECT().BuildClientFromKubeconfig(kubeconfig).Return(test.client, nil)
test.client.EXPECT().Get(test.ctx, namespace, "", &corev1.Namespace{}).Return(errors.NewNotFound(schema.GroupResource{}, ""))
test.client.EXPECT().List(test.ctx, gomock.AssignableToTypeOf(&corev1.NamespaceList{})).Return(nil)
test.client.EXPECT().Create(test.ctx, newNamespace(namespace)).Return(nil)

err := workflows.CreateNamespaceIfNotPresent(test.ctx, namespace, kubeconfig, test.clientFactory)
Expand All @@ -59,6 +61,20 @@ func TestCreateNamespaceNotExists(t *testing.T) {
}
}

func TestCreateNamespaceAlreadyExists(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)

err := workflows.CreateNamespaceIfNotPresent(test.ctx, namespace, kubeconfig, test.clientFactory)
if err != nil {
t.Fatalf("Expected nil, but got %v", err)
}
}

func TestCreateNamespaceBuildClientFail(t *testing.T) {
test := newCreatePrepTest(t)
kubeconfig := "testpath"
Expand All @@ -79,7 +95,7 @@ func TestCreateNamespaceGetNamespaceFail(t *testing.T) {
namespace := "test-ns"

test.clientFactory.EXPECT().BuildClientFromKubeconfig(kubeconfig).Return(test.client, nil)
test.client.EXPECT().Get(test.ctx, namespace, "", &corev1.Namespace{}).Return(fmt.Errorf(""))
test.client.EXPECT().List(test.ctx, &corev1.NamespaceList{}).Return(fmt.Errorf(""))

err := workflows.CreateNamespaceIfNotPresent(test.ctx, namespace, kubeconfig, test.clientFactory)

Expand All @@ -94,7 +110,7 @@ func TestCreateNamespaceFail(t *testing.T) {
namespace := "test-ns"

test.clientFactory.EXPECT().BuildClientFromKubeconfig(kubeconfig).Return(test.client, nil)
test.client.EXPECT().Get(test.ctx, namespace, "", &corev1.Namespace{})
test.client.EXPECT().List(test.ctx, &corev1.NamespaceList{}).Return(nil)
test.client.EXPECT().Create(test.ctx, newNamespace(namespace)).Return(fmt.Errorf(""))

err := workflows.CreateNamespaceIfNotPresent(test.ctx, namespace, kubeconfig, test.clientFactory)
Expand Down

0 comments on commit 5b16bc2

Please sign in to comment.