-
Notifications
You must be signed in to change notification settings - Fork 5
/
configmaps.go
177 lines (161 loc) · 4.83 KB
/
configmaps.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package configmap
import (
"context"
"sync"
"time"
"github.com/forbearing/k8s/typed"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
)
type Handler struct {
kubeconfig string
namespace string
ctx context.Context
config *rest.Config
restClient *rest.RESTClient
clientset *kubernetes.Clientset
dynamicClient dynamic.Interface
discoveryClient *discovery.DiscoveryClient
informerFactory informers.SharedInformerFactory
informer cache.SharedIndexInformer
Options *typed.HandlerOptions
sync.Mutex
}
// New returns a configmap handler from kubeconfig or in-cluster config.
func New(ctx context.Context, namespace, kubeconfig string) (handler *Handler, err error) {
var (
config *rest.Config
restClient *rest.RESTClient
clientset *kubernetes.Clientset
dynamicClient dynamic.Interface
discoveryClient *discovery.DiscoveryClient
informerFactory informers.SharedInformerFactory
)
handler = &Handler{}
// create rest config
if len(kubeconfig) != 0 {
// use the current context in kubeconfig
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return
}
} else {
// create the in-cluster config
config, err = rest.InClusterConfig()
if err != nil {
return
}
}
// setup APIPath, GroupVersion and NegotiatedSerializer before initializing a RESTClient
config.APIPath = "api"
config.GroupVersion = &corev1.SchemeGroupVersion
config.NegotiatedSerializer = scheme.Codecs
// create a RESTClient for the given config
restClient, err = rest.RESTClientFor(config)
if err != nil {
return
}
// create a Clientset for the given config
clientset, err = kubernetes.NewForConfig(config)
if err != nil {
return
}
// create a dynamic client for the given config
dynamicClient, err = dynamic.NewForConfig(config)
if err != nil {
return
}
// create a DiscoveryClient for the given config
discoveryClient, err = discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return
}
// create a sharedInformerFactory for all namespaces.
informerFactory = informers.NewSharedInformerFactory(clientset, time.Minute)
if len(namespace) == 0 {
namespace = metav1.NamespaceDefault
}
handler.kubeconfig = kubeconfig
handler.namespace = namespace
handler.ctx = ctx
handler.config = config
handler.restClient = restClient
handler.clientset = clientset
handler.dynamicClient = dynamicClient
handler.discoveryClient = discoveryClient
handler.informerFactory = informerFactory
handler.informer = informerFactory.Core().V1().ConfigMaps().Informer()
handler.Options = &typed.HandlerOptions{}
return
}
func (h *Handler) Namespace() string {
return h.namespace
}
func (in *Handler) DeepCopy() *Handler {
out := new(Handler)
out.kubeconfig = in.kubeconfig
out.namespace = in.namespace
out.ctx = in.ctx
out.config = in.config
out.restClient = in.restClient
out.clientset = in.clientset
out.dynamicClient = in.dynamicClient
out.discoveryClient = in.discoveryClient
out.informerFactory = in.informerFactory
out.informer = in.informer
out.Options = &typed.HandlerOptions{}
out.Options.ListOptions = *in.Options.ListOptions.DeepCopy()
out.Options.GetOptions = *in.Options.GetOptions.DeepCopy()
out.Options.CreateOptions = *in.Options.CreateOptions.DeepCopy()
out.Options.UpdateOptions = *in.Options.UpdateOptions.DeepCopy()
out.Options.PatchOptions = *in.Options.PatchOptions.DeepCopy()
out.Options.ApplyOptions = *in.Options.ApplyOptions.DeepCopy()
return out
}
func (h *Handler) setNamespace(namespace string) {
h.Lock()
defer h.Unlock()
h.namespace = namespace
}
func (h *Handler) WithNamespace(namespace string) *Handler {
cm := h.DeepCopy()
cm.setNamespace(namespace)
return cm
}
func (h *Handler) WithDryRun() *Handler {
handler := h.DeepCopy()
handler.Options.CreateOptions.DryRun = []string{metav1.DryRunAll}
handler.Options.UpdateOptions.DryRun = []string{metav1.DryRunAll}
handler.Options.DeleteOptions.DryRun = []string{metav1.DryRunAll}
handler.Options.PatchOptions.DryRun = []string{metav1.DryRunAll}
handler.Options.ApplyOptions.DryRun = []string{metav1.DryRunAll}
return handler
}
func (h *Handler) SetTimeout(timeout int64) {
h.Lock()
defer h.Unlock()
h.Options.ListOptions.TimeoutSeconds = &timeout
}
func (h *Handler) SetLimit(limit int64) {
h.Lock()
defer h.Unlock()
h.Options.ListOptions.Limit = limit
}
func (h *Handler) SetForceDelete(force bool) {
h.Lock()
defer h.Unlock()
if force {
gracePeriodSeconds := int64(0)
h.Options.DeleteOptions.GracePeriodSeconds = &gracePeriodSeconds
} else {
h.Options.DeleteOptions = metav1.DeleteOptions{}
}
}