-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use client factory to create namespace
- Loading branch information
Showing
10 changed files
with
205 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package workflows | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aws/eks-anywhere/pkg/workflows/interfaces" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// 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 | ||
} | ||
|
||
if err := client.Get(ctx, namespace, "", &corev1.Namespace{}); err != nil && !errors.IsNotFound(err) { | ||
return err | ||
} | ||
|
||
ns := &corev1.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,106 @@ | ||
package workflows_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
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" | ||
|
||
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" | ||
"github.com/golang/mock/gomock" | ||
) | ||
|
||
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{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: name, | ||
}, | ||
} | ||
} | ||
|
||
func TestCreateNamespaceNotExists(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().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 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().Get(test.ctx, namespace, "", &corev1.Namespace{}).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().Get(test.ctx, namespace, "", &corev1.Namespace{}) | ||
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
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.