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.
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"
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"
30 // Compile time check to see if namespacePlugin implements the correct interface
31 var _ plugin.Reference = namespacePlugin{}
33 // ExportedVariable is what we will look for when calling the plugin
34 var ExportedVariable namespacePlugin
36 type namespacePlugin struct {
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{
46 _, err := client.GetStandardClient().CoreV1().Namespaces().Create(context.TODO(), namespaceObj, metaV1.CreateOptions{})
48 return "", pkgerrors.Wrap(err, "Create Namespace error")
50 log.Printf("Namespace (%s) created", namespace)
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)
60 return "", pkgerrors.Wrap(err, "Get Namespace error")
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,
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")
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,
88 list, err := client.GetStandardClient().CoreV1().Namespaces().List(context.TODO(), opts)
90 return nil, pkgerrors.Wrap(err, "Get Namespace list error")
93 result := make([]helm.KubernetesResource, 0, utils.ResourcesListLimit)
95 for _, ns := range list.Items {
96 log.Printf("%v", ns.Name)
97 result = append(result,
98 helm.KubernetesResource{
99 GVK: schema.GroupVersionKind{
112 func (p namespacePlugin) Update(yamlFilePath string, namespace string, client plugin.KubernetesConnector) (string, error) {