Skip to content

Commit 05a6d92

Browse files
committed
Integrate CR feedback - tags
1 parent a093148 commit 05a6d92

File tree

2 files changed

+84
-17
lines changed

2 files changed

+84
-17
lines changed

pkg/resource/managed_prefix_list/hooks.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ func (rm *resourceManager) customUpdateManagedPrefixList(
6060
return desired, nil
6161
}
6262

63+
// Handle tag updates first
64+
if delta.DifferentAt("Spec.Tags") {
65+
if err := tags.Sync(
66+
ctx, rm.sdkapi, rm.metrics, *latest.ko.Status.PrefixListID,
67+
desired.ko.Spec.Tags, latest.ko.Spec.Tags,
68+
); err != nil {
69+
return nil, err
70+
}
71+
}
72+
73+
// Only continue if something other than Tags has changed in the Spec
74+
if !delta.DifferentExcept("Spec.Tags") {
75+
return desired, nil
76+
}
77+
6378
// Build the modify input
6479
input := &svcsdk.ModifyManagedPrefixListInput{}
6580
input.PrefixListId = latest.ko.Status.PrefixListID
@@ -164,15 +179,5 @@ func (rm *resourceManager) customUpdateManagedPrefixList(
164179
}
165180
}
166181

167-
// Handle tag updates separately
168-
if delta.DifferentAt("Spec.Tags") {
169-
if err := tags.Sync(
170-
ctx, rm.sdkapi, rm.metrics, *latest.ko.Status.PrefixListID,
171-
desired.ko.Spec.Tags, latest.ko.Spec.Tags,
172-
); err != nil {
173-
return nil, err
174-
}
175-
}
176-
177182
return desired, nil
178183
}

test/e2e/tests/test_managed_prefix_list.py

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,17 @@ def test_update_entries(self, prefix_list_ipv4, ec2_validator):
291291
assert entry_to_remove not in k8s_cidrs, \
292292
f"Entry {entry_to_remove} should not be in K8s: {k8s_cidrs}"
293293

294-
def test_update_tags(self, prefix_list_ipv4):
295-
"""Test updating prefix list tags."""
296-
(ref, _) = prefix_list_ipv4
294+
def test_update_tags(self, prefix_list_ipv4, ec2_validator):
295+
"""Test adding, updating, and removing prefix list tags."""
296+
(ref, cr) = prefix_list_ipv4
297+
298+
# Get the prefix list ID
299+
prefix_list_id = cr['status']['prefixListID']
297300

298301
# Get the latest version of the resource to avoid conflicts
299302
cr = k8s.get_resource(ref)
300303

301-
# Add a new tag
304+
# ===== TEST 1: Add a new tag =====
302305
new_tag = {
303306
'key': 'Environment',
304307
'value': 'Test'
@@ -313,14 +316,73 @@ def test_update_tags(self, prefix_list_ipv4):
313316

314317
# Wait for the resource to be synced
315318
assert k8s.wait_on_condition(ref, "ACK.ResourceSynced", "True", wait_periods=5), \
316-
"Resource did not sync after tag update"
319+
"Resource did not sync after adding tag"
320+
321+
# Get the updated resource
322+
cr = k8s.get_resource(ref)
323+
324+
# Verify tag was added in K8s
325+
tags = cr['spec'].get('tags', [])
326+
assert any(tag['key'] == 'Environment' and tag['value'] == 'Test' for tag in tags), \
327+
"Environment tag should be added in K8s"
328+
329+
# Verify tag was added in AWS
330+
aws_prefix_list = ec2_validator.get_managed_prefix_list(prefix_list_id)
331+
aws_tags = aws_prefix_list.get('Tags', [])
332+
assert any(tag['Key'] == 'Environment' and tag['Value'] == 'Test' for tag in aws_tags), \
333+
"Environment tag should be added in AWS"
334+
335+
# ===== TEST 2: Update an existing tag =====
336+
for tag in cr['spec']['tags']:
337+
if tag['key'] == 'Environment':
338+
tag['value'] = 'Development'
339+
340+
# Apply the update
341+
k8s.patch_custom_resource(ref, cr)
342+
time.sleep(UPDATE_WAIT_AFTER_SECONDS)
343+
344+
# Wait for the resource to be synced
345+
assert k8s.wait_on_condition(ref, "ACK.ResourceSynced", "True", wait_periods=5), \
346+
"Resource did not sync after updating tag"
347+
348+
# Get the updated resource
349+
cr = k8s.get_resource(ref)
350+
351+
# Verify tag was updated in K8s
352+
tags = cr['spec'].get('tags', [])
353+
assert any(tag['key'] == 'Environment' and tag['value'] == 'Development' for tag in tags), \
354+
"Environment tag should be updated to Development in K8s"
355+
356+
# Verify tag was updated in AWS
357+
aws_prefix_list = ec2_validator.get_managed_prefix_list(prefix_list_id)
358+
aws_tags = aws_prefix_list.get('Tags', [])
359+
assert any(tag['Key'] == 'Environment' and tag['Value'] == 'Development' for tag in aws_tags), \
360+
"Environment tag should be updated to Development in AWS"
361+
362+
# ===== TEST 3: Remove a tag =====
363+
cr['spec']['tags'] = [tag for tag in cr['spec']['tags'] if tag['key'] != 'Environment']
364+
365+
# Apply the update
366+
k8s.patch_custom_resource(ref, cr)
367+
time.sleep(UPDATE_WAIT_AFTER_SECONDS)
368+
369+
# Wait for the resource to be synced
370+
assert k8s.wait_on_condition(ref, "ACK.ResourceSynced", "True", wait_periods=5), \
371+
"Resource did not sync after removing tag"
317372

318373
# Get the updated resource
319374
cr = k8s.get_resource(ref)
320375

321-
# Verify tag was added
376+
# Verify tag was removed in K8s
322377
tags = cr['spec'].get('tags', [])
323-
assert any(tag['key'] == 'Environment' and tag['value'] == 'Test' for tag in tags)
378+
assert not any(tag['key'] == 'Environment' for tag in tags), \
379+
"Environment tag should be removed from K8s"
380+
381+
# Verify tag was removed in AWS
382+
aws_prefix_list = ec2_validator.get_managed_prefix_list(prefix_list_id)
383+
aws_tags = aws_prefix_list.get('Tags', [])
384+
assert not any(tag['Key'] == 'Environment' for tag in aws_tags), \
385+
"Environment tag should be removed from AWS"
324386

325387
def test_prefix_list_fields(self, prefix_list_ipv4):
326388
"""Test that all expected fields are present."""

0 commit comments

Comments
 (0)