-
Notifications
You must be signed in to change notification settings - Fork 5
/
configmap.go
271 lines (241 loc) · 7.9 KB
/
configmap.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package configmap
import (
"context"
"net/http"
"os"
"sync"
"github.com/forbearing/k8s/types"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"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/clientcmd"
)
type Handler struct {
kubeconfig string
namespace string
ctx context.Context
config *rest.Config
httpClient *http.Client
restClient *rest.RESTClient
clientset *kubernetes.Clientset
dynamicClient dynamic.Interface
discoveryClient *discovery.DiscoveryClient
informerFactory informers.SharedInformerFactory
Options *types.HandlerOptions
l sync.RWMutex
}
// NewOrDie simply call New() to get a configmap handler.
// panic if there is any error occurs.
func NewOrDie(ctx context.Context, kubeconfig, namespace string) *Handler {
handler, err := New(ctx, kubeconfig, namespace)
if err != nil {
panic(err)
}
return handler
}
// New returns a configmap handler from kubeconfig or in-cluster config.
// The kubeconfig precedence is:
// * kubeconfig variable passed.
// * KUBECONFIG environment variable pointing at a file
// * $HOME/.kube/config if exists.
// * In-cluster config if running in cluster
func New(ctx context.Context, kubeconfig, namespace string) (handler *Handler, err error) {
var (
config *rest.Config
httpClient *http.Client
restClient *rest.RESTClient
clientset *kubernetes.Clientset
dynamicClient dynamic.Interface
discoveryClient *discovery.DiscoveryClient
informerFactory informers.SharedInformerFactory
)
handler = &Handler{}
// create rest config, and config precedence.
// * kubeconfig variable passed.
// * KUBECONFIG environment variable pointing at a file
// * $HOME/.kube/config if exists.
// * In-cluster config if running in cluster
//
// create the outside-cluster config
if len(kubeconfig) != 0 {
if config, err = clientcmd.BuildConfigFromFlags("", kubeconfig); err != nil {
return nil, err
}
} else if len(os.Getenv(clientcmd.RecommendedConfigPathEnvVar)) != 0 {
if config, err = clientcmd.BuildConfigFromFlags("", os.Getenv(clientcmd.RecommendedConfigPathEnvVar)); err != nil {
return nil, err
}
} else if len(clientcmd.RecommendedHomeFile) != 0 {
if config, err = clientcmd.BuildConfigFromFlags("", clientcmd.RecommendedHomeFile); err != nil {
return nil, err
}
} else {
// create the in-cluster config
if config, err = rest.InClusterConfig(); err != nil {
return nil, err
}
}
// setup APIPath, GroupVersion and NegotiatedSerializer before initializing a RESTClient
config.APIPath = "api"
config.GroupVersion = &corev1.SchemeGroupVersion
config.NegotiatedSerializer = scheme.Codecs
// create a http client for the given config.
if httpClient, err = rest.HTTPClientFor(config); err != nil {
return nil, err
}
// create a RESTClient for the given config and http client.
if restClient, err = rest.RESTClientForConfigAndClient(config, httpClient); err != nil {
return nil, err
}
// create a Clientset for the given config and http client.
if clientset, err = kubernetes.NewForConfigAndClient(config, httpClient); err != nil {
return nil, err
}
// create a dynamic client for the given config and http client.
if dynamicClient, err = dynamic.NewForConfigAndClient(config, httpClient); err != nil {
return nil, err
}
// create a DiscoveryClient for the given config and http client.
if discoveryClient, err = discovery.NewDiscoveryClientForConfigAndClient(config, httpClient); err != nil {
return nil, err
}
// create a sharedInformerFactory for all namespaces.
informerFactory = informers.NewSharedInformerFactory(clientset, 0)
if len(namespace) == 0 {
namespace = metav1.NamespaceDefault
}
handler.kubeconfig = kubeconfig
handler.namespace = namespace
handler.ctx = ctx
handler.config = config
handler.httpClient = httpClient
handler.restClient = restClient
handler.clientset = clientset
handler.dynamicClient = dynamicClient
handler.discoveryClient = discoveryClient
handler.informerFactory = informerFactory
handler.Options = &types.HandlerOptions{}
return handler, nil
}
// WithNamespace deep copies a new handler, but set the handler.namespace to
// the provided namespace.
func (h *Handler) WithNamespace(namespace string) *Handler {
cm := h.DeepCopy()
cm.resetNamespace(namespace)
return cm
}
// WithDryRun deep copies a new handler and prints the create/update/apply/delete
// operations, without sending it to apiserver.
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 (in *Handler) DeepCopy() *Handler {
if in == nil {
return nil
}
out := new(Handler)
out.kubeconfig = in.kubeconfig
out.namespace = in.namespace
out.ctx = in.ctx
out.config = in.config
out.httpClient = in.httpClient
out.restClient = in.restClient
out.clientset = in.clientset
out.dynamicClient = in.dynamicClient
out.discoveryClient = in.discoveryClient
out.informerFactory = in.informerFactory
out.Options = &types.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) resetNamespace(namespace string) {
h.l.Lock()
defer h.l.Unlock()
h.namespace = namespace
}
func (h *Handler) SetTimeout(timeout int64) {
h.l.Lock()
defer h.l.Unlock()
h.Options.ListOptions.TimeoutSeconds = &timeout
}
func (h *Handler) SetLimit(limit int64) {
h.l.Lock()
defer h.l.Unlock()
h.Options.ListOptions.Limit = limit
}
func (h *Handler) SetForceDelete(force bool) {
h.l.Lock()
defer h.l.Unlock()
if force {
h.Options.DeleteOptions.GracePeriodSeconds = new(int64)
}
}
// RESTConfig returns underlying rest config.
func (h *Handler) RESTConfig() *rest.Config {
return h.config
}
// RESTClient returns underlying rest client.
func (h *Handler) RESTClient() *rest.RESTClient {
return h.restClient
}
// Clientset returns underlying clientset.
func (h *Handler) Clientset() *kubernetes.Clientset {
return h.clientset
}
// DynamicClient returns underlying dynamic client.
func (h *Handler) DynamicClient() dynamic.Interface {
return h.dynamicClient
}
// DiscoveryClient returns underlying discovery client.
func (h *Handler) DiscoveryClient() *discovery.DiscoveryClient {
return h.discoveryClient
}
// GVK returns the name of Group, Version, Kind of configmap resource.
func GVK() schema.GroupVersionKind {
return schema.GroupVersionKind{
Group: corev1.SchemeGroupVersion.Group,
Version: corev1.SchemeGroupVersion.Version,
Kind: types.KindConfigMap,
}
}
// GVR returns the name of Group, Version, Resource of configmap resource.
func GVR() schema.GroupVersionResource {
return schema.GroupVersionResource{
Group: corev1.SchemeGroupVersion.Group,
Version: corev1.SchemeGroupVersion.Version,
Resource: types.ResourceConfigMap,
}
}
// Group returns the Group name of configmap resource.
func Group() string {
return GVR().Group
}
// Kind returns the Kind name of configmap resource.
func Kind() string {
return GVK().Kind
}
// Version returns the Version name of configmap resource.
func Version() string {
return GVR().Version
}
// Resource returns the Resource name of configmap resource.
func Resource() string {
return GVR().Resource
}