Skip to content

Commit dbbf635

Browse files
authored
Add Adoption by annotation tests (#139)
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent b1893a4 commit dbbf635

12 files changed

+144
-14
lines changed

apis/v1alpha1/ack-generate-metadata.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
ack_generate_info:
2-
build_date: "2024-10-10T04:12:55Z"
3-
build_hash: 36c2d234498c2bc4f60773ab8df632af4067f43b
2+
build_date: "2024-12-02T23:36:35Z"
3+
build_hash: ce8a66e1fd3d15afacc33069bb788795863a4245
44
go_version: go1.23.2
5-
version: v0.39.1
5+
version: v0.39.1-6-gce8a66e
66
api_directory_checksum: 82bfc8d45e816b2a02a83a4b7cedd72056accddd
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.44.93

config/controller/deployment.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ spec:
4141
- "$(LEADER_ELECTION_NAMESPACE)"
4242
- --reconcile-default-max-concurrent-syncs
4343
- "$(RECONCILE_DEFAULT_MAX_CONCURRENT_SYNCS)"
44+
- --feature-gates
45+
- "$(FEATURE_GATES)"
4446
image: controller:latest
4547
name: controller
4648
ports:
@@ -76,6 +78,8 @@ spec:
7678
value: "ack-system"
7779
- name: "RECONCILE_DEFAULT_MAX_CONCURRENT_SYNCS"
7880
value: "1"
81+
- name: "FEATURE_GATES"
82+
value: ""
7983
securityContext:
8084
allowPrivilegeEscalation: false
8185
privileged: false

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.22.0
55
toolchain go1.22.5
66

77
require (
8-
github.com/aws-controllers-k8s/runtime v0.39.0
8+
github.com/aws-controllers-k8s/runtime v0.39.1-0.20241202082353-a6b0014a8130
99
github.com/aws/aws-sdk-go v1.49.0
1010
github.com/go-logr/logr v1.4.2
1111
github.com/pkg/errors v0.9.1

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
github.com/aws-controllers-k8s/runtime v0.39.0 h1:IgOXluSzvb4UcDr9eU7SPw5MJnL7kt5R6DuF5Qu9zVQ=
2-
github.com/aws-controllers-k8s/runtime v0.39.0/go.mod h1:G07g26y1cxyZO6Ngp+LwXf03CqFyLNL7os4Py4IdyGY=
1+
github.com/aws-controllers-k8s/runtime v0.39.1-0.20241202082353-a6b0014a8130 h1:EoXYRrpBX2hi5B1IawKr2LJTsVsreHsJdxULLlMNO9U=
2+
github.com/aws-controllers-k8s/runtime v0.39.1-0.20241202082353-a6b0014a8130/go.mod h1:G07g26y1cxyZO6Ngp+LwXf03CqFyLNL7os4Py4IdyGY=
33
github.com/aws/aws-sdk-go v1.49.0 h1:g9BkW1fo9GqKfwg2+zCD+TW/D36Ux+vtfJ8guF4AYmY=
44
github.com/aws/aws-sdk-go v1.49.0/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
55
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=

helm/templates/caches-role-binding.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ roleRef:
88
name: ack-namespaces-cache-s3-controller
99
subjects:
1010
- kind: ServiceAccount
11-
name: ack-s3-controller
11+
name: {{ include "ack-s3-controller.service-account.name" . }}
1212
namespace: {{ .Release.Namespace }}
1313
---
1414
apiVersion: rbac.authorization.k8s.io/v1
@@ -22,5 +22,5 @@ roleRef:
2222
name: ack-configmaps-cache-s3-controller
2323
subjects:
2424
- kind: ServiceAccount
25-
name: ack-s3-controller
26-
namespace: {{ .Release.Namespace }}
25+
name: {{ include "ack-s3-controller.service-account.name" . }}
26+
namespace: {{ .Release.Namespace }}

pkg/resource/bucket/descriptor.go

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/resource/bucket/resource.go

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/e2e/bootstrap_resources.py

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
@dataclass
2626
class BootstrapResources(Resources):
2727
ReplicationBucket: Bucket
28+
AdoptionBucket: Bucket
2829
ReplicationRole: Role
2930
NotificationTopic: Topic
3031

test/e2e/replacement_values.py

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
REPLACEMENT_VALUES = {
2020
"REPLICATION_ROLE_ARN": get_bootstrap_resources().ReplicationRole.arn,
21+
"ADOPTION_BUCKET_NAME": get_bootstrap_resources().AdoptionBucket.name,
2122
"REPLICATION_BUCKET_NAME": get_bootstrap_resources().ReplicationBucket.name,
2223
"NOTIFICATION_TOPIC_ARN": get_bootstrap_resources().NotificationTopic.arn,
2324
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: s3.services.k8s.aws/v1alpha1
2+
kind: Bucket
3+
metadata:
4+
name: $ADOPTION_BUCKET_NAME
5+
annotations:
6+
services.k8s.aws/adoption-policy: $ADOPTION_POLICY
7+
services.k8s.aws/adoption-fields: "$ADOPTION_FIELDS"
8+
services.k8s.aws/deletion-policy: retain

test/e2e/service_bootstrap.py

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def service_bootstrap() -> Resources:
6464

6565
resources = BootstrapResources(
6666
ReplicationBucket=Bucket("ack-s3-replication", enable_versioning=True),
67+
AdoptionBucket=Bucket("ack-s3-annotation-adoption", enable_versioning=True),
6768
ReplicationRole=Role("ack-s3-replication-role", "s3.amazonaws.com",
6869
user_policies=UserPolicies("ack-s3-replication-policy", [replication_policy])
6970
),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
# not use this file except in compliance with the License. A copy of the
5+
# License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is distributed
10+
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
# express or implied. See the License for the specific language governing
12+
# permissions and limitations under the License.
13+
14+
"""Integration tests for the deletion policy annotation on Bucket.
15+
"""
16+
17+
from enum import Enum
18+
import pytest
19+
import time
20+
import logging
21+
22+
from acktest.resources import random_suffix_name
23+
from acktest.k8s import resource as k8s
24+
from acktest import adoption as adoption
25+
from acktest import tags as tags
26+
from e2e import service_marker, CRD_GROUP, CRD_VERSION, load_s3_resource
27+
from e2e.tests.test_bucket import bucket_exists, get_bucket
28+
from e2e.replacement_values import REPLACEMENT_VALUES
29+
30+
CREATE_WAIT_AFTER_SECONDS = 10
31+
MODIFY_WAIT_AFTER_SECONDS = 10
32+
DELETE_WAIT_AFTER_SECONDS = 10
33+
34+
class AdoptionPolicy(str, Enum):
35+
NONE = ""
36+
ADOPT = "adopt"
37+
ADOPT_OR_CREATE = "adopt-or-create"
38+
39+
40+
@pytest.fixture(scope="module")
41+
def adoption_policy_adopt_bucket(s3_client):
42+
replacements = REPLACEMENT_VALUES.copy()
43+
bucket_name = replacements["ADOPTION_BUCKET_NAME"]
44+
replacements["ADOPTION_POLICY"] = AdoptionPolicy.ADOPT
45+
replacements["ADOPTION_FIELDS"] = f'{{\\\"name\\\": \\\"{bucket_name}\\\"}}'
46+
47+
resource_data = load_s3_resource(
48+
"bucket_adoption_policy",
49+
additional_replacements=replacements,
50+
)
51+
52+
# Create k8s resource
53+
ref = k8s.CustomResourceReference(
54+
CRD_GROUP, CRD_VERSION, "buckets",
55+
bucket_name, namespace="default")
56+
k8s.create_custom_resource(ref, resource_data)
57+
58+
time.sleep(CREATE_WAIT_AFTER_SECONDS)
59+
cr = k8s.wait_resource_consumed_by_controller(ref)
60+
61+
assert cr is not None
62+
assert k8s.get_resource_exists(ref)
63+
64+
yield (ref, cr)
65+
66+
_, deleted = k8s.delete_custom_resource(ref, DELETE_WAIT_AFTER_SECONDS)
67+
assert deleted
68+
69+
@service_marker
70+
@pytest.mark.canary
71+
class TestAdoptionPolicyBucket:
72+
def test_adoption_policy(
73+
self, s3_client, adoption_policy_adopt_bucket, s3_resource
74+
):
75+
(ref, cr) = adoption_policy_adopt_bucket
76+
77+
# Spec will be added by controller
78+
assert 'spec' in cr
79+
assert 'name' in cr['spec']
80+
bucket_name = cr['spec']['name']
81+
82+
updates = {
83+
"spec": {
84+
"versioning": {
85+
"status": "Suspended"
86+
},
87+
}
88+
}
89+
90+
k8s.patch_custom_resource(ref, updates)
91+
time.sleep(MODIFY_WAIT_AFTER_SECONDS)
92+
93+
cr = k8s.wait_resource_consumed_by_controller(ref)
94+
assert cr is not None
95+
assert 'spec' in cr
96+
assert 'versioning' in cr['spec']
97+
assert 'status' in cr['spec']['versioning']
98+
status = cr['spec']['versioning']['status']
99+
latest = get_bucket(s3_resource, bucket_name)
100+
assert latest is not None
101+
versioning = latest.Versioning()
102+
assert versioning.status == status
103+
104+

0 commit comments

Comments
 (0)