Skip to content

Commit 304dcc7

Browse files
authored
fix: authorization controller triggers deletion clusteradmin (#125)
1 parent 847b6e7 commit 304dcc7

File tree

5 files changed

+138
-0
lines changed

5 files changed

+138
-0
lines changed

internal/controller/core/authorization/controller.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,28 @@ func (ar *AuthorizationReconciler) reconcile(ctx context.Context, req ctrl.Reque
144144
return componentutils.ReconcileResult[*openmcpv1alpha1.Authorization]{Component: authz, Conditions: authorizationConditions(true, cconst.ReasonDeletionWaitingForDependingComponents, fmt.Sprintf("Deletion is waiting for the following dependencies to be removed: [%s]", depString)), Result: ctrl.Result{RequeueAfter: 60 * time.Second}}
145145
}
146146

147+
// If there is a ClusterAdmin resource with the same name and namespace as the Authorization, we need to delete it first and wait for it to be removed before we can delete the Authorization.
148+
log.Info("Deleting Cluster Admin resources")
149+
clusterAdmin := &openmcpv1alpha1.ClusterAdmin{}
150+
if err = ar.Client.Get(ctx, client.ObjectKey{Name: authz.Name, Namespace: authz.Namespace}, clusterAdmin); err != nil {
151+
if !apierrors.IsNotFound(err) {
152+
log.Error(err, "error fetching ClusterAdmin resource")
153+
return componentutils.ReconcileResult[*openmcpv1alpha1.Authorization]{Component: authz, ReconcileError: openmcperrors.WithReason(fmt.Errorf("error fetching ClusterAdmin resource: %w", err), cconst.ReasonCrateClusterInteractionProblem)}
154+
}
155+
} else {
156+
if !clusterAdmin.DeletionTimestamp.IsZero() {
157+
if err = ar.Client.Delete(ctx, clusterAdmin); err != nil {
158+
if !apierrors.IsNotFound(err) {
159+
log.Error(err, "error deleting ClusterAdmin resource")
160+
return componentutils.ReconcileResult[*openmcpv1alpha1.Authorization]{Component: authz, ReconcileError: openmcperrors.WithReason(fmt.Errorf("error deleting ClusterAdmin resource: %w", err), cconst.ReasonCrateClusterInteractionProblem)}
161+
}
162+
}
163+
}
164+
165+
log.Info("Deletion is waiting for the ClusterAdmin to be removed")
166+
return componentutils.ReconcileResult[*openmcpv1alpha1.Authorization]{Component: authz, Conditions: authorizationConditions(true, cconst.ReasonDeletionWaitingForDependingComponents, "Deletion is waiting for the ClusterAdmin to be removed"), Result: ctrl.Result{RequeueAfter: 60 * time.Second}}
167+
}
168+
147169
log.Info("Deleting Authorization")
148170
if err = ar.deleteAuthorization(ctx, apiServerClient); err != nil {
149171
log.Error(err, "error deleting authorization resources")

internal/controller/core/authorization/controller_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,53 @@ var _ = Describe("CO-1153 Authorization Controller", func() {
10321032
}))
10331033
})
10341034

