diff options
Diffstat (limited to 'src/k8splugin/plugins/namespace')
-rw-r--r-- | src/k8splugin/plugins/namespace/plugin.go | 60 | ||||
-rw-r--r-- | src/k8splugin/plugins/namespace/plugin_test.go | 106 |
2 files changed, 104 insertions, 62 deletions
diff --git a/src/k8splugin/plugins/namespace/plugin.go b/src/k8splugin/plugins/namespace/plugin.go index 6f823918..2d5d2ab8 100644 --- a/src/k8splugin/plugins/namespace/plugin.go +++ b/src/k8splugin/plugins/namespace/plugin.go @@ -16,39 +16,42 @@ package main import ( "log" - "k8s.io/client-go/kubernetes" - pkgerrors "github.com/pkg/errors" - coreV1 "k8s.io/api/core/v1" metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" utils "k8splugin/internal" + "k8splugin/internal/helm" + "k8splugin/internal/plugin" ) +// ExportedVariable is what we will look for when calling the plugin +var ExportedVariable namespacePlugin + +type namespacePlugin struct { +} + // Create a namespace object in a specific Kubernetes cluster -func Create(data *utils.ResourceData, client kubernetes.Interface) (string, error) { - namespace := &coreV1.Namespace{ +func (p namespacePlugin) Create(yamlFilePath string, namespace string, client plugin.KubernetesConnector) (string, error) { + namespaceObj := &coreV1.Namespace{ ObjectMeta: metaV1.ObjectMeta{ - Name: data.Namespace, + Name: namespace, }, } - _, err := client.CoreV1().Namespaces().Create(namespace) + _, err := client.GetStandardClient().CoreV1().Namespaces().Create(namespaceObj) if err != nil { return "", pkgerrors.Wrap(err, "Create Namespace error") } - log.Printf("Namespace (%s) created", data.Namespace) + log.Printf("Namespace (%s) created", namespace) - return data.Namespace, nil + return namespace, nil } // Get an existing namespace hosted in a specific Kubernetes cluster -func Get(name string, namespace string, client kubernetes.Interface) (string, error) { +func (p namespacePlugin) Get(resource helm.KubernetesResource, namespace string, client plugin.KubernetesConnector) (string, error) { opts := metaV1.GetOptions{} - opts.APIVersion = "apps/v1" - opts.Kind = "Deployment" - - ns, err := client.CoreV1().Namespaces().Get(name, opts) + ns, err := client.GetStandardClient().CoreV1().Namespaces().Get(resource.Name, opts) if err != nil { return "", pkgerrors.Wrap(err, "Get Namespace error") } @@ -57,14 +60,14 @@ func Get(name string, namespace string, client kubernetes.Interface) (string, er } // Delete an existing namespace hosted in a specific Kubernetes cluster -func Delete(name string, namespace string, client kubernetes.Interface) error { +func (p namespacePlugin) Delete(resource helm.KubernetesResource, namespace string, client plugin.KubernetesConnector) error { deletePolicy := metaV1.DeletePropagationForeground opts := &metaV1.DeleteOptions{ PropagationPolicy: &deletePolicy, } - log.Println("Deleting namespace: " + name) - if err := client.CoreV1().Namespaces().Delete(name, opts); err != nil { + log.Println("Deleting namespace: " + resource.Name) + if err := client.GetStandardClient().CoreV1().Namespaces().Delete(resource.Name, opts); err != nil { return pkgerrors.Wrap(err, "Delete namespace error") } @@ -72,23 +75,30 @@ func Delete(name string, namespace string, client kubernetes.Interface) error { } // List of existing namespaces hosted in a specific Kubernetes cluster -func List(namespace string, client kubernetes.Interface) ([]string, error) { +// This plugin ignores both gvk and namespace arguments +func (p namespacePlugin) List(gvk schema.GroupVersionKind, namespace string, client plugin.KubernetesConnector) ([]helm.KubernetesResource, error) { opts := metaV1.ListOptions{ Limit: utils.ResourcesListLimit, } - opts.APIVersion = "apps/v1" - opts.Kind = "Namespace" - list, err := client.CoreV1().Namespaces().List(opts) + list, err := client.GetStandardClient().CoreV1().Namespaces().List(opts) if err != nil { return nil, pkgerrors.Wrap(err, "Get Namespace list error") } - result := make([]string, 0, utils.ResourcesListLimit) + result := make([]helm.KubernetesResource, 0, utils.ResourcesListLimit) if list != nil { - for _, deployment := range list.Items { - log.Printf("%v", deployment.Name) - result = append(result, deployment.Name) + for _, ns := range list.Items { + log.Printf("%v", ns.Name) + result = append(result, + helm.KubernetesResource{ + GVK: schema.GroupVersionKind{ + Group: "", + Version: "v1", + Kind: "Namespace", + }, + Name: ns.Name, + }) } } diff --git a/src/k8splugin/plugins/namespace/plugin_test.go b/src/k8splugin/plugins/namespace/plugin_test.go index 0019df1c..9e57b971 100644 --- a/src/k8splugin/plugins/namespace/plugin_test.go +++ b/src/k8splugin/plugins/namespace/plugin_test.go @@ -18,36 +18,54 @@ import ( "strings" "testing" - utils "k8splugin/internal" + "k8splugin/internal/helm" coreV1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/meta" metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" - testclient "k8s.io/client-go/kubernetes/fake" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/client-go/dynamic" + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/kubernetes/fake" ) +type TestKubernetesConnector struct { + object runtime.Object +} + +func (t TestKubernetesConnector) GetMapper() meta.RESTMapper { + return nil +} + +func (t TestKubernetesConnector) GetDynamicClient() dynamic.Interface { + return nil +} + +func (t TestKubernetesConnector) GetStandardClient() kubernetes.Interface { + return fake.NewSimpleClientset(t.object) +} + func TestCreateNamespace(t *testing.T) { - namespace := "test1" testCases := []struct { label string - input *utils.ResourceData - clientOutput *coreV1.Namespace + input string + object *coreV1.Namespace expectedResult string expectedError string }{ { - label: "Successfully create a namespace", - input: &utils.ResourceData{ - Namespace: namespace, - }, - clientOutput: &coreV1.Namespace{}, - expectedResult: namespace, + label: "Successfully create a namespace", + input: "test1", + object: &coreV1.Namespace{}, + expectedResult: "test1", }, } for _, testCase := range testCases { - client := testclient.NewSimpleClientset(testCase.clientOutput) + client := TestKubernetesConnector{testCase.object} t.Run(testCase.label, func(t *testing.T) { - result, err := Create(testCase.input, client) + result, err := namespacePlugin{}.Create("", testCase.input, client) if err != nil { if testCase.expectedError == "" { t.Fatalf("Create method return an un-expected (%s)", err) @@ -72,39 +90,47 @@ func TestCreateNamespace(t *testing.T) { } func TestListNamespace(t *testing.T) { - namespace := "test1" testCases := []struct { label string input string - clientOutput *coreV1.NamespaceList - expectedResult []string + object *coreV1.NamespaceList + expectedResult []helm.KubernetesResource }{ { label: "Sucessfully to display an empty namespace list", - input: namespace, - clientOutput: &coreV1.NamespaceList{}, - expectedResult: []string{}, + input: "", + object: &coreV1.NamespaceList{}, + expectedResult: []helm.KubernetesResource{}, }, { label: "Sucessfully to display a list of existing namespaces", - input: namespace, - clientOutput: &coreV1.NamespaceList{ + input: "test1", + object: &coreV1.NamespaceList{ Items: []coreV1.Namespace{ coreV1.Namespace{ ObjectMeta: metaV1.ObjectMeta{ - Name: namespace, + Name: "test1", }, }, }, }, - expectedResult: []string{namespace}, + expectedResult: []helm.KubernetesResource{ + { + Name: "test1", + GVK: schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Namespace"}, + }, + }, }, } for _, testCase := range testCases { - client := testclient.NewSimpleClientset(testCase.clientOutput) + client := TestKubernetesConnector{testCase.object} t.Run(testCase.label, func(t *testing.T) { - result, err := List(testCase.input, client) + result, err := namespacePlugin{}.List(schema.GroupVersionKind{ + Group: "", + Version: "v1", + Kind: "Namespace", + }, testCase.input, client) if err != nil { t.Fatalf("List method returned an error (%s)", err) } else { @@ -113,7 +139,7 @@ func TestListNamespace(t *testing.T) { } if !reflect.DeepEqual(testCase.expectedResult, result) { - t.Fatalf("List method returned: \n%v\n and it was expected: \n%v", result, testCase.expectedResult) + t.Fatalf("List method returned: \n%+v\n and it was expected: \n%+v", result, testCase.expectedResult) } } }) @@ -122,14 +148,14 @@ func TestListNamespace(t *testing.T) { func TestDeleteNamespace(t *testing.T) { testCases := []struct { - label string - input map[string]string - clientOutput *coreV1.Namespace + label string + input map[string]string + object *coreV1.Namespace }{ { label: "Sucessfully to delete an existing namespace", input: map[string]string{"name": "test-name", "namespace": "test-namespace"}, - clientOutput: &coreV1.Namespace{ + object: &coreV1.Namespace{ ObjectMeta: metaV1.ObjectMeta{ Name: "test-name", }, @@ -138,9 +164,12 @@ func TestDeleteNamespace(t *testing.T) { } for _, testCase := range testCases { - client := testclient.NewSimpleClientset(testCase.clientOutput) + client := TestKubernetesConnector{testCase.object} t.Run(testCase.label, func(t *testing.T) { - err := Delete(testCase.input["name"], testCase.input["namespace"], client) + err := namespacePlugin{}.Delete(helm.KubernetesResource{ + GVK: schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Namespace"}, + Name: testCase.input["name"], + }, testCase.input["namespace"], client) if err != nil { t.Fatalf("Delete method returned an error (%s)", err) } @@ -152,14 +181,14 @@ func TestGetNamespace(t *testing.T) { testCases := []struct { label string input map[string]string - clientOutput *coreV1.Namespace + object *coreV1.Namespace expectedResult string expectedError string }{ { label: "Sucessfully to get an existing namespace", input: map[string]string{"name": "test-name", "namespace": "test-namespace"}, - clientOutput: &coreV1.Namespace{ + object: &coreV1.Namespace{ ObjectMeta: metaV1.ObjectMeta{ Name: "test-name", }, @@ -169,7 +198,7 @@ func TestGetNamespace(t *testing.T) { { label: "Fail to get an non-existing namespace", input: map[string]string{"name": "test-name", "namespace": "test-namespace"}, - clientOutput: &coreV1.Namespace{ + object: &coreV1.Namespace{ ObjectMeta: metaV1.ObjectMeta{ Name: "test-name2", }, @@ -179,9 +208,12 @@ func TestGetNamespace(t *testing.T) { } for _, testCase := range testCases { - client := testclient.NewSimpleClientset(testCase.clientOutput) + client := TestKubernetesConnector{testCase.object} t.Run(testCase.label, func(t *testing.T) { - result, err := Get(testCase.input["name"], testCase.input["namespace"], client) + result, err := namespacePlugin{}.Get(helm.KubernetesResource{ + GVK: schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Namespace"}, + Name: testCase.input["name"], + }, testCase.input["namespace"], client) if err != nil { if testCase.expectedError == "" { t.Fatalf("Get method return an un-expected (%s)", err) |