forked from aws-controllers-k8s/code-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlate_initialize.go
More file actions
340 lines (334 loc) · 12.2 KB
/
late_initialize.go
File metadata and controls
340 lines (334 loc) · 12.2 KB
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
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 code
import (
"fmt"
"sort"
"strings"
ackgenconfig "github.com/aws-controllers-k8s/code-generator/pkg/config"
"github.com/aws-controllers-k8s/code-generator/pkg/model"
)
// FindLateInitializedFieldNames outputs the code to create a sorted slice of fieldNames to
// late initialize. This slice helps with short circuiting the AWSResourceManager.LateInitialize()
// method if there are no fields to late initialize.
//
// Sample Output:
// var lateInitializeFieldNames = []string{"Name"}
func FindLateInitializedFieldNames(
cfg *ackgenconfig.Config,
r *model.CRD,
resVarName string,
// Number of levels of indentation to use
indentLevel int,
) string {
out := ""
indent := strings.Repeat("\t", indentLevel)
var lateInitFieldNames []string
lateInitConfigs := cfg.GetLateInitConfigs(r.Names.Original)
for fieldName := range lateInitConfigs {
lateInitFieldNames = append(lateInitFieldNames, fieldName)
}
if len(lateInitFieldNames) == 0 {
return fmt.Sprintf("%svar %s = []string{}\n", indent, resVarName)
}
// sort the slice to help with short circuiting AWSResourceManager.LateInitialize()
sort.Strings(lateInitFieldNames)
out += fmt.Sprintf("%svar %s = []string{", indent, resVarName)
for _, fName := range lateInitFieldNames {
out += fmt.Sprintf("%q,", fName)
}
out += "}\n"
return out
}
// LateInitializeFromReadOne returns the gocode to set LateInitialization fields from the ReadOne output
// Field path separated by '.' indicates members in a struct
// Field path separated by '..' indicates member/key in a map
// Note: Unlike Map, updating individual element of a list is not supported. LateInitializing complete list is supported.
//
// Sample generator config:
// fields:
//
// Name:
// late_initialize: {}
// ImageScanningConfiguration.ScanOnPush:
// late_initialize:
// min_backoff_seconds: 5
// max_backoff_seconds: 15
// map..subfield.x:
// late_initialize:
// min_backoff_seconds: 5
// another.map..lastfield:
// late_initialize:
// min_backoff_seconds: 5
// some.list:
// late_initialize:
// min_backoff_seconds: 10
// structA.mapB..structC.valueD:
// late_initialize:
// min_backoff_seconds: 20
//
// Sample output:
//
// observedKo := rm.concreteResource(observed).ko
// latestKo := rm.concreteResource(latest).ko
// if observedKo.Spec.ImageScanningConfiguration != nil && latestKo.Spec.ImageScanningConfiguration != nil {
// if observedKo.Spec.ImageScanningConfiguration.ScanOnPush != nil && latestKo.Spec.ImageScanningConfiguration.ScanOnPush == nil {
// latestKo.Spec.ImageScanningConfiguration.ScanOnPush = observedKo.Spec.ImageScanningConfiguration.ScanOnPush
// }
// }
// if observedKo.Spec.Name != nil && latestKo.Spec.Name == nil {
// latestKo.Spec.Name = observedKo.Spec.Name
// }
// if observedKo.Spec.another != nil && latestKo.Spec.another != nil {
// if observedKo.Spec.another.map != nil && latestKo.Spec.another.map != nil {
// if observedKo.Spec.another.map["lastfield"] != nil && latestKo.Spec.another.map["lastfield"] == nil {
// latestKo.Spec.another.map["lastfield"] = observedKo.Spec.another.map["lastfield"]
// }
// }
// }
// if observedKo.Spec.map != nil && latestKo.Spec.map != nil {
// if observedKo.Spec.map["subfield"] != nil && latestKo.Spec.map["subfield"] != nil {
// if observedKo.Spec.map["subfield"].x != nil && latestKo.Spec.map["subfield"].x == nil {
// latestKo.Spec.map["subfield"].x = observedKo.Spec.map["subfield"].x
// }
// }
// }
// if observedKo.Spec.some != nil && latestKo.Spec.some != nil {
// if observedKo.Spec.some.list != nil && latestKo.Spec.some.list == nil {
// latestKo.Spec.some.list = observedKo.Spec.some.list
// }
// }
// if observedKo.Spec.structA != nil && latestKo.Spec.structA != nil {
// if observedKo.Spec.structA.mapB != nil && latestKo.Spec.structA.mapB != nil {
// if observedKo.Spec.structA.mapB["structC"] != nil && latestKo.Spec.structA.mapB["structC"] != nil {
// if observedKo.Spec.structA.mapB["structC"].valueD != nil && latestKo.Spec.structA.mapB["structC"].valueD == nil {
// latestKo.Spec.structA.mapB["structC"].valueD = observedKo.Spec.structA.mapB["structC"].valueD
// }
// }
// }
// }
// return &resource{latestKo}
func LateInitializeFromReadOne(
cfg *ackgenconfig.Config,
r *model.CRD,
sourceResVarName string,
targetResVarName string,
// Number of levels of indentation to use
indentLevel int,
) string {
out := ""
indent := strings.Repeat("\t", indentLevel)
var lateInitFieldNames []string
lateInitConfigs := cfg.GetLateInitConfigs(r.Names.Original)
for fieldName := range lateInitConfigs {
lateInitFieldNames = append(lateInitFieldNames, fieldName)
}
if len(lateInitFieldNames) == 0 {
return fmt.Sprintf("%sreturn %s", indent, targetResVarName)
}
sort.Strings(lateInitFieldNames)
out += fmt.Sprintf("%sobservedKo := rm.concreteResource(%s).ko.DeepCopy()\n", indent, sourceResVarName)
out += fmt.Sprintf("%slatestKo := rm.concreteResource(%s).ko.DeepCopy()\n", indent, targetResVarName)
// TODO(vijat@): Add validation for correct field path in lateInitializedFieldNames
for _, fName := range lateInitFieldNames {
// split the field name by period
// each substring represents a field.
fNameParts := strings.Split(fName, ".")
// fNameIndentLevel tracks the indentation level for every new line added
// This variable is incremented when building nested if blocks and decremented when closing those if blocks.
fNameIndentLevel := indentLevel
// fParentPath keeps track of parent path for any fNamePart
fParentPath := ""
mapShapedParent := false
// for every part except last, perform the nil check
// entries in both source and target koVarName should not be nil
for i, fNamePart := range fNameParts {
if fNamePart == "" {
mapShapedParent = true
continue
}
indent := strings.Repeat("\t", fNameIndentLevel)
fNamePartAccesor := fmt.Sprintf("Spec%s.%s", fParentPath, fNamePart)
if mapShapedParent {
fNamePartAccesor = fmt.Sprintf("Spec%s[%q]", fParentPath, fNamePart)
}
// Handling for all parts except last one
if i != len(fNameParts)-1 {
out += fmt.Sprintf("%sif observedKo.%s != nil && latestKo.%s != nil {\n", indent, fNamePartAccesor, fNamePartAccesor)
// update fParentPath and fNameIndentLevel for next iteration
if mapShapedParent {
fParentPath = fmt.Sprintf("%s[%q]", fParentPath, fNamePart)
mapShapedParent = false
} else {
fParentPath = fmt.Sprintf("%s.%s", fParentPath, fNamePart)
}
fNameIndentLevel = fNameIndentLevel + 1
} else {
// handle last part here
// for last part, set the lateInitialized field if user did not specify field value and readOne has server side defaulted value.
// i.e. field is not nil in sourceKoVarName but is nil in targetkoVarName
out += fmt.Sprintf("%sif observedKo.%s != nil && latestKo.%s == nil {\n", indent, fNamePartAccesor, fNamePartAccesor)
fNameIndentLevel = fNameIndentLevel + 1
indent = strings.Repeat("\t", fNameIndentLevel)
out += fmt.Sprintf("%slatestKo.%s = observedKo.%s\n", indent, fNamePartAccesor, fNamePartAccesor)
}
}
// Close all if blocks with proper indentation
fNameIndentLevel = fNameIndentLevel - 1
for fNameIndentLevel >= indentLevel {
out += fmt.Sprintf("%s}\n", strings.Repeat("\t", fNameIndentLevel))
fNameIndentLevel = fNameIndentLevel - 1
}
}
out += fmt.Sprintf("%sreturn &resource{latestKo}", indent)
return out
}
// IncompleteLateInitialization returns the go code which checks whether all the fields are late initialized.
// If all the fields are not late initialized, this method also returns the requeue delay needed to attempt
// late initialization again.
//
// Sample GeneratorConfig:
// fields:
//
// Name:
// late_initialize: {}
// ImageScanningConfiguration.ScanOnPush:
// late_initialize:
// min_backoff_seconds: 5
// max_backoff_seconds: 15
// map..subfield.x:
// late_initialize:
// min_backoff_seconds: 5
// another.map..lastfield:
// late_initialize:
// min_backoff_seconds: 5
// some.list:
// late_initialize:
// min_backoff_seconds: 10
// structA.mapB..structC.valueD:
// late_initialize:
// min_backoff_seconds: 20
//
// Sample Output:
//
// ko := rm.concreteResource(latest).ko.DeepCopy()
// if ko.Spec.ImageScanningConfiguration != nil {
// if ko.Spec.ImageScanningConfiguration.ScanOnPush == nil {
// return true
// }
// }
// if ko.Spec.Name == nil {
// return true
// }
// if ko.Spec.another != nil {
// if ko.Spec.another.map != nil {
// if ko.Spec.another.map["lastfield"] == nil {
// return true
// }
// }
// }
// if ko.Spec.map != nil {
// if ko.Spec.map["subfield"] != nil {
// if ko.Spec.map["subfield"].x == nil {
// return true
// }
// }
// }
// if ko.Spec.some != nil {
// if ko.Spec.some.list == nil {
// return true
// }
// }
// if ko.Spec.structA != nil {
// if ko.Spec.structA.mapB != nil {
// if ko.Spec.structA.mapB["structC"] != nil {
// if ko.Spec.structA.mapB["structC"].valueD == nil {
// return true
// }
// }
// }
// }
// return false
func IncompleteLateInitialization(
cfg *ackgenconfig.Config,
r *model.CRD,
resVarName string,
// Number of levels of indentation to use
indentLevel int,
) string {
out := ""
indent := strings.Repeat("\t", indentLevel)
var lateInitFieldNames []string
lateInitConfigs := cfg.GetLateInitConfigs(r.Names.Original)
for fieldName, lateInitConfig := range lateInitConfigs {
if lateInitConfig.SkipIncompleteCheck != nil {
continue
}
lateInitFieldNames = append(lateInitFieldNames, fieldName)
}
if len(lateInitFieldNames) == 0 {
return fmt.Sprintf("%sreturn false", indent)
}
sort.Strings(lateInitFieldNames)
out += fmt.Sprintf("%sko := rm.concreteResource(%s).ko.DeepCopy()\n", indent, resVarName)
for _, fName := range lateInitFieldNames {
// split the field name by period
// each substring represents a field.
fNameParts := strings.Split(fName, ".")
// fNameIndentLevel tracks the indentation level for every new line added
// This variable is incremented when building nested if blocks and decremented when closing those if blocks.
fNameIndentLevel := indentLevel
// fParentPath keeps track of parent path for any fNamePart
fParentPath := ""
mapShapedParent := false
for i, fNamePart := range fNameParts {
if fNamePart == "" {
mapShapedParent = true
continue
}
indent := strings.Repeat("\t", fNameIndentLevel)
fNamePartAccesor := fmt.Sprintf("Spec%s.%s", fParentPath, fNamePart)
if mapShapedParent {
fNamePartAccesor = fmt.Sprintf("Spec%s[%q]", fParentPath, fNamePart)
}
// Handling for all parts except last one
if i != len(fNameParts)-1 {
out += fmt.Sprintf("%sif ko.%s != nil {\n", indent, fNamePartAccesor)
// update fParentPath and fNameIndentLevel for next iteration
if mapShapedParent {
fParentPath = fmt.Sprintf("%s[%q]", fParentPath, fNamePart)
mapShapedParent = false
} else {
fParentPath = fmt.Sprintf("%s.%s", fParentPath, fNamePart)
}
fNameIndentLevel = fNameIndentLevel + 1
} else {
// handle last part here
// for last part, if the late initialized field is still nil, calculate the retry backoff using
// acktypes.LateInitializationRetryConfig abstraction and set the incompleteInitialization flag to true
out += fmt.Sprintf("%sif ko.%s == nil {\n", indent, fNamePartAccesor)
fNameIndentLevel = fNameIndentLevel + 1
indent = strings.Repeat("\t", fNameIndentLevel)
out += fmt.Sprintf("%sreturn true\n", indent)
}
}
// Close all if blocks with proper indentation
fNameIndentLevel = fNameIndentLevel - 1
for fNameIndentLevel >= indentLevel {
out += fmt.Sprintf("%s}\n", strings.Repeat("\t", fNameIndentLevel))
fNameIndentLevel = fNameIndentLevel - 1
}
}
out += fmt.Sprintf("%sreturn false", indent)
return out
}