Skip to content

Commit 7e3d66c

Browse files
committed
Immutable fields warning
Add a warning condition if an immutable field is modified. This commit allows one to specify immutable fields in generator.yaml.
1 parent ad3bd3d commit 7e3d66c

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

pkg/generate/config/field.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ type FieldConfig struct {
155155
// IsSecret instructs the code generator that this field should be a
156156
// SecretKeyReference.
157157
IsSecret bool `json:"is_secret"`
158+
// IsImmutable instructs the code generator to add warning conditions
159+
// if user modifies the spec field after resource was created.
160+
IsImmutable bool `json:"is_immutable"`
158161
// From instructs the code generator that the value of the field should
159162
// be retrieved from the specified operation and member path
160163
From *SourceFieldConfig `json:"from,omitempty"`

pkg/model/crd.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,20 @@ func (r *CRD) IsSecretField(path string) bool {
303303
return false
304304
}
305305

306+
// GetImmutableFields returns list of immutable fields present in CRD
307+
func (r *CRD) GetImmutableFields() []string {
308+
fConfigs := r.cfg.ResourceFields(r.Names.Original)
309+
var immutableFields []string
310+
311+
for field,fieldConfig := range fConfigs {
312+
if fieldConfig.IsImmutable {
313+
immutableFields = append(immutableFields, field)
314+
}
315+
}
316+
317+
return immutableFields
318+
}
319+
306320
// SetOutputCustomMethodName returns custom set output operation as *string for
307321
// given operation on custom resource, if specified in generator config
308322
func (r *CRD) SetOutputCustomMethodName(

templates/pkg/resource/sdk.go.tpl

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,55 @@ func (rm *resourceManager) terminalAWSError(err error) bool {
273273
return false
274274
{{- end }}
275275
}
276+
277+
{{- if .CRD.GetImmutableFields }}
278+
// validateImmutableFields validates the immutable fields and set appropriate condition
279+
func (rm *resourceManager) validateImmutableFields(ko *svcapitypes.{{ .CRD.Names.Camel }},
280+
delta *ackcompare.Delta,) {
281+
var fields []string;
282+
283+
for _, diff := range delta.Differences {
284+
if diff.Path.Contains("Spec") {
285+
{{- range $immutableField := .CRD.GetImmutableFields }}
286+
if diff.Path.Contains("{{$immutableField}}") {
287+
fields = append(fields,"{{$immutableField}}")
288+
}
289+
{{- end }}
290+
}
291+
}
292+
293+
// Warning condition
294+
var warningCondition *ackv1alpha1.Condition = nil
295+
for _, condition := range ko.Status.Conditions {
296+
if condition.Type == ackv1alpha1.ConditionTypeWarning {
297+
warningCondition = condition
298+
break
299+
}
300+
}
301+
302+
// Remove the warning condition if warning is no longer present
303+
if len(fields) == 0 && warningCondition != nil{
304+
var newConditions []*ackv1alpha1.Condition
305+
for _, condition := range ko.Status.Conditions {
306+
if condition.Type != ackv1alpha1.ConditionTypeWarning {
307+
newConditions = append(newConditions,condition)
308+
}
309+
}
310+
ko.Status.Conditions = newConditions
311+
}
312+
313+
if len(fields) > 0 {
314+
// Add warning condition
315+
if warningCondition == nil {
316+
warningCondition = &ackv1alpha1.Condition{
317+
Type: ackv1alpha1.ConditionTypeWarning,
318+
}
319+
ko.Status.Conditions = append(ko.Status.Conditions, warningCondition)
320+
}
321+
322+
warningCondition.Status = corev1.ConditionTrue
323+
message := "Immutable Spec fields have been modified : " + strings.Join(fields, ",")
324+
warningCondition.Message = &message
325+
}
326+
}
327+
{{- end }}

templates/pkg/resource/sdk_update.go.tpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ func (rm *resourceManager) sdkUpdate(
3838
{{- if $hookCode := Hook .CRD "sdk_update_pre_set_output" }}
3939
{{ $hookCode }}
4040
{{- end }}
41+
{{- if .CRD.GetImmutableFields }}
42+
rm.validateImmutableFields(ko, delta)
43+
{{- end }}
4144
{{ $setCode }}
4245
rm.setStatusDefaults(ko)
4346
{{ if $setOutputCustomMethodName := .CRD.SetOutputCustomMethodName .CRD.Ops.Update }}

0 commit comments

Comments
 (0)