aboutsummaryrefslogtreecommitdiffstats
path: root/src/k8splugin/plugins/namespace/plugin.go
diff options
context:
space:
mode:
authorVictor Morales <victor.morales@intel.com>2018-09-18 17:19:00 -0700
committerVictor Morales <victor.morales@intel.com>2018-09-18 17:19:00 -0700
commit05274b1b149139d91445ca10a73defe41f14824a (patch)
tree2ad5e9169a521d29d0c909a271f1ff23b35d2dac /src/k8splugin/plugins/namespace/plugin.go
parentb368dfe25337494060eb8cd85a5becaf7a465643 (diff)
Add UTs to plugins
Deployment, service and namespace are plugins which offers CRUD operations to manage their resources. They haven't implemented Unit Tests which makes fragile to change/refactor the source code. This change adds their corresponding Unit Tests and defines a standard interface. Change-Id: I1e1eb40f1a18ba33c74069a117462c8df17767ac Signed-off-by: Victor Morales <victor.morales@intel.com> Issue-ID: MULTICLOUD-301
Diffstat (limited to 'src/k8splugin/plugins/namespace/plugin.go')
-rw-r--r--src/k8splugin/plugins/namespace/plugin.go74
1 files changed, 52 insertions, 22 deletions
diff --git a/src/k8splugin/plugins/namespace/plugin.go b/src/k8splugin/plugins/namespace/plugin.go
index 986de863..e29ff43d 100644
--- a/src/k8splugin/plugins/namespace/plugin.go
+++ b/src/k8splugin/plugins/namespace/plugin.go
@@ -14,55 +14,85 @@ limitations under the License.
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/client-go/kubernetes"
+
+ "k8splugin/krd"
)
-// CreateResource is used to create a new Namespace
-func CreateResource(namespace string, client *kubernetes.Clientset) error {
- namespaceStruct := &coreV1.Namespace{
+// Create a namespace object in a specific Kubernetes cluster
+func Create(data *krd.ResourceData, client kubernetes.Interface) (string, error) {
+ namespace := &coreV1.Namespace{
ObjectMeta: metaV1.ObjectMeta{
- Name: namespace,
+ Name: data.Namespace,
},
}
- _, err := client.CoreV1().Namespaces().Create(namespaceStruct)
+ _, err := client.CoreV1().Namespaces().Create(namespace)
if err != nil {
- return pkgerrors.Wrap(err, "Create Namespace error")
+ return "", pkgerrors.Wrap(err, "Create Namespace error")
}
- return nil
+ return data.Namespace, nil
}
-// GetResource is used to check if a given namespace actually exists in Kubernetes
-func GetResource(namespace string, client *kubernetes.Clientset) (bool, error) {
+// Get an existing namespace hosted in a specific Kubernetes cluster
+func Get(name string, namespace string, client kubernetes.Interface) (string, error) {
opts := metaV1.ListOptions{}
- namespaceList, err := client.CoreV1().Namespaces().List(opts)
+ list, err := client.CoreV1().Namespaces().List(opts)
if err != nil {
- return false, pkgerrors.Wrap(err, "Get Namespace list error")
+ return "", pkgerrors.Wrap(err, "Get Namespace list error")
}
- for _, ns := range namespaceList.Items {
+ for _, ns := range list.Items {
if namespace == ns.Name {
- return true, nil
+ return ns.Name, nil
}
}
- return false, nil
+ return "", nil
}
-// DeleteResource is used to delete a namespace
-func DeleteResource(namespace string, client *kubernetes.Clientset) error {
+// Delete an existing namespace hosted in a specific Kubernetes cluster
+func Delete(name string, namespace string, client kubernetes.Interface) error {
deletePolicy := metaV1.DeletePropagationForeground
-
- err := client.CoreV1().Namespaces().Delete(namespace, &metaV1.DeleteOptions{
+ opts := &metaV1.DeleteOptions{
PropagationPolicy: &deletePolicy,
- })
+ }
- if err != nil {
- return pkgerrors.Wrap(err, "Delete Namespace error")
+ log.Println("Deleting namespace: " + name)
+ if err := client.CoreV1().Namespaces().Delete(name, opts); err != nil {
+ return pkgerrors.Wrap(err, "Delete namespace error")
}
+
return nil
}
+
+// List of existing namespaces hosted in a specific Kubernetes cluster
+func List(namespace string, client kubernetes.Interface) ([]string, error) {
+ opts := metaV1.ListOptions{
+ Limit: krd.ResourcesListLimit,
+ }
+ opts.APIVersion = "apps/v1"
+ opts.Kind = "Namespace"
+
+ list, err := client.CoreV1().Namespaces().List(opts)
+ if err != nil {
+ return nil, pkgerrors.Wrap(err, "Get Namespace list error")
+ }
+
+ result := make([]string, 0, krd.ResourcesListLimit)
+ if list != nil {
+ for _, deployment := range list.Items {
+ log.Printf("%v", deployment.Name)
+ result = append(result, deployment.Name)
+ }
+ }
+
+ return result, nil
+}