This repository has been archived by the owner on Oct 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
apply.go
371 lines (318 loc) · 10.7 KB
/
apply.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
Copyright 2018 Pusher Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package client has been copied from kubectl code almost verbatim
// Some methods have been moved from k/k into the utils.go file to remove the
// dependency on K/K
// https://github.com/kubernetes/kubernetes/blob/v1.13.1/pkg/kubectl/cmd/apply/apply.go
package client
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/go-logr/logr"
"github.com/jonboulle/clockwork"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/cli-runtime/pkg/resource"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
"k8s.io/kubernetes/pkg/kubectl/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
rlogr "sigs.k8s.io/controller-runtime/pkg/runtime/log"
)
// Options are creation options for a Applier
type Options struct {
// Scheme, if provided, will be used to map go structs to GroupVersionKinds
Scheme *runtime.Scheme
// Mapper, if provided, will be used to map GroupVersionKinds to Resources
Mapper meta.RESTMapper
}
// Client defines the interface for the Applier
type Client interface {
Apply(context.Context, *ApplyOptions, runtime.Object) error
}
// Make sure Applier implements Client
var _ Client = &Applier{}
// Applier is a client that can perform a three-way-merge
// Based on the `kubectl apply` command
type Applier struct {
mapper meta.RESTMapper
scheme *runtime.Scheme
client client.Client
dynamicClient dynamic.Interface
config *rest.Config
codecs serializer.CodecFactory
log logr.Logger
}
// NewApplier constucts a new Applier client
func NewApplier(config *rest.Config, options Options) (*Applier, error) {
if config == nil {
return nil, fmt.Errorf("must provide non-nil rest.Config to client.New")
}
// Init a scheme if none provided
if options.Scheme == nil {
options.Scheme = scheme.Scheme
}
// Init a Mapper if none provided
if options.Mapper == nil {
var err error
drm, err := apiutil.NewDiscoveryRESTMapper(config)
if err != nil {
return nil, err
}
lrm := meta.NewLazyRESTMapperLoader(func() (meta.RESTMapper, error) {
return apiutil.NewDiscoveryRESTMapper(config)
})
options.Mapper = meta.FirstHitRESTMapper{MultiRESTMapper: meta.MultiRESTMapper{drm, lrm}}
}
cachingClient, err := client.New(config, client.Options{Scheme: options.Scheme, Mapper: options.Mapper})
if err != nil {
return nil, err
}
dynamicClient, err := dynamic.NewForConfig(config)
if err != nil {
return nil, err
}
a := &Applier{
mapper: options.Mapper,
scheme: options.Scheme,
codecs: serializer.NewCodecFactory(options.Scheme),
client: cachingClient,
dynamicClient: dynamicClient,
config: config,
log: rlogr.Log.WithName("applier"),
}
return a, nil
}
// ApplyOptions defines the possible options for the Apply command
type ApplyOptions struct {
Overwrite *bool // Automatically resolve conflicts between the modified and live configuration by using values from the modified configuration
ForceDeletion *bool
CascadeDeletion *bool
DeletionTimeout *time.Duration
DeletionGracePeriod *int
ServerDryRun *bool
}
// Complete defaults valus within the ApplyOptions struct
func (a *ApplyOptions) Complete() {
// setup option defaults
overwrite := true
forceDeletion := false
cascadeDeletion := true
deletionTimeout := time.Duration(30 * time.Second)
deletionGracePeriod := -1
serverDryRun := false
if a.Overwrite == nil {
a.Overwrite = &overwrite
}
if a.ForceDeletion == nil {
a.ForceDeletion = &forceDeletion
}
if a.CascadeDeletion == nil {
a.CascadeDeletion = &cascadeDeletion
}
if a.DeletionTimeout == nil {
a.DeletionTimeout = &deletionTimeout
}
if a.DeletionGracePeriod == nil {
a.DeletionGracePeriod = &deletionGracePeriod
}
if a.ServerDryRun == nil {
a.ServerDryRun = &serverDryRun
}
}
// Apply performs a strategic three way merge update to the resource if it exists,
// else it creates the resource
func (a *Applier) Apply(ctx context.Context, opts *ApplyOptions, modified runtime.Object) error {
// Default option values
opts.Complete()
current := newUnstructuredFor(modified)
objectKey, err := getNamespacedName(modified)
if err != nil {
return fmt.Errorf("unable to determine NamespacedName: %v", err)
}
// Check if the resource already exists
err = a.client.Get(context.TODO(), objectKey, current)
if err != nil && errors.IsNotFound(err) {
// Object is not found, create it
return a.create(ctx, opts, modified)
} else if err != nil {
return fmt.Errorf("unable to get current resource: %v", err)
}
// Update the object
err = a.update(ctx, opts, current, modified)
if err != nil {
return fmt.Errorf("error applying update: %v", err)
}
return nil
}
func (a *Applier) create(ctx context.Context, opts *ApplyOptions, obj runtime.Object) error {
metadata, err := meta.Accessor(obj)
if err != nil {
return fmt.Errorf("unable to read metadata from object: %v", err)
}
log := a.log.WithValues(
"kind", obj.GetObjectKind().GroupVersionKind().String(),
"name", metadata.GetName(),
"namespace", metadata.GetNamespace(),
)
log.V(2).Info("creating resource", "dry-run", *opts.ServerDryRun)
err = createApplyAnnotation(obj, unstructured.UnstructuredJSONScheme)
if err != nil {
return fmt.Errorf("unable to apply LastAppliedAnnotation to object: %v", err)
}
gvk := obj.GetObjectKind().GroupVersionKind()
restClient, err := a.restClientFor(gvk.GroupVersion())
if err != nil {
return fmt.Errorf("unable to construct REST client for GroupVersion %s: %v", gvk.GroupVersion().String(), err)
}
mapping, err := a.mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
return fmt.Errorf("unable to get REST mapping for GroupVersionKind %s: %v", gvk.String(), err)
}
createOptions := &metav1.CreateOptions{}
if *opts.ServerDryRun {
createOptions.DryRun = []string{metav1.DryRunAll}
}
err = restClient.Post().
NamespaceIfScoped(metadata.GetNamespace(), isNamespaced(mapping)).
Resource(mapping.Resource.Resource).
Body(obj).
VersionedParams(createOptions, metav1.ParameterCodec).
Context(ctx).
Do().
Into(obj)
if err != nil {
return fmt.Errorf("error creating object: %v", err)
}
return nil
}
func (a *Applier) update(ctx context.Context, opts *ApplyOptions, current, modified runtime.Object) error {
metadata, err := meta.Accessor(modified)
if err != nil {
return fmt.Errorf("unable to get object metadata: %v", err)
}
log := a.log.WithValues(
"kind", modified.GetObjectKind().GroupVersionKind().String(),
"name", metadata.GetName(),
"namespace", metadata.GetNamespace(),
)
log.V(2).Info("updating resource", "dry-run", *opts.ServerDryRun)
modifiedJSON, err := getModifiedConfiguration(modified, true, unstructured.UnstructuredJSONScheme)
if err != nil {
return fmt.Errorf("unable to get modified configuration: %v", err)
}
patcher, err := a.newPatcher(opts, modified)
if err != nil {
return fmt.Errorf("unable to construct patcher: %v", err)
}
source := metadata.GetSelfLink() // This is optional and would normally be the file path
_, patchedObj, err := patcher.Patch(current, modifiedJSON, source, metadata.GetNamespace(), metadata.GetName(), nil)
if err != nil {
return fmt.Errorf("unable to patch object: %v", err)
}
// Copy the patchedObj into the modified runtime.Object
err = a.copyInto(patchedObj, modified)
if err != nil {
return fmt.Errorf("error copying response: %v", err)
}
return nil
}
func (a *Applier) newPatcher(opts *ApplyOptions, obj runtime.Object) (*Patcher, error) {
gvk := obj.GetObjectKind().GroupVersionKind()
mapping, err := a.mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
return nil, fmt.Errorf("couldn't construct rest mapping from GVK %s: %v", gvk.String(), err)
}
restClient, err := a.restClientFor(gvk.GroupVersion())
if err != nil {
return nil, fmt.Errorf("unable to get REST Client: %v", err)
}
helper := resource.NewHelper(restClient, mapping)
p := &Patcher{
Mapping: mapping,
Helper: helper,
DynamicClient: a.dynamicClient,
Overwrite: *opts.Overwrite,
BackOff: clockwork.NewRealClock(),
Force: *opts.ForceDeletion,
Cascade: *opts.CascadeDeletion,
Timeout: *opts.DeletionTimeout,
GracePeriod: *opts.DeletionGracePeriod,
ServerDryRun: *opts.ServerDryRun,
OpenapiSchema: nil, // Not supporting OpenapiSchema patching
Retries: maxPatchRetry,
}
return p, nil
}
func (a *Applier) configFor(gv schema.GroupVersion) (*rest.Config, error) {
config := rest.CopyConfig(a.config)
err := rest.SetKubernetesDefaults(config)
if err != nil {
return nil, fmt.Errorf("error defaulting config: %v", err)
}
config.GroupVersion = &gv
// Set correct APIPath for core API group
if gv.String() == "v1" {
config.APIPath = "api/"
} else {
config.APIPath = "apis/"
}
contentConfig := resource.UnstructuredPlusDefaultContentConfig()
config.NegotiatedSerializer = contentConfig.NegotiatedSerializer
return config, nil
}
func newUnstructuredFor(obj runtime.Object) *unstructured.Unstructured {
gvk := obj.GetObjectKind().GroupVersionKind()
apiVersion, kind := gvk.ToAPIVersionAndKind()
u := &unstructured.Unstructured{}
u.SetKind(kind)
u.SetAPIVersion(apiVersion)
return u
}
func (a *Applier) restClientFor(gv schema.GroupVersion) (rest.Interface, error) {
restConfig, err := a.configFor(gv)
if err != nil {
return nil, fmt.Errorf("failed to construct config for Group Version %+v: %v", gv, err)
}
restClient, err := rest.UnversionedRESTClientFor(restConfig)
if err != nil {
return nil, fmt.Errorf("failed to initialise rest client: %v", err)
}
return restClient, nil
}
func (a *Applier) copyInto(in, out runtime.Object) error {
data, err := json.Marshal(in)
if err != nil {
return err
}
gvk := in.GetObjectKind().GroupVersionKind()
_, _, err = a.codecs.UniversalDecoder().Decode(data, &gvk, out)
if err != nil {
return err
}
return nil
}
func isNamespaced(mapping *meta.RESTMapping) bool {
if mapping.Scope.Name() == meta.RESTScopeNameRoot {
return false
}
return true
}