diff --git a/apis/v1alpha1/ack-generate-metadata.yaml b/apis/v1alpha1/ack-generate-metadata.yaml index e410b320..e6c3128c 100755 --- a/apis/v1alpha1/ack-generate-metadata.yaml +++ b/apis/v1alpha1/ack-generate-metadata.yaml @@ -1,13 +1,13 @@ ack_generate_info: - build_date: "2025-03-10T20:08:48Z" - build_hash: cfbd9fc8a32a564e2f05252b60248053ff09e744 + build_date: "2025-03-25T22:26:20Z" + build_hash: 3722729cebe6d3c03c7e442655ef0846f91566a2 go_version: go1.24.0 - version: v0.43.2-4-gcfbd9fc + version: v0.43.2-7-g3722729 api_directory_checksum: b31faecf6092fab9677498f3624e624fee4cbaed api_version: v1alpha1 aws_sdk_go_version: v1.32.6 generator_config_info: - file_checksum: 4f9d21bd7c46b495cd9374e454a3a0e1e6de5d08 + file_checksum: 33b2c8e790a68bd28b5efe9faa3b8e5fb980d1f4 original_file_name: generator.yaml last_modification: reason: API generation diff --git a/apis/v1alpha1/generator.yaml b/apis/v1alpha1/generator.yaml index 6b279605..6013e90f 100644 --- a/apis/v1alpha1/generator.yaml +++ b/apis/v1alpha1/generator.yaml @@ -707,6 +707,8 @@ resources: input_fields: NetworkAclId: Id hooks: + delta_pre_compare: + code: customPreCompare(delta, a, b) sdk_create_post_build_request: template_path: hooks/network_acl/sdk_create_post_build_request.go.tpl sdk_file_end: diff --git a/generator.yaml b/generator.yaml index 6b279605..6013e90f 100644 --- a/generator.yaml +++ b/generator.yaml @@ -707,6 +707,8 @@ resources: input_fields: NetworkAclId: Id hooks: + delta_pre_compare: + code: customPreCompare(delta, a, b) sdk_create_post_build_request: template_path: hooks/network_acl/sdk_create_post_build_request.go.tpl sdk_file_end: diff --git a/pkg/resource/capacity_reservation/delta.go b/pkg/resource/capacity_reservation/delta.go index 92acf887..7e2f239e 100644 --- a/pkg/resource/capacity_reservation/delta.go +++ b/pkg/resource/capacity_reservation/delta.go @@ -155,7 +155,9 @@ func newResourceDelta( delta.Add("Spec.StartDate", a.ko.Spec.StartDate, b.ko.Spec.StartDate) } } - if !ackcompare.MapStringStringEqual(ToACKTags(a.ko.Spec.Tags), ToACKTags(b.ko.Spec.Tags)) { + desiredACKTags, _ := convertToOrderedACKTags(a.ko.Spec.Tags) + latestACKTags, _ := convertToOrderedACKTags(b.ko.Spec.Tags) + if !ackcompare.MapStringStringEqual(desiredACKTags, latestACKTags) { delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags) } if ackcompare.HasNilDifference(a.ko.Spec.Tenancy, b.ko.Spec.Tenancy) { diff --git a/pkg/resource/capacity_reservation/manager.go b/pkg/resource/capacity_reservation/manager.go index 1983c3ee..1ed5b4cd 100644 --- a/pkg/resource/capacity_reservation/manager.go +++ b/pkg/resource/capacity_reservation/manager.go @@ -291,9 +291,9 @@ func (rm *resourceManager) EnsureTags( defaultTags := ackrt.GetDefaultTags(&rm.cfg, r.ko, md) var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags := ToACKTags(existingTags) + resourceTags, keyOrder := convertToOrderedACKTags(existingTags) tags := acktags.Merge(resourceTags, defaultTags) - r.ko.Spec.Tags = FromACKTags(tags) + r.ko.Spec.Tags = fromACKTags(tags, keyOrder) return nil } @@ -310,9 +310,9 @@ func (rm *resourceManager) FilterSystemTags(res acktypes.AWSResource) { } var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags, tagKeyOrder := toACKTagsWithKeyOrder(existingTags) + resourceTags, tagKeyOrder := convertToOrderedACKTags(existingTags) ignoreSystemTags(resourceTags) - r.ko.Spec.Tags = fromACKTagsWithKeyOrder(resourceTags, tagKeyOrder) + r.ko.Spec.Tags = fromACKTags(resourceTags, tagKeyOrder) } // mirrorAWSTags ensures that AWS tags are included in the desired resource @@ -334,10 +334,10 @@ func mirrorAWSTags(a *resource, b *resource) { var existingDesiredTags []*svcapitypes.Tag existingDesiredTags = a.ko.Spec.Tags existingLatestTags = b.ko.Spec.Tags - desiredTags, desiredTagKeyOrder := toACKTagsWithKeyOrder(existingDesiredTags) - latestTags, _ := toACKTagsWithKeyOrder(existingLatestTags) + desiredTags, desiredTagKeyOrder := convertToOrderedACKTags(existingDesiredTags) + latestTags, _ := convertToOrderedACKTags(existingLatestTags) syncAWSTags(desiredTags, latestTags) - a.ko.Spec.Tags = fromACKTagsWithKeyOrder(desiredTags, desiredTagKeyOrder) + a.ko.Spec.Tags = fromACKTags(desiredTags, desiredTagKeyOrder) } // newResourceManager returns a new struct implementing diff --git a/pkg/resource/capacity_reservation/tags.go b/pkg/resource/capacity_reservation/tags.go index 6ce56a47..c11e156c 100644 --- a/pkg/resource/capacity_reservation/tags.go +++ b/pkg/resource/capacity_reservation/tags.go @@ -30,69 +30,38 @@ var ( ACKSystemTags = []string{"services.k8s.aws/namespace", "services.k8s.aws/controller-version"} ) -// ToACKTags converts the tags parameter into 'acktags.Tags' shape. -// This method helps in creating the hub(acktags.Tags) for merging -// default controller tags with existing resource tags. -func ToACKTags(tags []*svcapitypes.Tag) acktags.Tags { - result := acktags.NewTags() - if tags == nil || len(tags) == 0 { - return result - } - - for _, t := range tags { - if t.Key != nil { - if t.Value == nil { - result[*t.Key] = "" - } else { - result[*t.Key] = *t.Value - } - } - } - - return result -} - -// toACKTagsWithKeyOrder converts the tags parameter into 'acktags.Tags' shape. +// convertToOrderedACKTags converts the tags parameter into 'acktags.Tags' shape. // This method helps in creating the hub(acktags.Tags) for merging // default controller tags with existing resource tags. It also returns a slice // of keys maintaining the original key Order when the tags are a list -func toACKTagsWithKeyOrder(tags []*svcapitypes.Tag) (acktags.Tags, []string) { +func convertToOrderedACKTags(tags []*svcapitypes.Tag) (acktags.Tags, []string) { result := acktags.NewTags() keyOrder := []string{} - if tags == nil || len(tags) == 0 { + + if len(tags) == 0 { return result, keyOrder } - for _, t := range tags { if t.Key != nil { keyOrder = append(keyOrder, *t.Key) + if t.Value != nil { + result[*t.Key] = *t.Value + } else { + result[*t.Key] = "" + } } } - result = ToACKTags(tags) return result, keyOrder } -// FromACKTags converts the tags parameter into []*svcapitypes.Tag shape. -// This method helps in setting the tags back inside AWSResource after merging -// default controller tags with existing resource tags. -func FromACKTags(tags acktags.Tags) []*svcapitypes.Tag { - result := []*svcapitypes.Tag{} - for k, v := range tags { - kCopy := k - vCopy := v - tag := svcapitypes.Tag{Key: &kCopy, Value: &vCopy} - result = append(result, &tag) - } - return result -} - -// fromACKTagsWithTagKeys converts the tags parameter into []*svcapitypes.Tag shape. +// fromACKTags converts the tags parameter into []*svcapitypes.Tag shape. // This method helps in setting the tags back inside AWSResource after merging // default controller tags with existing resource tags. When a list, // it maintains the order from original -func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { +func fromACKTags(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { result := []*svcapitypes.Tag{} + for _, k := range keyOrder { v, ok := tags[k] if ok { @@ -101,7 +70,11 @@ func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitype delete(tags, k) } } - result = append(result, FromACKTags(tags)...) + for k, v := range tags { + tag := svcapitypes.Tag{Key: &k, Value: &v} + result = append(result, &tag) + } + return result } diff --git a/pkg/resource/dhcp_options/delta.go b/pkg/resource/dhcp_options/delta.go index d2f9f70d..20053306 100644 --- a/pkg/resource/dhcp_options/delta.go +++ b/pkg/resource/dhcp_options/delta.go @@ -50,7 +50,9 @@ func newResourceDelta( delta.Add("Spec.DHCPConfigurations", a.ko.Spec.DHCPConfigurations, b.ko.Spec.DHCPConfigurations) } } - if !ackcompare.MapStringStringEqual(ToACKTags(a.ko.Spec.Tags), ToACKTags(b.ko.Spec.Tags)) { + desiredACKTags, _ := convertToOrderedACKTags(a.ko.Spec.Tags) + latestACKTags, _ := convertToOrderedACKTags(b.ko.Spec.Tags) + if !ackcompare.MapStringStringEqual(desiredACKTags, latestACKTags) { delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags) } if len(a.ko.Spec.VPC) != len(b.ko.Spec.VPC) { diff --git a/pkg/resource/dhcp_options/manager.go b/pkg/resource/dhcp_options/manager.go index 0b46dd58..fc93b2cd 100644 --- a/pkg/resource/dhcp_options/manager.go +++ b/pkg/resource/dhcp_options/manager.go @@ -291,9 +291,9 @@ func (rm *resourceManager) EnsureTags( defaultTags := ackrt.GetDefaultTags(&rm.cfg, r.ko, md) var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags := ToACKTags(existingTags) + resourceTags, keyOrder := convertToOrderedACKTags(existingTags) tags := acktags.Merge(resourceTags, defaultTags) - r.ko.Spec.Tags = FromACKTags(tags) + r.ko.Spec.Tags = fromACKTags(tags, keyOrder) return nil } @@ -310,9 +310,9 @@ func (rm *resourceManager) FilterSystemTags(res acktypes.AWSResource) { } var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags, tagKeyOrder := toACKTagsWithKeyOrder(existingTags) + resourceTags, tagKeyOrder := convertToOrderedACKTags(existingTags) ignoreSystemTags(resourceTags) - r.ko.Spec.Tags = fromACKTagsWithKeyOrder(resourceTags, tagKeyOrder) + r.ko.Spec.Tags = fromACKTags(resourceTags, tagKeyOrder) } // mirrorAWSTags ensures that AWS tags are included in the desired resource @@ -334,10 +334,10 @@ func mirrorAWSTags(a *resource, b *resource) { var existingDesiredTags []*svcapitypes.Tag existingDesiredTags = a.ko.Spec.Tags existingLatestTags = b.ko.Spec.Tags - desiredTags, desiredTagKeyOrder := toACKTagsWithKeyOrder(existingDesiredTags) - latestTags, _ := toACKTagsWithKeyOrder(existingLatestTags) + desiredTags, desiredTagKeyOrder := convertToOrderedACKTags(existingDesiredTags) + latestTags, _ := convertToOrderedACKTags(existingLatestTags) syncAWSTags(desiredTags, latestTags) - a.ko.Spec.Tags = fromACKTagsWithKeyOrder(desiredTags, desiredTagKeyOrder) + a.ko.Spec.Tags = fromACKTags(desiredTags, desiredTagKeyOrder) } // newResourceManager returns a new struct implementing diff --git a/pkg/resource/dhcp_options/tags.go b/pkg/resource/dhcp_options/tags.go index 2a787ceb..77524501 100644 --- a/pkg/resource/dhcp_options/tags.go +++ b/pkg/resource/dhcp_options/tags.go @@ -30,69 +30,38 @@ var ( ACKSystemTags = []string{"services.k8s.aws/namespace", "services.k8s.aws/controller-version"} ) -// ToACKTags converts the tags parameter into 'acktags.Tags' shape. -// This method helps in creating the hub(acktags.Tags) for merging -// default controller tags with existing resource tags. -func ToACKTags(tags []*svcapitypes.Tag) acktags.Tags { - result := acktags.NewTags() - if tags == nil || len(tags) == 0 { - return result - } - - for _, t := range tags { - if t.Key != nil { - if t.Value == nil { - result[*t.Key] = "" - } else { - result[*t.Key] = *t.Value - } - } - } - - return result -} - -// toACKTagsWithKeyOrder converts the tags parameter into 'acktags.Tags' shape. +// convertToOrderedACKTags converts the tags parameter into 'acktags.Tags' shape. // This method helps in creating the hub(acktags.Tags) for merging // default controller tags with existing resource tags. It also returns a slice // of keys maintaining the original key Order when the tags are a list -func toACKTagsWithKeyOrder(tags []*svcapitypes.Tag) (acktags.Tags, []string) { +func convertToOrderedACKTags(tags []*svcapitypes.Tag) (acktags.Tags, []string) { result := acktags.NewTags() keyOrder := []string{} - if tags == nil || len(tags) == 0 { + + if len(tags) == 0 { return result, keyOrder } - for _, t := range tags { if t.Key != nil { keyOrder = append(keyOrder, *t.Key) + if t.Value != nil { + result[*t.Key] = *t.Value + } else { + result[*t.Key] = "" + } } } - result = ToACKTags(tags) return result, keyOrder } -// FromACKTags converts the tags parameter into []*svcapitypes.Tag shape. -// This method helps in setting the tags back inside AWSResource after merging -// default controller tags with existing resource tags. -func FromACKTags(tags acktags.Tags) []*svcapitypes.Tag { - result := []*svcapitypes.Tag{} - for k, v := range tags { - kCopy := k - vCopy := v - tag := svcapitypes.Tag{Key: &kCopy, Value: &vCopy} - result = append(result, &tag) - } - return result -} - -// fromACKTagsWithTagKeys converts the tags parameter into []*svcapitypes.Tag shape. +// fromACKTags converts the tags parameter into []*svcapitypes.Tag shape. // This method helps in setting the tags back inside AWSResource after merging // default controller tags with existing resource tags. When a list, // it maintains the order from original -func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { +func fromACKTags(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { result := []*svcapitypes.Tag{} + for _, k := range keyOrder { v, ok := tags[k] if ok { @@ -101,7 +70,11 @@ func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitype delete(tags, k) } } - result = append(result, FromACKTags(tags)...) + for k, v := range tags { + tag := svcapitypes.Tag{Key: &k, Value: &v} + result = append(result, &tag) + } + return result } diff --git a/pkg/resource/elastic_ip_address/delta.go b/pkg/resource/elastic_ip_address/delta.go index c45c0c06..12b4f6cf 100644 --- a/pkg/resource/elastic_ip_address/delta.go +++ b/pkg/resource/elastic_ip_address/delta.go @@ -71,7 +71,9 @@ func newResourceDelta( delta.Add("Spec.PublicIPv4Pool", a.ko.Spec.PublicIPv4Pool, b.ko.Spec.PublicIPv4Pool) } } - if !ackcompare.MapStringStringEqual(ToACKTags(a.ko.Spec.Tags), ToACKTags(b.ko.Spec.Tags)) { + desiredACKTags, _ := convertToOrderedACKTags(a.ko.Spec.Tags) + latestACKTags, _ := convertToOrderedACKTags(b.ko.Spec.Tags) + if !ackcompare.MapStringStringEqual(desiredACKTags, latestACKTags) { delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags) } diff --git a/pkg/resource/elastic_ip_address/manager.go b/pkg/resource/elastic_ip_address/manager.go index 4aaf5a96..51f47a3b 100644 --- a/pkg/resource/elastic_ip_address/manager.go +++ b/pkg/resource/elastic_ip_address/manager.go @@ -291,9 +291,9 @@ func (rm *resourceManager) EnsureTags( defaultTags := ackrt.GetDefaultTags(&rm.cfg, r.ko, md) var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags := ToACKTags(existingTags) + resourceTags, keyOrder := convertToOrderedACKTags(existingTags) tags := acktags.Merge(resourceTags, defaultTags) - r.ko.Spec.Tags = FromACKTags(tags) + r.ko.Spec.Tags = fromACKTags(tags, keyOrder) return nil } @@ -310,9 +310,9 @@ func (rm *resourceManager) FilterSystemTags(res acktypes.AWSResource) { } var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags, tagKeyOrder := toACKTagsWithKeyOrder(existingTags) + resourceTags, tagKeyOrder := convertToOrderedACKTags(existingTags) ignoreSystemTags(resourceTags) - r.ko.Spec.Tags = fromACKTagsWithKeyOrder(resourceTags, tagKeyOrder) + r.ko.Spec.Tags = fromACKTags(resourceTags, tagKeyOrder) } // mirrorAWSTags ensures that AWS tags are included in the desired resource @@ -334,10 +334,10 @@ func mirrorAWSTags(a *resource, b *resource) { var existingDesiredTags []*svcapitypes.Tag existingDesiredTags = a.ko.Spec.Tags existingLatestTags = b.ko.Spec.Tags - desiredTags, desiredTagKeyOrder := toACKTagsWithKeyOrder(existingDesiredTags) - latestTags, _ := toACKTagsWithKeyOrder(existingLatestTags) + desiredTags, desiredTagKeyOrder := convertToOrderedACKTags(existingDesiredTags) + latestTags, _ := convertToOrderedACKTags(existingLatestTags) syncAWSTags(desiredTags, latestTags) - a.ko.Spec.Tags = fromACKTagsWithKeyOrder(desiredTags, desiredTagKeyOrder) + a.ko.Spec.Tags = fromACKTags(desiredTags, desiredTagKeyOrder) } // newResourceManager returns a new struct implementing diff --git a/pkg/resource/elastic_ip_address/tags.go b/pkg/resource/elastic_ip_address/tags.go index bb907021..2f7a79f8 100644 --- a/pkg/resource/elastic_ip_address/tags.go +++ b/pkg/resource/elastic_ip_address/tags.go @@ -30,69 +30,38 @@ var ( ACKSystemTags = []string{"services.k8s.aws/namespace", "services.k8s.aws/controller-version"} ) -// ToACKTags converts the tags parameter into 'acktags.Tags' shape. -// This method helps in creating the hub(acktags.Tags) for merging -// default controller tags with existing resource tags. -func ToACKTags(tags []*svcapitypes.Tag) acktags.Tags { - result := acktags.NewTags() - if tags == nil || len(tags) == 0 { - return result - } - - for _, t := range tags { - if t.Key != nil { - if t.Value == nil { - result[*t.Key] = "" - } else { - result[*t.Key] = *t.Value - } - } - } - - return result -} - -// toACKTagsWithKeyOrder converts the tags parameter into 'acktags.Tags' shape. +// convertToOrderedACKTags converts the tags parameter into 'acktags.Tags' shape. // This method helps in creating the hub(acktags.Tags) for merging // default controller tags with existing resource tags. It also returns a slice // of keys maintaining the original key Order when the tags are a list -func toACKTagsWithKeyOrder(tags []*svcapitypes.Tag) (acktags.Tags, []string) { +func convertToOrderedACKTags(tags []*svcapitypes.Tag) (acktags.Tags, []string) { result := acktags.NewTags() keyOrder := []string{} - if tags == nil || len(tags) == 0 { + + if len(tags) == 0 { return result, keyOrder } - for _, t := range tags { if t.Key != nil { keyOrder = append(keyOrder, *t.Key) + if t.Value != nil { + result[*t.Key] = *t.Value + } else { + result[*t.Key] = "" + } } } - result = ToACKTags(tags) return result, keyOrder } -// FromACKTags converts the tags parameter into []*svcapitypes.Tag shape. -// This method helps in setting the tags back inside AWSResource after merging -// default controller tags with existing resource tags. -func FromACKTags(tags acktags.Tags) []*svcapitypes.Tag { - result := []*svcapitypes.Tag{} - for k, v := range tags { - kCopy := k - vCopy := v - tag := svcapitypes.Tag{Key: &kCopy, Value: &vCopy} - result = append(result, &tag) - } - return result -} - -// fromACKTagsWithTagKeys converts the tags parameter into []*svcapitypes.Tag shape. +// fromACKTags converts the tags parameter into []*svcapitypes.Tag shape. // This method helps in setting the tags back inside AWSResource after merging // default controller tags with existing resource tags. When a list, // it maintains the order from original -func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { +func fromACKTags(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { result := []*svcapitypes.Tag{} + for _, k := range keyOrder { v, ok := tags[k] if ok { @@ -101,7 +70,11 @@ func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitype delete(tags, k) } } - result = append(result, FromACKTags(tags)...) + for k, v := range tags { + tag := svcapitypes.Tag{Key: &k, Value: &v} + result = append(result, &tag) + } + return result } diff --git a/pkg/resource/flow_log/delta.go b/pkg/resource/flow_log/delta.go index fa5de1b9..3c49ba82 100644 --- a/pkg/resource/flow_log/delta.go +++ b/pkg/resource/flow_log/delta.go @@ -124,7 +124,9 @@ func newResourceDelta( delta.Add("Spec.ResourceType", a.ko.Spec.ResourceType, b.ko.Spec.ResourceType) } } - if !ackcompare.MapStringStringEqual(ToACKTags(a.ko.Spec.Tags), ToACKTags(b.ko.Spec.Tags)) { + desiredACKTags, _ := convertToOrderedACKTags(a.ko.Spec.Tags) + latestACKTags, _ := convertToOrderedACKTags(b.ko.Spec.Tags) + if !ackcompare.MapStringStringEqual(desiredACKTags, latestACKTags) { delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags) } if ackcompare.HasNilDifference(a.ko.Spec.TrafficType, b.ko.Spec.TrafficType) { diff --git a/pkg/resource/flow_log/manager.go b/pkg/resource/flow_log/manager.go index 011bb506..a37d95de 100644 --- a/pkg/resource/flow_log/manager.go +++ b/pkg/resource/flow_log/manager.go @@ -291,9 +291,9 @@ func (rm *resourceManager) EnsureTags( defaultTags := ackrt.GetDefaultTags(&rm.cfg, r.ko, md) var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags := ToACKTags(existingTags) + resourceTags, keyOrder := convertToOrderedACKTags(existingTags) tags := acktags.Merge(resourceTags, defaultTags) - r.ko.Spec.Tags = FromACKTags(tags) + r.ko.Spec.Tags = fromACKTags(tags, keyOrder) return nil } @@ -310,9 +310,9 @@ func (rm *resourceManager) FilterSystemTags(res acktypes.AWSResource) { } var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags, tagKeyOrder := toACKTagsWithKeyOrder(existingTags) + resourceTags, tagKeyOrder := convertToOrderedACKTags(existingTags) ignoreSystemTags(resourceTags) - r.ko.Spec.Tags = fromACKTagsWithKeyOrder(resourceTags, tagKeyOrder) + r.ko.Spec.Tags = fromACKTags(resourceTags, tagKeyOrder) } // mirrorAWSTags ensures that AWS tags are included in the desired resource @@ -334,10 +334,10 @@ func mirrorAWSTags(a *resource, b *resource) { var existingDesiredTags []*svcapitypes.Tag existingDesiredTags = a.ko.Spec.Tags existingLatestTags = b.ko.Spec.Tags - desiredTags, desiredTagKeyOrder := toACKTagsWithKeyOrder(existingDesiredTags) - latestTags, _ := toACKTagsWithKeyOrder(existingLatestTags) + desiredTags, desiredTagKeyOrder := convertToOrderedACKTags(existingDesiredTags) + latestTags, _ := convertToOrderedACKTags(existingLatestTags) syncAWSTags(desiredTags, latestTags) - a.ko.Spec.Tags = fromACKTagsWithKeyOrder(desiredTags, desiredTagKeyOrder) + a.ko.Spec.Tags = fromACKTags(desiredTags, desiredTagKeyOrder) } // newResourceManager returns a new struct implementing diff --git a/pkg/resource/flow_log/tags.go b/pkg/resource/flow_log/tags.go index 6608f053..45ed0517 100644 --- a/pkg/resource/flow_log/tags.go +++ b/pkg/resource/flow_log/tags.go @@ -30,69 +30,38 @@ var ( ACKSystemTags = []string{"services.k8s.aws/namespace", "services.k8s.aws/controller-version"} ) -// ToACKTags converts the tags parameter into 'acktags.Tags' shape. -// This method helps in creating the hub(acktags.Tags) for merging -// default controller tags with existing resource tags. -func ToACKTags(tags []*svcapitypes.Tag) acktags.Tags { - result := acktags.NewTags() - if tags == nil || len(tags) == 0 { - return result - } - - for _, t := range tags { - if t.Key != nil { - if t.Value == nil { - result[*t.Key] = "" - } else { - result[*t.Key] = *t.Value - } - } - } - - return result -} - -// toACKTagsWithKeyOrder converts the tags parameter into 'acktags.Tags' shape. +// convertToOrderedACKTags converts the tags parameter into 'acktags.Tags' shape. // This method helps in creating the hub(acktags.Tags) for merging // default controller tags with existing resource tags. It also returns a slice // of keys maintaining the original key Order when the tags are a list -func toACKTagsWithKeyOrder(tags []*svcapitypes.Tag) (acktags.Tags, []string) { +func convertToOrderedACKTags(tags []*svcapitypes.Tag) (acktags.Tags, []string) { result := acktags.NewTags() keyOrder := []string{} - if tags == nil || len(tags) == 0 { + + if len(tags) == 0 { return result, keyOrder } - for _, t := range tags { if t.Key != nil { keyOrder = append(keyOrder, *t.Key) + if t.Value != nil { + result[*t.Key] = *t.Value + } else { + result[*t.Key] = "" + } } } - result = ToACKTags(tags) return result, keyOrder } -// FromACKTags converts the tags parameter into []*svcapitypes.Tag shape. -// This method helps in setting the tags back inside AWSResource after merging -// default controller tags with existing resource tags. -func FromACKTags(tags acktags.Tags) []*svcapitypes.Tag { - result := []*svcapitypes.Tag{} - for k, v := range tags { - kCopy := k - vCopy := v - tag := svcapitypes.Tag{Key: &kCopy, Value: &vCopy} - result = append(result, &tag) - } - return result -} - -// fromACKTagsWithTagKeys converts the tags parameter into []*svcapitypes.Tag shape. +// fromACKTags converts the tags parameter into []*svcapitypes.Tag shape. // This method helps in setting the tags back inside AWSResource after merging // default controller tags with existing resource tags. When a list, // it maintains the order from original -func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { +func fromACKTags(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { result := []*svcapitypes.Tag{} + for _, k := range keyOrder { v, ok := tags[k] if ok { @@ -101,7 +70,11 @@ func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitype delete(tags, k) } } - result = append(result, FromACKTags(tags)...) + for k, v := range tags { + tag := svcapitypes.Tag{Key: &k, Value: &v} + result = append(result, &tag) + } + return result } diff --git a/pkg/resource/instance/delta.go b/pkg/resource/instance/delta.go index bedf0d10..7093e950 100644 --- a/pkg/resource/instance/delta.go +++ b/pkg/resource/instance/delta.go @@ -516,7 +516,9 @@ func newResourceDelta( delta.Add("Spec.SubnetID", a.ko.Spec.SubnetID, b.ko.Spec.SubnetID) } } - if !ackcompare.MapStringStringEqual(ToACKTags(a.ko.Spec.Tags), ToACKTags(b.ko.Spec.Tags)) { + desiredACKTags, _ := convertToOrderedACKTags(a.ko.Spec.Tags) + latestACKTags, _ := convertToOrderedACKTags(b.ko.Spec.Tags) + if !ackcompare.MapStringStringEqual(desiredACKTags, latestACKTags) { delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags) } if ackcompare.HasNilDifference(a.ko.Spec.UserData, b.ko.Spec.UserData) { diff --git a/pkg/resource/instance/manager.go b/pkg/resource/instance/manager.go index 74ec88c9..c33f8c1b 100644 --- a/pkg/resource/instance/manager.go +++ b/pkg/resource/instance/manager.go @@ -300,9 +300,9 @@ func (rm *resourceManager) EnsureTags( defaultTags := ackrt.GetDefaultTags(&rm.cfg, r.ko, md) var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags := ToACKTags(existingTags) + resourceTags, keyOrder := convertToOrderedACKTags(existingTags) tags := acktags.Merge(resourceTags, defaultTags) - r.ko.Spec.Tags = FromACKTags(tags) + r.ko.Spec.Tags = fromACKTags(tags, keyOrder) return nil } @@ -319,9 +319,9 @@ func (rm *resourceManager) FilterSystemTags(res acktypes.AWSResource) { } var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags, tagKeyOrder := toACKTagsWithKeyOrder(existingTags) + resourceTags, tagKeyOrder := convertToOrderedACKTags(existingTags) ignoreSystemTags(resourceTags) - r.ko.Spec.Tags = fromACKTagsWithKeyOrder(resourceTags, tagKeyOrder) + r.ko.Spec.Tags = fromACKTags(resourceTags, tagKeyOrder) } // mirrorAWSTags ensures that AWS tags are included in the desired resource @@ -343,10 +343,10 @@ func mirrorAWSTags(a *resource, b *resource) { var existingDesiredTags []*svcapitypes.Tag existingDesiredTags = a.ko.Spec.Tags existingLatestTags = b.ko.Spec.Tags - desiredTags, desiredTagKeyOrder := toACKTagsWithKeyOrder(existingDesiredTags) - latestTags, _ := toACKTagsWithKeyOrder(existingLatestTags) + desiredTags, desiredTagKeyOrder := convertToOrderedACKTags(existingDesiredTags) + latestTags, _ := convertToOrderedACKTags(existingLatestTags) syncAWSTags(desiredTags, latestTags) - a.ko.Spec.Tags = fromACKTagsWithKeyOrder(desiredTags, desiredTagKeyOrder) + a.ko.Spec.Tags = fromACKTags(desiredTags, desiredTagKeyOrder) } // newResourceManager returns a new struct implementing diff --git a/pkg/resource/instance/tags.go b/pkg/resource/instance/tags.go index b1e3af87..48673f18 100644 --- a/pkg/resource/instance/tags.go +++ b/pkg/resource/instance/tags.go @@ -30,69 +30,38 @@ var ( ACKSystemTags = []string{"services.k8s.aws/namespace", "services.k8s.aws/controller-version"} ) -// ToACKTags converts the tags parameter into 'acktags.Tags' shape. -// This method helps in creating the hub(acktags.Tags) for merging -// default controller tags with existing resource tags. -func ToACKTags(tags []*svcapitypes.Tag) acktags.Tags { - result := acktags.NewTags() - if tags == nil || len(tags) == 0 { - return result - } - - for _, t := range tags { - if t.Key != nil { - if t.Value == nil { - result[*t.Key] = "" - } else { - result[*t.Key] = *t.Value - } - } - } - - return result -} - -// toACKTagsWithKeyOrder converts the tags parameter into 'acktags.Tags' shape. +// convertToOrderedACKTags converts the tags parameter into 'acktags.Tags' shape. // This method helps in creating the hub(acktags.Tags) for merging // default controller tags with existing resource tags. It also returns a slice // of keys maintaining the original key Order when the tags are a list -func toACKTagsWithKeyOrder(tags []*svcapitypes.Tag) (acktags.Tags, []string) { +func convertToOrderedACKTags(tags []*svcapitypes.Tag) (acktags.Tags, []string) { result := acktags.NewTags() keyOrder := []string{} - if tags == nil || len(tags) == 0 { + + if len(tags) == 0 { return result, keyOrder } - for _, t := range tags { if t.Key != nil { keyOrder = append(keyOrder, *t.Key) + if t.Value != nil { + result[*t.Key] = *t.Value + } else { + result[*t.Key] = "" + } } } - result = ToACKTags(tags) return result, keyOrder } -// FromACKTags converts the tags parameter into []*svcapitypes.Tag shape. -// This method helps in setting the tags back inside AWSResource after merging -// default controller tags with existing resource tags. -func FromACKTags(tags acktags.Tags) []*svcapitypes.Tag { - result := []*svcapitypes.Tag{} - for k, v := range tags { - kCopy := k - vCopy := v - tag := svcapitypes.Tag{Key: &kCopy, Value: &vCopy} - result = append(result, &tag) - } - return result -} - -// fromACKTagsWithTagKeys converts the tags parameter into []*svcapitypes.Tag shape. +// fromACKTags converts the tags parameter into []*svcapitypes.Tag shape. // This method helps in setting the tags back inside AWSResource after merging // default controller tags with existing resource tags. When a list, // it maintains the order from original -func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { +func fromACKTags(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { result := []*svcapitypes.Tag{} + for _, k := range keyOrder { v, ok := tags[k] if ok { @@ -101,7 +70,11 @@ func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitype delete(tags, k) } } - result = append(result, FromACKTags(tags)...) + for k, v := range tags { + tag := svcapitypes.Tag{Key: &k, Value: &v} + result = append(result, &tag) + } + return result } diff --git a/pkg/resource/internet_gateway/delta.go b/pkg/resource/internet_gateway/delta.go index 1c143cc4..558d9946 100644 --- a/pkg/resource/internet_gateway/delta.go +++ b/pkg/resource/internet_gateway/delta.go @@ -53,7 +53,9 @@ func newResourceDelta( delta.Add("Spec.RouteTables", a.ko.Spec.RouteTables, b.ko.Spec.RouteTables) } } - if !ackcompare.MapStringStringEqual(ToACKTags(a.ko.Spec.Tags), ToACKTags(b.ko.Spec.Tags)) { + desiredACKTags, _ := convertToOrderedACKTags(a.ko.Spec.Tags) + latestACKTags, _ := convertToOrderedACKTags(b.ko.Spec.Tags) + if !ackcompare.MapStringStringEqual(desiredACKTags, latestACKTags) { delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags) } if ackcompare.HasNilDifference(a.ko.Spec.VPC, b.ko.Spec.VPC) { diff --git a/pkg/resource/internet_gateway/manager.go b/pkg/resource/internet_gateway/manager.go index b467dff1..926b6669 100644 --- a/pkg/resource/internet_gateway/manager.go +++ b/pkg/resource/internet_gateway/manager.go @@ -291,9 +291,9 @@ func (rm *resourceManager) EnsureTags( defaultTags := ackrt.GetDefaultTags(&rm.cfg, r.ko, md) var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags := ToACKTags(existingTags) + resourceTags, keyOrder := convertToOrderedACKTags(existingTags) tags := acktags.Merge(resourceTags, defaultTags) - r.ko.Spec.Tags = FromACKTags(tags) + r.ko.Spec.Tags = fromACKTags(tags, keyOrder) return nil } @@ -310,9 +310,9 @@ func (rm *resourceManager) FilterSystemTags(res acktypes.AWSResource) { } var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags, tagKeyOrder := toACKTagsWithKeyOrder(existingTags) + resourceTags, tagKeyOrder := convertToOrderedACKTags(existingTags) ignoreSystemTags(resourceTags) - r.ko.Spec.Tags = fromACKTagsWithKeyOrder(resourceTags, tagKeyOrder) + r.ko.Spec.Tags = fromACKTags(resourceTags, tagKeyOrder) } // mirrorAWSTags ensures that AWS tags are included in the desired resource @@ -334,10 +334,10 @@ func mirrorAWSTags(a *resource, b *resource) { var existingDesiredTags []*svcapitypes.Tag existingDesiredTags = a.ko.Spec.Tags existingLatestTags = b.ko.Spec.Tags - desiredTags, desiredTagKeyOrder := toACKTagsWithKeyOrder(existingDesiredTags) - latestTags, _ := toACKTagsWithKeyOrder(existingLatestTags) + desiredTags, desiredTagKeyOrder := convertToOrderedACKTags(existingDesiredTags) + latestTags, _ := convertToOrderedACKTags(existingLatestTags) syncAWSTags(desiredTags, latestTags) - a.ko.Spec.Tags = fromACKTagsWithKeyOrder(desiredTags, desiredTagKeyOrder) + a.ko.Spec.Tags = fromACKTags(desiredTags, desiredTagKeyOrder) } // newResourceManager returns a new struct implementing diff --git a/pkg/resource/internet_gateway/tags.go b/pkg/resource/internet_gateway/tags.go index bc5970c5..0e61157f 100644 --- a/pkg/resource/internet_gateway/tags.go +++ b/pkg/resource/internet_gateway/tags.go @@ -30,69 +30,38 @@ var ( ACKSystemTags = []string{"services.k8s.aws/namespace", "services.k8s.aws/controller-version"} ) -// ToACKTags converts the tags parameter into 'acktags.Tags' shape. -// This method helps in creating the hub(acktags.Tags) for merging -// default controller tags with existing resource tags. -func ToACKTags(tags []*svcapitypes.Tag) acktags.Tags { - result := acktags.NewTags() - if tags == nil || len(tags) == 0 { - return result - } - - for _, t := range tags { - if t.Key != nil { - if t.Value == nil { - result[*t.Key] = "" - } else { - result[*t.Key] = *t.Value - } - } - } - - return result -} - -// toACKTagsWithKeyOrder converts the tags parameter into 'acktags.Tags' shape. +// convertToOrderedACKTags converts the tags parameter into 'acktags.Tags' shape. // This method helps in creating the hub(acktags.Tags) for merging // default controller tags with existing resource tags. It also returns a slice // of keys maintaining the original key Order when the tags are a list -func toACKTagsWithKeyOrder(tags []*svcapitypes.Tag) (acktags.Tags, []string) { +func convertToOrderedACKTags(tags []*svcapitypes.Tag) (acktags.Tags, []string) { result := acktags.NewTags() keyOrder := []string{} - if tags == nil || len(tags) == 0 { + + if len(tags) == 0 { return result, keyOrder } - for _, t := range tags { if t.Key != nil { keyOrder = append(keyOrder, *t.Key) + if t.Value != nil { + result[*t.Key] = *t.Value + } else { + result[*t.Key] = "" + } } } - result = ToACKTags(tags) return result, keyOrder } -// FromACKTags converts the tags parameter into []*svcapitypes.Tag shape. -// This method helps in setting the tags back inside AWSResource after merging -// default controller tags with existing resource tags. -func FromACKTags(tags acktags.Tags) []*svcapitypes.Tag { - result := []*svcapitypes.Tag{} - for k, v := range tags { - kCopy := k - vCopy := v - tag := svcapitypes.Tag{Key: &kCopy, Value: &vCopy} - result = append(result, &tag) - } - return result -} - -// fromACKTagsWithTagKeys converts the tags parameter into []*svcapitypes.Tag shape. +// fromACKTags converts the tags parameter into []*svcapitypes.Tag shape. // This method helps in setting the tags back inside AWSResource after merging // default controller tags with existing resource tags. When a list, // it maintains the order from original -func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { +func fromACKTags(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { result := []*svcapitypes.Tag{} + for _, k := range keyOrder { v, ok := tags[k] if ok { @@ -101,7 +70,11 @@ func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitype delete(tags, k) } } - result = append(result, FromACKTags(tags)...) + for k, v := range tags { + tag := svcapitypes.Tag{Key: &k, Value: &v} + result = append(result, &tag) + } + return result } diff --git a/pkg/resource/nat_gateway/delta.go b/pkg/resource/nat_gateway/delta.go index efe7226a..dc483e9e 100644 --- a/pkg/resource/nat_gateway/delta.go +++ b/pkg/resource/nat_gateway/delta.go @@ -70,7 +70,9 @@ func newResourceDelta( if !reflect.DeepEqual(a.ko.Spec.SubnetRef, b.ko.Spec.SubnetRef) { delta.Add("Spec.SubnetRef", a.ko.Spec.SubnetRef, b.ko.Spec.SubnetRef) } - if !ackcompare.MapStringStringEqual(ToACKTags(a.ko.Spec.Tags), ToACKTags(b.ko.Spec.Tags)) { + desiredACKTags, _ := convertToOrderedACKTags(a.ko.Spec.Tags) + latestACKTags, _ := convertToOrderedACKTags(b.ko.Spec.Tags) + if !ackcompare.MapStringStringEqual(desiredACKTags, latestACKTags) { delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags) } diff --git a/pkg/resource/nat_gateway/manager.go b/pkg/resource/nat_gateway/manager.go index 76cd85ab..e18337e1 100644 --- a/pkg/resource/nat_gateway/manager.go +++ b/pkg/resource/nat_gateway/manager.go @@ -299,9 +299,9 @@ func (rm *resourceManager) EnsureTags( defaultTags := ackrt.GetDefaultTags(&rm.cfg, r.ko, md) var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags := ToACKTags(existingTags) + resourceTags, keyOrder := convertToOrderedACKTags(existingTags) tags := acktags.Merge(resourceTags, defaultTags) - r.ko.Spec.Tags = FromACKTags(tags) + r.ko.Spec.Tags = fromACKTags(tags, keyOrder) return nil } @@ -318,9 +318,9 @@ func (rm *resourceManager) FilterSystemTags(res acktypes.AWSResource) { } var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags, tagKeyOrder := toACKTagsWithKeyOrder(existingTags) + resourceTags, tagKeyOrder := convertToOrderedACKTags(existingTags) ignoreSystemTags(resourceTags) - r.ko.Spec.Tags = fromACKTagsWithKeyOrder(resourceTags, tagKeyOrder) + r.ko.Spec.Tags = fromACKTags(resourceTags, tagKeyOrder) } // mirrorAWSTags ensures that AWS tags are included in the desired resource @@ -342,10 +342,10 @@ func mirrorAWSTags(a *resource, b *resource) { var existingDesiredTags []*svcapitypes.Tag existingDesiredTags = a.ko.Spec.Tags existingLatestTags = b.ko.Spec.Tags - desiredTags, desiredTagKeyOrder := toACKTagsWithKeyOrder(existingDesiredTags) - latestTags, _ := toACKTagsWithKeyOrder(existingLatestTags) + desiredTags, desiredTagKeyOrder := convertToOrderedACKTags(existingDesiredTags) + latestTags, _ := convertToOrderedACKTags(existingLatestTags) syncAWSTags(desiredTags, latestTags) - a.ko.Spec.Tags = fromACKTagsWithKeyOrder(desiredTags, desiredTagKeyOrder) + a.ko.Spec.Tags = fromACKTags(desiredTags, desiredTagKeyOrder) } // newResourceManager returns a new struct implementing diff --git a/pkg/resource/nat_gateway/tags.go b/pkg/resource/nat_gateway/tags.go index 86d8db7f..cabf544d 100644 --- a/pkg/resource/nat_gateway/tags.go +++ b/pkg/resource/nat_gateway/tags.go @@ -30,69 +30,38 @@ var ( ACKSystemTags = []string{"services.k8s.aws/namespace", "services.k8s.aws/controller-version"} ) -// ToACKTags converts the tags parameter into 'acktags.Tags' shape. -// This method helps in creating the hub(acktags.Tags) for merging -// default controller tags with existing resource tags. -func ToACKTags(tags []*svcapitypes.Tag) acktags.Tags { - result := acktags.NewTags() - if tags == nil || len(tags) == 0 { - return result - } - - for _, t := range tags { - if t.Key != nil { - if t.Value == nil { - result[*t.Key] = "" - } else { - result[*t.Key] = *t.Value - } - } - } - - return result -} - -// toACKTagsWithKeyOrder converts the tags parameter into 'acktags.Tags' shape. +// convertToOrderedACKTags converts the tags parameter into 'acktags.Tags' shape. // This method helps in creating the hub(acktags.Tags) for merging // default controller tags with existing resource tags. It also returns a slice // of keys maintaining the original key Order when the tags are a list -func toACKTagsWithKeyOrder(tags []*svcapitypes.Tag) (acktags.Tags, []string) { +func convertToOrderedACKTags(tags []*svcapitypes.Tag) (acktags.Tags, []string) { result := acktags.NewTags() keyOrder := []string{} - if tags == nil || len(tags) == 0 { + + if len(tags) == 0 { return result, keyOrder } - for _, t := range tags { if t.Key != nil { keyOrder = append(keyOrder, *t.Key) + if t.Value != nil { + result[*t.Key] = *t.Value + } else { + result[*t.Key] = "" + } } } - result = ToACKTags(tags) return result, keyOrder } -// FromACKTags converts the tags parameter into []*svcapitypes.Tag shape. -// This method helps in setting the tags back inside AWSResource after merging -// default controller tags with existing resource tags. -func FromACKTags(tags acktags.Tags) []*svcapitypes.Tag { - result := []*svcapitypes.Tag{} - for k, v := range tags { - kCopy := k - vCopy := v - tag := svcapitypes.Tag{Key: &kCopy, Value: &vCopy} - result = append(result, &tag) - } - return result -} - -// fromACKTagsWithTagKeys converts the tags parameter into []*svcapitypes.Tag shape. +// fromACKTags converts the tags parameter into []*svcapitypes.Tag shape. // This method helps in setting the tags back inside AWSResource after merging // default controller tags with existing resource tags. When a list, // it maintains the order from original -func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { +func fromACKTags(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { result := []*svcapitypes.Tag{} + for _, k := range keyOrder { v, ok := tags[k] if ok { @@ -101,7 +70,11 @@ func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitype delete(tags, k) } } - result = append(result, FromACKTags(tags)...) + for k, v := range tags { + tag := svcapitypes.Tag{Key: &k, Value: &v} + result = append(result, &tag) + } + return result } diff --git a/pkg/resource/network_acl/delta.go b/pkg/resource/network_acl/delta.go index f28c6c80..1c87edfc 100644 --- a/pkg/resource/network_acl/delta.go +++ b/pkg/resource/network_acl/delta.go @@ -42,6 +42,7 @@ func newResourceDelta( delta.Add("", a, b) return delta } + customPreCompare(delta, a, b) if len(a.ko.Spec.Associations) != len(b.ko.Spec.Associations) { delta.Add("Spec.Associations", a.ko.Spec.Associations, b.ko.Spec.Associations) @@ -57,7 +58,9 @@ func newResourceDelta( delta.Add("Spec.Entries", a.ko.Spec.Entries, b.ko.Spec.Entries) } } - if !ackcompare.MapStringStringEqual(ToACKTags(a.ko.Spec.Tags), ToACKTags(b.ko.Spec.Tags)) { + desiredACKTags, _ := convertToOrderedACKTags(a.ko.Spec.Tags) + latestACKTags, _ := convertToOrderedACKTags(b.ko.Spec.Tags) + if !ackcompare.MapStringStringEqual(desiredACKTags, latestACKTags) { delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags) } if ackcompare.HasNilDifference(a.ko.Spec.VPCID, b.ko.Spec.VPCID) { diff --git a/pkg/resource/network_acl/hooks.go b/pkg/resource/network_acl/hooks.go index 64fc1375..cf279b81 100644 --- a/pkg/resource/network_acl/hooks.go +++ b/pkg/resource/network_acl/hooks.go @@ -88,10 +88,11 @@ func (rm *resourceManager) requiredFieldsMissingForCreateNetworkAcl( func (rm *resourceManager) createEntries( ctx context.Context, - r *resource, + desired *resource, ) error { - if r.ko.Spec.Entries != nil { - if err := rm.syncEntries(ctx, r, nil); err != nil { + filteredEntries := filterDefaultRules(desired.ko.Spec.Entries) + for _, entry := range filteredEntries { + if err := rm.createEntry(ctx, desired, *entry); err != nil { return err } } @@ -324,18 +325,6 @@ func (rm *resourceManager) syncEntries( toDelete := []*svcapitypes.NetworkACLEntry{} toUpdate := []*svcapitypes.NetworkACLEntry{} - // Filter out AWS default rules (rule #32767) from desired entries to ensure - // they don't interfere with GitOps workflows. These rules are automatically - // managed by AWS and should not be included in the desired state. - filteredDesiredEntries := []*svcapitypes.NetworkACLEntry{} - for _, entry := range desired.ko.Spec.Entries { - if entry.RuleNumber != nil && *entry.RuleNumber == int64(DefaultRuleNumber) { - continue - } - filteredDesiredEntries = append(filteredDesiredEntries, entry) - } - desired.ko.Spec.Entries = filteredDesiredEntries - // Check for duplicate rule numbers within the same direction (egress/ingress) uniqEntries := lo.UniqBy(desired.ko.Spec.Entries, func(entry *svcapitypes.NetworkACLEntry) string { return strconv.FormatBool(*entry.Egress) + strconv.Itoa(int(*entry.RuleNumber)) @@ -351,20 +340,9 @@ func (rm *resourceManager) syncEntries( toAdd = append(toAdd, desiredEntry) } } - if latest != nil { - // Filter out AWS default rules from latest entries before comparison - // to ensure consistent state management between desired and actual - filteredLatestEntries := []*svcapitypes.NetworkACLEntry{} - for _, entry := range latest.ko.Spec.Entries { - if entry.RuleNumber != nil && *entry.RuleNumber == int64(DefaultRuleNumber) { - continue - } - filteredLatestEntries = append(filteredLatestEntries, entry) - } - // Identify entries that need to be deleted (exist in latest but not in desired) - for _, latestEntry := range filteredLatestEntries { + for _, latestEntry := range latest.ko.Spec.Entries { if !containsEntry(desired.ko.Spec.Entries, latestEntry) { // entry is in latest resource, but not in desired resource; therefore, delete toDelete = append(toDelete, latestEntry) @@ -611,3 +589,26 @@ func updateTagSpecificationsInCreateRequest(r *resource, input.TagSpecifications = []svcsdktypes.TagSpecification{desiredTagSpecs} } } + +// Filter out AWS default rules from latest entries before comparison +// to ensure consistent state management between desired and actual +func customPreCompare( + delta *ackcompare.Delta, + a *resource, + b *resource, +) { + a.ko.Spec.Entries = filterDefaultRules(a.ko.Spec.Entries) + b.ko.Spec.Entries = filterDefaultRules(b.ko.Spec.Entries) +} + +// filter default rules 32767 +func filterDefaultRules(entries []*svcapitypes.NetworkACLEntry) []*svcapitypes.NetworkACLEntry { + filteredEntries := []*svcapitypes.NetworkACLEntry{} + for _, entry := range entries { + if entry.RuleNumber != nil && *entry.RuleNumber == int64(DefaultRuleNumber) { + continue + } + filteredEntries = append(filteredEntries, entry) + } + return filteredEntries +} diff --git a/pkg/resource/network_acl/manager.go b/pkg/resource/network_acl/manager.go index 422a6ed3..a5d07680 100644 --- a/pkg/resource/network_acl/manager.go +++ b/pkg/resource/network_acl/manager.go @@ -291,9 +291,9 @@ func (rm *resourceManager) EnsureTags( defaultTags := ackrt.GetDefaultTags(&rm.cfg, r.ko, md) var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags := ToACKTags(existingTags) + resourceTags, keyOrder := convertToOrderedACKTags(existingTags) tags := acktags.Merge(resourceTags, defaultTags) - r.ko.Spec.Tags = FromACKTags(tags) + r.ko.Spec.Tags = fromACKTags(tags, keyOrder) return nil } @@ -310,9 +310,9 @@ func (rm *resourceManager) FilterSystemTags(res acktypes.AWSResource) { } var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags, tagKeyOrder := toACKTagsWithKeyOrder(existingTags) + resourceTags, tagKeyOrder := convertToOrderedACKTags(existingTags) ignoreSystemTags(resourceTags) - r.ko.Spec.Tags = fromACKTagsWithKeyOrder(resourceTags, tagKeyOrder) + r.ko.Spec.Tags = fromACKTags(resourceTags, tagKeyOrder) } // mirrorAWSTags ensures that AWS tags are included in the desired resource @@ -334,10 +334,10 @@ func mirrorAWSTags(a *resource, b *resource) { var existingDesiredTags []*svcapitypes.Tag existingDesiredTags = a.ko.Spec.Tags existingLatestTags = b.ko.Spec.Tags - desiredTags, desiredTagKeyOrder := toACKTagsWithKeyOrder(existingDesiredTags) - latestTags, _ := toACKTagsWithKeyOrder(existingLatestTags) + desiredTags, desiredTagKeyOrder := convertToOrderedACKTags(existingDesiredTags) + latestTags, _ := convertToOrderedACKTags(existingLatestTags) syncAWSTags(desiredTags, latestTags) - a.ko.Spec.Tags = fromACKTagsWithKeyOrder(desiredTags, desiredTagKeyOrder) + a.ko.Spec.Tags = fromACKTags(desiredTags, desiredTagKeyOrder) } // newResourceManager returns a new struct implementing diff --git a/pkg/resource/network_acl/tags.go b/pkg/resource/network_acl/tags.go index ea5ff3aa..43e18125 100644 --- a/pkg/resource/network_acl/tags.go +++ b/pkg/resource/network_acl/tags.go @@ -30,69 +30,38 @@ var ( ACKSystemTags = []string{"services.k8s.aws/namespace", "services.k8s.aws/controller-version"} ) -// ToACKTags converts the tags parameter into 'acktags.Tags' shape. -// This method helps in creating the hub(acktags.Tags) for merging -// default controller tags with existing resource tags. -func ToACKTags(tags []*svcapitypes.Tag) acktags.Tags { - result := acktags.NewTags() - if tags == nil || len(tags) == 0 { - return result - } - - for _, t := range tags { - if t.Key != nil { - if t.Value == nil { - result[*t.Key] = "" - } else { - result[*t.Key] = *t.Value - } - } - } - - return result -} - -// toACKTagsWithKeyOrder converts the tags parameter into 'acktags.Tags' shape. +// convertToOrderedACKTags converts the tags parameter into 'acktags.Tags' shape. // This method helps in creating the hub(acktags.Tags) for merging // default controller tags with existing resource tags. It also returns a slice // of keys maintaining the original key Order when the tags are a list -func toACKTagsWithKeyOrder(tags []*svcapitypes.Tag) (acktags.Tags, []string) { +func convertToOrderedACKTags(tags []*svcapitypes.Tag) (acktags.Tags, []string) { result := acktags.NewTags() keyOrder := []string{} - if tags == nil || len(tags) == 0 { + + if len(tags) == 0 { return result, keyOrder } - for _, t := range tags { if t.Key != nil { keyOrder = append(keyOrder, *t.Key) + if t.Value != nil { + result[*t.Key] = *t.Value + } else { + result[*t.Key] = "" + } } } - result = ToACKTags(tags) return result, keyOrder } -// FromACKTags converts the tags parameter into []*svcapitypes.Tag shape. -// This method helps in setting the tags back inside AWSResource after merging -// default controller tags with existing resource tags. -func FromACKTags(tags acktags.Tags) []*svcapitypes.Tag { - result := []*svcapitypes.Tag{} - for k, v := range tags { - kCopy := k - vCopy := v - tag := svcapitypes.Tag{Key: &kCopy, Value: &vCopy} - result = append(result, &tag) - } - return result -} - -// fromACKTagsWithTagKeys converts the tags parameter into []*svcapitypes.Tag shape. +// fromACKTags converts the tags parameter into []*svcapitypes.Tag shape. // This method helps in setting the tags back inside AWSResource after merging // default controller tags with existing resource tags. When a list, // it maintains the order from original -func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { +func fromACKTags(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { result := []*svcapitypes.Tag{} + for _, k := range keyOrder { v, ok := tags[k] if ok { @@ -101,7 +70,11 @@ func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitype delete(tags, k) } } - result = append(result, FromACKTags(tags)...) + for k, v := range tags { + tag := svcapitypes.Tag{Key: &k, Value: &v} + result = append(result, &tag) + } + return result } diff --git a/pkg/resource/route_table/delta.go b/pkg/resource/route_table/delta.go index ed6ae41e..98ceb86f 100644 --- a/pkg/resource/route_table/delta.go +++ b/pkg/resource/route_table/delta.go @@ -44,7 +44,9 @@ func newResourceDelta( } customPreCompare(delta, a, b) - if !ackcompare.MapStringStringEqual(ToACKTags(a.ko.Spec.Tags), ToACKTags(b.ko.Spec.Tags)) { + desiredACKTags, _ := convertToOrderedACKTags(a.ko.Spec.Tags) + latestACKTags, _ := convertToOrderedACKTags(b.ko.Spec.Tags) + if !ackcompare.MapStringStringEqual(desiredACKTags, latestACKTags) { delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags) } if ackcompare.HasNilDifference(a.ko.Spec.VPCID, b.ko.Spec.VPCID) { diff --git a/pkg/resource/route_table/manager.go b/pkg/resource/route_table/manager.go index f9e0b89c..8771860c 100644 --- a/pkg/resource/route_table/manager.go +++ b/pkg/resource/route_table/manager.go @@ -291,9 +291,9 @@ func (rm *resourceManager) EnsureTags( defaultTags := ackrt.GetDefaultTags(&rm.cfg, r.ko, md) var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags := ToACKTags(existingTags) + resourceTags, keyOrder := convertToOrderedACKTags(existingTags) tags := acktags.Merge(resourceTags, defaultTags) - r.ko.Spec.Tags = FromACKTags(tags) + r.ko.Spec.Tags = fromACKTags(tags, keyOrder) return nil } @@ -310,9 +310,9 @@ func (rm *resourceManager) FilterSystemTags(res acktypes.AWSResource) { } var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags, tagKeyOrder := toACKTagsWithKeyOrder(existingTags) + resourceTags, tagKeyOrder := convertToOrderedACKTags(existingTags) ignoreSystemTags(resourceTags) - r.ko.Spec.Tags = fromACKTagsWithKeyOrder(resourceTags, tagKeyOrder) + r.ko.Spec.Tags = fromACKTags(resourceTags, tagKeyOrder) } // mirrorAWSTags ensures that AWS tags are included in the desired resource @@ -334,10 +334,10 @@ func mirrorAWSTags(a *resource, b *resource) { var existingDesiredTags []*svcapitypes.Tag existingDesiredTags = a.ko.Spec.Tags existingLatestTags = b.ko.Spec.Tags - desiredTags, desiredTagKeyOrder := toACKTagsWithKeyOrder(existingDesiredTags) - latestTags, _ := toACKTagsWithKeyOrder(existingLatestTags) + desiredTags, desiredTagKeyOrder := convertToOrderedACKTags(existingDesiredTags) + latestTags, _ := convertToOrderedACKTags(existingLatestTags) syncAWSTags(desiredTags, latestTags) - a.ko.Spec.Tags = fromACKTagsWithKeyOrder(desiredTags, desiredTagKeyOrder) + a.ko.Spec.Tags = fromACKTags(desiredTags, desiredTagKeyOrder) } // newResourceManager returns a new struct implementing diff --git a/pkg/resource/route_table/tags.go b/pkg/resource/route_table/tags.go index 8f88683b..96095dfe 100644 --- a/pkg/resource/route_table/tags.go +++ b/pkg/resource/route_table/tags.go @@ -30,69 +30,38 @@ var ( ACKSystemTags = []string{"services.k8s.aws/namespace", "services.k8s.aws/controller-version"} ) -// ToACKTags converts the tags parameter into 'acktags.Tags' shape. -// This method helps in creating the hub(acktags.Tags) for merging -// default controller tags with existing resource tags. -func ToACKTags(tags []*svcapitypes.Tag) acktags.Tags { - result := acktags.NewTags() - if tags == nil || len(tags) == 0 { - return result - } - - for _, t := range tags { - if t.Key != nil { - if t.Value == nil { - result[*t.Key] = "" - } else { - result[*t.Key] = *t.Value - } - } - } - - return result -} - -// toACKTagsWithKeyOrder converts the tags parameter into 'acktags.Tags' shape. +// convertToOrderedACKTags converts the tags parameter into 'acktags.Tags' shape. // This method helps in creating the hub(acktags.Tags) for merging // default controller tags with existing resource tags. It also returns a slice // of keys maintaining the original key Order when the tags are a list -func toACKTagsWithKeyOrder(tags []*svcapitypes.Tag) (acktags.Tags, []string) { +func convertToOrderedACKTags(tags []*svcapitypes.Tag) (acktags.Tags, []string) { result := acktags.NewTags() keyOrder := []string{} - if tags == nil || len(tags) == 0 { + + if len(tags) == 0 { return result, keyOrder } - for _, t := range tags { if t.Key != nil { keyOrder = append(keyOrder, *t.Key) + if t.Value != nil { + result[*t.Key] = *t.Value + } else { + result[*t.Key] = "" + } } } - result = ToACKTags(tags) return result, keyOrder } -// FromACKTags converts the tags parameter into []*svcapitypes.Tag shape. -// This method helps in setting the tags back inside AWSResource after merging -// default controller tags with existing resource tags. -func FromACKTags(tags acktags.Tags) []*svcapitypes.Tag { - result := []*svcapitypes.Tag{} - for k, v := range tags { - kCopy := k - vCopy := v - tag := svcapitypes.Tag{Key: &kCopy, Value: &vCopy} - result = append(result, &tag) - } - return result -} - -// fromACKTagsWithTagKeys converts the tags parameter into []*svcapitypes.Tag shape. +// fromACKTags converts the tags parameter into []*svcapitypes.Tag shape. // This method helps in setting the tags back inside AWSResource after merging // default controller tags with existing resource tags. When a list, // it maintains the order from original -func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { +func fromACKTags(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { result := []*svcapitypes.Tag{} + for _, k := range keyOrder { v, ok := tags[k] if ok { @@ -101,7 +70,11 @@ func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitype delete(tags, k) } } - result = append(result, FromACKTags(tags)...) + for k, v := range tags { + tag := svcapitypes.Tag{Key: &k, Value: &v} + result = append(result, &tag) + } + return result } diff --git a/pkg/resource/security_group/delta.go b/pkg/resource/security_group/delta.go index b0892b94..bd180886 100644 --- a/pkg/resource/security_group/delta.go +++ b/pkg/resource/security_group/delta.go @@ -71,7 +71,9 @@ func newResourceDelta( delta.Add("Spec.Name", a.ko.Spec.Name, b.ko.Spec.Name) } } - if !ackcompare.MapStringStringEqual(ToACKTags(a.ko.Spec.Tags), ToACKTags(b.ko.Spec.Tags)) { + desiredACKTags, _ := convertToOrderedACKTags(a.ko.Spec.Tags) + latestACKTags, _ := convertToOrderedACKTags(b.ko.Spec.Tags) + if !ackcompare.MapStringStringEqual(desiredACKTags, latestACKTags) { delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags) } if ackcompare.HasNilDifference(a.ko.Spec.VPCID, b.ko.Spec.VPCID) { diff --git a/pkg/resource/security_group/manager.go b/pkg/resource/security_group/manager.go index 38d9486f..a1bb1f56 100644 --- a/pkg/resource/security_group/manager.go +++ b/pkg/resource/security_group/manager.go @@ -291,9 +291,9 @@ func (rm *resourceManager) EnsureTags( defaultTags := ackrt.GetDefaultTags(&rm.cfg, r.ko, md) var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags := ToACKTags(existingTags) + resourceTags, keyOrder := convertToOrderedACKTags(existingTags) tags := acktags.Merge(resourceTags, defaultTags) - r.ko.Spec.Tags = FromACKTags(tags) + r.ko.Spec.Tags = fromACKTags(tags, keyOrder) return nil } @@ -310,9 +310,9 @@ func (rm *resourceManager) FilterSystemTags(res acktypes.AWSResource) { } var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags, tagKeyOrder := toACKTagsWithKeyOrder(existingTags) + resourceTags, tagKeyOrder := convertToOrderedACKTags(existingTags) ignoreSystemTags(resourceTags) - r.ko.Spec.Tags = fromACKTagsWithKeyOrder(resourceTags, tagKeyOrder) + r.ko.Spec.Tags = fromACKTags(resourceTags, tagKeyOrder) } // mirrorAWSTags ensures that AWS tags are included in the desired resource @@ -334,10 +334,10 @@ func mirrorAWSTags(a *resource, b *resource) { var existingDesiredTags []*svcapitypes.Tag existingDesiredTags = a.ko.Spec.Tags existingLatestTags = b.ko.Spec.Tags - desiredTags, desiredTagKeyOrder := toACKTagsWithKeyOrder(existingDesiredTags) - latestTags, _ := toACKTagsWithKeyOrder(existingLatestTags) + desiredTags, desiredTagKeyOrder := convertToOrderedACKTags(existingDesiredTags) + latestTags, _ := convertToOrderedACKTags(existingLatestTags) syncAWSTags(desiredTags, latestTags) - a.ko.Spec.Tags = fromACKTagsWithKeyOrder(desiredTags, desiredTagKeyOrder) + a.ko.Spec.Tags = fromACKTags(desiredTags, desiredTagKeyOrder) } // newResourceManager returns a new struct implementing diff --git a/pkg/resource/security_group/tags.go b/pkg/resource/security_group/tags.go index dec1d52c..d0da2103 100644 --- a/pkg/resource/security_group/tags.go +++ b/pkg/resource/security_group/tags.go @@ -30,69 +30,38 @@ var ( ACKSystemTags = []string{"services.k8s.aws/namespace", "services.k8s.aws/controller-version"} ) -// ToACKTags converts the tags parameter into 'acktags.Tags' shape. -// This method helps in creating the hub(acktags.Tags) for merging -// default controller tags with existing resource tags. -func ToACKTags(tags []*svcapitypes.Tag) acktags.Tags { - result := acktags.NewTags() - if tags == nil || len(tags) == 0 { - return result - } - - for _, t := range tags { - if t.Key != nil { - if t.Value == nil { - result[*t.Key] = "" - } else { - result[*t.Key] = *t.Value - } - } - } - - return result -} - -// toACKTagsWithKeyOrder converts the tags parameter into 'acktags.Tags' shape. +// convertToOrderedACKTags converts the tags parameter into 'acktags.Tags' shape. // This method helps in creating the hub(acktags.Tags) for merging // default controller tags with existing resource tags. It also returns a slice // of keys maintaining the original key Order when the tags are a list -func toACKTagsWithKeyOrder(tags []*svcapitypes.Tag) (acktags.Tags, []string) { +func convertToOrderedACKTags(tags []*svcapitypes.Tag) (acktags.Tags, []string) { result := acktags.NewTags() keyOrder := []string{} - if tags == nil || len(tags) == 0 { + + if len(tags) == 0 { return result, keyOrder } - for _, t := range tags { if t.Key != nil { keyOrder = append(keyOrder, *t.Key) + if t.Value != nil { + result[*t.Key] = *t.Value + } else { + result[*t.Key] = "" + } } } - result = ToACKTags(tags) return result, keyOrder } -// FromACKTags converts the tags parameter into []*svcapitypes.Tag shape. -// This method helps in setting the tags back inside AWSResource after merging -// default controller tags with existing resource tags. -func FromACKTags(tags acktags.Tags) []*svcapitypes.Tag { - result := []*svcapitypes.Tag{} - for k, v := range tags { - kCopy := k - vCopy := v - tag := svcapitypes.Tag{Key: &kCopy, Value: &vCopy} - result = append(result, &tag) - } - return result -} - -// fromACKTagsWithTagKeys converts the tags parameter into []*svcapitypes.Tag shape. +// fromACKTags converts the tags parameter into []*svcapitypes.Tag shape. // This method helps in setting the tags back inside AWSResource after merging // default controller tags with existing resource tags. When a list, // it maintains the order from original -func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { +func fromACKTags(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { result := []*svcapitypes.Tag{} + for _, k := range keyOrder { v, ok := tags[k] if ok { @@ -101,7 +70,11 @@ func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitype delete(tags, k) } } - result = append(result, FromACKTags(tags)...) + for k, v := range tags { + tag := svcapitypes.Tag{Key: &k, Value: &v} + result = append(result, &tag) + } + return result } diff --git a/pkg/resource/subnet/delta.go b/pkg/resource/subnet/delta.go index 232c4a7b..4df50fe6 100644 --- a/pkg/resource/subnet/delta.go +++ b/pkg/resource/subnet/delta.go @@ -144,7 +144,9 @@ func newResourceDelta( delta.Add("Spec.RouteTables", a.ko.Spec.RouteTables, b.ko.Spec.RouteTables) } } - if !ackcompare.MapStringStringEqual(ToACKTags(a.ko.Spec.Tags), ToACKTags(b.ko.Spec.Tags)) { + desiredACKTags, _ := convertToOrderedACKTags(a.ko.Spec.Tags) + latestACKTags, _ := convertToOrderedACKTags(b.ko.Spec.Tags) + if !ackcompare.MapStringStringEqual(desiredACKTags, latestACKTags) { delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags) } if ackcompare.HasNilDifference(a.ko.Spec.VPCID, b.ko.Spec.VPCID) { diff --git a/pkg/resource/subnet/manager.go b/pkg/resource/subnet/manager.go index 81d5890e..2d9ae22f 100644 --- a/pkg/resource/subnet/manager.go +++ b/pkg/resource/subnet/manager.go @@ -291,9 +291,9 @@ func (rm *resourceManager) EnsureTags( defaultTags := ackrt.GetDefaultTags(&rm.cfg, r.ko, md) var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags := ToACKTags(existingTags) + resourceTags, keyOrder := convertToOrderedACKTags(existingTags) tags := acktags.Merge(resourceTags, defaultTags) - r.ko.Spec.Tags = FromACKTags(tags) + r.ko.Spec.Tags = fromACKTags(tags, keyOrder) return nil } @@ -310,9 +310,9 @@ func (rm *resourceManager) FilterSystemTags(res acktypes.AWSResource) { } var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags, tagKeyOrder := toACKTagsWithKeyOrder(existingTags) + resourceTags, tagKeyOrder := convertToOrderedACKTags(existingTags) ignoreSystemTags(resourceTags) - r.ko.Spec.Tags = fromACKTagsWithKeyOrder(resourceTags, tagKeyOrder) + r.ko.Spec.Tags = fromACKTags(resourceTags, tagKeyOrder) } // mirrorAWSTags ensures that AWS tags are included in the desired resource @@ -334,10 +334,10 @@ func mirrorAWSTags(a *resource, b *resource) { var existingDesiredTags []*svcapitypes.Tag existingDesiredTags = a.ko.Spec.Tags existingLatestTags = b.ko.Spec.Tags - desiredTags, desiredTagKeyOrder := toACKTagsWithKeyOrder(existingDesiredTags) - latestTags, _ := toACKTagsWithKeyOrder(existingLatestTags) + desiredTags, desiredTagKeyOrder := convertToOrderedACKTags(existingDesiredTags) + latestTags, _ := convertToOrderedACKTags(existingLatestTags) syncAWSTags(desiredTags, latestTags) - a.ko.Spec.Tags = fromACKTagsWithKeyOrder(desiredTags, desiredTagKeyOrder) + a.ko.Spec.Tags = fromACKTags(desiredTags, desiredTagKeyOrder) } // newResourceManager returns a new struct implementing diff --git a/pkg/resource/subnet/tags.go b/pkg/resource/subnet/tags.go index 3d44f151..c544bb3b 100644 --- a/pkg/resource/subnet/tags.go +++ b/pkg/resource/subnet/tags.go @@ -30,69 +30,38 @@ var ( ACKSystemTags = []string{"services.k8s.aws/namespace", "services.k8s.aws/controller-version"} ) -// ToACKTags converts the tags parameter into 'acktags.Tags' shape. -// This method helps in creating the hub(acktags.Tags) for merging -// default controller tags with existing resource tags. -func ToACKTags(tags []*svcapitypes.Tag) acktags.Tags { - result := acktags.NewTags() - if tags == nil || len(tags) == 0 { - return result - } - - for _, t := range tags { - if t.Key != nil { - if t.Value == nil { - result[*t.Key] = "" - } else { - result[*t.Key] = *t.Value - } - } - } - - return result -} - -// toACKTagsWithKeyOrder converts the tags parameter into 'acktags.Tags' shape. +// convertToOrderedACKTags converts the tags parameter into 'acktags.Tags' shape. // This method helps in creating the hub(acktags.Tags) for merging // default controller tags with existing resource tags. It also returns a slice // of keys maintaining the original key Order when the tags are a list -func toACKTagsWithKeyOrder(tags []*svcapitypes.Tag) (acktags.Tags, []string) { +func convertToOrderedACKTags(tags []*svcapitypes.Tag) (acktags.Tags, []string) { result := acktags.NewTags() keyOrder := []string{} - if tags == nil || len(tags) == 0 { + + if len(tags) == 0 { return result, keyOrder } - for _, t := range tags { if t.Key != nil { keyOrder = append(keyOrder, *t.Key) + if t.Value != nil { + result[*t.Key] = *t.Value + } else { + result[*t.Key] = "" + } } } - result = ToACKTags(tags) return result, keyOrder } -// FromACKTags converts the tags parameter into []*svcapitypes.Tag shape. -// This method helps in setting the tags back inside AWSResource after merging -// default controller tags with existing resource tags. -func FromACKTags(tags acktags.Tags) []*svcapitypes.Tag { - result := []*svcapitypes.Tag{} - for k, v := range tags { - kCopy := k - vCopy := v - tag := svcapitypes.Tag{Key: &kCopy, Value: &vCopy} - result = append(result, &tag) - } - return result -} - -// fromACKTagsWithTagKeys converts the tags parameter into []*svcapitypes.Tag shape. +// fromACKTags converts the tags parameter into []*svcapitypes.Tag shape. // This method helps in setting the tags back inside AWSResource after merging // default controller tags with existing resource tags. When a list, // it maintains the order from original -func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { +func fromACKTags(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { result := []*svcapitypes.Tag{} + for _, k := range keyOrder { v, ok := tags[k] if ok { @@ -101,7 +70,11 @@ func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitype delete(tags, k) } } - result = append(result, FromACKTags(tags)...) + for k, v := range tags { + tag := svcapitypes.Tag{Key: &k, Value: &v} + result = append(result, &tag) + } + return result } diff --git a/pkg/resource/transit_gateway/delta.go b/pkg/resource/transit_gateway/delta.go index 32b3ec50..6878dee3 100644 --- a/pkg/resource/transit_gateway/delta.go +++ b/pkg/resource/transit_gateway/delta.go @@ -110,7 +110,9 @@ func newResourceDelta( } } } - if !ackcompare.MapStringStringEqual(ToACKTags(a.ko.Spec.Tags), ToACKTags(b.ko.Spec.Tags)) { + desiredACKTags, _ := convertToOrderedACKTags(a.ko.Spec.Tags) + latestACKTags, _ := convertToOrderedACKTags(b.ko.Spec.Tags) + if !ackcompare.MapStringStringEqual(desiredACKTags, latestACKTags) { delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags) } diff --git a/pkg/resource/transit_gateway/manager.go b/pkg/resource/transit_gateway/manager.go index 9aea950e..44b7b164 100644 --- a/pkg/resource/transit_gateway/manager.go +++ b/pkg/resource/transit_gateway/manager.go @@ -291,9 +291,9 @@ func (rm *resourceManager) EnsureTags( defaultTags := ackrt.GetDefaultTags(&rm.cfg, r.ko, md) var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags := ToACKTags(existingTags) + resourceTags, keyOrder := convertToOrderedACKTags(existingTags) tags := acktags.Merge(resourceTags, defaultTags) - r.ko.Spec.Tags = FromACKTags(tags) + r.ko.Spec.Tags = fromACKTags(tags, keyOrder) return nil } @@ -310,9 +310,9 @@ func (rm *resourceManager) FilterSystemTags(res acktypes.AWSResource) { } var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags, tagKeyOrder := toACKTagsWithKeyOrder(existingTags) + resourceTags, tagKeyOrder := convertToOrderedACKTags(existingTags) ignoreSystemTags(resourceTags) - r.ko.Spec.Tags = fromACKTagsWithKeyOrder(resourceTags, tagKeyOrder) + r.ko.Spec.Tags = fromACKTags(resourceTags, tagKeyOrder) } // mirrorAWSTags ensures that AWS tags are included in the desired resource @@ -334,10 +334,10 @@ func mirrorAWSTags(a *resource, b *resource) { var existingDesiredTags []*svcapitypes.Tag existingDesiredTags = a.ko.Spec.Tags existingLatestTags = b.ko.Spec.Tags - desiredTags, desiredTagKeyOrder := toACKTagsWithKeyOrder(existingDesiredTags) - latestTags, _ := toACKTagsWithKeyOrder(existingLatestTags) + desiredTags, desiredTagKeyOrder := convertToOrderedACKTags(existingDesiredTags) + latestTags, _ := convertToOrderedACKTags(existingLatestTags) syncAWSTags(desiredTags, latestTags) - a.ko.Spec.Tags = fromACKTagsWithKeyOrder(desiredTags, desiredTagKeyOrder) + a.ko.Spec.Tags = fromACKTags(desiredTags, desiredTagKeyOrder) } // newResourceManager returns a new struct implementing diff --git a/pkg/resource/transit_gateway/tags.go b/pkg/resource/transit_gateway/tags.go index d8e49ad6..13781d02 100644 --- a/pkg/resource/transit_gateway/tags.go +++ b/pkg/resource/transit_gateway/tags.go @@ -30,69 +30,38 @@ var ( ACKSystemTags = []string{"services.k8s.aws/namespace", "services.k8s.aws/controller-version"} ) -// ToACKTags converts the tags parameter into 'acktags.Tags' shape. -// This method helps in creating the hub(acktags.Tags) for merging -// default controller tags with existing resource tags. -func ToACKTags(tags []*svcapitypes.Tag) acktags.Tags { - result := acktags.NewTags() - if tags == nil || len(tags) == 0 { - return result - } - - for _, t := range tags { - if t.Key != nil { - if t.Value == nil { - result[*t.Key] = "" - } else { - result[*t.Key] = *t.Value - } - } - } - - return result -} - -// toACKTagsWithKeyOrder converts the tags parameter into 'acktags.Tags' shape. +// convertToOrderedACKTags converts the tags parameter into 'acktags.Tags' shape. // This method helps in creating the hub(acktags.Tags) for merging // default controller tags with existing resource tags. It also returns a slice // of keys maintaining the original key Order when the tags are a list -func toACKTagsWithKeyOrder(tags []*svcapitypes.Tag) (acktags.Tags, []string) { +func convertToOrderedACKTags(tags []*svcapitypes.Tag) (acktags.Tags, []string) { result := acktags.NewTags() keyOrder := []string{} - if tags == nil || len(tags) == 0 { + + if len(tags) == 0 { return result, keyOrder } - for _, t := range tags { if t.Key != nil { keyOrder = append(keyOrder, *t.Key) + if t.Value != nil { + result[*t.Key] = *t.Value + } else { + result[*t.Key] = "" + } } } - result = ToACKTags(tags) return result, keyOrder } -// FromACKTags converts the tags parameter into []*svcapitypes.Tag shape. -// This method helps in setting the tags back inside AWSResource after merging -// default controller tags with existing resource tags. -func FromACKTags(tags acktags.Tags) []*svcapitypes.Tag { - result := []*svcapitypes.Tag{} - for k, v := range tags { - kCopy := k - vCopy := v - tag := svcapitypes.Tag{Key: &kCopy, Value: &vCopy} - result = append(result, &tag) - } - return result -} - -// fromACKTagsWithTagKeys converts the tags parameter into []*svcapitypes.Tag shape. +// fromACKTags converts the tags parameter into []*svcapitypes.Tag shape. // This method helps in setting the tags back inside AWSResource after merging // default controller tags with existing resource tags. When a list, // it maintains the order from original -func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { +func fromACKTags(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { result := []*svcapitypes.Tag{} + for _, k := range keyOrder { v, ok := tags[k] if ok { @@ -101,7 +70,11 @@ func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitype delete(tags, k) } } - result = append(result, FromACKTags(tags)...) + for k, v := range tags { + tag := svcapitypes.Tag{Key: &k, Value: &v} + result = append(result, &tag) + } + return result } diff --git a/pkg/resource/vpc/delta.go b/pkg/resource/vpc/delta.go index 2b247bfd..518e5b84 100644 --- a/pkg/resource/vpc/delta.go +++ b/pkg/resource/vpc/delta.go @@ -135,7 +135,9 @@ func newResourceDelta( delta.Add("Spec.IPv6Pool", a.ko.Spec.IPv6Pool, b.ko.Spec.IPv6Pool) } } - if !ackcompare.MapStringStringEqual(ToACKTags(a.ko.Spec.Tags), ToACKTags(b.ko.Spec.Tags)) { + desiredACKTags, _ := convertToOrderedACKTags(a.ko.Spec.Tags) + latestACKTags, _ := convertToOrderedACKTags(b.ko.Spec.Tags) + if !ackcompare.MapStringStringEqual(desiredACKTags, latestACKTags) { delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags) } diff --git a/pkg/resource/vpc/manager.go b/pkg/resource/vpc/manager.go index 76e2edc1..8f4866d0 100644 --- a/pkg/resource/vpc/manager.go +++ b/pkg/resource/vpc/manager.go @@ -291,9 +291,9 @@ func (rm *resourceManager) EnsureTags( defaultTags := ackrt.GetDefaultTags(&rm.cfg, r.ko, md) var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags := ToACKTags(existingTags) + resourceTags, keyOrder := convertToOrderedACKTags(existingTags) tags := acktags.Merge(resourceTags, defaultTags) - r.ko.Spec.Tags = FromACKTags(tags) + r.ko.Spec.Tags = fromACKTags(tags, keyOrder) return nil } @@ -310,9 +310,9 @@ func (rm *resourceManager) FilterSystemTags(res acktypes.AWSResource) { } var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags, tagKeyOrder := toACKTagsWithKeyOrder(existingTags) + resourceTags, tagKeyOrder := convertToOrderedACKTags(existingTags) ignoreSystemTags(resourceTags) - r.ko.Spec.Tags = fromACKTagsWithKeyOrder(resourceTags, tagKeyOrder) + r.ko.Spec.Tags = fromACKTags(resourceTags, tagKeyOrder) } // mirrorAWSTags ensures that AWS tags are included in the desired resource @@ -334,10 +334,10 @@ func mirrorAWSTags(a *resource, b *resource) { var existingDesiredTags []*svcapitypes.Tag existingDesiredTags = a.ko.Spec.Tags existingLatestTags = b.ko.Spec.Tags - desiredTags, desiredTagKeyOrder := toACKTagsWithKeyOrder(existingDesiredTags) - latestTags, _ := toACKTagsWithKeyOrder(existingLatestTags) + desiredTags, desiredTagKeyOrder := convertToOrderedACKTags(existingDesiredTags) + latestTags, _ := convertToOrderedACKTags(existingLatestTags) syncAWSTags(desiredTags, latestTags) - a.ko.Spec.Tags = fromACKTagsWithKeyOrder(desiredTags, desiredTagKeyOrder) + a.ko.Spec.Tags = fromACKTags(desiredTags, desiredTagKeyOrder) } // newResourceManager returns a new struct implementing diff --git a/pkg/resource/vpc/tags.go b/pkg/resource/vpc/tags.go index de0c7910..dcb6464a 100644 --- a/pkg/resource/vpc/tags.go +++ b/pkg/resource/vpc/tags.go @@ -30,69 +30,38 @@ var ( ACKSystemTags = []string{"services.k8s.aws/namespace", "services.k8s.aws/controller-version"} ) -// ToACKTags converts the tags parameter into 'acktags.Tags' shape. -// This method helps in creating the hub(acktags.Tags) for merging -// default controller tags with existing resource tags. -func ToACKTags(tags []*svcapitypes.Tag) acktags.Tags { - result := acktags.NewTags() - if tags == nil || len(tags) == 0 { - return result - } - - for _, t := range tags { - if t.Key != nil { - if t.Value == nil { - result[*t.Key] = "" - } else { - result[*t.Key] = *t.Value - } - } - } - - return result -} - -// toACKTagsWithKeyOrder converts the tags parameter into 'acktags.Tags' shape. +// convertToOrderedACKTags converts the tags parameter into 'acktags.Tags' shape. // This method helps in creating the hub(acktags.Tags) for merging // default controller tags with existing resource tags. It also returns a slice // of keys maintaining the original key Order when the tags are a list -func toACKTagsWithKeyOrder(tags []*svcapitypes.Tag) (acktags.Tags, []string) { +func convertToOrderedACKTags(tags []*svcapitypes.Tag) (acktags.Tags, []string) { result := acktags.NewTags() keyOrder := []string{} - if tags == nil || len(tags) == 0 { + + if len(tags) == 0 { return result, keyOrder } - for _, t := range tags { if t.Key != nil { keyOrder = append(keyOrder, *t.Key) + if t.Value != nil { + result[*t.Key] = *t.Value + } else { + result[*t.Key] = "" + } } } - result = ToACKTags(tags) return result, keyOrder } -// FromACKTags converts the tags parameter into []*svcapitypes.Tag shape. -// This method helps in setting the tags back inside AWSResource after merging -// default controller tags with existing resource tags. -func FromACKTags(tags acktags.Tags) []*svcapitypes.Tag { - result := []*svcapitypes.Tag{} - for k, v := range tags { - kCopy := k - vCopy := v - tag := svcapitypes.Tag{Key: &kCopy, Value: &vCopy} - result = append(result, &tag) - } - return result -} - -// fromACKTagsWithTagKeys converts the tags parameter into []*svcapitypes.Tag shape. +// fromACKTags converts the tags parameter into []*svcapitypes.Tag shape. // This method helps in setting the tags back inside AWSResource after merging // default controller tags with existing resource tags. When a list, // it maintains the order from original -func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { +func fromACKTags(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { result := []*svcapitypes.Tag{} + for _, k := range keyOrder { v, ok := tags[k] if ok { @@ -101,7 +70,11 @@ func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitype delete(tags, k) } } - result = append(result, FromACKTags(tags)...) + for k, v := range tags { + tag := svcapitypes.Tag{Key: &k, Value: &v} + result = append(result, &tag) + } + return result } diff --git a/pkg/resource/vpc_endpoint/delta.go b/pkg/resource/vpc_endpoint/delta.go index 4f99d047..867cf4f8 100644 --- a/pkg/resource/vpc_endpoint/delta.go +++ b/pkg/resource/vpc_endpoint/delta.go @@ -112,7 +112,9 @@ func newResourceDelta( if !reflect.DeepEqual(a.ko.Spec.SubnetRefs, b.ko.Spec.SubnetRefs) { delta.Add("Spec.SubnetRefs", a.ko.Spec.SubnetRefs, b.ko.Spec.SubnetRefs) } - if !ackcompare.MapStringStringEqual(ToACKTags(a.ko.Spec.Tags), ToACKTags(b.ko.Spec.Tags)) { + desiredACKTags, _ := convertToOrderedACKTags(a.ko.Spec.Tags) + latestACKTags, _ := convertToOrderedACKTags(b.ko.Spec.Tags) + if !ackcompare.MapStringStringEqual(desiredACKTags, latestACKTags) { delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags) } if ackcompare.HasNilDifference(a.ko.Spec.VPCEndpointType, b.ko.Spec.VPCEndpointType) { diff --git a/pkg/resource/vpc_endpoint/manager.go b/pkg/resource/vpc_endpoint/manager.go index 7bce7f08..faec8438 100644 --- a/pkg/resource/vpc_endpoint/manager.go +++ b/pkg/resource/vpc_endpoint/manager.go @@ -300,9 +300,9 @@ func (rm *resourceManager) EnsureTags( defaultTags := ackrt.GetDefaultTags(&rm.cfg, r.ko, md) var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags := ToACKTags(existingTags) + resourceTags, keyOrder := convertToOrderedACKTags(existingTags) tags := acktags.Merge(resourceTags, defaultTags) - r.ko.Spec.Tags = FromACKTags(tags) + r.ko.Spec.Tags = fromACKTags(tags, keyOrder) return nil } @@ -319,9 +319,9 @@ func (rm *resourceManager) FilterSystemTags(res acktypes.AWSResource) { } var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags, tagKeyOrder := toACKTagsWithKeyOrder(existingTags) + resourceTags, tagKeyOrder := convertToOrderedACKTags(existingTags) ignoreSystemTags(resourceTags) - r.ko.Spec.Tags = fromACKTagsWithKeyOrder(resourceTags, tagKeyOrder) + r.ko.Spec.Tags = fromACKTags(resourceTags, tagKeyOrder) } // mirrorAWSTags ensures that AWS tags are included in the desired resource @@ -343,10 +343,10 @@ func mirrorAWSTags(a *resource, b *resource) { var existingDesiredTags []*svcapitypes.Tag existingDesiredTags = a.ko.Spec.Tags existingLatestTags = b.ko.Spec.Tags - desiredTags, desiredTagKeyOrder := toACKTagsWithKeyOrder(existingDesiredTags) - latestTags, _ := toACKTagsWithKeyOrder(existingLatestTags) + desiredTags, desiredTagKeyOrder := convertToOrderedACKTags(existingDesiredTags) + latestTags, _ := convertToOrderedACKTags(existingLatestTags) syncAWSTags(desiredTags, latestTags) - a.ko.Spec.Tags = fromACKTagsWithKeyOrder(desiredTags, desiredTagKeyOrder) + a.ko.Spec.Tags = fromACKTags(desiredTags, desiredTagKeyOrder) } // newResourceManager returns a new struct implementing diff --git a/pkg/resource/vpc_endpoint/tags.go b/pkg/resource/vpc_endpoint/tags.go index 8e6f9409..1ee5e00d 100644 --- a/pkg/resource/vpc_endpoint/tags.go +++ b/pkg/resource/vpc_endpoint/tags.go @@ -30,69 +30,38 @@ var ( ACKSystemTags = []string{"services.k8s.aws/namespace", "services.k8s.aws/controller-version"} ) -// ToACKTags converts the tags parameter into 'acktags.Tags' shape. -// This method helps in creating the hub(acktags.Tags) for merging -// default controller tags with existing resource tags. -func ToACKTags(tags []*svcapitypes.Tag) acktags.Tags { - result := acktags.NewTags() - if tags == nil || len(tags) == 0 { - return result - } - - for _, t := range tags { - if t.Key != nil { - if t.Value == nil { - result[*t.Key] = "" - } else { - result[*t.Key] = *t.Value - } - } - } - - return result -} - -// toACKTagsWithKeyOrder converts the tags parameter into 'acktags.Tags' shape. +// convertToOrderedACKTags converts the tags parameter into 'acktags.Tags' shape. // This method helps in creating the hub(acktags.Tags) for merging // default controller tags with existing resource tags. It also returns a slice // of keys maintaining the original key Order when the tags are a list -func toACKTagsWithKeyOrder(tags []*svcapitypes.Tag) (acktags.Tags, []string) { +func convertToOrderedACKTags(tags []*svcapitypes.Tag) (acktags.Tags, []string) { result := acktags.NewTags() keyOrder := []string{} - if tags == nil || len(tags) == 0 { + + if len(tags) == 0 { return result, keyOrder } - for _, t := range tags { if t.Key != nil { keyOrder = append(keyOrder, *t.Key) + if t.Value != nil { + result[*t.Key] = *t.Value + } else { + result[*t.Key] = "" + } } } - result = ToACKTags(tags) return result, keyOrder } -// FromACKTags converts the tags parameter into []*svcapitypes.Tag shape. -// This method helps in setting the tags back inside AWSResource after merging -// default controller tags with existing resource tags. -func FromACKTags(tags acktags.Tags) []*svcapitypes.Tag { - result := []*svcapitypes.Tag{} - for k, v := range tags { - kCopy := k - vCopy := v - tag := svcapitypes.Tag{Key: &kCopy, Value: &vCopy} - result = append(result, &tag) - } - return result -} - -// fromACKTagsWithTagKeys converts the tags parameter into []*svcapitypes.Tag shape. +// fromACKTags converts the tags parameter into []*svcapitypes.Tag shape. // This method helps in setting the tags back inside AWSResource after merging // default controller tags with existing resource tags. When a list, // it maintains the order from original -func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { +func fromACKTags(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { result := []*svcapitypes.Tag{} + for _, k := range keyOrder { v, ok := tags[k] if ok { @@ -101,7 +70,11 @@ func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitype delete(tags, k) } } - result = append(result, FromACKTags(tags)...) + for k, v := range tags { + tag := svcapitypes.Tag{Key: &k, Value: &v} + result = append(result, &tag) + } + return result } diff --git a/pkg/resource/vpc_endpoint_service_configuration/delta.go b/pkg/resource/vpc_endpoint_service_configuration/delta.go index cf97e389..1b0089f9 100644 --- a/pkg/resource/vpc_endpoint_service_configuration/delta.go +++ b/pkg/resource/vpc_endpoint_service_configuration/delta.go @@ -85,7 +85,9 @@ func newResourceDelta( delta.Add("Spec.SupportedIPAddressTypes", a.ko.Spec.SupportedIPAddressTypes, b.ko.Spec.SupportedIPAddressTypes) } } - if !ackcompare.MapStringStringEqual(ToACKTags(a.ko.Spec.Tags), ToACKTags(b.ko.Spec.Tags)) { + desiredACKTags, _ := convertToOrderedACKTags(a.ko.Spec.Tags) + latestACKTags, _ := convertToOrderedACKTags(b.ko.Spec.Tags) + if !ackcompare.MapStringStringEqual(desiredACKTags, latestACKTags) { delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags) } diff --git a/pkg/resource/vpc_endpoint_service_configuration/manager.go b/pkg/resource/vpc_endpoint_service_configuration/manager.go index 71d72a30..ff1edc87 100644 --- a/pkg/resource/vpc_endpoint_service_configuration/manager.go +++ b/pkg/resource/vpc_endpoint_service_configuration/manager.go @@ -299,9 +299,9 @@ func (rm *resourceManager) EnsureTags( defaultTags := ackrt.GetDefaultTags(&rm.cfg, r.ko, md) var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags := ToACKTags(existingTags) + resourceTags, keyOrder := convertToOrderedACKTags(existingTags) tags := acktags.Merge(resourceTags, defaultTags) - r.ko.Spec.Tags = FromACKTags(tags) + r.ko.Spec.Tags = fromACKTags(tags, keyOrder) return nil } @@ -318,9 +318,9 @@ func (rm *resourceManager) FilterSystemTags(res acktypes.AWSResource) { } var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags, tagKeyOrder := toACKTagsWithKeyOrder(existingTags) + resourceTags, tagKeyOrder := convertToOrderedACKTags(existingTags) ignoreSystemTags(resourceTags) - r.ko.Spec.Tags = fromACKTagsWithKeyOrder(resourceTags, tagKeyOrder) + r.ko.Spec.Tags = fromACKTags(resourceTags, tagKeyOrder) } // mirrorAWSTags ensures that AWS tags are included in the desired resource @@ -342,10 +342,10 @@ func mirrorAWSTags(a *resource, b *resource) { var existingDesiredTags []*svcapitypes.Tag existingDesiredTags = a.ko.Spec.Tags existingLatestTags = b.ko.Spec.Tags - desiredTags, desiredTagKeyOrder := toACKTagsWithKeyOrder(existingDesiredTags) - latestTags, _ := toACKTagsWithKeyOrder(existingLatestTags) + desiredTags, desiredTagKeyOrder := convertToOrderedACKTags(existingDesiredTags) + latestTags, _ := convertToOrderedACKTags(existingLatestTags) syncAWSTags(desiredTags, latestTags) - a.ko.Spec.Tags = fromACKTagsWithKeyOrder(desiredTags, desiredTagKeyOrder) + a.ko.Spec.Tags = fromACKTags(desiredTags, desiredTagKeyOrder) } // newResourceManager returns a new struct implementing diff --git a/pkg/resource/vpc_endpoint_service_configuration/tags.go b/pkg/resource/vpc_endpoint_service_configuration/tags.go index 1ab78c7d..48708510 100644 --- a/pkg/resource/vpc_endpoint_service_configuration/tags.go +++ b/pkg/resource/vpc_endpoint_service_configuration/tags.go @@ -30,69 +30,38 @@ var ( ACKSystemTags = []string{"services.k8s.aws/namespace", "services.k8s.aws/controller-version"} ) -// ToACKTags converts the tags parameter into 'acktags.Tags' shape. -// This method helps in creating the hub(acktags.Tags) for merging -// default controller tags with existing resource tags. -func ToACKTags(tags []*svcapitypes.Tag) acktags.Tags { - result := acktags.NewTags() - if tags == nil || len(tags) == 0 { - return result - } - - for _, t := range tags { - if t.Key != nil { - if t.Value == nil { - result[*t.Key] = "" - } else { - result[*t.Key] = *t.Value - } - } - } - - return result -} - -// toACKTagsWithKeyOrder converts the tags parameter into 'acktags.Tags' shape. +// convertToOrderedACKTags converts the tags parameter into 'acktags.Tags' shape. // This method helps in creating the hub(acktags.Tags) for merging // default controller tags with existing resource tags. It also returns a slice // of keys maintaining the original key Order when the tags are a list -func toACKTagsWithKeyOrder(tags []*svcapitypes.Tag) (acktags.Tags, []string) { +func convertToOrderedACKTags(tags []*svcapitypes.Tag) (acktags.Tags, []string) { result := acktags.NewTags() keyOrder := []string{} - if tags == nil || len(tags) == 0 { + + if len(tags) == 0 { return result, keyOrder } - for _, t := range tags { if t.Key != nil { keyOrder = append(keyOrder, *t.Key) + if t.Value != nil { + result[*t.Key] = *t.Value + } else { + result[*t.Key] = "" + } } } - result = ToACKTags(tags) return result, keyOrder } -// FromACKTags converts the tags parameter into []*svcapitypes.Tag shape. -// This method helps in setting the tags back inside AWSResource after merging -// default controller tags with existing resource tags. -func FromACKTags(tags acktags.Tags) []*svcapitypes.Tag { - result := []*svcapitypes.Tag{} - for k, v := range tags { - kCopy := k - vCopy := v - tag := svcapitypes.Tag{Key: &kCopy, Value: &vCopy} - result = append(result, &tag) - } - return result -} - -// fromACKTagsWithTagKeys converts the tags parameter into []*svcapitypes.Tag shape. +// fromACKTags converts the tags parameter into []*svcapitypes.Tag shape. // This method helps in setting the tags back inside AWSResource after merging // default controller tags with existing resource tags. When a list, // it maintains the order from original -func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { +func fromACKTags(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { result := []*svcapitypes.Tag{} + for _, k := range keyOrder { v, ok := tags[k] if ok { @@ -101,7 +70,11 @@ func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitype delete(tags, k) } } - result = append(result, FromACKTags(tags)...) + for k, v := range tags { + tag := svcapitypes.Tag{Key: &k, Value: &v} + result = append(result, &tag) + } + return result } diff --git a/pkg/resource/vpc_peering_connection/delta.go b/pkg/resource/vpc_peering_connection/delta.go index f0a32fa2..2ed24daf 100644 --- a/pkg/resource/vpc_peering_connection/delta.go +++ b/pkg/resource/vpc_peering_connection/delta.go @@ -124,7 +124,9 @@ func newResourceDelta( } } } - if !ackcompare.MapStringStringEqual(ToACKTags(a.ko.Spec.Tags), ToACKTags(b.ko.Spec.Tags)) { + desiredACKTags, _ := convertToOrderedACKTags(a.ko.Spec.Tags) + latestACKTags, _ := convertToOrderedACKTags(b.ko.Spec.Tags) + if !ackcompare.MapStringStringEqual(desiredACKTags, latestACKTags) { delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags) } if ackcompare.HasNilDifference(a.ko.Spec.VPCID, b.ko.Spec.VPCID) { diff --git a/pkg/resource/vpc_peering_connection/manager.go b/pkg/resource/vpc_peering_connection/manager.go index eecbf4bd..22488ef7 100644 --- a/pkg/resource/vpc_peering_connection/manager.go +++ b/pkg/resource/vpc_peering_connection/manager.go @@ -291,9 +291,9 @@ func (rm *resourceManager) EnsureTags( defaultTags := ackrt.GetDefaultTags(&rm.cfg, r.ko, md) var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags := ToACKTags(existingTags) + resourceTags, keyOrder := convertToOrderedACKTags(existingTags) tags := acktags.Merge(resourceTags, defaultTags) - r.ko.Spec.Tags = FromACKTags(tags) + r.ko.Spec.Tags = fromACKTags(tags, keyOrder) return nil } @@ -310,9 +310,9 @@ func (rm *resourceManager) FilterSystemTags(res acktypes.AWSResource) { } var existingTags []*svcapitypes.Tag existingTags = r.ko.Spec.Tags - resourceTags, tagKeyOrder := toACKTagsWithKeyOrder(existingTags) + resourceTags, tagKeyOrder := convertToOrderedACKTags(existingTags) ignoreSystemTags(resourceTags) - r.ko.Spec.Tags = fromACKTagsWithKeyOrder(resourceTags, tagKeyOrder) + r.ko.Spec.Tags = fromACKTags(resourceTags, tagKeyOrder) } // mirrorAWSTags ensures that AWS tags are included in the desired resource @@ -334,10 +334,10 @@ func mirrorAWSTags(a *resource, b *resource) { var existingDesiredTags []*svcapitypes.Tag existingDesiredTags = a.ko.Spec.Tags existingLatestTags = b.ko.Spec.Tags - desiredTags, desiredTagKeyOrder := toACKTagsWithKeyOrder(existingDesiredTags) - latestTags, _ := toACKTagsWithKeyOrder(existingLatestTags) + desiredTags, desiredTagKeyOrder := convertToOrderedACKTags(existingDesiredTags) + latestTags, _ := convertToOrderedACKTags(existingLatestTags) syncAWSTags(desiredTags, latestTags) - a.ko.Spec.Tags = fromACKTagsWithKeyOrder(desiredTags, desiredTagKeyOrder) + a.ko.Spec.Tags = fromACKTags(desiredTags, desiredTagKeyOrder) } // newResourceManager returns a new struct implementing diff --git a/pkg/resource/vpc_peering_connection/tags.go b/pkg/resource/vpc_peering_connection/tags.go index b42cf70e..634e5e9b 100644 --- a/pkg/resource/vpc_peering_connection/tags.go +++ b/pkg/resource/vpc_peering_connection/tags.go @@ -30,69 +30,38 @@ var ( ACKSystemTags = []string{"services.k8s.aws/namespace", "services.k8s.aws/controller-version"} ) -// ToACKTags converts the tags parameter into 'acktags.Tags' shape. -// This method helps in creating the hub(acktags.Tags) for merging -// default controller tags with existing resource tags. -func ToACKTags(tags []*svcapitypes.Tag) acktags.Tags { - result := acktags.NewTags() - if tags == nil || len(tags) == 0 { - return result - } - - for _, t := range tags { - if t.Key != nil { - if t.Value == nil { - result[*t.Key] = "" - } else { - result[*t.Key] = *t.Value - } - } - } - - return result -} - -// toACKTagsWithKeyOrder converts the tags parameter into 'acktags.Tags' shape. +// convertToOrderedACKTags converts the tags parameter into 'acktags.Tags' shape. // This method helps in creating the hub(acktags.Tags) for merging // default controller tags with existing resource tags. It also returns a slice // of keys maintaining the original key Order when the tags are a list -func toACKTagsWithKeyOrder(tags []*svcapitypes.Tag) (acktags.Tags, []string) { +func convertToOrderedACKTags(tags []*svcapitypes.Tag) (acktags.Tags, []string) { result := acktags.NewTags() keyOrder := []string{} - if tags == nil || len(tags) == 0 { + + if len(tags) == 0 { return result, keyOrder } - for _, t := range tags { if t.Key != nil { keyOrder = append(keyOrder, *t.Key) + if t.Value != nil { + result[*t.Key] = *t.Value + } else { + result[*t.Key] = "" + } } } - result = ToACKTags(tags) return result, keyOrder } -// FromACKTags converts the tags parameter into []*svcapitypes.Tag shape. -// This method helps in setting the tags back inside AWSResource after merging -// default controller tags with existing resource tags. -func FromACKTags(tags acktags.Tags) []*svcapitypes.Tag { - result := []*svcapitypes.Tag{} - for k, v := range tags { - kCopy := k - vCopy := v - tag := svcapitypes.Tag{Key: &kCopy, Value: &vCopy} - result = append(result, &tag) - } - return result -} - -// fromACKTagsWithTagKeys converts the tags parameter into []*svcapitypes.Tag shape. +// fromACKTags converts the tags parameter into []*svcapitypes.Tag shape. // This method helps in setting the tags back inside AWSResource after merging // default controller tags with existing resource tags. When a list, // it maintains the order from original -func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { +func fromACKTags(tags acktags.Tags, keyOrder []string) []*svcapitypes.Tag { result := []*svcapitypes.Tag{} + for _, k := range keyOrder { v, ok := tags[k] if ok { @@ -101,7 +70,11 @@ func fromACKTagsWithKeyOrder(tags acktags.Tags, keyOrder []string) []*svcapitype delete(tags, k) } } - result = append(result, FromACKTags(tags)...) + for k, v := range tags { + tag := svcapitypes.Tag{Key: &k, Value: &v} + result = append(result, &tag) + } + return result }