Debian Codegraph @

api/internal/plugins/builtinconfig/namebackreferences.go

raw
1 // Copyright 2019 The Kubernetes Authors.
2 // SPDX-License-Identifier: Apache-2.0
3
4 package builtinconfig
5
6 import (
7 "strings"
8
9 "sigs.k8s.io/kustomize/api/types"
10 "sigs.k8s.io/kustomize/kyaml/resid"
11 )
12
13 // NameBackReferences is an association between a gvk.GVK (a ReferralTarget)
14 // and a list of Referrers that could refer to it.
15 //
16 // It is used to handle name changes, and can be thought of as a
17 // a contact list. If you change your own contact info (name,
18 // phone number, etc.), you must tell your contacts or they won't
19 // know about the change.
20 //
21 // For example, ConfigMaps can be used by Pods and everything that
22 // contains a Pod; Deployment, Job, StatefulSet, etc.
23 // The ConfigMap is the ReferralTarget, the others are Referrers.
24 //
25 // If the name of a ConfigMap instance changed from 'alice' to 'bob',
26 // one must
27 // - visit all objects that could refer to the ConfigMap (the Referrers)
28 // - see if they mention 'alice',
29 // - if so, change the Referrer's name reference to 'bob'.
30 //
31 // The NameBackReferences instance to aid in this could look like
32 // {
33 // kind: ConfigMap
34 // version: v1
35 // fieldSpecs:
36 // - kind: Pod
37 // version: v1
38 // path: spec/volumes/configMap/name
39 // - kind: Deployment
40 // path: spec/template/spec/volumes/configMap/name
41 // - kind: Job
42 // path: spec/template/spec/volumes/configMap/name
43 // (etc.)
44 // }
45 type NameBackReferences struct {
46 resid.Gvk `json:",inline,omitempty" yaml:",inline,omitempty"`
47 // TODO: rename json 'fieldSpecs' to 'referrers' for clarity.
48 // This will, however, break anyone using a custom config.
49 Referrers types.FsSlice `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
50
51 // Note: If any new pointer based members are added, DeepCopy needs to be updated
52 }
53
54 func (n NameBackReferences) String() string {
55 var r []string
56 for _, f := range n.Referrers {
57 r = append(r, f.String())
58 }
59 return n.Gvk.String() + ": (\n" +
60 strings.Join(r, "\n") + "\n)"
61 }
62
63 type nbrSlice []NameBackReferences
64
65 func (s nbrSlice) Len() int { return len(s) }
66 func (s nbrSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
67 func (s nbrSlice) Less(i, j int) bool {
68 return s[i].Gvk.IsLessThan(s[j].Gvk)
69 }
70
71 // DeepCopy returns a new copy of nbrSlice
72 func (s nbrSlice) DeepCopy() nbrSlice {
73 ret := make(nbrSlice, len(s))
74 copy(ret, s)
75 for i, slice := range ret {
76 ret[i].Referrers = slice.Referrers.DeepCopy()
77 }
78
79 return ret
80 }
81
82 func (s nbrSlice) mergeAll(o nbrSlice) (result nbrSlice, err error) {
83 result = s
84 for _, r := range o {
85 result, err = result.mergeOne(r)
86 if err != nil {
87 return nil, err
88 }
89 }
90 return result, nil
91 }
92
93 func (s nbrSlice) mergeOne(other NameBackReferences) (nbrSlice, error) {
94 var result nbrSlice
95 var err error
96 found := false
97 for _, c := range s {
98 if c.Gvk.Equals(other.Gvk) {
99 c.Referrers, err = c.Referrers.MergeAll(other.Referrers)
100 if err != nil {
101 return nil, err
102 }
103 found = true
104 }
105 result = append(result, c)
106 }
107
108 if !found {
109 result = append(result, other)
110 }
111 return result, nil
112 }