This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
sync.go
562 lines (492 loc) · 16.7 KB
/
sync.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
package kubernetes
import (
"bytes"
"context"
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"fmt"
"io"
"os/exec"
"sort"
"strings"
"time"
"github.com/go-kit/kit/log"
"github.com/imdario/mergo"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
apierrors "k8s.io/apimachinery/pkg/api/errors"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/rest"
"github.com/fluxcd/flux/pkg/cluster"
kresource "github.com/fluxcd/flux/pkg/cluster/kubernetes/resource"
"github.com/fluxcd/flux/pkg/policy"
"github.com/fluxcd/flux/pkg/resource"
)
const (
// We use mark-and-sweep garbage collection to delete cluster objects.
// Marking is done by adding a label when creating and updating the objects.
// Sweeping is done by comparing Marked cluster objects with the manifests in Git.
gcMarkLabel = kresource.PolicyPrefix + "sync-gc-mark"
// We want to prevent garbage-collecting cluster objects which haven't been updated.
// We annotate objects with the checksum of their Git manifest to verify this.
checksumAnnotation = kresource.PolicyPrefix + "sync-checksum"
)
// Sync takes a definition of what should be running in the cluster,
// and attempts to make the cluster conform. An error return does not
// necessarily indicate complete failure; some resources may succeed
// in being synced, and some may fail (for example, they may be
// malformed).
func (c *Cluster) Sync(syncSet cluster.SyncSet) error {
logger := log.With(c.logger, "method", "Sync")
// Keep track of the checksum of each resource, so we can compare
// them during garbage collection.
checksums := map[string]string{}
// NB we get all resources, since we care about leaving unsynced,
// _ignored_ resources alone.
clusterResources, err := c.getAllowedResourcesBySelector("")
if err != nil {
return errors.Wrap(err, "collating resources in cluster for sync")
}
cs := makeChangeSet()
var errs cluster.SyncError
var excluded []string
for _, res := range syncSet.Resources {
resID := res.ResourceID()
id := resID.String()
if !c.IsAllowedResource(resID) {
excluded = append(excluded, id)
continue
}
// make a record of the checksum, whether we stage it to
// be applied or not, so that we don't delete it later.
csum := sha1.Sum(res.Bytes())
checkHex := hex.EncodeToString(csum[:])
checksums[id] = checkHex
if res.Policies().Has(policy.Ignore) {
logger.Log("info", "not applying resource; ignore annotation in file", "resource", res.ResourceID(), "source", res.Source())
continue
}
// It's possible to give a cluster resource the "ignore"
// annotation directly -- e.g., with `kubectl annotate` -- so
// we need to examine the cluster resource here too.
if cres, ok := clusterResources[id]; ok && cres.Policies().Has(policy.Ignore) {
logger.Log("info", "not applying resource; ignore annotation in cluster resource", "resource", cres.ResourceID())
continue
}
resBytes, err := applyMetadata(res, syncSet.Name, checkHex)
if err == nil {
cs.stage("apply", res.ResourceID(), res.Source(), resBytes)
} else {
errs = append(errs, cluster.ResourceError{ResourceID: res.ResourceID(), Source: res.Source(), Error: err})
break
}
}
if len(excluded) > 0 {
logger.Log("warning", "not applying resources; excluded by namespace constraints", "resources", strings.Join(excluded, ","))
}
c.mu.Lock()
defer c.mu.Unlock()
c.muSyncErrors.RLock()
if applyErrs := c.applier.apply(logger, cs, c.syncErrors); len(applyErrs) > 0 {
errs = append(errs, applyErrs...)
}
c.muSyncErrors.RUnlock()
if c.GC || c.DryGC {
deleteErrs, gcFailure := c.collectGarbage(syncSet, checksums, logger, c.DryGC)
if gcFailure != nil {
return gcFailure
}
errs = append(errs, deleteErrs...)
}
// If `nil`, errs is a cluster.SyncError(nil) rather than error(nil), so it cannot be returned directly.
if errs == nil {
return nil
}
// It is expected that Cluster.Sync is invoked with *all* resources.
// Otherwise it will override previously recorded sync errors.
c.setSyncErrors(errs)
return errs
}
func (c *Cluster) collectGarbage(
syncSet cluster.SyncSet,
checksums map[string]string,
logger log.Logger,
dryRun bool) (cluster.SyncError, error) {
orphanedResources := makeChangeSet()
clusterResources, err := c.getAllowedGCMarkedResourcesInSyncSet(syncSet.Name)
if err != nil {
return nil, errors.Wrap(err, "collating resources in cluster for calculating garbage collection")
}
for resourceID, res := range clusterResources {
actual := res.GetChecksum()
expected, ok := checksums[resourceID]
switch {
case !ok: // was not recorded as having been staged for application
c.logger.Log("info", "cluster resource not in resources to be synced; deleting", "dry-run", dryRun, "resource", resourceID)
if !dryRun {
orphanedResources.stage("delete", res.ResourceID(), "<cluster>", res.IdentifyingBytes())
}
case actual != expected:
c.logger.Log("warning", "resource to be synced has not been updated; skipping", "dry-run", dryRun, "resource", resourceID)
continue
default:
// The checksum is the same, indicating that it was
// applied earlier. Leave it alone.
}
}
return c.applier.apply(logger, orphanedResources, nil), nil
}
// --- internals in support of Sync
type kuberesource struct {
obj *unstructured.Unstructured
namespaced bool
}
// ResourceID returns the ID for this resource loaded from the
// cluster.
func (r *kuberesource) ResourceID() resource.ID {
ns, kind, name := r.obj.GetNamespace(), r.obj.GetKind(), r.obj.GetName()
if !r.namespaced {
ns = kresource.ClusterScope
}
return resource.MakeID(ns, kind, name)
}
// Bytes returns a byte slice description, including enough info to
// identify the resource (but not momre)
func (r *kuberesource) IdentifyingBytes() []byte {
return []byte(fmt.Sprintf(`
apiVersion: %s
kind: %s
metadata:
namespace: %q
name: %q
`, r.obj.GetAPIVersion(), r.obj.GetKind(), r.obj.GetNamespace(), r.obj.GetName()))
}
func (r *kuberesource) Policies() policy.Set {
return kresource.PoliciesFromAnnotations(r.obj.GetAnnotations())
}
func (r *kuberesource) GetChecksum() string {
return r.obj.GetAnnotations()[checksumAnnotation]
}
func (r *kuberesource) GetGCMark() string {
return r.obj.GetLabels()[gcMarkLabel]
}
func (c *Cluster) getAllowedResourcesBySelector(selector string) (map[string]*kuberesource, error) {
listOptions := meta_v1.ListOptions{}
if selector != "" {
listOptions.LabelSelector = selector
}
sgs, err := c.client.discoveryClient.ServerGroups()
if sgs == nil {
return nil, err
}
resources := []*meta_v1.APIResourceList{}
for i := range sgs.Groups {
gv := sgs.Groups[i].PreferredVersion.GroupVersion
// exclude the *.metrics.k8s.io resources to avoid querying the cluster metrics
if sgs.Groups[i].Name != "metrics.k8s.io" && !strings.HasSuffix(sgs.Groups[i].Name, ".metrics.k8s.io") {
if r, err := c.client.discoveryClient.ServerResourcesForGroupVersion(gv); err == nil {
if r != nil {
resources = append(resources, r)
}
} else {
// ignore errors for resources with empty group version instead of failing to sync
if err.Error() != fmt.Sprintf("Got empty response for: %v", gv) {
return nil, err
}
}
}
}
result := map[string]*kuberesource{}
contains := func(a []string, x string) bool {
for _, n := range a {
if x == n {
return true
}
}
return false
}
for _, resource := range resources {
for _, apiResource := range resource.APIResources {
verbs := apiResource.Verbs
if !contains(verbs, "list") {
continue
}
groupVersion, err := schema.ParseGroupVersion(resource.GroupVersion)
if err != nil {
return nil, err
}
gvr := groupVersion.WithResource(apiResource.Name)
list, err := c.listAllowedResources(apiResource.Namespaced, gvr, listOptions)
if err != nil {
if apierrors.IsForbidden(err) {
// we are not allowed to list this resource but
// shouldn't prevent us from listing the rest
continue
}
return nil, err
}
for i, item := range list {
apiVersion := item.GetAPIVersion()
kind := item.GetKind()
itemDesc := fmt.Sprintf("%s:%s", apiVersion, kind)
// https://github.com/kontena/k8s-client/blob/6e9a7ba1f03c255bd6f06e8724a1c7286b22e60f/lib/k8s/stack.rb#L17-L22
if itemDesc == "v1:ComponentStatus" || itemDesc == "v1:Endpoints" {
continue
}
// TODO(michael) also exclude anything that has an ownerReference (that isn't "standard"?)
res := &kuberesource{obj: &list[i], namespaced: apiResource.Namespaced}
result[res.ResourceID().String()] = res
}
}
}
return result, nil
}
func (c *Cluster) listAllowedResources(
namespaced bool, gvr schema.GroupVersionResource, options meta_v1.ListOptions) ([]unstructured.Unstructured, error) {
if !namespaced {
// The resource is not namespaced, everything is allowed
resourceClient := c.client.dynamicClient.Resource(gvr)
data, err := resourceClient.List(options)
if err != nil {
return nil, err
}
return data.Items, nil
}
// List resources only from the allowed namespaces
namespaces, err := c.getAllowedAndExistingNamespaces(context.Background())
if err != nil {
return nil, err
}
var result []unstructured.Unstructured
for _, ns := range namespaces {
data, err := c.client.dynamicClient.Resource(gvr).Namespace(ns).List(options)
if err != nil {
return result, err
}
result = append(result, data.Items...)
}
return result, nil
}
func (c *Cluster) getAllowedGCMarkedResourcesInSyncSet(syncSetName string) (map[string]*kuberesource, error) {
allGCMarkedResources, err := c.getAllowedResourcesBySelector(gcMarkLabel) // means "gcMarkLabel exists"
if err != nil {
return nil, err
}
allowedSyncSetGCMarkedResources := map[string]*kuberesource{}
for resID, kres := range allGCMarkedResources {
// Discard resources whose mark doesn't match their resource ID
if kres.GetGCMark() != makeGCMark(syncSetName, resID) {
continue
}
allowedSyncSetGCMarkedResources[resID] = kres
}
return allowedSyncSetGCMarkedResources, nil
}
func applyMetadata(res resource.Resource, syncSetName, checksum string) ([]byte, error) {
definition := map[interface{}]interface{}{}
if err := yaml.Unmarshal(res.Bytes(), &definition); err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("failed to parse yaml from %s", res.Source()))
}
mixin := map[string]interface{}{}
if syncSetName != "" {
mixinLabels := map[string]string{}
mixinLabels[gcMarkLabel] = makeGCMark(syncSetName, res.ResourceID().String())
mixin["labels"] = mixinLabels
}
if checksum != "" {
mixinAnnotations := map[string]string{}
mixinAnnotations[checksumAnnotation] = checksum
mixin["annotations"] = mixinAnnotations
}
mergo.Merge(&definition, map[interface{}]interface{}{
"metadata": mixin,
})
bytes, err := yaml.Marshal(definition)
if err != nil {
return nil, errors.Wrap(err, "failed to serialize yaml after applying metadata")
}
return bytes, nil
}
func makeGCMark(syncSetName, resourceID string) string {
hasher := sha256.New()
hasher.Write([]byte(syncSetName))
// To prevent deleting objects with copied labels
// an object-specific mark is created (by including its identifier).
hasher.Write([]byte(resourceID))
// The prefix is to make sure it's a valid (Kubernetes) label value.
return "sha256." + base64.RawURLEncoding.EncodeToString(hasher.Sum(nil))
}
// --- internal types for keeping track of syncing
type applyObject struct {
ResourceID resource.ID
Source string
Payload []byte
}
type changeSet struct {
objs map[string][]applyObject
}
func makeChangeSet() changeSet {
return changeSet{objs: make(map[string][]applyObject)}
}
func (c *changeSet) stage(cmd string, id resource.ID, source string, bytes []byte) {
c.objs[cmd] = append(c.objs[cmd], applyObject{id, source, bytes})
}
// Applier is something that will apply a changeset to the cluster.
type Applier interface {
apply(log.Logger, changeSet, map[resource.ID]error) cluster.SyncError
}
type Kubectl struct {
exe string
config *rest.Config
}
func NewKubectl(exe string, config *rest.Config) *Kubectl {
return &Kubectl{
exe: exe,
config: config,
}
}
func (c *Kubectl) connectArgs() []string {
var args []string
if c.config.Host != "" {
args = append(args, fmt.Sprintf("--server=%s", c.config.Host))
}
if c.config.Username != "" {
args = append(args, fmt.Sprintf("--username=%s", c.config.Username))
}
if c.config.Password != "" {
args = append(args, fmt.Sprintf("--password=%s", c.config.Password))
}
if c.config.TLSClientConfig.CertFile != "" {
args = append(args, fmt.Sprintf("--client-certificate=%s", c.config.TLSClientConfig.CertFile))
}
if c.config.TLSClientConfig.CAFile != "" {
args = append(args, fmt.Sprintf("--certificate-authority=%s", c.config.TLSClientConfig.CAFile))
}
if c.config.TLSClientConfig.KeyFile != "" {
args = append(args, fmt.Sprintf("--client-key=%s", c.config.TLSClientConfig.KeyFile))
}
if c.config.BearerToken != "" {
args = append(args, fmt.Sprintf("--token=%s", c.config.BearerToken))
}
return args
}
// rankOfKind returns an int denoting the position of the given kind
// in the partial ordering of Kubernetes resources, according to which
// kinds depend on which (derived by hand).
func rankOfKind(kind string) int {
switch strings.ToLower(kind) {
// Namespaces answer to NOONE
case "namespace":
return 0
// These don't go in namespaces; or do, but don't depend on anything else
case "customresourcedefinition", "serviceaccount", "clusterrole", "role", "persistentvolume", "service":
return 1
// These depend on something above, but not each other
case "resourcequota", "limitrange", "secret", "configmap", "rolebinding", "clusterrolebinding", "persistentvolumeclaim", "ingress":
return 2
// Same deal, next layer
case "daemonset", "deployment", "replicationcontroller", "replicaset", "job", "cronjob", "statefulset":
return 3
// Assumption: anything not mentioned isn't depended _upon_, so
// can come last.
default:
return 4
}
}
type applyOrder []applyObject
func (objs applyOrder) Len() int {
return len(objs)
}
func (objs applyOrder) Swap(i, j int) {
objs[i], objs[j] = objs[j], objs[i]
}
func (objs applyOrder) Less(i, j int) bool {
_, ki, ni := objs[i].ResourceID.Components()
_, kj, nj := objs[j].ResourceID.Components()
ranki, rankj := rankOfKind(ki), rankOfKind(kj)
if ranki == rankj {
return ni < nj
}
return ranki < rankj
}
func (c *Kubectl) apply(logger log.Logger, cs changeSet, errored map[resource.ID]error) (errs cluster.SyncError) {
f := func(objs []applyObject, cmd string, args ...string) {
if len(objs) == 0 {
return
}
logger.Log("cmd", cmd, "args", strings.Join(args, " "), "count", len(objs))
args = append(args, cmd)
var multi, single []applyObject
if len(errored) == 0 {
multi = objs
} else {
for _, obj := range objs {
if _, ok := errored[obj.ResourceID]; ok {
// Resources that errored before shall be applied separately
single = append(single, obj)
} else {
// everything else will be tried in a multidoc apply.
multi = append(multi, obj)
}
}
}
if len(multi) > 0 {
if err := c.doCommand(logger, makeMultidoc(multi), args...); err != nil {
single = append(single, multi...)
}
}
for _, obj := range single {
r := bytes.NewReader(obj.Payload)
if err := c.doCommand(logger, r, args...); err != nil {
errs = append(errs, cluster.ResourceError{
ResourceID: obj.ResourceID,
Source: obj.Source,
Error: err,
})
}
}
}
// When deleting objects, the only real concern is that we don't
// try to delete things that have already been deleted by
// Kubernetes' GC -- most notably, resources in a namespace which
// is also being deleted. GC does not have the dependency ranking,
// but we can use it as a shortcut to avoid the above problem at
// least.
objs := cs.objs["delete"]
sort.Sort(sort.Reverse(applyOrder(objs)))
f(objs, "delete")
objs = cs.objs["apply"]
sort.Sort(applyOrder(objs))
f(objs, "apply")
return errs
}
func (c *Kubectl) doCommand(logger log.Logger, r io.Reader, args ...string) error {
args = append(args, "-f", "-")
cmd := c.kubectlCommand(args...)
cmd.Stdin = r
stderr := &bytes.Buffer{}
cmd.Stderr = stderr
stdout := &bytes.Buffer{}
cmd.Stdout = stdout
begin := time.Now()
err := cmd.Run()
if err != nil {
err = errors.Wrap(errors.New(strings.TrimSpace(stderr.String())), "running kubectl")
}
logger.Log("cmd", "kubectl "+strings.Join(args, " "), "took", time.Since(begin), "err", err, "output", strings.TrimSpace(stdout.String()))
return err
}
func makeMultidoc(objs []applyObject) *bytes.Buffer {
buf := &bytes.Buffer{}
for _, obj := range objs {
appendYAMLToBuffer(obj.Payload, buf)
}
return buf
}
func (c *Kubectl) kubectlCommand(args ...string) *exec.Cmd {
return exec.Command(c.exe, append(c.connectArgs(), args...)...)
}