/
namsepace.go
150 lines (129 loc) · 3.6 KB
/
namsepace.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package namespace
import (
"context"
coreV1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/informers"
namespacesV1 "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/kubernetes"
)
type (
//namespace interface
Namespace interface {
//create namespace
Create(ctx context.Context, namespace string) error
//delete namespace
Delete(ctx context.Context, name string) error
//list namespaces
List(ctx context.Context, selector labels.Selector) ([]*coreV1.Namespace, error)
//get namespaces
Get(ctx context.Context, name string) (*coreV1.Namespace, error)
//watch event handler
WatchEvent(ctx context.Context, handler NamespaceHandlerFuncs)
}
//namespace object
namespaceImpl struct {
clientset *kubernetes.Clientset
informer namespacesV1.NamespaceInformer
factory informers.SharedInformerFactory
}
// NamespaceHandlerFuncs
NamespaceHandlerFuncs struct {
AddFunc func(obj *coreV1.Namespace)
UpdateFunc func(oldObj, newObj *coreV1.Namespace)
DeleteFunc func(obj *coreV1.Namespace)
}
)
//new namespace
func NewNamespace(clientset *kubernetes.Clientset, factory informers.SharedInformerFactory) Namespace {
//new namespace
p := &namespaceImpl{
clientset: clientset,
informer: factory.Core().V1().Namespaces(),
factory: factory,
}
return p
}
//create namespace
func (p *namespaceImpl) Create(ctx context.Context, namespace string) error {
ns := &coreV1.Namespace{}
ns.Name = namespace
_, err := p.clientset.CoreV1().Namespaces().Create(ctx, ns, metav1.CreateOptions{})
if err != nil {
return err
}
return nil
}
//delete namespace
func (p *namespaceImpl) Delete(ctx context.Context, name string) error {
err := p.clientset.CoreV1().Namespaces().Delete(ctx, name, metav1.DeleteOptions{})
if err != nil {
return err
}
return nil
}
//list namespaces
func (p *namespaceImpl) List(ctx context.Context, selector labels.Selector) (list []*coreV1.Namespace, err error) {
list, err = p.informer.Lister().List(selector)
if err != nil || len(list) <= 0 {
namespaces, err := p.clientset.CoreV1().Namespaces().List(ctx, metav1.ListOptions{LabelSelector: selector.String()})
if err != nil {
return nil, err
}
for i := range namespaces.Items {
list = append(list, &namespaces.Items[i])
}
}
return list, nil
}
//get namespaces
func (p *namespaceImpl) Get(ctx context.Context, name string) (*coreV1.Namespace, error) {
namespace, err := p.informer.Lister().Get(name)
if err != nil {
namespace, err = p.clientset.CoreV1().Namespaces().Get(ctx, name, metav1.GetOptions{})
if err != nil {
return nil, err
}
}
return namespace, nil
}
//watch event
func (p *namespaceImpl) WatchEvent(ctx context.Context, handler NamespaceHandlerFuncs) {
//add event handler
p.informer.Informer().AddEventHandler(handler)
//start
p.factory.Start(ctx.Done())
//wait sync
p.factory.WaitForCacheSync(ctx.Done())
}
// OnAdd calls AddFunc if it's not nil.
func (j NamespaceHandlerFuncs) OnAdd(obj interface{}) {
if j.AddFunc != nil {
if event, ok := obj.(*coreV1.Namespace); ok {
j.AddFunc(event)
}
}
}
// OnUpdate calls UpdateFunc if it's not nil.
func (j NamespaceHandlerFuncs) OnUpdate(oldObj, newObj interface{}) {
if j.UpdateFunc != nil {
old, ok := oldObj.(*coreV1.Namespace)
if !ok {
return
}
new, ok := newObj.(*coreV1.Namespace)
if !ok {
return
}
j.UpdateFunc(old, new)
}
}
// OnDelete calls DeleteFunc if it's not nil.
func (j NamespaceHandlerFuncs) OnDelete(obj interface{}) {
if j.DeleteFunc != nil {
if event, ok := obj.(*coreV1.Namespace); ok {
j.DeleteFunc(event)
}
}
}