Skip to content

Commit 60becc4

Browse files
committed
make accessrequests fail when referencing preemptive clusterrequests
1 parent 7fd267b commit 60becc4

File tree

5 files changed

+54
-0
lines changed

5 files changed

+54
-0
lines changed

api/clusters/v1alpha1/constants/reasons.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ const (
1111
ReasonConfigurationProblem = "ConfigurationProblem"
1212
// ReasonInternalError indicates that something went wrong internally.
1313
ReasonInternalError = "InternalError"
14+
// ReasonPreemptiveRequest indicates that the ClusterRequest is preemptive and AccessRequests referencing it are denied.
15+
ReasonPreemptiveRequest = "PreemptiveRequest"
1416
)

internal/controllers/accessrequest/controller.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ func (r *AccessRequestReconciler) Reconcile(ctx context.Context, req reconcile.R
5353
WithFieldOverride(ctrlutils.STATUS_FIELD_PHASE, "Phase").
5454
WithoutFields(ctrlutils.STATUS_FIELD_CONDITIONS).
5555
WithPhaseUpdateFunc(func(obj *clustersv1alpha1.AccessRequest, rr ReconcileResult) (clustersv1alpha1.RequestPhase, error) {
56+
if rr.Reason == cconst.ReasonPreemptiveRequest {
57+
return clustersv1alpha1.REQUEST_DENIED, nil
58+
}
5659
return clustersv1alpha1.REQUEST_PENDING, nil
5760
}).
5861
Build().
@@ -112,6 +115,11 @@ func (r *AccessRequestReconciler) reconcile(ctx context.Context, req reconcile.R
112115
rr.ReconcileError = errutils.WithReason(fmt.Errorf("unable to get ClusterRequest '%s/%s': %w", cr.Namespace, cr.Name, err), cconst.ReasonPlatformClusterInteractionProblem)
113116
return rr
114117
}
118+
if cr.Spec.Preemptive {
119+
rr.Reason = cconst.ReasonPreemptiveRequest
120+
rr.Message = "The referenced ClusterRequest is preemptive and access cannot be granted."
121+
return rr
122+
}
115123
if cr.Status.Phase != clustersv1alpha1.REQUEST_GRANTED {
116124
rr.ReconcileError = errutils.WithReason(fmt.Errorf("ClusterRequest '%s/%s' is not granted", cr.Namespace, cr.Name), cconst.ReasonInvalidReference)
117125
return rr

internal/controllers/accessrequest/controller_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,21 @@ var _ = Describe("AccessRequest Controller", func() {
145145
Expect(ar.Spec.ClusterRef).To(BeNil())
146146
})
147147

148+
It("should deny the AccessRequest, if it references a preemptive ClusterRequest", func() {
149+
env := testutils.NewEnvironmentBuilder().WithFakeClient(scheme).WithInitObjectPath("testdata", "test-01").WithReconcilerConstructor(arReconciler).Build()
150+
ar := &clustersv1alpha1.AccessRequest{}
151+
Expect(env.Client().Get(env.Ctx, ctrlutils.ObjectKey("mcr-access-p", "bar"), ar)).To(Succeed())
152+
Expect(ar.Labels).ToNot(HaveKey(clustersv1alpha1.ProviderLabel))
153+
Expect(ar.Labels).ToNot(HaveKey(clustersv1alpha1.ProfileLabel))
154+
Expect(ar.Spec.ClusterRef).To(BeNil())
155+
env.ShouldReconcile(testutils.RequestFromObject(ar))
156+
Expect(env.Client().Get(env.Ctx, client.ObjectKeyFromObject(ar), ar)).To(Succeed())
157+
Expect(ar.Status.Phase).To(Equal(clustersv1alpha1.REQUEST_DENIED))
158+
Expect(ar.Status.Reason).To(Equal(cconst.ReasonPreemptiveRequest))
159+
Expect(ar.Status.Message).To(ContainSubstring("preemptive"))
160+
Expect(ar.Labels).ToNot(HaveKey(clustersv1alpha1.ProviderLabel))
161+
Expect(ar.Labels).ToNot(HaveKey(clustersv1alpha1.ProfileLabel))
162+
Expect(ar.Spec.ClusterRef).To(BeNil())
163+
})
164+
148165
})
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apiVersion: clusters.openmcp.cloud/v1alpha1
2+
kind: AccessRequest
3+
metadata:
4+
name: mcr-access-p
5+
namespace: bar
6+
spec:
7+
requestRef:
8+
name: my-cluster-p
9+
namespace: foo
10+
permissions:
11+
- rules:
12+
- apiGroups:
13+
- "*"
14+
resources:
15+
- "*"
16+
verbs:
17+
- "*"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apiVersion: clusters.openmcp.cloud/v1alpha1
2+
kind: ClusterRequest
3+
metadata:
4+
name: my-cluster-p
5+
namespace: foo
6+
spec:
7+
purpose: test
8+
preemptive: true
9+
status:
10+
phase: Granted

0 commit comments

Comments
 (0)