forked from kubernetes-sigs/cluster-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjectgraph.go
491 lines (411 loc) · 15.9 KB
/
objectgraph.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
/*
Copyright 2020 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 cluster
import (
"fmt"
"strings"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"
addonsv1 "sigs.k8s.io/cluster-api/exp/addons/api/v1alpha4"
secretutil "sigs.k8s.io/cluster-api/util/secret"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type empty struct{}
type ownerReferenceAttributes struct {
Controller *bool
BlockOwnerDeletion *bool
}
// node defines a node in the Kubernetes object graph that is visited during the discovery phase for the move operation.
type node struct {
identity corev1.ObjectReference
// owners contains the list of nodes that are owned by the current node.
owners map[*node]ownerReferenceAttributes
// softOwners contains the list of nodes that are soft-owned by the current node.
// E.g. secrets are soft-owned by a cluster via a naming convention, but without an explicit OwnerReference.
softOwners map[*node]empty
// forceMove is set to true if the CRD of this object has the "move" label attached.
// This ensures the node is moved, regardless of its owner refs.
forceMove bool
// isGlobal gets set to true if this object is a global resource (no namespace).
isGlobal bool
// virtual records if this node was discovered indirectly, e.g. by processing an OwnerRef, but not yet observed as a concrete object.
virtual bool
//newID stores the new UID the objects gets once created in the target cluster.
newUID types.UID
// tenantClusters define the list of Clusters which are tenant for the node, no matter if the node has a direct OwnerReference to the Cluster or if
// the node is linked to a Cluster indirectly in the OwnerReference chain.
tenantClusters map[*node]empty
// tenantCRSs define the list of ClusterResourceSet which are tenant for the node, no matter if the node has a direct OwnerReference to the ClusterResourceSet or if
// the node is linked to a ClusterResourceSet indirectly in the OwnerReference chain.
tenantCRSs map[*node]empty
}
type discoveryTypeInfo struct {
typeMeta metav1.TypeMeta
forceMove bool
}
// markObserved marks the fact that a node was observed as a concrete object.
func (n *node) markObserved() {
n.virtual = false
}
func (n *node) addOwner(owner *node, attributes ownerReferenceAttributes) {
n.owners[owner] = attributes
}
func (n *node) addSoftOwner(owner *node) {
n.softOwners[owner] = struct{}{}
}
func (n *node) isOwnedBy(other *node) bool {
_, ok := n.owners[other]
return ok
}
func (n *node) isSoftOwnedBy(other *node) bool {
_, ok := n.softOwners[other]
return ok
}
// objectGraph manages the Kubernetes object graph that is generated during the discovery phase for the move operation.
type objectGraph struct {
proxy Proxy
uidToNode map[types.UID]*node
types map[string]*discoveryTypeInfo
}
func newObjectGraph(proxy Proxy) *objectGraph {
return &objectGraph{
proxy: proxy,
uidToNode: map[types.UID]*node{},
}
}
// addObj adds a Kubernetes object to the object graph that is generated during the move discovery phase.
// During add, OwnerReferences are processed in order to create the dependency graph.
func (o *objectGraph) addObj(obj *unstructured.Unstructured) {
// Adds the node to the Graph.
newNode := o.objToNode(obj)
// Process OwnerReferences; if the owner object doe not exists yet, create a virtual node as a placeholder for it.
for _, ownerReference := range obj.GetOwnerReferences() {
ownerNode, ok := o.uidToNode[ownerReference.UID]
if !ok {
ownerNode = o.ownerToVirtualNode(ownerReference, newNode.identity.Namespace)
}
newNode.addOwner(ownerNode, ownerReferenceAttributes{
Controller: ownerReference.Controller,
BlockOwnerDeletion: ownerReference.BlockOwnerDeletion,
})
}
}
// ownerToVirtualNode creates a virtual node as a placeholder for the Kubernetes owner object received in input.
// The virtual node will be eventually converted to an actual node when the node will be visited during discovery.
func (o *objectGraph) ownerToVirtualNode(owner metav1.OwnerReference, namespace string) *node {
isGlobal := false
if namespace == "" {
isGlobal = true
}
ownerNode := &node{
identity: corev1.ObjectReference{
APIVersion: owner.APIVersion,
Kind: owner.Kind,
Name: owner.Name,
UID: owner.UID,
Namespace: namespace,
},
owners: make(map[*node]ownerReferenceAttributes),
softOwners: make(map[*node]empty),
tenantClusters: make(map[*node]empty),
tenantCRSs: make(map[*node]empty),
virtual: true,
forceMove: o.getForceMove(owner.Kind, owner.APIVersion, nil),
isGlobal: isGlobal,
}
o.uidToNode[ownerNode.identity.UID] = ownerNode
return ownerNode
}
// objToNode creates a node for the Kubernetes object received in input.
// If the node corresponding to the Kubernetes object already exists as a virtual node detected when processing OwnerReferences,
// the node is marked as Observed.
func (o *objectGraph) objToNode(obj *unstructured.Unstructured) *node {
existingNode, found := o.uidToNode[obj.GetUID()]
if found {
existingNode.markObserved()
// In order to compensate the lack of labels when adding a virtual node,
// it is required to re-compute the forceMove flag when the real node is processed
// Without this, there is the risk that, forceMove will report false negatives depending on the discovery order
existingNode.forceMove = o.getForceMove(obj.GetKind(), obj.GetAPIVersion(), obj.GetLabels())
return existingNode
}
isGlobal := false
if obj.GetNamespace() == "" {
isGlobal = true
}
newNode := &node{
identity: corev1.ObjectReference{
APIVersion: obj.GetAPIVersion(),
Kind: obj.GetKind(),
UID: obj.GetUID(),
Name: obj.GetName(),
Namespace: obj.GetNamespace(),
},
owners: make(map[*node]ownerReferenceAttributes),
softOwners: make(map[*node]empty),
tenantClusters: make(map[*node]empty),
tenantCRSs: make(map[*node]empty),
virtual: false,
forceMove: o.getForceMove(obj.GetKind(), obj.GetAPIVersion(), obj.GetLabels()),
isGlobal: isGlobal,
}
o.uidToNode[newNode.identity.UID] = newNode
return newNode
}
func (o *objectGraph) getForceMove(kind, apiVersion string, labels map[string]string) bool {
if _, ok := labels[clusterctlv1.ClusterctlMoveLabelName]; ok {
return true
}
kindAPIStr := getKindAPIString(metav1.TypeMeta{Kind: kind, APIVersion: apiVersion})
if discoveryType, ok := o.types[kindAPIStr]; ok {
return discoveryType.forceMove
}
return false
}
// getDiscoveryTypes returns the list of TypeMeta to be considered for the the move discovery phase.
// This list includes all the types defines by the CRDs installed by clusterctl and the ConfigMap/Secret core types.
func (o *objectGraph) getDiscoveryTypes() error {
crdList := &apiextensionsv1.CustomResourceDefinitionList{}
getDiscoveryTypesBackoff := newReadBackoff()
if err := retryWithExponentialBackoff(getDiscoveryTypesBackoff, func() error {
return getCRDList(o.proxy, crdList)
}); err != nil {
return err
}
o.types = make(map[string]*discoveryTypeInfo)
for _, crd := range crdList.Items {
for _, version := range crd.Spec.Versions {
if !version.Storage {
continue
}
forceMove := false
if _, ok := crd.Labels[clusterctlv1.ClusterctlMoveLabelName]; ok {
forceMove = true
}
typeMeta := metav1.TypeMeta{
Kind: crd.Spec.Names.Kind,
APIVersion: metav1.GroupVersion{
Group: crd.Spec.Group,
Version: version.Name,
}.String(),
}
o.types[getKindAPIString(typeMeta)] = &discoveryTypeInfo{
typeMeta: typeMeta,
forceMove: forceMove,
}
}
}
secretTypeMeta := metav1.TypeMeta{Kind: "Secret", APIVersion: "v1"}
o.types[getKindAPIString(secretTypeMeta)] = &discoveryTypeInfo{typeMeta: secretTypeMeta}
configMapTypeMeta := metav1.TypeMeta{Kind: "ConfigMap", APIVersion: "v1"}
o.types[getKindAPIString(configMapTypeMeta)] = &discoveryTypeInfo{typeMeta: configMapTypeMeta}
return nil
}
// getKindAPIString returns a concatenated string of the API name and the plural of the kind
// Ex: KIND=Foo API NAME=foo.bar.domain.tld => foos.foo.bar.domain.tld
func getKindAPIString(typeMeta metav1.TypeMeta) string {
api := strings.Split(typeMeta.APIVersion, "/")[0]
return fmt.Sprintf("%ss.%s", strings.ToLower(typeMeta.Kind), api)
}
func getCRDList(proxy Proxy, crdList *apiextensionsv1.CustomResourceDefinitionList) error {
c, err := proxy.NewClient()
if err != nil {
return err
}
if err := c.List(ctx, crdList, client.HasLabels{clusterctlv1.ClusterctlLabelName}); err != nil {
return errors.Wrap(err, "failed to get the list of CRDs required for the move discovery phase")
}
return nil
}
// Discovery reads all the Kubernetes objects existing in a namespace (or in all namespaces if empty) for the types received in input, and then adds
// everything to the objects graph.
func (o *objectGraph) Discovery(namespace string) error {
log := logf.Log
log.Info("Discovering Cluster API objects")
selectors := []client.ListOption{}
if namespace != "" {
selectors = append(selectors, client.InNamespace(namespace))
}
discoveryBackoff := newReadBackoff()
for _, discoveryType := range o.types {
typeMeta := discoveryType.typeMeta
objList := new(unstructured.UnstructuredList)
if err := retryWithExponentialBackoff(discoveryBackoff, func() error {
return getObjList(o.proxy, typeMeta, selectors, objList)
}); err != nil {
return err
}
if len(objList.Items) == 0 {
continue
}
log.V(5).Info(typeMeta.Kind, "Count", len(objList.Items))
for i := range objList.Items {
obj := objList.Items[i]
o.addObj(&obj)
}
}
log.V(1).Info("Total objects", "Count", len(o.uidToNode))
// Completes the graph by searching for soft ownership relations such as secrets linked to the cluster
// by a naming convention (without any explicit OwnerReference).
o.setSoftOwnership()
// Completes the graph by setting for each node the list of Clusters the node belong to.
o.setClusterTenants()
// Completes the graph by setting for each node the list of ClusterResourceSet the node belong to.
o.setCRSTenants()
return nil
}
func getObjList(proxy Proxy, typeMeta metav1.TypeMeta, selectors []client.ListOption, objList *unstructured.UnstructuredList) error {
c, err := proxy.NewClient()
if err != nil {
return err
}
objList.SetAPIVersion(typeMeta.APIVersion)
objList.SetKind(typeMeta.Kind)
if err := c.List(ctx, objList, selectors...); err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return errors.Wrapf(err, "failed to list %q resources", objList.GroupVersionKind())
}
return nil
}
// getClusters returns the list of Clusters existing in the object graph.
func (o *objectGraph) getClusters() []*node {
clusters := []*node{}
for _, node := range o.uidToNode {
if node.identity.GroupVersionKind().GroupKind() == clusterv1.GroupVersion.WithKind("Cluster").GroupKind() {
clusters = append(clusters, node)
}
}
return clusters
}
// getClusters returns the list of Secrets existing in the object graph.
func (o *objectGraph) getSecrets() []*node {
secrets := []*node{}
for _, node := range o.uidToNode {
if node.identity.APIVersion == "v1" && node.identity.Kind == "Secret" {
secrets = append(secrets, node)
}
}
return secrets
}
// getNodes returns the list of nodes existing in the object graph.
func (o *objectGraph) getNodes() []*node {
nodes := []*node{}
for _, node := range o.uidToNode {
nodes = append(nodes, node)
}
return nodes
}
// getCRSs returns the list of ClusterResourceSet existing in the object graph.
func (o *objectGraph) getCRSs() []*node {
clusters := []*node{}
for _, node := range o.uidToNode {
if node.identity.GroupVersionKind().GroupKind() == addonsv1.GroupVersion.WithKind("ClusterResourceSet").GroupKind() {
clusters = append(clusters, node)
}
}
return clusters
}
// getMoveNodes returns the list of nodes existing in the object graph that belong at least to one Cluster or to a ClusterResourceSet
// or to a CRD containing the "move" label.
func (o *objectGraph) getMoveNodes() []*node {
nodes := []*node{}
for _, node := range o.uidToNode {
if len(node.tenantClusters) > 0 || len(node.tenantCRSs) > 0 || node.forceMove {
nodes = append(nodes, node)
}
}
return nodes
}
// getMachines returns the list of Machine existing in the object graph.
func (o *objectGraph) getMachines() []*node {
machines := []*node{}
for _, node := range o.uidToNode {
if node.identity.GroupVersionKind().GroupKind() == clusterv1.GroupVersion.WithKind("Machine").GroupKind() {
machines = append(machines, node)
}
}
return machines
}
// setSoftOwnership searches for soft ownership relations such as secrets linked to the cluster by a naming convention (without any explicit OwnerReference).
func (o *objectGraph) setSoftOwnership() {
log := logf.Log
clusters := o.getClusters()
for _, secret := range o.getSecrets() {
// If the secret has at least one OwnerReference ignore it.
// NB. Cluster API generated secrets have an explicit OwnerReference to the ControlPlane or the KubeadmConfig object while user provided secrets might not have one.
if len(secret.owners) > 0 {
continue
}
// If the secret name is not a valid cluster secret name, ignore it.
secretClusterName, _, err := secretutil.ParseSecretName(secret.identity.Name)
if err != nil {
log.V(5).Info("Excluding secret from move (not linked with any Cluster)", "name", secret.identity.Name)
continue
}
// If the secret is linked to a cluster, then add the cluster to the list of the secrets's softOwners.
for _, cluster := range clusters {
if secretClusterName == cluster.identity.Name && secret.identity.Namespace == cluster.identity.Namespace {
secret.addSoftOwner(cluster)
}
}
}
}
// setClusterTenants sets the cluster tenants for the clusters itself and all their dependent object tree.
func (o *objectGraph) setClusterTenants() {
for _, cluster := range o.getClusters() {
o.setClusterTenant(cluster, cluster)
}
}
// setNodeTenant sets a cluster tenant for a node and for its own dependents/sofDependents.
func (o *objectGraph) setClusterTenant(node, tenant *node) {
node.tenantClusters[tenant] = empty{}
for _, other := range o.getNodes() {
if other.isOwnedBy(node) || other.isSoftOwnedBy(node) {
o.setClusterTenant(other, tenant)
}
}
}
// setClusterTenants sets the ClusterResourceSet tenants for the ClusterResourceSet itself and all their dependent object tree.
func (o *objectGraph) setCRSTenants() {
for _, crs := range o.getCRSs() {
o.setCRSTenant(crs, crs)
}
}
// setCRSTenant sets a ClusterResourceSet tenant for a node and for its own dependents/sofDependents.
func (o *objectGraph) setCRSTenant(node, tenant *node) {
node.tenantCRSs[tenant] = empty{}
for _, other := range o.getNodes() {
if other.isOwnedBy(node) {
o.setCRSTenant(other, tenant)
}
}
}
// checkVirtualNode logs if nodes are still virtual
func (o *objectGraph) checkVirtualNode() {
log := logf.Log
for _, node := range o.uidToNode {
if node.virtual {
log.V(5).Info("Object won't be moved because it's not included in GVK considered for move", "kind", node.identity.Kind, "name", node.identity.Name)
}
}
}