-
Notifications
You must be signed in to change notification settings - Fork 1
/
apply.go
258 lines (224 loc) · 6.63 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
package k8sutil
import (
"encoding/json"
"fmt"
"sort"
"github.com/bryanl/woowoo/pkg/client"
"github.com/ksonnet/ksonnet/utils"
log "github.com/sirupsen/logrus"
"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/types"
kdiff "k8s.io/apimachinery/pkg/util/diff"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
)
const (
// AnnotationGcTag annotation that triggers
// garbage collection. Objects with value equal to
// command-line flag that are *not* in config will be deleted.
AnnotationGcTag = "kubecfg.ksonnet.io/garbage-collect-tag"
// AnnotationGcStrategy controls gc logic. Current values:
// `auto` (default if absent) - do garbage collection
// `ignore` - never garbage collect this object
AnnotationGcStrategy = "kubecfg.ksonnet.io/garbage-collect-strategy"
// GcStrategyAuto is the default automatic gc logic
GcStrategyAuto = "auto"
// GcStrategyIgnore means this object should be ignored by garbage collection
GcStrategyIgnore = "ignore"
)
// ApplyCmd represents the apply subcommand
type ApplyCmd struct {
ClientConfig *client.Config
Env string
Create bool
GcTag string
SkipGc bool
DryRun bool
}
// Run applies the components to the designated environment cluster.
func (c ApplyCmd) Run(apiObjects []*unstructured.Unstructured, wd string) error {
clientPool, discovery, namespace, err := c.ClientConfig.RestClient(&c.Env)
if err != nil {
return err
}
dryRunText := ""
if c.DryRun {
dryRunText = " (dry-run)"
}
sort.Sort(utils.DependencyOrder(apiObjects))
seenUids := sets.NewString()
for _, obj := range apiObjects {
if c.GcTag != "" {
utils.SetMetaDataAnnotation(obj, AnnotationGcTag, c.GcTag)
}
desc := fmt.Sprintf("%s %s", utils.ResourceNameFor(discovery, obj), utils.FqName(obj))
log.Info("Updating ", desc, dryRunText)
rc, err := utils.ClientForResource(clientPool, discovery, obj, namespace)
if err != nil {
return err
}
asPatch, err := json.Marshal(obj)
if err != nil {
return err
}
var newobj metav1.Object
if !c.DryRun {
newobj, err = rc.Patch(obj.GetName(), types.MergePatchType, asPatch)
log.Debugf("Patch(%s) returned (%v, %v)", obj.GetName(), newobj, err)
} else {
newobj, err = rc.Get(obj.GetName(), metav1.GetOptions{})
}
if c.Create && errors.IsNotFound(err) {
log.Info(" Creating non-existent ", desc, dryRunText)
if !c.DryRun {
newobj, err = rc.Create(obj)
log.Debugf("Create(%s) returned (%v, %v)", obj.GetName(), newobj, err)
} else {
newobj = obj
err = nil
}
}
if err != nil {
// TODO: retry
return fmt.Errorf("Error updating %s: %s", desc, err)
}
log.Debug("Updated object: ", kdiff.ObjectDiff(obj, newobj))
// Some objects appear under multiple kinds
// (eg: Deployment is both extensions/v1beta1
// and apps/v1beta1). UID is the only stable
// identifier that links these two views of
// the same object.
seenUids.Insert(string(newobj.GetUID()))
}
if c.GcTag != "" && !c.SkipGc {
version, err := utils.FetchVersion(discovery)
if err != nil {
return err
}
err = walkObjects(clientPool, discovery, metav1.ListOptions{}, func(o runtime.Object) error {
meta, err := meta.Accessor(o)
if err != nil {
return err
}
gvk := o.GetObjectKind().GroupVersionKind()
desc := fmt.Sprintf("%s %s (%s)", utils.ResourceNameFor(discovery, o), utils.FqName(meta), gvk.GroupVersion())
log.Debugf("Considering %v for gc", desc)
if eligibleForGc(meta, c.GcTag) && !seenUids.Has(string(meta.GetUID())) {
log.Info("Garbage collecting ", desc, dryRunText)
if !c.DryRun {
err := gcDelete(clientPool, discovery, &version, o)
if err != nil {
return err
}
}
}
return nil
})
if err != nil {
return err
}
}
return nil
}
func stringListContains(list []string, value string) bool {
for _, item := range list {
if item == value {
return true
}
}
return false
}
func gcDelete(clientpool dynamic.ClientPool, disco discovery.DiscoveryInterface, version *utils.ServerVersion, o runtime.Object) error {
obj, err := meta.Accessor(o)
if err != nil {
return fmt.Errorf("Unexpected object type: %s", err)
}
uid := obj.GetUID()
desc := fmt.Sprintf("%s %s", utils.ResourceNameFor(disco, o), utils.FqName(obj))
deleteOpts := metav1.DeleteOptions{
Preconditions: &metav1.Preconditions{UID: &uid},
}
if version.Compare(1, 6) < 0 {
// 1.5.x option
boolFalse := false
deleteOpts.OrphanDependents = &boolFalse
} else {
// 1.6.x option (NB: Background is broken)
fg := metav1.DeletePropagationForeground
deleteOpts.PropagationPolicy = &fg
}
c, err := utils.ClientForResource(clientpool, disco, o, metav1.NamespaceNone)
if err != nil {
return err
}
err = c.Delete(obj.GetName(), &deleteOpts)
if err != nil && (errors.IsNotFound(err) || errors.IsConflict(err)) {
// We lost a race with something else changing the object
log.Debugf("Ignoring error while deleting %s: %s", desc, err)
err = nil
}
if err != nil {
return fmt.Errorf("Error deleting %s: %s", desc, err)
}
return nil
}
func walkObjects(pool dynamic.ClientPool, disco discovery.DiscoveryInterface, listopts metav1.ListOptions, callback func(runtime.Object) error) error {
rsrclists, err := disco.ServerResources()
if err != nil {
return err
}
for _, rsrclist := range rsrclists {
gv, err := schema.ParseGroupVersion(rsrclist.GroupVersion)
if err != nil {
return err
}
for _, rsrc := range rsrclist.APIResources {
gvk := gv.WithKind(rsrc.Kind)
if !stringListContains(rsrc.Verbs, "list") {
log.Debugf("Don't know how to list %v, skipping", rsrc)
continue
}
client, err := pool.ClientForGroupVersionKind(gvk)
if err != nil {
return err
}
var ns string
if rsrc.Namespaced {
ns = metav1.NamespaceAll
} else {
ns = metav1.NamespaceNone
}
rc := client.Resource(&rsrc, ns)
log.Debugf("Listing %s", gvk)
obj, err := rc.List(listopts)
if err != nil {
return err
}
if err = meta.EachListItem(obj, callback); err != nil {
return err
}
}
}
return nil
}
func eligibleForGc(obj metav1.Object, gcTag string) bool {
for _, ref := range obj.GetOwnerReferences() {
if ref.Controller != nil && *ref.Controller {
// Has a controller ref
return false
}
}
a := obj.GetAnnotations()
strategy, ok := a[AnnotationGcStrategy]
if !ok {
strategy = GcStrategyAuto
}
return a[AnnotationGcTag] == gcTag &&
strategy == GcStrategyAuto
}