Skip to content

Commit a8d5fa4

Browse files
authored
Add Terminal condition when immutable field is updated (#350)
Issue #, if available: aws-controllers-k8s/community#1374 Description of changes: * Use the correct delta key `Spec.FieldName` when finding changes to immutable field * If an immutable field is updated, add the Terminal condition in resource and do not continue with update operation until desired state is valid again By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 208b1e1 commit a8d5fa4

File tree

3 files changed

+15
-49
lines changed

3 files changed

+15
-49
lines changed

pkg/generate/ack/controller.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ var (
5656
controllerCopyPaths = []string{}
5757
controllerFuncMap = ttpl.FuncMap{
5858
"ToLower": strings.ToLower,
59+
"TrimPrefix": func(s string, prefix string) string {
60+
return strings.TrimPrefix(s, prefix)
61+
},
5962
"Dereference": func(s *string) string {
6063
return *s
6164
},
@@ -169,9 +172,9 @@ var (
169172
"CheckNilReferencesPath": func(f *ackmodel.Field, sourceVarName string) string {
170173
return code.CheckNilReferencesPath(f, sourceVarName)
171174
},
172-
"Each": func (args ...interface{}) []interface{} {
173-
return args
174-
},
175+
"Each": func(args ...interface{}) []interface{} {
176+
return args
177+
},
175178
"GoCodeInitializeNestedStructField": func(r *ackmodel.CRD,
176179
sourceVarName string, f *ackmodel.Field, apiPkgImportName string,
177180
indentLevel int) string {

templates/pkg/resource/sdk.go.tpl

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package {{ .CRD.Names.Snake }}
55
import (
66
"context"
77
"errors"
8+
"fmt"
89
"reflect"
910
"strings"
1011

@@ -32,6 +33,7 @@ var (
3233
_ = &ackerr.NotFound
3334
_ = &ackcondition.NotManagedMessage
3435
_ = &reflect.Value{}
36+
_ = fmt.Sprintf("")
3537
)
3638

3739
// sdkFind returns SDK-specific information about a supplied resource
@@ -326,58 +328,16 @@ func (rm *resourceManager) getImmutableFieldChanges(
326328
delta *ackcompare.Delta,
327329
) []string {
328330
var fields []string;
329-
331+
{{- $prefixConfig := .CRD.Config.PrefixConfig }}
330332
{{- range $immutableField := .CRD.GetImmutableFieldPaths }}
331-
if delta.DifferentAt("{{$immutableField}}") {
333+
{{- $specPrefix := $prefixConfig.SpecField }}
334+
if delta.DifferentAt("{{ TrimPrefix $specPrefix "." }}.{{$immutableField}}") {
332335
fields = append(fields,"{{$immutableField}}")
333336
}
334337
{{- end }}
335338

336339
return fields
337340
}
338-
339-
// handleImmutableFieldsChangedCondition validates the immutable fields and set appropriate condition
340-
func (rm *resourceManager) handleImmutableFieldsChangedCondition(
341-
r *resource,
342-
delta *ackcompare.Delta,
343-
) *resource {
344-
345-
fields := rm.getImmutableFieldChanges(delta)
346-
ko := r.ko.DeepCopy()
347-
var advisoryCondition *ackv1alpha1.Condition = nil
348-
for _, condition := range ko.Status.Conditions {
349-
if condition.Type == ackv1alpha1.ConditionTypeAdvisory {
350-
advisoryCondition = condition
351-
break
352-
}
353-
}
354-
355-
// Remove the advisory condition if issue is no longer present
356-
if len(fields) == 0 && advisoryCondition != nil{
357-
var newConditions []*ackv1alpha1.Condition
358-
for _, condition := range ko.Status.Conditions {
359-
if condition.Type != ackv1alpha1.ConditionTypeAdvisory {
360-
newConditions = append(newConditions,condition)
361-
}
362-
}
363-
ko.Status.Conditions = newConditions
364-
}
365-
366-
if len(fields) > 0 {
367-
if advisoryCondition == nil {
368-
advisoryCondition = &ackv1alpha1.Condition{
369-
Type: ackv1alpha1.ConditionTypeAdvisory,
370-
}
371-
ko.Status.Conditions = append(ko.Status.Conditions, advisoryCondition)
372-
}
373-
374-
advisoryCondition.Status = corev1.ConditionTrue
375-
message := "Immutable Spec fields have been modified : " + strings.Join(fields, ",")
376-
advisoryCondition.Message = &message
377-
}
378-
379-
return &resource{ko}
380-
}
381341
{{- end }}
382342
{{- if $hookCode := Hook .CRD "sdk_file_end" }}
383343
{{ $hookCode }}

templates/pkg/resource/sdk_update.go.tpl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ func (rm *resourceManager) sdkUpdate(
3838
return nil, err
3939
}
4040
{{- if .CRD.HasImmutableFieldChanges }}
41-
desired = rm.handleImmutableFieldsChangedCondition(desired, delta)
41+
if immutableFieldChanges := rm.getImmutableFieldChanges(delta); len(immutableFieldChanges) > 0 {
42+
msg := fmt.Sprintf("Immutable Spec fields have been modified: %s", strings.Join(immutableFieldChanges, ","))
43+
return nil, ackerr.NewTerminalError(fmt.Errorf(msg))
44+
}
4245
{{- end }}
4346
// Merge in the information we read from the API call above to the copy of
4447
// the original Kubernetes object we passed to the function

0 commit comments

Comments
 (0)