Skip to content

Commit 3979d23

Browse files
CLOUDP-90175: cleanup all resources (#624)
1 parent 133731d commit 3979d23

File tree

2 files changed

+59
-29
lines changed

2 files changed

+59
-29
lines changed

scripts/dev/e2e.py

+48-19
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
import yaml
1212

1313
TEST_POD_NAME = "e2e-test"
14+
TEST_CLUSTER_ROLE_NAME = "e2e-test"
15+
TEST_CLUSTER_ROLE_BINDING_NAME = "e2e-test"
16+
TEST_SERVICE_ACCOUNT_NAME = "e2e-test"
1417

1518

1619
def load_yaml_from_file(path: str) -> Dict:
@@ -32,27 +35,29 @@ def _load_test_role_binding() -> Dict:
3235

3336
def _prepare_test_environment(config_file: str) -> None:
3437
"""
35-
_prepare_testrunner_environment ensures the ServiceAccount,
36-
Role and ClusterRole and bindings are created for the test runner.
38+
_prepare_test_environment ensures that the old test pod is deleted
39+
and that namespace, cluster role, cluster role binding and service account
40+
are created for the test pod.
3741
"""
3842
rbacv1 = client.RbacAuthorizationV1Api()
3943
corev1 = client.CoreV1Api()
4044
dev_config = load_config(config_file)
4145

46+
_delete_test_pod(config_file)
47+
4248
print("Creating Namespace")
4349
k8s_conditions.ignore_if_already_exists(
4450
lambda: corev1.create_namespace(
4551
client.V1Namespace(metadata=dict(name=dev_config.namespace))
4652
)
4753
)
48-
_delete_test_pod(config_file)
4954

50-
print("Creating Role")
55+
print("Creating Cluster Role")
5156
k8s_conditions.ignore_if_already_exists(
5257
lambda: rbacv1.create_cluster_role(_load_test_role())
5358
)
5459

55-
print("Creating Role Binding")
60+
print("Creating Cluster Role Binding")
5661
role_binding = _load_test_role_binding()
5762
# set namespace specified in config.json
5863
role_binding["subjects"][0]["namespace"] = dev_config.namespace
@@ -61,26 +66,14 @@ def _prepare_test_environment(config_file: str) -> None:
6166
lambda: rbacv1.create_cluster_role_binding(role_binding)
6267
)
6368

64-
print("Creating ServiceAccount")
69+
print("Creating Service Account")
6570
k8s_conditions.ignore_if_already_exists(
6671
lambda: corev1.create_namespaced_service_account(
6772
dev_config.namespace, _load_test_service_account()
6873
)
6974
)
7075

7176

72-
def _delete_test_pod(config_file: str) -> None:
73-
"""
74-
_delete_testrunner_pod deletes the test runner pod
75-
if it already exists.
76-
"""
77-
dev_config = load_config(config_file)
78-
corev1 = client.CoreV1Api()
79-
k8s_conditions.ignore_if_doesnt_exist(
80-
lambda: corev1.delete_namespaced_pod(TEST_POD_NAME, dev_config.namespace)
81-
)
82-
83-
8477
def create_test_pod(args: argparse.Namespace, dev_config: DevConfig) -> None:
8578
corev1 = client.CoreV1Api()
8679
test_pod = {
@@ -158,7 +151,7 @@ def create_test_pod(args: argparse.Namespace, dev_config: DevConfig) -> None:
158151
timeout=60,
159152
exceptions_to_ignore=ApiException,
160153
):
161-
raise Exception("Could not create test_runner pod!")
154+
raise Exception("Could not create test pod!")
162155

163156

