This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
/
apply.go
281 lines (235 loc) · 7.22 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
// Copyright 2018 The ksonnet authors
//
//
// 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 cluster
import (
"fmt"
"sort"
"time"
"github.com/ksonnet/ksonnet/pkg/app"
"github.com/ksonnet/ksonnet/pkg/client"
"github.com/ksonnet/ksonnet/pkg/metadata"
"github.com/ksonnet/ksonnet/utils"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
kerrors "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/util/sets"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
)
const (
// applyConflictRetryCount sets how many times an apply is retried before giving up
// after a conflict error is detected.
applyConflictRetryCount = 5
// defaultConflictTimeout sets the wait time before retrying after a conflict is detected.
defaultConflictTimeout = 1 * time.Second
appKsonnet = "ksonnet"
)
var (
errApplyConflict = errors.Errorf("apply conflict detected; retried %d times", applyConflictRetryCount)
)
// ApplyConfig is configuration for Apply.
type ApplyConfig struct {
App app.App
ClientConfig *client.Config
ComponentNames []string
Create bool
DryRun bool
EnvName string
GcTag string
SkipGc bool
}
// ApplyOpts are options for configuring Apply.
type ApplyOpts func(a *Apply)
// Apply applies objects to the cluster
type Apply struct {
ApplyConfig
// these make it easier to test Apply.
findObjectsFn findObjectsFn
resourceClientFactory resourceClientFactoryFn
clientOpts *Clients
objectInfo ObjectInfo
ksonnetObjectFactory func() ksonnetObject
upserterFactory func() Upserter
conflictTimeout time.Duration
}
// RunApply runs apply against a cluster given a configuration.
func RunApply(config ApplyConfig, opts ...ApplyOpts) error {
if config.ClientConfig == nil {
return errors.New("ksonnet client config is required")
}
a := &Apply{
ApplyConfig: config,
findObjectsFn: findObjects,
resourceClientFactory: resourceClientFactory,
objectInfo: &objectInfo{},
ksonnetObjectFactory: func() ksonnetObject {
factory := cmdutil.NewFactory(config.ClientConfig.Config)
return newDefaultKsonnetObject(factory)
},
conflictTimeout: 1 * time.Second,
}
for _, opt := range opts {
opt(a)
}
if a.clientOpts == nil {
co, err := GenClients(a.App, a.ClientConfig, a.EnvName)
if err != nil {
return err
}
a.clientOpts = &co
}
if a.upserterFactory == nil {
u, err := newDefaultUpserter(a.ApplyConfig, a.objectInfo, *a.clientOpts, a.resourceClientFactory)
if err != nil {
return errors.Wrap(err, "creating upserter")
}
a.upserterFactory = func() Upserter {
return u
}
}
return a.Apply()
}
// Apply applies against a cluster.
func (a *Apply) Apply() error {
apiObjects, err := a.findObjectsFn(a.App, a.EnvName, a.ComponentNames)
if err != nil {
return errors.Wrap(err, "find objects")
}
sort.Sort(utils.DependencyOrder(apiObjects))
seenUids := sets.NewString()
for _, obj := range apiObjects {
var uid string
uid, err = a.handleObject(obj)
if err != nil {
return errors.Wrap(err, "handle object")
}
// 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(uid)
}
if a.GcTag != "" && !a.SkipGc {
if err = a.runGc(seenUids); err != nil {
return errors.Wrap(err, "run gc")
}
}
return nil
}
func (a *Apply) handleObject(obj *unstructured.Unstructured) (string, error) {
if err := a.preprocessObject(obj); err != nil {
return "", errors.Wrap(err, "preprocessing object before apply")
}
mergedObject, err := a.patchFromCluster(obj)
if err != nil {
return "", errors.Wrap(err, "patching object from cluster")
}
a.setupGC(mergedObject)
return a.upsert(mergedObject)
}
// preprocessObject preprocesses an object for it is applied to the cluster.
func (a *Apply) preprocessObject(obj *unstructured.Unstructured) error {
aa := newDefaultAnnotationApplier()
if !a.DryRun {
return errors.Wrap(aa.SetOriginalConfiguration(obj), "tagging ksonnet managed object")
}
log.Info("tagging ksonnet managed object", a.dryRunText())
return nil
}
// patchFromCluster patches an object with values that may exist in the cluster.
func (a *Apply) patchFromCluster(obj *unstructured.Unstructured) (*unstructured.Unstructured, error) {
return a.ksonnetObjectFactory().MergeFromCluster(*a.clientOpts, obj)
}
func (a *Apply) upsert(obj *unstructured.Unstructured) (string, error) {
if a.DryRun {
log.Info("upserting object", a.dryRunText())
return "12345", nil
}
u := a.upserterFactory()
for i := applyConflictRetryCount; i > 0; i-- {
uid, err := u.Upsert(obj)
if err != nil {
cause := errors.Cause(err)
if !kerrors.IsConflict(cause) {
return "", err
}
// In order for the next try to work, update the resource version on the object
updatedObj, err := a.getUpdatedObject(obj)
if err == nil {
obj.SetResourceVersion(updatedObj.GetResourceVersion())
}
time.Sleep(a.conflictTimeout)
continue
}
return uid, nil
}
return "", errApplyConflict
}
func (a *Apply) getUpdatedObject(obj *unstructured.Unstructured) (*unstructured.Unstructured, error) {
rc, err := a.resourceClientFactory(*a.clientOpts, obj)
if err != nil {
return nil, err
}
return rc.Get(metav1.GetOptions{})
}
// setupGC setups ksonnet's garbage collection process for objects.
func (a *Apply) setupGC(obj *unstructured.Unstructured) {
if a.GcTag != "" {
SetMetaDataAnnotation(obj, metadata.AnnotationGcTag, a.GcTag)
}
}
func (a *Apply) runGc(seenUids sets.String) error {
co := a.clientOpts
version, err := utils.FetchVersion(co.discovery)
if err != nil {
return err
}
err = walkObjects(*co, metav1.ListOptions{}, func(o runtime.Object) error {
var metav1Object metav1.Object
metav1Object, err = meta.Accessor(o)
if err != nil {
return err
}
gvk := o.GetObjectKind().GroupVersionKind()
desc := fmt.Sprintf("%s %s (%s)",
utils.ResourceNameFor(co.discovery, o), utils.FqName(metav1Object), gvk.GroupVersion())
log.Debugf("Considering %v for gc", desc)
if eligibleForGc(metav1Object, a.GcTag) && !seenUids.Has(string(metav1Object.GetUID())) {
log.Info("Garbage collecting ", desc, a.dryRunText())
if !a.DryRun {
err = gcDelete(*co, a.resourceClientFactory, &version, o)
if err != nil {
return err
}
}
}
return nil
})
if err != nil {
return err
}
return nil
}
func (a *Apply) dryRunText() string {
text := ""
if a.DryRun {
text = " (dry-run)"
}
return text
}