Skip to content

Commit 6f0c2c6

Browse files
committed
Update AdmissionCheck depending on success/failure.
1 parent 9c1c13b commit 6f0c2c6

File tree

14 files changed

+761
-111
lines changed

14 files changed

+761
-111
lines changed

slice/cmd/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,8 @@ func setupControllers(mgr ctrl.Manager, certsReady chan struct{}) {
254254
os.Exit(1)
255255
}
256256

257-
if err := controller.NewWorkloadReconciler(mgr.GetClient()).SetupWithManager(mgr); err != nil {
258-
setupLog.Error(err, "unable to create controller", "controller", "Workload")
257+
if failedCtrl, err := controller.SetupControllers(mgr); err != nil {
258+
setupLog.Error(err, "unable to create controller", "controller", failedCtrl)
259259
os.Exit(1)
260260
}
261261

slice/config/rbac/role.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ kind: ClusterRole
44
metadata:
55
name: manager-role
66
rules:
7+
- apiGroups:
8+
- ""
9+
resources:
10+
- events
11+
verbs:
12+
- create
13+
- patch
14+
- update
15+
- watch
716
- apiGroups:
817
- ""
918
resources:
@@ -22,6 +31,23 @@ rules:
2231
- list
2332
- update
2433
- watch
34+
- apiGroups:
35+
- kueue.x-k8s.io
36+
resources:
37+
- admissionchecks
38+
verbs:
39+
- get
40+
- list
41+
- watch
42+
- apiGroups:
43+
- kueue.x-k8s.io
44+
resources:
45+
- admissionchecks/status
46+
- workloads/status
47+
verbs:
48+
- get
49+
- patch
50+
- update
2551
- apiGroups:
2652
- kueue.x-k8s.io
2753
resources:

slice/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ require (
9999
k8s.io/apiserver v0.33.2 // indirect
100100
k8s.io/component-base v0.33.2 // indirect
101101
k8s.io/component-helpers v0.33.2 // indirect
102-
k8s.io/klog/v2 v2.130.1 // indirect
102+
k8s.io/klog/v2 v2.130.1
103103
k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff // indirect
104104
k8s.io/utils v0.0.0-20250321185631-1f6e0b77f77e
105105
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.31.2 // indirect
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
Copyright The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package controller
18+
19+
import (
20+
"context"
21+
22+
apimeta "k8s.io/apimachinery/pkg/api/meta"
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
"k8s.io/utils/ptr"
25+
ctrl "sigs.k8s.io/controller-runtime"
26+
"sigs.k8s.io/controller-runtime/pkg/client"
27+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
28+
kueue "sigs.k8s.io/kueue/apis/kueue/v1beta1"
29+
)
30+
31+
type AdmissionCheckReconciler struct {
32+
client client.Client
33+
}
34+
35+
var _ reconcile.Reconciler = (*AdmissionCheckReconciler)(nil)
36+
37+
func NewAdmissionCheckReconciler(cl client.Client) *AdmissionCheckReconciler {
38+
return &AdmissionCheckReconciler{
39+
client: cl,
40+
}
41+
}
42+
43+
// +kubebuilder:rbac:groups=kueue.x-k8s.io,resources=admissionchecks,verbs=get;list;watch
44+
// +kubebuilder:rbac:groups=kueue.x-k8s.io,resources=admissionchecks/status,verbs=get;update;patch
45+
46+
func (r *AdmissionCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
47+
ac := &kueue.AdmissionCheck{}
48+
if err := r.client.Get(ctx, req.NamespacedName, ac); err != nil || ac.Spec.ControllerName != SliceControllerName {
49+
return reconcile.Result{}, client.IgnoreNotFound(err)
50+
}
51+
52+
log := ctrl.LoggerFrom(ctx)
53+
log.V(2).Info("Reconcile AdmissionCheck")
54+
55+
currentCondition := ptr.Deref(apimeta.FindStatusCondition(ac.Status.Conditions, kueue.AdmissionCheckActive), metav1.Condition{})
56+
newCondition := metav1.Condition{
57+
Type: kueue.AdmissionCheckActive,
58+
Status: metav1.ConditionTrue,
59+
Reason: "Active",
60+
Message: "The admission check is active",
61+
ObservedGeneration: ac.Generation,
62+
}
63+
64+
if currentCondition.Status != newCondition.Status {
65+
apimeta.SetStatusCondition(&ac.Status.Conditions, newCondition)
66+
return reconcile.Result{}, client.IgnoreNotFound(r.client.Status().Update(ctx, ac))
67+
}
68+
69+
return reconcile.Result{}, nil
70+
}
71+
72+
// SetupWithManager sets up the controller with the Manager.
73+
func (r *AdmissionCheckReconciler) SetupWithManager(mgr ctrl.Manager) error {
74+
return ctrl.NewControllerManagedBy(mgr).
75+
For(&kueue.AdmissionCheck{}).
76+
Named("admissioncheck_controller").
77+
Complete(r)
78+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
Copyright The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package controller
18+
19+
import (
20+
"testing"
21+
22+
"github.com/google/go-cmp/cmp"
23+
corev1 "k8s.io/api/core/v1"
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
"k8s.io/apimachinery/pkg/runtime"
26+
"k8s.io/apimachinery/pkg/types"
27+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
28+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
29+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
30+
kueue "sigs.k8s.io/kueue/apis/kueue/v1beta1"
31+
32+
utiltesting "tpu-slice-controller/internal/util/testing"
33+
)
34+
35+
func TestAdmissionCheckReconciler(t *testing.T) {
36+
baseAdmissionCheckName := "ac"
37+
baseGeneration := int64(1)
38+
baseRequest := types.NamespacedName{Name: baseAdmissionCheckName, Namespace: corev1.NamespaceDefault}
39+
baseAdmissionCheckWrapper := utiltesting.MakeAdmissionCheck(baseAdmissionCheckName).
40+
Generation(baseGeneration).
41+
ControllerName(SliceControllerName)
42+
43+
testCases := map[string]struct {
44+
request types.NamespacedName
45+
admissionCheck *kueue.AdmissionCheck
46+
wantAdmissionChecks []kueue.AdmissionCheck
47+
wantErr error
48+
}{
49+
"unrelated check": {
50+
request: baseRequest,
51+
admissionCheck: baseAdmissionCheckWrapper.Clone().ControllerName("other-controller").Obj(),
52+
wantAdmissionChecks: []kueue.AdmissionCheck{
53+
*baseAdmissionCheckWrapper.Clone().ControllerName("other-controller").Obj(),
54+
},
55+
},
56+
"should set Active status": {
57+
request: baseRequest,
58+
admissionCheck: baseAdmissionCheckWrapper.DeepCopy(),
59+
wantAdmissionChecks: []kueue.AdmissionCheck{
60+
*baseAdmissionCheckWrapper.Clone().
61+
Condition(metav1.Condition{
62+
Type: kueue.AdmissionCheckActive,
63+
Status: metav1.ConditionTrue,
64+
Reason: "Active",
65+
Message: "The admission check is active",
66+
ObservedGeneration: baseGeneration,
67+
}).
68+
Obj(),
69+
},
70+
},
71+
}
72+
for name, tc := range testCases {
73+
t.Run(name, func(t *testing.T) {
74+
scheme := runtime.NewScheme()
75+
utilruntime.Must(kueue.AddToScheme(scheme))
76+
utilruntime.Must(kueue.AddToScheme(scheme))
77+
78+
clientBuilder := fake.NewClientBuilder().WithScheme(scheme)
79+
80+
if tc.admissionCheck != nil {
81+
clientBuilder = clientBuilder.WithObjects(tc.admissionCheck)
82+
}
83+
84+
kClient := clientBuilder.Build()
85+
reconciler := NewAdmissionCheckReconciler(kClient)
86+
87+
ctx, _ := utiltesting.ContextWithLog(t)
88+
89+
_, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: tc.request})
90+
if diff := cmp.Diff(tc.wantErr, err); diff != "" {
91+
t.Errorf("Error after reconcile (-want,+got):\n%s", diff)
92+
}
93+
})
94+
}
95+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Copyright The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package controller
18+
19+
import (
20+
ctrl "sigs.k8s.io/controller-runtime"
21+
)
22+
23+
const (
24+
SliceName = "slice"
25+
WorkloadControllerName = SliceName + "-workload-controller"
26+
)
27+
28+
func SetupControllers(mgr ctrl.Manager) (string, error) {
29+
wlRec := NewWorkloadReconciler(mgr.GetClient(), mgr.GetEventRecorderFor(WorkloadControllerName))
30+
if err := wlRec.SetupWithManager(mgr); err != nil {
31+
return "Workload", err
32+
}
33+
acRec := NewAdmissionCheckReconciler(mgr.GetClient())
34+
if err := acRec.SetupWithManager(mgr); err != nil {
35+
return "AdmissionCheck", err
36+
}
37+
return "", nil
38+
}

0 commit comments

Comments
 (0)