...
1
2
3
4 package mutation
5
6 import (
7 "fmt"
8
9 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
10 "k8s.io/apimachinery/pkg/runtime/schema"
11 "sigs.k8s.io/cli-utils/pkg/object"
12 )
13
14
15
16
17
18 type ApplyTimeMutation []FieldSubstitution
19
20
21
22 func (a ApplyTimeMutation) Equal(b ApplyTimeMutation) bool {
23 if len(a) != len(b) {
24 return false
25 }
26
27 mapA := make(map[FieldSubstitution]struct{}, len(a))
28 for _, sub := range a {
29 mapA[sub] = struct{}{}
30 }
31 mapB := make(map[FieldSubstitution]struct{}, len(b))
32 for _, sub := range b {
33 mapB[sub] = struct{}{}
34 }
35 if len(mapA) != len(mapB) {
36 return false
37 }
38 for b := range mapB {
39 if _, exists := mapA[b]; !exists {
40 return false
41 }
42 }
43 return true
44 }
45
46
47
48
49 type FieldSubstitution struct {
50
51 SourceRef ResourceReference `json:"sourceRef"`
52
53
54
55 SourcePath string `json:"sourcePath"`
56
57
58
59 TargetPath string `json:"targetPath"`
60
61
62
63
64
65 Token string `json:"token,omitempty"`
66 }
67
68
69
70
71
72
73 type ResourceReference struct {
74
75
76 Kind string `json:"kind"`
77
78
79
80
81 APIVersion string `json:"apiVersion,omitempty"`
82
83
84
85
86 Group string `json:"group,omitempty"`
87
88
89
90 Name string `json:"name,omitempty"`
91
92
93
94
95 Namespace string `json:"namespace,omitempty"`
96 }
97
98
99 func ResourceReferenceFromUnstructured(obj *unstructured.Unstructured) ResourceReference {
100 return ResourceReference{
101 Name: obj.GetName(),
102 Namespace: obj.GetNamespace(),
103 Kind: obj.GetKind(),
104 APIVersion: obj.GetAPIVersion(),
105 }
106 }
107
108
109 func ResourceReferenceFromObjMetadata(id object.ObjMetadata) ResourceReference {
110 return ResourceReference{
111 Name: id.Name,
112 Namespace: id.Namespace,
113 Kind: id.GroupKind.Kind,
114 Group: id.GroupKind.Group,
115 }
116 }
117
118
119
120 func (r ResourceReference) GroupVersionKind() schema.GroupVersionKind {
121 if r.Group != "" {
122 return schema.GroupVersionKind{Group: r.Group, Kind: r.Kind}
123 }
124 return schema.FromAPIVersionAndKind(r.APIVersion, r.Kind)
125 }
126
127
128
129
130
131 func (r ResourceReference) ToUnstructured() *unstructured.Unstructured {
132 obj := &unstructured.Unstructured{}
133 obj.SetName(r.Name)
134 obj.SetNamespace(r.Namespace)
135 obj.SetGroupVersionKind(r.GroupVersionKind())
136 return obj
137 }
138
139
140
141 func (r ResourceReference) ToObjMetadata() object.ObjMetadata {
142 return object.ObjMetadata{
143 Namespace: r.Namespace,
144 Name: r.Name,
145 GroupKind: r.GroupVersionKind().GroupKind(),
146 }
147 }
148
149
150 func (r ResourceReference) String() string {
151 group := r.Group
152 if group == "" {
153 group = r.APIVersion
154 }
155 if r.Namespace != "" {
156 return fmt.Sprintf("%s/namespaces/%s/%s/%s", group, r.Namespace, r.Kind, r.Name)
157 }
158 return fmt.Sprintf("%s/%s/%s", group, r.Kind, r.Name)
159 }
160
161
162
163 func (r ResourceReference) Equal(b ResourceReference) bool {
164 return r.GroupVersionKind().GroupKind() == b.GroupVersionKind().GroupKind() &&
165 r.Name == b.Name &&
166 r.Namespace == b.Namespace
167 }
168
View as plain text