summaryrefslogtreecommitdiffstats
path: root/src/k8splugin/plugins/namespace/plugin.go
blob: de2f4f8baedcd10bfbee71dfcc67de1e1ac87fcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
Copyright 2018 Intel Corporation.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
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"

	"k8splugin/krd"
)

// 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: data.Namespace,
		},
	}
	_, err := client.CoreV1().Namespaces().Create(namespace)
	if err != nil {
		return "", pkgerrors.Wrap(err, "Create Namespace error")
	}
	log.Printf("Namespace (%s) created", data.Namespace)

	return data.Namespace, nil
}

// Get an existing namespace hosted in a specific Kubernetes cluster
func Get(name string, namespace string, client kubernetes.Interface) (string, error) {
	opts := metaV1.GetOptions{}
	opts.APIVersion = "apps/v1"
	opts.Kind = "Deployment"

	ns, err := client.CoreV1().Namespaces().Get(name, opts)
	if err != nil {
		return "", pkgerrors.Wrap(err, "Get Namespace error")
	}

	return ns.Name, nil
}

// Delete an existing namespace hosted in a specific Kubernetes cluster
func Delete(name string, namespace string, client kubernetes.Interface) error {
	deletePolicy := metaV1.DeletePropagationForeground
	opts := &metaV1.DeleteOptions{
		PropagationPolicy: &deletePolicy,
	}

	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
}