851a556809a699647f296d2d78dfb8462aa8944f
[multicloud/k8s.git] / src / k8splugin / plugins / namespace / plugin.go
1 /*
2 Copyright 2018 Intel Corporation.
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6     http://www.apache.org/licenses/LICENSE-2.0
7 Unless required by applicable law or agreed to in writing, software
8 distributed under the License is distributed on an "AS IS" BASIS,
9 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 See the License for the specific language governing permissions and
11 limitations under the License.
12 */
13
14 package main
15
16 import (
17         "context"
18         "log"
19
20         pkgerrors "github.com/pkg/errors"
21         coreV1 "k8s.io/api/core/v1"
22         metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23         "k8s.io/apimachinery/pkg/runtime/schema"
24
25         utils "github.com/onap/multicloud-k8s/src/k8splugin/internal"
26         "github.com/onap/multicloud-k8s/src/k8splugin/internal/helm"
27         "github.com/onap/multicloud-k8s/src/k8splugin/internal/plugin"
28 )
29
30 // Compile time check to see if namespacePlugin implements the correct interface
31 var _ plugin.Reference = namespacePlugin{}
32
33 // ExportedVariable is what we will look for when calling the plugin
34 var ExportedVariable namespacePlugin
35
36 type namespacePlugin struct {
37 }
38
39 // Create a namespace object in a specific Kubernetes cluster
40 func (p namespacePlugin) Create(yamlFilePath string, namespace string, client plugin.KubernetesConnector) (string, error) {
41         namespaceObj := &coreV1.Namespace{
42                 ObjectMeta: metaV1.ObjectMeta{
43                         Name: namespace,
44                 },
45         }
46         _, err := client.GetStandardClient().CoreV1().Namespaces().Create(context.TODO(), namespaceObj, metaV1.CreateOptions{})
47         if err != nil {
48                 return "", pkgerrors.Wrap(err, "Create Namespace error")
49         }
50         log.Printf("Namespace (%s) created", namespace)
51
52         return namespace, nil
53 }
54
55 // Get an existing namespace hosted in a specific Kubernetes cluster
56 func (p namespacePlugin) Get(resource helm.KubernetesResource, namespace string, client plugin.KubernetesConnector) (string, error) {
57         opts := metaV1.GetOptions{}
58         ns, err := client.GetStandardClient().CoreV1().Namespaces().Get(context.TODO(), resource.Name, opts)
59         if err != nil {
60                 return "", pkgerrors.Wrap(err, "Get Namespace error")
61         }
62
63         return ns.Name, nil
64 }
65
66 // Delete an existing namespace hosted in a specific Kubernetes cluster
67 func (p namespacePlugin) Delete(resource helm.KubernetesResource, namespace string, client plugin.KubernetesConnector) error {
68         deletePolicy := metaV1.DeletePropagationBackground
69         opts := metaV1.DeleteOptions{
70                 PropagationPolicy: &deletePolicy,
71         }
72
73         log.Println("Deleting namespace: " + resource.Name)
74         if err := client.GetStandardClient().CoreV1().Namespaces().Delete(context.TODO(), resource.Name, opts); err != nil {
75                 return pkgerrors.Wrap(err, "Delete namespace error")
76         }
77
78         return nil
79 }
80
81 // List of existing namespaces hosted in a specific Kubernetes cluster
82 // This plugin ignores both gvk and namespace arguments
83 func (p namespacePlugin) List(gvk schema.GroupVersionKind, namespace string, client plugin.KubernetesConnector) ([]helm.KubernetesResource, error) {
84         opts := metaV1.ListOptions{
85                 Limit: utils.ResourcesListLimit,
86         }
87
88         list, err := client.GetStandardClient().CoreV1().Namespaces().List(context.TODO(), opts)
89         if err != nil {
90                 return nil, pkgerrors.Wrap(err, "Get Namespace list error")
91         }
92
93         result := make([]helm.KubernetesResource, 0, utils.ResourcesListLimit)
94         if list != nil {
95                 for _, ns := range list.Items {
96                         log.Printf("%v", ns.Name)
97                         result = append(result,
98                                 helm.KubernetesResource{
99                                         GVK: schema.GroupVersionKind{
100                                                 Group:   "",
101                                                 Version: "v1",
102                                                 Kind:    "Namespace",
103                                         },
104                                         Name: ns.Name,
105                                 })
106                 }
107         }
108
109         return result, nil
110 }
111
112 func (p namespacePlugin) Update(yamlFilePath string, namespace string, client plugin.KubernetesConnector) (string, error) {
113
114    return "", nil
115 }