Skip to content

Commit bced95a

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 bced95a

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

templates/pkg/resource/manager.go.tpl

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ func (rm *resourceManager) Update(
140140
// Should never happen... if it does, it's buggy code.
141141
panic("resource manager's Update() method received resource with nil CR object")
142142
}
143+
syncAWSTags(desired, latest)
143144
updated, err := rm.sdkUpdate(ctx, desired, latest, delta)
144145
if err != nil {
145146
if updated != nil {
@@ -311,6 +312,92 @@ 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 (rm *resourceManager) FilterAWSTags(res acktypes.AWSResource) {
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+
r := rm.concreteResource(res)
333+
if r == nil || r.ko == nil {
334+
return
335+
}
336+
var existingTags {{ $tagFieldGoType }}
337+
{{ $nilCheck := CheckNilFieldPath $tagField "r.ko.Spec" -}}
338+
{{ if not (eq $nilCheck "") -}}
339+
if {{ $nilCheck }} {
340+
return
341+
}
342+
{{ end -}}
343+
existingTags = r.ko.Spec.{{ $tagField.Path }}
344+
resourceTags := ToACKTags(existingTags)
345+
acktags.IgnoreAWSTags(resourceTags)
346+
{{ GoCodeInitializeNestedStructField .CRD "r.ko" $tagField "svcapitypes" 1 -}}
347+
r.ko.Spec.{{ $tagField.Path }} = FromACKTags(resourceTags)
348+
{{- end }}
349+
{{- end }}
350+
}
351+
352+
353+
// syncAWSTags ignores tags that have keys that start with "aws:"
354+
// is needed to ensure the controller does not attempt to remove
355+
// tags set by AWS. This function needs to be called after each Read
356+
// operation.
357+
// Eg. resources created with cloudformation have tags that cannot be
358+
//removed by an ACK controller
359+
func syncAWSTags(a *resource, b *resource) {
360+
{{- if $hookCode := Hook .CRD "sync_tags" }}
361+
{{ $hookCode }}
362+
{{ else }}
363+
{{ $tagField := .CRD.GetTagField -}}
364+
{{ if $tagField -}}
365+
{{ $tagFieldShapeType := $tagField.ShapeRef.Shape.Type -}}
366+
{{ $tagFieldGoType := $tagField.GoType -}}
367+
{{ if eq "list" $tagFieldShapeType -}}
368+
{{ $tagFieldGoType = (print "[]*svcapitypes." $tagField.GoTypeElem) -}}
369+
{{ end -}}
370+
if a == nil || a.ko == nil || b == nil || b.ko == nil {
371+
return
372+
}
373+
var existingLatestTags []*svcapitypes.Tag
374+
var existingDesiredTags []*svcapitypes.Tag
375+
{{ $nilCheck := CheckNilFieldPath $tagField "b.ko.Spec" -}}
376+
{{ if not (eq $nilCheck "") -}}
377+
if {{ $nilCheck }} {
378+
return
379+
}
380+
{{ end -}}
381+
{{ $nilCheck = CheckNilFieldPath $tagField "a.ko.Spec" -}}
382+
{{if not (eq $nilCheck "") -}}
383+
if {{ $nilCheck }} {
384+
existingDesiredTags = nil
385+
} else {
386+
existingDesiredTags = a.ko.Spec.{{ $tagField.Path }}
387+
}
388+
{{ else -}}
389+
existingDesiredTags = a.ko.Spec.{{ $tagField.Path }}
390+
{{ end -}}
391+
existingLatestTags = b.ko.Spec.{{ $tagField.Path }}
392+
desiredTags := ToACKTags(existingDesiredTags)
393+
latestTags := ToACKTags(existingLatestTags)
394+
acktags.SyncAWSTags(desiredTags, latestTags)
395+
{{ GoCodeInitializeNestedStructField .CRD "a.ko" $tagField "svcapitypes" 1 -}}
396+
a.ko.Spec.{{ $tagField.Path }} = FromACKTags(desiredTags)
397+
{{- end }}
398+
{{- end }}
399+
}
400+
314401
// newResourceManager returns a new struct implementing
315402
// acktypes.AWSResourceManager
316403
// This is for AWS-SDK-GO-V2 - Created newResourceManager With AWS sdk-Go-ClientV2

0 commit comments

Comments
 (0)