@@ -26,6 +26,7 @@ import (
2626
2727 corev1 "k8s.io/api/core/v1"
2828 "k8s.io/apimachinery/pkg/types"
29+ "k8s.io/utils/strings/slices"
2930
3031 "github.com/aws/aws-sdk-go/aws"
3132 "github.com/aws/aws-sdk-go/aws/awserr"
@@ -40,6 +41,7 @@ type EIPReconciler struct {
4041 NonCachingClient client.Client
4142 Log logr.Logger
4243 EC2 * ec2.EC2
44+ Tags map [string ]string
4345}
4446
4547// +kubebuilder:rbac:groups=aws.k8s.logmein.com,resources=eips,verbs=get;list;watch;create;update;patch;delete
@@ -190,6 +192,12 @@ func (r *EIPReconciler) allocateEIP(ctx context.Context, eip *awsv1alpha1.EIP, l
190192 }
191193 }
192194
195+ tags := ec2.TagSpecification {
196+ ResourceType : aws .String ("elastic-ip" ),
197+ Tags : r .combineDefaultAndDefinedTags (eip ),
198+ }
199+ input .TagSpecifications = []* ec2.TagSpecification {& tags }
200+
193201 if resp , err := r .EC2 .AllocateAddressWithContext (ctx , input ); err != nil {
194202 return err
195203 } else {
@@ -202,7 +210,27 @@ func (r *EIPReconciler) allocateEIP(ctx context.Context, eip *awsv1alpha1.EIP, l
202210 }
203211 }
204212
205- return r .reconcileTags (ctx , eip , []* ec2.Tag {})
213+ return nil
214+ }
215+
216+ // combineDefaultAndDefinedTags combines the default tags defined in the controller
217+ // with the tags defined in the EIP spec. Tags defined in the EIP spec override
218+ // default tags in case of key conflicts.
219+ func (r EIPReconciler ) combineDefaultAndDefinedTags (eip * awsv1alpha1.EIP ) []* ec2.Tag {
220+ var tags []* ec2.Tag
221+ for k , v := range r .Tags {
222+ tags = append (tags , & ec2.Tag {
223+ Key : aws .String (k ),
224+ Value : aws .String (v ),
225+ })
226+ }
227+ for k , v := range * eip .Spec .Tags {
228+ tags = append (tags , & ec2.Tag {
229+ Key : aws .String (k ),
230+ Value : aws .String (v ),
231+ })
232+ }
233+ return tags
206234}
207235
208236func (r * EIPReconciler ) reconcileTags (ctx context.Context , eip * awsv1alpha1.EIP , existingTags []* ec2.Tag ) error {
@@ -225,6 +253,7 @@ func (r *EIPReconciler) reconcileTags(ctx context.Context, eip *awsv1alpha1.EIP,
225253 tagsToCreate = append (tagsToCreate , & ec2.Tag {Key : aws .String (k ), Value : aws .String (v )})
226254 }
227255 }
256+
228257 if len (tagsToCreate ) > 0 {
229258 if _ , err := r .EC2 .CreateTagsWithContext (ctx , & ec2.CreateTagsInput {
230259 Resources : resources ,
@@ -234,12 +263,22 @@ func (r *EIPReconciler) reconcileTags(ctx context.Context, eip *awsv1alpha1.EIP,
234263 }
235264 }
236265
266+ convertToStringSlice := func (tags []* ec2.Tag ) []string {
267+ var keys []string
268+ for _ , tag := range tags {
269+ keys = append (keys , aws .StringValue (tag .Key ))
270+ }
271+ return keys
272+ }
273+ combinedTags := convertToStringSlice (r .combineDefaultAndDefinedTags (eip ))
274+
237275 var tagsToRemove []* ec2.Tag
238276 for _ , tag := range existingTags {
239- if _ , ok := ( * eip . Spec . Tags )[ aws .StringValue (tag .Key )]; ! ok {
277+ if slices . Index ( combinedTags , aws .StringValue (tag .Key )) == - 1 {
240278 tagsToRemove = append (tagsToRemove , tag )
241279 }
242280 }
281+
243282 if len (tagsToRemove ) > 0 {
244283 _ , err := r .EC2 .DeleteTagsWithContext (ctx , & ec2.DeleteTagsInput {
245284 Resources : resources ,
0 commit comments