-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
pruning.go
151 lines (127 loc) · 4.62 KB
/
pruning.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
/*
Copyright 2019 The Kubernetes 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 bootstrapchannelbuilder
import (
"fmt"
"sort"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog/v2"
channelsapi "k8s.io/kops/channels/pkg/api"
"k8s.io/kops/pkg/kubemanifest"
)
func (b *BootstrapChannelBuilder) addPruneDirectives(addons *AddonList) error {
for _, addon := range addons.Items {
if !addon.BuildPrune {
continue
}
id := *addon.Spec.Name
if err := b.addPruneDirectivesForAddon(addon); err != nil {
return fmt.Errorf("failed to configure pruning for %s: %w", id, err)
}
}
return nil
}
func (b *BootstrapChannelBuilder) addPruneDirectivesForAddon(addon *Addon) error {
addon.Spec.Prune = &channelsapi.PruneSpec{}
// We add these labels to all objects we manage, so we reuse them for pruning.
selectorMap := map[string]string{
"app.kubernetes.io/managed-by": "kops",
"addon.kops.k8s.io/name": *addon.Spec.Name,
}
selector, err := labels.ValidatedSelectorFromSet(selectorMap)
if err != nil {
return fmt.Errorf("error parsing selector %v: %w", selectorMap, err)
}
// We always include a set of well-known group kinds,
// so that we prune even if we end up removing something from the manifest.
alwaysPruneGroupKinds := []schema.GroupKind{
{Group: "", Kind: "ConfigMap"},
{Group: "", Kind: "Service"},
{Group: "", Kind: "ServiceAccount"},
{Group: "apps", Kind: "Deployment"},
{Group: "apps", Kind: "DaemonSet"},
{Group: "apps", Kind: "StatefulSet"},
{Group: "rbac.authorization.k8s.io", Kind: "ClusterRole"},
{Group: "rbac.authorization.k8s.io", Kind: "ClusterRoleBinding"},
{Group: "rbac.authorization.k8s.io", Kind: "Role"},
{Group: "rbac.authorization.k8s.io", Kind: "RoleBinding"},
{Group: "policy", Kind: "PodDisruptionBudget"},
}
pruneGroupKind := make(map[schema.GroupKind]bool)
for _, gk := range alwaysPruneGroupKinds {
pruneGroupKind[gk] = true
}
// In addition, we deliberately exclude a few types that are riskier to delete:
//
// * Namespace: because it deletes anything else that happens to be in the namespace
//
// * CustomResourceDefinition: because it deletes all instances of the CRD
neverPruneGroupKinds := map[schema.GroupKind]bool{
{Group: "", Kind: "Namespace"}: true,
{Group: "apiextensions.k8s.io", Kind: "CustomResourceDefinition"}: true,
}
// Parse the manifest; we use this to scope pruning to namespaces
objects, err := kubemanifest.LoadObjectsFrom(addon.ManifestData)
if err != nil {
return fmt.Errorf("failed to parse manifest: %w", err)
}
objectsByGK := make(map[schema.GroupKind][]*kubemanifest.Object)
for _, object := range objects {
gv, err := schema.ParseGroupVersion(object.APIVersion())
if err != nil || gv.Version == "" {
return fmt.Errorf("failed to parse apiVersion %q", object.APIVersion())
}
gvk := gv.WithKind(object.Kind())
if gvk.Kind == "" {
return fmt.Errorf("failed to get kind for object")
}
gk := gvk.GroupKind()
objectsByGK[gk] = append(objectsByGK[gk], object)
// Warn if there are objects in the manifest that we haven't considered
if !pruneGroupKind[gk] {
if !neverPruneGroupKinds[gk] {
klog.Warningf("manifest includes an object of GroupKind %v, which will not be pruned", gk)
}
}
}
var groupKinds []schema.GroupKind
for gk := range pruneGroupKind {
groupKinds = append(groupKinds, gk)
}
sort.Slice(groupKinds, func(i, j int) bool {
if groupKinds[i].Group != groupKinds[j].Group {
return groupKinds[i].Group < groupKinds[j].Group
}
return groupKinds[i].Kind < groupKinds[j].Kind
})
for _, gk := range groupKinds {
pruneSpec := channelsapi.PruneKindSpec{}
pruneSpec.Group = gk.Group
pruneSpec.Kind = gk.Kind
namespaces := sets.NewString()
for _, object := range objectsByGK[gk] {
namespace := object.GetNamespace()
if namespace != "" {
namespaces.Insert(namespace)
}
}
if namespaces.Len() != 0 {
pruneSpec.Namespaces = namespaces.List()
}
pruneSpec.LabelSelector = selector.String()
addon.Spec.Prune.Kinds = append(addon.Spec.Prune.Kinds, pruneSpec)
}
return nil
}