Skip to content

Commit 6b76870

Browse files
committed
Filter AWS injected tags
We are adding a new generated code that handles the filtering of tags. These tags may be injected AWS resources during creation, or are added to resources managed by cloudformation for example, and the controller would see them in the diff and attempt to remove them. With these changes we ensure that any tag that has `aws:` prefix will be filtered out after the Find operation. Doing so will ensure that we don't attempt to remove these tags.
1 parent 8762917 commit 6b76870

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

templates/pkg/resource/manager.go.tpl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ func (rm *resourceManager) ReadOne(
8989
panic("resource manager's ReadOne() method received resource with nil CR object")
9090
}
9191
observed, err := rm.sdkFind(ctx, r)
92+
filterAWSTags(observed)
9293
if err != nil {
9394
if observed != nil {
9495
return rm.onError(observed, err)
@@ -311,6 +312,45 @@ func (rm *resourceManager) EnsureTags(
311312
{{- end }}
312313
}
313314
315+
// filterAWSTags ignores tags that have keys that start with "aws:"
316+
// is needed to ensure the controller does not attempt to remove
317+
// tags set by AWS. This function needs to be called after each Read
318+
// operation.
319+
// Eg. resources created with cloudformation have tags that cannot be
320+
//removed by an ACK controller
321+
func filterAWSTags(r *resource) {
322+
{{- if $hookCode := Hook .CRD "filter_tags" }}
323+
{{ $hookCode }}
324+
{{ else }}
325+
{{ $tagField := .CRD.GetTagField -}}
326+
{{ if $tagField -}}
327+
{{ $tagFieldShapeType := $tagField.ShapeRef.Shape.Type -}}
328+
{{ $tagFieldGoType := $tagField.GoType -}}
329+
{{ if eq "list" $tagFieldShapeType -}}
330+
{{ $tagFieldGoType = (print "[]*svcapitypes." $tagField.GoTypeElem) -}}
331+
{{ end -}}
332+
if r == nil || r.ko == nil {
333+
return
334+
}
335+
var existingTags {{ $tagFieldGoType }}
336+
{{ $nilCheck := CheckNilFieldPath $tagField "r.ko.Spec" -}}
337+
{{ if not (eq $nilCheck "") -}}
338+
if {{ $nilCheck }} {
339+
return
340+
} else {
341+
existingTags = r.ko.Spec.{{ $tagField.Path }}
342+
}
343+
{{ else -}}
344+
existingTags = r.ko.Spec.{{ $tagField.Path }}
345+
{{ end -}}
346+
resourceTags := ToACKTags(existingTags)
347+
acktags.IgnoreAWSTags(resourceTags)
348+
{{ GoCodeInitializeNestedStructField .CRD "r.ko" $tagField "svcapitypes" 1 -}}
349+
r.ko.Spec.{{ $tagField.Path }} = FromACKTags(resourceTags)
350+
{{- end }}
351+
{{- end }}
352+
}
353+
314354
// newResourceManager returns a new struct implementing
315355
// acktypes.AWSResourceManager
316356
// This is for AWS-SDK-GO-V2 - Created newResourceManager With AWS sdk-Go-ClientV2

0 commit comments

Comments
 (0)