@@ -19,8 +19,6 @@ package controllers
19
19
import (
20
20
"fmt"
21
21
v1 "k8s.io/api/core/v1"
22
- "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
23
- "k8s.io/apimachinery/pkg/runtime"
24
22
"math/rand/v2"
25
23
"sigs.k8s.io/controller-runtime/pkg/client"
26
24
"time"
@@ -31,168 +29,6 @@ import (
31
29
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
32
30
)
33
31
34
- func toUnstructured (o client.Object ) * unstructured.Unstructured {
35
- m , err := runtime .DefaultUnstructuredConverter .ToUnstructured (o )
36
- Expect (err ).To (Succeed ())
37
- return & unstructured.Unstructured {
38
- Object : m ,
39
- }
40
- }
41
-
42
- func buildTestConfigMap (name string , namespace string , data map [string ]string ) * unstructured.Unstructured {
43
- return toUnstructured (& v1.ConfigMap {
44
- TypeMeta : metav1.TypeMeta {
45
- APIVersion : "v1" ,
46
- Kind : "ConfigMap" ,
47
- },
48
- ObjectMeta : metav1.ObjectMeta {
49
- Name : name ,
50
- Namespace : namespace ,
51
- },
52
- Data : data ,
53
- })
54
- }
55
-
56
- func buildTestSecret (name string , namespace string , data map [string ]string ) * unstructured.Unstructured {
57
- return toUnstructured (& v1.Secret {
58
- TypeMeta : metav1.TypeMeta {
59
- APIVersion : "v1" ,
60
- Kind : "Secret" ,
61
- },
62
- ObjectMeta : metav1.ObjectMeta {
63
- Name : name ,
64
- Namespace : namespace ,
65
- },
66
- StringData : data ,
67
- })
68
- }
69
-
70
- func buildObjectTemplate (name string , namespace string , matrixEntries []templatesv1alpha1.MatrixEntry , templates []templatesv1alpha1.Template ) * templatesv1alpha1.ObjectTemplate {
71
- t := & templatesv1alpha1.ObjectTemplate {
72
- TypeMeta : metav1.TypeMeta {
73
- APIVersion : templatesv1alpha1 .GroupVersion .String (),
74
- Kind : "ObjectTemplate" ,
75
- },
76
- ObjectMeta : metav1.ObjectMeta {
77
- Name : name ,
78
- Namespace : namespace ,
79
- },
80
- Spec : templatesv1alpha1.ObjectTemplateSpec {
81
- Interval : metav1.Duration {Duration : time .Second },
82
- Matrix : matrixEntries ,
83
- Templates : templates ,
84
- },
85
- }
86
- return t
87
- }
88
-
89
- func buildMatrixListEntry (name string ) templatesv1alpha1.MatrixEntry {
90
- return templatesv1alpha1.MatrixEntry {
91
- Name : name ,
92
- List : []runtime.RawExtension {
93
- {
94
- Raw : []byte (`{"k1": 1, "k2": 2}` ),
95
- },
96
- },
97
- }
98
- }
99
-
100
- func buildMatrixObjectEntry (name string , objName string , objNamespace string , objKind string , jsonPath string , expandLists bool ) templatesv1alpha1.MatrixEntry {
101
- var jsonPathPtr * string
102
- if jsonPath != "" {
103
- jsonPathPtr = & jsonPath
104
- }
105
- return templatesv1alpha1.MatrixEntry {
106
- Name : name ,
107
- Object : & templatesv1alpha1.MatrixEntryObject {
108
- Ref : templatesv1alpha1.ObjectRef {
109
- APIVersion : "v1" ,
110
- Kind : objKind ,
111
- Name : objName ,
112
- Namespace : objNamespace ,
113
- },
114
- JsonPath : jsonPathPtr ,
115
- ExpandLists : expandLists ,
116
- },
117
- }
118
- }
119
-
120
- func updateObjectTemplate (key client.ObjectKey , fn func (t * templatesv1alpha1.ObjectTemplate )) {
121
- t := getObjectTemplate (key )
122
- fn (t )
123
- err := k8sClient .Update (ctx , t , client .FieldOwner ("tests" ))
124
- Expect (err ).To (Succeed ())
125
- }
126
-
127
- func triggerReconcile (key client.ObjectKey ) {
128
- updateObjectTemplate (key , func (t * templatesv1alpha1.ObjectTemplate ) {
129
- t .Spec .Interval .Duration += time .Millisecond
130
- })
131
- }
132
-
133
- func waitUntiReconciled (key client.ObjectKey , timeout time.Duration ) {
134
- Eventually (func () bool {
135
- t := getObjectTemplate (key )
136
- if t == nil {
137
- return false
138
- }
139
- c := getReadyCondition (t .GetConditions ())
140
- if c == nil {
141
- return false
142
- }
143
- return c .ObservedGeneration == t .Generation
144
- }, timeout , time .Millisecond * 250 ).Should (BeTrue ())
145
- }
146
-
147
- func getObjectTemplate (key client.ObjectKey ) * templatesv1alpha1.ObjectTemplate {
148
- var t templatesv1alpha1.ObjectTemplate
149
- err := k8sClient .Get (ctx , key , & t )
150
- if err != nil {
151
- return nil
152
- }
153
- return & t
154
- }
155
-
156
- func assertAppliedConfigMaps (key client.ObjectKey , keys ... client.ObjectKey ) {
157
- t := getObjectTemplate (key )
158
- Expect (t ).ToNot (BeNil ())
159
-
160
- var found []client.ObjectKey
161
- for _ , as := range t .Status .AppliedResources {
162
- if as .Success {
163
- found = append (found , client.ObjectKey {Name : as .Ref .Name , Namespace : as .Ref .Namespace })
164
- }
165
- }
166
-
167
- Expect (found ).To (ConsistOf (keys ))
168
- }
169
-
170
- func assertFailedConfigMaps (key client.ObjectKey , keys ... client.ObjectKey ) {
171
- t := getObjectTemplate (key )
172
- Expect (t ).ToNot (BeNil ())
173
-
174
- var found []client.ObjectKey
175
- for _ , as := range t .Status .AppliedResources {
176
- if ! as .Success {
177
- found = append (found , client.ObjectKey {Name : as .Ref .Name , Namespace : as .Ref .Namespace })
178
- }
179
- }
180
-
181
- Expect (found ).To (ConsistOf (keys ))
182
- }
183
-
184
- func assertFailedConfigMap (key client.ObjectKey , cmKey client.ObjectKey , errStr string ) {
185
- t := getObjectTemplate (key )
186
- Expect (t ).ToNot (BeNil ())
187
- for _ , as := range t .Status .AppliedResources {
188
- if ! as .Success && as .Ref .Name == cmKey .Name && as .Ref .Namespace == cmKey .Namespace {
189
- Expect (as .Error ).To (ContainSubstring (errStr ))
190
- return
191
- }
192
- }
193
- Expect (false ).To (BeTrue ())
194
- }
195
-
196
32
var _ = Describe ("ObjectTemplate controller" , func () {
197
33
const (
198
34
timeout = time .Second * 1000
0 commit comments