164157
def wait_for_pod_to_be_running(
@@ -178,6 +171,41 @@ def wait_for_pod_to_be_running(
178171
print("Pod is running")
179172

180173

174+
def _delete_test_environment(config_file: str) -> None:
175+
"""
176+
_delete_test_environment ensures that the cluster role, cluster role binding and service account
177+
for the test pod are deleted.
178+
"""
179+
rbacv1 = client.RbacAuthorizationV1Api()
180+
corev1 = client.CoreV1Api()
181+
dev_config = load_config(config_file)
182+
183+
k8s_conditions.ignore_if_doesnt_exist(
184+
lambda: rbacv1.delete_cluster_role(TEST_CLUSTER_ROLE_NAME)
185+
)
186+
187+
k8s_conditions.ignore_if_doesnt_exist(
188+
lambda: rbacv1.delete_cluster_role_binding(TEST_CLUSTER_ROLE_BINDING_NAME)
189+
)
190+
191+
k8s_conditions.ignore_if_doesnt_exist(
192+
lambda: corev1.delete_namespaced_service_account(
193+
TEST_SERVICE_ACCOUNT_NAME, dev_config.namespace
194+
)
195+
)
196+
197+
198+
def _delete_test_pod(config_file: str) -> None:
199+
"""
200+
_delete_test_pod deletes the test pod.
201+
"""
202+
dev_config = load_config(config_file)
203+
corev1 = client.CoreV1Api()
204+
k8s_conditions.ignore_if_doesnt_exist(
205+
lambda: corev1.delete_namespaced_pod(TEST_POD_NAME, dev_config.namespace)
206+
)
207+
208+
181209
def parse_args() -> argparse.Namespace:
182210
parser = argparse.ArgumentParser()
183211
parser.add_argument("--test", help="Name of the test to run")
@@ -246,6 +274,7 @@ def main() -> int:
246274
exceptions_to_ignore=ApiException,
247275
):
248276
return 1
277+
_delete_test_environment(args.config_file)
249278
return 0
250279

251280

test/e2e/setup/setup.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func Setup(t *testing.T) *e2eutil.Context {
4646
t.Fatal(err)
4747
}
4848

49-
if err := deployOperator(); err != nil {
49+
if err := deployOperator(ctx); err != nil {
5050
t.Fatal(err)
5151
}
5252

@@ -130,7 +130,8 @@ func deployDir() string {
130130
return envvar.GetEnvOrDefault(deployDirEnv, "/workspace/config/manager")
131131
}
132132

133-
func deployOperator() error {
133+
func deployOperator(ctx *e2eutil.Context) error {
134+
134135
testConfig := loadTestConfigFromEnv()
135136

136137
e2eutil.OperatorNamespace = testConfig.namespace
@@ -141,24 +142,24 @@ func deployOperator() error {
141142
}
142143
fmt.Printf("Setting namespace to watch to %s\n", watchNamespace)
143144

144-
if err := buildKubernetesResourceFromYamlFile(path.Join(roleDir(), "role.yaml"), &rbacv1.Role{}, withNamespace(testConfig.namespace)); err != nil {
145+
if err := buildKubernetesResourceFromYamlFile(ctx, path.Join(roleDir(), "role.yaml"), &rbacv1.Role{}, withNamespace(testConfig.namespace)); err != nil {
145146
return errors.Errorf("error building operator role: %s", err)
146147
}
147148
fmt.Println("Successfully created the operator Role")
148149

149-
if err := buildKubernetesResourceFromYamlFile(path.Join(roleDir(), "service_account.yaml"), &corev1.ServiceAccount{}, withNamespace(testConfig.namespace)); err != nil {
150+
if err := buildKubernetesResourceFromYamlFile(ctx, path.Join(roleDir(), "service_account.yaml"), &corev1.ServiceAccount{}, withNamespace(testConfig.namespace)); err != nil {
150151
return errors.Errorf("error building operator service account: %s", err)
151152
}
152153
fmt.Println("Successfully created the operator Service Account")
153154

154-
if err := buildKubernetesResourceFromYamlFile(path.Join(roleDir(), "role_binding.yaml"), &rbacv1.RoleBinding{}, withNamespace(testConfig.namespace)); err != nil {
155+
if err := buildKubernetesResourceFromYamlFile(ctx, path.Join(roleDir(), "role_binding.yaml"), &rbacv1.RoleBinding{}, withNamespace(testConfig.namespace)); err != nil {
155156
return errors.Errorf("error building operator role binding: %s", err)
156157
}
157158
fmt.Println("Successfully created the operator Role Binding")
158159

159160
dep := &appsv1.Deployment{}
160161

161-
if err := buildKubernetesResourceFromYamlFile(path.Join(deployDir(), "manager.yaml"),
162+
if err := buildKubernetesResourceFromYamlFile(ctx, path.Join(deployDir(), "manager.yaml"),
162163
dep,
163164
withNamespace(testConfig.namespace),
164165
withOperatorImage(testConfig.operatorImage),
@@ -201,7 +202,7 @@ func hasDeploymentRequiredReplicas(dep *appsv1.Deployment) wait.ConditionFunc {
201202

202203
// buildKubernetesResourceFromYamlFile will create the kubernetes resource defined in yamlFilePath. All of the functional options
203204
// provided will be applied before creation.
204-
func buildKubernetesResourceFromYamlFile(yamlFilePath string, obj client.Object, options ...func(obj runtime.Object)) error {
205+
func buildKubernetesResourceFromYamlFile(ctx *e2eutil.Context, yamlFilePath string, obj client.Object, options ...func(obj runtime.Object)) error {
205206
data, err := ioutil.ReadFile(yamlFilePath)
206207
if err != nil {
207208
return errors.Errorf("error reading file: %s", err)
@@ -215,7 +216,7 @@ func buildKubernetesResourceFromYamlFile(yamlFilePath string, obj client.Object,
215216
opt(obj)
216217
}
217218

218-
return createOrUpdate(obj)
219+
return createOrUpdate(ctx, obj)
219220
}
220221

221222
// marshalRuntimeObjectFromYAMLBytes accepts the bytes of a yaml resource
@@ -228,8 +229,8 @@ func marshalRuntimeObjectFromYAMLBytes(bytes []byte, obj runtime.Object) error {
228229
return json.Unmarshal(jsonBytes, &obj)
229230
}
230231

231-
func createOrUpdate(obj client.Object) error {
232-
if err := e2eutil.TestClient.Create(context.TODO(), obj, &e2eutil.CleanupOptions{}); err != nil {
232+
func createOrUpdate(ctx *e2eutil.Context, obj client.Object) error {
233+
if err := e2eutil.TestClient.Create(context.TODO(), obj, &e2eutil.CleanupOptions{TestContext: ctx}); err != nil {
233234
if apiErrors.IsAlreadyExists(err) {
234235
return e2eutil.TestClient.Update(context.TODO(), obj)
235236
}

0 commit comments

Comments
 (0)