-
Notifications
You must be signed in to change notification settings - Fork 27
/
k8s_namespace.go
54 lines (46 loc) · 1.42 KB
/
k8s_namespace.go
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
package k8s
import (
"context"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func (c *Client) CreateNamespace(ctx context.Context, name string) error {
namespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}
_, err := c.clientset.CoreV1().Namespaces().Create(ctx, namespace, metav1.CreateOptions{})
if err != nil {
if !errors.IsAlreadyExists(err) {
return ErrCreatingNamespace.WithParams(name).Wrap(err)
}
logrus.Debugf("Namespace %s already exists, continuing.\n", name)
}
logrus.Debugf("Namespace %s created.\n", name)
return nil
}
func (c *Client) DeleteNamespace(ctx context.Context, name string) error {
err := c.clientset.CoreV1().Namespaces().Delete(ctx, name, metav1.DeleteOptions{})
if err != nil {
return ErrDeletingNamespace.WithParams(name).Wrap(err)
}
return nil
}
func (c *Client) GetNamespace(ctx context.Context, name string) (*corev1.Namespace, error) {
namespace, err := c.clientset.CoreV1().Namespaces().Get(ctx, name, metav1.GetOptions{})
if err != nil {
return nil, ErrGettingNamespace.WithParams(name).Wrap(err)
}
return namespace, nil
}
func (c *Client) NamespaceExists(ctx context.Context, name string) bool {
_, err := c.GetNamespace(ctx, name)
if err != nil {
logrus.Debugf("Namespace %s does not exist, err: %v", name, err)
return false
}
return true
}