-
Notifications
You must be signed in to change notification settings - Fork 5
/
job.go
275 lines (248 loc) · 8.45 KB
/
job.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
272
273
274
275
package job
import (
"context"
"net/http"
"strings"
"sync"
"time"
"github.com/forbearing/k8s/types"
"github.com/forbearing/k8s/util/client"
batchv1 "k8s.io/api/batch/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/informers/internalinterfaces"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
)
type Handler struct {
ctx context.Context
kubeconfig string
namespace string
config *rest.Config
httpClient *http.Client
restClient *rest.RESTClient
clientset *kubernetes.Clientset
dynamicClient dynamic.Interface
discoveryClient *discovery.DiscoveryClient
resyncPeriod time.Duration
informerScope string
tweakListOptions internalinterfaces.TweakListOptionsFunc
informerFactory informers.SharedInformerFactory
Options *types.HandlerOptions
l sync.RWMutex
}
// NewOrDie simply call New() to get a job 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 job 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, error) {
var (
err error
config *rest.Config
httpClient *http.Client
restClient *rest.RESTClient
clientset *kubernetes.Clientset
dynamicClient dynamic.Interface
discoveryClient *discovery.DiscoveryClient
informerFactory informers.SharedInformerFactory
)
// 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.
if config, err = client.RESTConfig(kubeconfig); err != nil {
return nil, err
}
// setup APIPath, GroupVersion and NegotiatedSerializer before initializing a RESTClient
config.APIPath = "api"
config.GroupVersion = &batchv1.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
}
if len(namespace) == 0 {
namespace = metav1.NamespaceDefault
}
// create a sharedInformerFactory for all namespaces.
informerFactory = informers.NewSharedInformerFactory(clientset, 0)
handler := &Handler{
ctx: ctx,
kubeconfig: kubeconfig,
namespace: namespace,
config: config,
httpClient: httpClient,
restClient: restClient,
clientset: clientset,
dynamicClient: dynamicClient,
discoveryClient: discoveryClient,
informerFactory: informerFactory,
Options: &types.HandlerOptions{},
}
handler.SetPropagationPolicy("background")
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 {
handler := h.DeepCopy()
handler.ResetNamespace(namespace)
return handler
}
// 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.ApplyOptions.DryRun = []string{metav1.DryRunAll}
handler.Options.PatchOptions.DryRun = []string{metav1.DryRunAll}
handler.Options.DeleteOptions.DryRun = []string{metav1.DryRunAll}
handler.SetPropagationPolicy("background")
return handler
}
func (in *Handler) DeepCopy() *Handler {
if in == nil {
return nil
}
handler := &Handler{
ctx: in.ctx,
kubeconfig: in.kubeconfig,
namespace: in.namespace,
config: in.config,
httpClient: in.httpClient,
restClient: in.restClient,
clientset: in.clientset,
dynamicClient: in.dynamicClient,
discoveryClient: in.discoveryClient,
informerFactory: in.informerFactory,
resyncPeriod: in.resyncPeriod,
informerScope: in.informerScope,
tweakListOptions: in.tweakListOptions,
Options: &types.HandlerOptions{
CreateOptions: *in.Options.CreateOptions.DeepCopy(),
UpdateOptions: *in.Options.UpdateOptions.DeepCopy(),
ApplyOptions: *in.Options.ApplyOptions.DeepCopy(),
DeleteOptions: *in.Options.DeleteOptions.DeepCopy(),
GetOptions: *in.Options.GetOptions.DeepCopy(),
ListOptions: *in.Options.ListOptions.DeepCopy(),
PatchOptions: *in.Options.PatchOptions.DeepCopy(),
},
}
handler.SetPropagationPolicy("background")
return handler
}
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)
}
}
// SetPropagationPolicy determined whether and how garbage collection will be performed.
// There are supported values are "Background", "Orphan", "Foreground", default is "Background".
func (h *Handler) SetPropagationPolicy(policy string) {
h.l.Lock()
defer h.l.Unlock()
switch strings.ToLower(policy) {
case strings.ToLower(string(metav1.DeletePropagationBackground)):
propagationPolicy := metav1.DeletePropagationBackground
h.Options.DeleteOptions.PropagationPolicy = &propagationPolicy
case strings.ToLower(string(metav1.DeletePropagationForeground)):
propagationPolicy := metav1.DeletePropagationForeground
h.Options.DeleteOptions.PropagationPolicy = &propagationPolicy
case strings.ToLower(string(metav1.DeletePropagationOrphan)):
propagationPolicy := metav1.DeletePropagationOrphan
h.Options.DeleteOptions.PropagationPolicy = &propagationPolicy
default:
propagationPolicy := metav1.DeletePropagationBackground
h.Options.DeleteOptions.PropagationPolicy = &propagationPolicy
}
}
// 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 contains the Group, Version, Kind name of job.
var GVK = schema.GroupVersionKind{
Group: batchv1.SchemeGroupVersion.Group,
Version: batchv1.SchemeGroupVersion.Version,
Kind: types.KindJob,
}
// GVR contains the Group, Version and Resource name of job.
var GVR = schema.GroupVersionResource{
Group: batchv1.SchemeGroupVersion.Group,
Version: batchv1.SchemeGroupVersion.Version,
Resource: types.ResourceJob,
}
// Kind is the job Kind name.
var Kind = GVK.Kind
// Group is the job Group name.
var Group = GVK.Group
// Version is the job Version name.
var Version = GVK.Version
// Resource is the job Resource name.
var Resource = GVR.Resource