This repository has been archived by the owner on May 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
deploy.go
472 lines (416 loc) · 15.4 KB
/
deploy.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
package client
import (
"encoding/base64"
"fmt"
"log"
"strings"
"time"
"github.com/pkg/errors"
"github.com/replicatedhq/kotsadm/operator/pkg/applier"
"github.com/replicatedhq/kotsadm/operator/pkg/util"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sruntime "k8s.io/apimachinery/pkg/runtime"
k8sschema "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client/config"
)
type applyResult struct {
hasErr bool
multiStdout [][]byte
multiStderr [][]byte
}
func (c *Client) diffAndRemovePreviousManifests(applicationManifests ApplicationManifests) error {
decodedPrevious, err := base64.StdEncoding.DecodeString(applicationManifests.PreviousManifests)
if err != nil {
return errors.Wrap(err, "failed to decode previous manifests")
}
decodedCurrent, err := base64.StdEncoding.DecodeString(applicationManifests.Manifests)
if err != nil {
return errors.Wrap(err, "failed to decode manifests")
}
// we need to find the gvk+names that are present in the previous, but not in the current and then remove them
decodedPreviousStrings := strings.Split(string(decodedPrevious), "\n---\n")
decodedPreviousMap := map[string]string{}
for _, decodedPreviousString := range decodedPreviousStrings {
decodedPreviousMap[GetGVKWithName([]byte(decodedPreviousString))] = decodedPreviousString
}
// now get the current names
decodedCurrentStrings := strings.Split(string(decodedCurrent), "\n---\n")
decodedCurrentMap := map[string]string{}
for _, decodedCurrentString := range decodedCurrentStrings {
decodedCurrentMap[GetGVKWithName([]byte(decodedCurrentString))] = decodedCurrentString
}
// now remove anything that's in previous but not in current
kubectl, err := util.FindKubectlVersion(applicationManifests.KubectlVersion)
if err != nil {
return errors.Wrap(err, "failed to find kubectl")
}
config, err := rest.InClusterConfig()
if err != nil {
return errors.Wrap(err, "failed to get in cluster config")
}
// this is pretty raw, and required kubectl... we should
// consider some other options here?
kubernetesApplier := applier.NewKubectl(kubectl, "", "", config)
targetNamespace := c.TargetNamespace
if applicationManifests.Namespace != "." {
targetNamespace = applicationManifests.Namespace
}
allPVCs := make([]string, 0)
for k, oldContents := range decodedPreviousMap {
if _, ok := decodedCurrentMap[k]; ok {
continue
}
obj, gvk, err := parseK8sYaml([]byte(oldContents))
if err != nil {
log.Printf("deleting unidentified manifest. unable to parse error: %s", err.Error())
}
group := ""
kind := ""
if obj != nil && gvk != nil {
group = gvk.Group
kind = gvk.Kind
log.Printf("deleting manifest(s): %s/%s/%s", group, kind, getObjectName(obj))
pvcs, err := getPVCs(targetNamespace, obj, gvk)
if err != nil {
return errors.Wrap(err, "failed to list PVCs")
}
allPVCs = append(allPVCs, pvcs...)
}
wait := applicationManifests.Wait
if gvk != nil && gvk.Kind == "PersistentVolumeClaim" {
// blocking on PVC delete will create a deadlock if
// it's used by a pod that has not been deleted yet.
wait = false
}
stdout, stderr, err := kubernetesApplier.Remove(targetNamespace, []byte(oldContents), wait)
if err != nil {
log.Printf("stdout (delete) = %s", stdout)
log.Printf("stderr (delete) = %s", stderr)
log.Printf("error: %s", err.Error())
} else {
log.Printf("manifest(s) deleted: %s/%s/%s", group, kind, getObjectName(obj))
}
}
if applicationManifests.ClearPVCs {
log.Printf("deleting pvcs: %s", strings.Join(allPVCs, ","))
// TODO: multi-namespace support
err := deletePVCs(targetNamespace, allPVCs)
if err != nil {
return errors.Wrap(err, "failed to delete PVCs")
}
}
for _, namespace := range applicationManifests.ClearNamespaces {
log.Printf("Ensuring all %s objects have been removed from namespace %s\n", applicationManifests.AppSlug, namespace)
for i := 10; i >= 0; i-- {
gone, err := c.clearNamespace(applicationManifests.AppSlug, namespace)
if err != nil {
log.Printf("Failed to check if app %s objects have been removed from namespace %s: %v\n", applicationManifests.AppSlug, namespace, err)
} else if gone {
break
}
if i == 0 {
return fmt.Errorf("Failed to clear app %s from namespace %s\n", applicationManifests.AppSlug, namespace)
}
log.Printf("Namespace %s still has objects from app %s: sleeping...\n", namespace, applicationManifests.AppSlug)
time.Sleep(time.Second * 2)
}
log.Printf("Namepsace %s successfully cleared of app %s\n", namespace, applicationManifests.AppSlug)
}
if len(applicationManifests.ClearNamespaces) > 0 {
// Extra time in case the app-slug annotation was not being used.
time.Sleep(time.Second * 20)
}
return nil
}
func (c *Client) ensureNamespacePresent(name string) error {
restconfig, err := rest.InClusterConfig()
if err != nil {
return errors.Wrap(err, "failed to get in cluster config")
}
clientset, err := kubernetes.NewForConfig(restconfig)
if err != nil {
return errors.Wrap(err, "failed to get new kubernetes client")
}
_, err = clientset.CoreV1().Namespaces().Get(name, metav1.GetOptions{})
if kuberneteserrors.IsNotFound(err) {
namespace := &corev1.Namespace{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Namespace",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}
_, err = clientset.CoreV1().Namespaces().Create(namespace)
if err != nil {
return errors.Wrap(err, "failed to create namespace")
}
}
return nil
}
func (c *Client) ensureResourcesPresent(applicationManifests ApplicationManifests) (*applyResult, error) {
targetNamespace := c.TargetNamespace
if applicationManifests.Namespace != "." {
targetNamespace = applicationManifests.Namespace
}
kubernetesApplier, err := c.getApplier(applicationManifests.KubectlVersion)
if err != nil {
return nil, errors.Wrap(err, "failed to get applier")
}
decoded, err := base64.StdEncoding.DecodeString(applicationManifests.Manifests)
if err != nil {
return nil, errors.Wrap(err, "failed to decode manifests")
}
customResourceDefinitions, otherDocs, err := splitMutlidocYAMLIntoCRDsAndOthers(decoded)
if err != nil {
return nil, errors.Wrap(err, "failed to split decoded into crds and other")
}
// We don't dry run if there's a crd becasue there's a likely chance that the
// other docs has a custom resource using it
shouldDryRun := customResourceDefinitions == nil
if shouldDryRun {
byNamespace, err := docsByNamespace(decoded, targetNamespace)
if err != nil {
return nil, errors.Wrap(err, "failed to get docs by requested namespace")
}
for requestedNamespace, docs := range byNamespace {
if len(docs) == 0 {
continue
}
log.Printf("dry run applying manifests(s) in requested namespace: %s", requestedNamespace)
dryrunStdout, dryrunStderr, dryRunErr := kubernetesApplier.Apply(requestedNamespace, applicationManifests.AppSlug, docs, true, applicationManifests.Wait, applicationManifests.AnnotateSlug)
if dryRunErr != nil {
log.Printf("stdout (dryrun) = %s", dryrunStdout)
log.Printf("stderr (dryrun) = %s", dryrunStderr)
log.Printf("error: %s", dryRunErr.Error())
} else {
log.Printf("dry run applied manifests(s) in requested namespace: %s", requestedNamespace)
}
if dryRunErr != nil {
if err := c.sendResult(applicationManifests, true, dryrunStdout, dryrunStderr, []byte{}, []byte{}); err != nil {
return nil, errors.Wrap(err, "failed to report dry run status")
}
return nil, nil // don't return an error because execution is proper, the api now has the error
}
}
}
if len(customResourceDefinitions) > 0 {
log.Println("applying custom resource definition(s)")
// CRDs don't have namespaces, so we can skip splitting
applyStdout, applyStderr, applyErr := kubernetesApplier.Apply("", applicationManifests.AppSlug, customResourceDefinitions, false, applicationManifests.Wait, applicationManifests.AnnotateSlug)
if applyErr != nil {
log.Printf("stdout (apply CRDS) = %s", applyStdout)
log.Printf("stderr (apply CRDS) = %s", applyStderr)
log.Printf("error (CRDS): %s", applyErr.Error())
if err := c.sendResult(applicationManifests, applyErr != nil, []byte{}, []byte{}, applyStdout, applyStderr); err != nil {
return nil, errors.Wrap(err, "failed to report crd status")
}
return nil, nil
} else {
log.Println("custom resource definition(s) applied")
}
// Give the API server a minute (well, 5 seconds) to cache the CRDs
time.Sleep(time.Second * 5)
}
byNamespace, err := docsByNamespace(otherDocs, targetNamespace)
if err != nil {
return nil, errors.Wrap(err, "failed to get docs by requested namespace")
}
var hasErr bool
var multiStdout, multiStderr [][]byte
for requestedNamespace, docs := range byNamespace {
if len(docs) == 0 {
continue
}
log.Printf("applying manifest(s) in namespace %s", requestedNamespace)
applyStdout, applyStderr, applyErr := kubernetesApplier.Apply(requestedNamespace, applicationManifests.AppSlug, docs, false, applicationManifests.Wait, applicationManifests.AnnotateSlug)
if applyErr != nil {
log.Printf("stdout (apply) = %s", applyStdout)
log.Printf("stderr (apply) = %s", applyStderr)
log.Printf("error: %s", applyErr.Error())
hasErr = true
} else {
log.Printf("manifest(s) applied in namespace %s", requestedNamespace)
}
if len(applyStdout) > 0 {
multiStdout = append(multiStdout, applyStdout)
}
if len(applyStderr) > 0 {
multiStderr = append(multiStderr, applyStderr)
}
}
result := &applyResult{
hasErr: hasErr,
multiStderr: multiStderr,
multiStdout: multiStdout,
}
return result, nil
}
func (c *Client) clearNamespace(slug string, namespace string) (bool, error) {
cfg, err := config.GetConfig()
if err != nil {
return false, errors.Wrap(err, "failed to get config")
}
disc, err := discovery.NewDiscoveryClientForConfig(cfg)
if err != nil {
return false, errors.Wrap(err, "failed to create discovery client")
}
dyn, err := dynamic.NewForConfig(cfg)
if err != nil {
return false, errors.Wrap(err, "failed to create dynamic client")
}
resourceList, err := disc.ServerPreferredNamespacedResources()
if err != nil {
// An application can define an APIService handled by a Deployment in the application itself.
// In that case there will be a race condition listing resources in that API group and there
// could be an error here. Most of the API groups would still be in the resource list so the
// error is not terminal.
log.Printf("Failed to list all resources: %v", err)
}
gvrs, err := discovery.GroupVersionResources(resourceList)
if err != nil {
return false, errors.Wrap(err, "failed to convert resource list to groupversionresource map")
}
// skip resources that don't have API endpoints or don't have applied objects
var skip = sets.NewString(
"/v1/bindings",
"/v1/events",
"extensions/v1beta1/replicationcontrollers",
"apps/v1/controllerrevisions",
"authentication.k8s.io/v1/tokenreviews",
"authorization.k8s.io/v1/localsubjectaccessreviews",
"authorization.k8s.io/v1/subjectaccessreviews",
"authorization.k8s.io/v1/selfsubjectaccessreviews",
"authorization.k8s.io/v1/selfsubjectrulesreviews",
)
clear := true
for gvr := range gvrs {
s := fmt.Sprintf("%s/%s/%s", gvr.Group, gvr.Version, gvr.Resource)
if skip.Has(s) {
continue
}
// there may be other resources that can't be listed besides what's in the skip set so ignore error
unstructuredList, _ := dyn.Resource(gvr).Namespace(namespace).List(metav1.ListOptions{})
for _, u := range unstructuredList.Items {
annotations := u.GetAnnotations()
if annotations["kots.io/app-slug"] == slug {
clear = false
if u.GetDeletionTimestamp() != nil {
log.Printf("%s %s is pending deletion\n", gvr, u.GetName())
continue
}
log.Printf("Deleting %s/%s/%s\n", namespace, gvr, u.GetName())
err := dyn.Resource(gvr).Namespace(namespace).Delete(u.GetName(), &metav1.DeleteOptions{})
if err != nil {
log.Printf("Resource %s (%s) in namepsace %s could not be deleted: %v\n", u.GetName(), gvr, namespace, err)
return false, err
}
}
}
}
return clear, nil
}
func parseK8sYaml(doc []byte) (k8sruntime.Object, *k8sschema.GroupVersionKind, error) {
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, gvk, err := decode(doc, nil, nil)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to decode k8s yaml")
}
return obj, gvk, err
}
func getObjectName(obj k8sruntime.Object) string {
// TODO: something like...
// if o, ok := obj.(metav1.ObjectMeta); ok {
// return o.Name
// }
return ""
}
func getPVCs(targetNamespace string, obj k8sruntime.Object, gvk *k8sschema.GroupVersionKind) ([]string, error) {
var err error
var pods []*corev1.Pod
ns := func(objNs string) string {
if objNs != "" {
return objNs
}
return targetNamespace
}
if gvk.Group == "apps" && gvk.Version == "v1" && gvk.Kind == "Deployment" {
o := obj.(*appsv1.Deployment)
pods, err = findPodsByOwner(o.Name, ns(o.Namespace), gvk)
if err != nil {
return nil, errors.Wrapf(err, "failed to find pods for deployment %s", o.Name)
}
} else if gvk.Group == "apps" && gvk.Version == "v1" && gvk.Kind == "StatefulSet" {
o := obj.(*appsv1.StatefulSet)
pods, err = findPodsByOwner(o.Name, ns(o.Namespace), gvk)
if err != nil {
return nil, errors.Wrapf(err, "failed to find pods for stateful set %s", o.Name)
}
} else if gvk.Group == "batch" && gvk.Version == "v1" && gvk.Kind == "Job" {
o := obj.(*batchv1.Job)
pods, err = findPodsByOwner(o.Name, ns(o.Namespace), gvk)
if err != nil {
return nil, errors.Wrapf(err, "failed to find pods for job %s", o.Name)
}
} else if gvk.Group == "batch" && gvk.Version == "v1beta1" && gvk.Kind == "CronJob" {
o := obj.(*batchv1beta1.CronJob)
pods, err = findPodsByOwner(o.Name, ns(o.Namespace), gvk)
if err != nil {
return nil, errors.Wrapf(err, "failed to find pods for cron job %s", o.Name)
}
} else if gvk.Group == "" && gvk.Version == "v1" && gvk.Kind == "Pod" {
o := obj.(*corev1.Pod)
pod, err := findPodByName(o.Name, ns(o.Namespace))
if err != nil {
if !kuberneteserrors.IsNotFound(err) {
return nil, errors.Wrapf(err, "failed to find pod %s", o.Name)
}
}
pods = []*corev1.Pod{pod}
}
pvcs := make([]string, 0)
for _, pod := range pods {
for _, v := range pod.Spec.Volumes {
if v.PersistentVolumeClaim != nil {
pvcs = append(pvcs, v.PersistentVolumeClaim.ClaimName)
}
}
}
return pvcs, nil
}
func deletePVCs(namespace string, pvcs []string) error {
cfg, err := config.GetConfig()
if err != nil {
return errors.Wrap(err, "failed to get config")
}
clientset, err := kubernetes.NewForConfig(cfg)
if err != nil {
return errors.Wrap(err, "failed to get client set")
}
for _, pvc := range pvcs {
grace := int64(0)
policy := metav1.DeletePropagationBackground
opts := &metav1.DeleteOptions{
GracePeriodSeconds: &grace,
PropagationPolicy: &policy,
}
log.Printf("deleting pvc: %s", pvc)
err := clientset.CoreV1().PersistentVolumeClaims(namespace).Delete(pvc, opts)
if err != nil {
return errors.Wrapf(err, "failed to delete pvc %s", pvc)
}
}
return nil
}