-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create namespace using client factory
- Loading branch information
Showing
13 changed files
with
236 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package workflows | ||
|
||
import ( | ||
"context" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/aws/eks-anywhere/pkg/workflows/interfaces" | ||
) | ||
|
||
// CreateNamespaceIfNotPresent creates the namespace on the cluster if it does not already exist. | ||
func CreateNamespaceIfNotPresent(ctx context.Context, namespace, kubeconfig string, clientFactory interfaces.ClientFactory) error { | ||
client, err := clientFactory.BuildClientFromKubeconfig(kubeconfig) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
namespaces := &corev1.NamespaceList{Items: []corev1.Namespace{{ObjectMeta: metav1.ObjectMeta{Name: "default"}}}} | ||
if err := client.List(ctx, namespaces); err != nil { | ||
return err | ||
} | ||
|
||
var found = false | ||
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, | ||
}, | ||
} | ||
|
||
if err = client.Create(ctx, ns); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package workflows_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
corev1 "k8s.io/api/core/v1" | ||
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" | ||
"github.com/aws/eks-anywhere/pkg/workflows/interfaces/mocks" | ||
) | ||
|
||
type createPrepTestSetup struct { | ||
t *testing.T | ||
ctx context.Context | ||
client *clientmocks.MockClient | ||
clientFactory *mocks.MockClientFactory | ||
} | ||
|
||
func newCreatePrepTest(t *testing.T) *createPrepTestSetup { | ||
mockCtrl := gomock.NewController(t) | ||
client := clientmocks.NewMockClient(mockCtrl) | ||
clientFactory := mocks.NewMockClientFactory(mockCtrl) | ||
|
||
return &createPrepTestSetup{ | ||
t: t, | ||
ctx: context.Background(), | ||
client: client, | ||
clientFactory: clientFactory, | ||
} | ||
} | ||
|
||
func newNamespace(name string) *corev1.Namespace { | ||
return &corev1.Namespace{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: "v1", | ||
Kind: "Namespace", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
}, | ||
} | ||
} | ||
|
||
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().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) | ||
if err != nil { | ||
t.Fatalf("Expected nil, but got %v", err) | ||
} | ||
} | ||
|
||
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" | ||
namespace := "test-ns" | ||
|
||
test.clientFactory.EXPECT().BuildClientFromKubeconfig(kubeconfig).Return(test.client, fmt.Errorf("")) | ||
|
||
err := workflows.CreateNamespaceIfNotPresent(test.ctx, namespace, kubeconfig, test.clientFactory) | ||
|
||
if err == nil { | ||
t.Fatalf("Expected error, but got nil") | ||
} | ||
} | ||
|
||
func TestCreateNamespaceGetNamespaceFail(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, &corev1.NamespaceList{}).Return(fmt.Errorf("")) | ||
|
||
err := workflows.CreateNamespaceIfNotPresent(test.ctx, namespace, kubeconfig, test.clientFactory) | ||
|
||
if err == nil { | ||
t.Fatalf("Expected error, but got nil") | ||
} | ||
} | ||
|
||
func TestCreateNamespaceFail(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, &corev1.NamespaceList{}).Return(nil) | ||
test.client.EXPECT().Create(test.ctx, newNamespace(namespace)).Return(fmt.Errorf("")) | ||
|
||
err := workflows.CreateNamespaceIfNotPresent(test.ctx, namespace, kubeconfig, test.clientFactory) | ||
|
||
if err == nil { | ||
t.Fatalf("Expected error, but got nil") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.