1035+
It("should delete the corresponding ClusterAdmin resource when the Authorization is deleted", func() {
1036+
var err error
1037+
env := testEnvWithAPIServerAccess("testdata", "test-09")
1038+
1039+
authz := &openmcpv1alpha1.Authorization{}
1040+
err = env.Client(testutils.CrateCluster).Get(env.Ctx, types.NamespacedName{Name: "test", Namespace: "test"}, authz)
1041+
Expect(err).ToNot(HaveOccurred())
1042+
1043+
req := testing.RequestFromObject(authz)
1044+
_ = env.ShouldReconcile(authzReconciler, req)
1045+
1046+
err = env.Client(testutils.CrateCluster).Get(env.Ctx, client.ObjectKeyFromObject(authz), authz)
1047+
Expect(err).ToNot(HaveOccurred())
1048+
Expect(authz.Status.Conditions).To(ContainElements(
1049+
MatchComponentCondition(openmcpv1alpha1.ComponentCondition{
1050+
Type: openmcpv1alpha1.AuthorizationComponent.ReconciliationCondition(),
1051+
Status: openmcpv1alpha1.ComponentConditionStatusTrue,
1052+
}),
1053+
))
1054+
1055+
clusterAdmin := &openmcpv1alpha1.ClusterAdmin{}
1056+
err = env.Client(testutils.CrateCluster).Get(env.Ctx, types.NamespacedName{Name: "test", Namespace: "test"}, clusterAdmin)
1057+
Expect(err).ToNot(HaveOccurred())
1058+
1059+
err = env.Client(testutils.CrateCluster).Delete(env.Ctx, authz)
1060+
Expect(err).ToNot(HaveOccurred())
1061+
1062+
req = testing.RequestFromObject(authz)
1063+
_ = env.ShouldReconcile(authzReconciler, req)
1064+
1065+
err = env.Client(testutils.CrateCluster).Get(env.Ctx, types.NamespacedName{Name: "test", Namespace: "test"}, authz)
1066+
Expect(err).ToNot(HaveOccurred())
1067+
1068+
err = env.Client(testutils.CrateCluster).Delete(env.Ctx, clusterAdmin)
1069+
Expect(err).ToNot(HaveOccurred())
1070+
1071+
_ = env.ShouldReconcile(authzReconciler, req)
1072+
1073+
err = env.Client(testutils.CrateCluster).Get(env.Ctx, types.NamespacedName{Name: "test", Namespace: "test"}, authz)
1074+
Expect(err).To(HaveOccurred())
1075+
Expect(errors.IsNotFound(err)).To(BeTrue())
1076+
1077+
err = env.Client(testutils.CrateCluster).Get(env.Ctx, types.NamespacedName{Name: "test", Namespace: "test"}, clusterAdmin)
1078+
Expect(err).To(HaveOccurred())
1079+
Expect(errors.IsNotFound(err)).To(BeTrue())
1080+
})
1081+
10351082
})
10361083

10371084
func verifyStandardClusterRole(role *rbacv1.ClusterRole) {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
apiVersion: core.openmcp.cloud/v1alpha1
2+
kind: APIServer
3+
metadata:
4+
name: test
5+
namespace: test
6+
labels:
7+
"openmcp.cloud/mcp-generation": "1"
8+
spec:
9+
desiredRegion:
10+
direction: central
11+
name: europe
12+
type: GardenerDedicated
13+
status:
14+
conditions:
15+
- lastTransitionTime: "2024-05-22T08:23:47Z"
16+
status: "True"
17+
type: apiServerHealthy
18+
observedGenerations:
19+
internalConfiguration: -1
20+
managedControlPlane: 1
21+
resource: 0
22+
adminAccess:
23+
creationTimestamp: "2024-05-22T08:23:47Z"
24+
expirationTimestamp: "2024-11-18T08:23:47Z"
25+
kubeconfig: |
26+
apiVersion: v1
27+
clusters:
28+
- name: apiserver
29+
cluster:
30+
server: https://apiserver.dummy
31+
certificate-authority-data: ZHVtbXkK
32+
contexts:
33+
- name: apiserver
34+
context:
35+
cluster: apiserver
36+
user: apiserver
37+
current-context: apiserver
38+
users:
39+
- name: apiserver
40+
user:
41+
client-certificate-data: ZHVtbXkK
42+
client-key-data: ZHVtbXkK
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apiVersion: core.openmcp.cloud/v1alpha1
2+
kind: Authorization
3+
metadata:
4+
name: test
5+
namespace: test
6+
labels:
7+
"openmcp.cloud/mcp-generation": "1"
8+
spec:
9+
roleBindings:
10+
- role: admin
11+
subjects:
12+
- kind: User
13+
name: admin
14+
- role: admin
15+
subjects:
16+
- kind: ServiceAccount
17+
name: pipeline
18+
namespace: automate
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apiVersion: core.openmcp.cloud/v1alpha1
2+
kind: ClusterAdmin
3+
metadata:
4+
name: test
5+
namespace: test
6+
spec:
7+
subjects:
8+
- kind: User
9+
name: admin

0 commit comments

Comments
 (0)