Skip to content

Commit cf51f5f

Browse files
committed
Update AdmissionCheck depending on success/failure.
1 parent 33659e5 commit cf51f5f

File tree

13 files changed

+393
-31
lines changed

13 files changed

+393
-31
lines changed

slice/cmd/main.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ import (
2020
"crypto/tls"
2121
"errors"
2222
"flag"
23-
"net/http"
24-
"os"
25-
"path/filepath"
26-
2723
"k8s.io/apimachinery/pkg/runtime"
2824
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2925
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
26+
"net/http"
27+
"os"
28+
"path/filepath"
3029
ctrl "sigs.k8s.io/controller-runtime"
3130
"sigs.k8s.io/controller-runtime/pkg/certwatcher"
3231
"sigs.k8s.io/controller-runtime/pkg/healthz"
@@ -254,8 +253,8 @@ func setupControllers(mgr ctrl.Manager, certsReady chan struct{}) {
254253
os.Exit(1)
255254
}
256255

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

slice/config/dev/manager_config_patch.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
value: --zap-devel
55
- op: add
66
path: /spec/template/spec/containers/0/args/0
7-
value: --zap-log-level=3
7+
value: --zap-log-level=10

slice/config/manager/manager.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ spec:
8787
# More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
8888
resources:
8989
limits:
90-
cpu: 500m
91-
memory: 128Mi
90+
cpu: "1"
91+
memory: 512Mi
9292
requests:
93-
cpu: 10m
94-
memory: 64Mi
93+
cpu: 500m
94+
memory: 256Mi
9595
volumeMounts: []
9696
volumes: []
9797
serviceAccountName: controller-manager

slice/config/rbac/role.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,23 @@ rules:
2222
- list
2323
- update
2424
- watch
25+
- apiGroups:
26+
- kueue.x-k8s.io
27+
resources:
28+
- admissionchecks
29+
verbs:
30+
- get
31+
- list
32+
- watch
33+
- apiGroups:
34+
- kueue.x-k8s.io
35+
resources:
36+
- admissionchecks/status
37+
- workloads/status
38+
verbs:
39+
- get
40+
- patch
41+
- update
2542
- apiGroups:
2643
- kueue.x-k8s.io
2744
resources:
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
apimeta "k8s.io/apimachinery/pkg/api/meta"
22+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
"k8s.io/utils/ptr"
24+
ctrl "sigs.k8s.io/controller-runtime"
25+
"sigs.k8s.io/controller-runtime/pkg/client"
26+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
27+
kueue "sigs.k8s.io/kueue/apis/kueue/v1beta1"
28+
)
29+
30+
type AdmissionCheckReconciler struct {
31+
client client.Client
32+
}
33+
34+
var _ reconcile.Reconciler = (*AdmissionCheckReconciler)(nil)
35+
36+
func NewAdmissionCheckReconciler(cl client.Client) *AdmissionCheckReconciler {
37+
return &AdmissionCheckReconciler{
38+
client: cl,
39+
}
40+
}
41+
42+
// +kubebuilder:rbac:groups=kueue.x-k8s.io,resources=admissionchecks,verbs=get;list;watch
43+
// +kubebuilder:rbac:groups=kueue.x-k8s.io,resources=admissionchecks/status,verbs=get;update;patch
44+
45+
func (r *AdmissionCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
46+
ac := &kueue.AdmissionCheck{}
47+
if err := r.client.Get(ctx, req.NamespacedName, ac); err != nil || ac.Spec.ControllerName != SliceControllerName {
48+
return reconcile.Result{}, client.IgnoreNotFound(err)
49+
}
50+
51+
log := ctrl.LoggerFrom(ctx)
52+
log.V(2).Info("Reconcile AdmissionCheck")
53+
54+
currentCondition := ptr.Deref(apimeta.FindStatusCondition(ac.Status.Conditions, kueue.AdmissionCheckActive), metav1.Condition{})
55+
newCondition := metav1.Condition{
56+
Type: kueue.AdmissionCheckActive,
57+
Status: metav1.ConditionTrue,
58+
Reason: "Active",
59+
Message: "The admission check is active",
60+
ObservedGeneration: ac.Generation,
61+
}
62+
63+
if currentCondition.Status != newCondition.Status {
64+
apimeta.SetStatusCondition(&ac.Status.Conditions, newCondition)
65+
return reconcile.Result{}, client.IgnoreNotFound(r.client.Status().Update(ctx, ac))
66+
}
67+
68+
return reconcile.Result{}, nil
69+
}
70+
71+
// SetupWithManager sets up the controller with the Manager.
72+
func (r *AdmissionCheckReconciler) SetupWithManager(mgr ctrl.Manager) error {
73+
return ctrl.NewControllerManagedBy(mgr).
74+
For(&kueue.AdmissionCheck{}).
75+
Named("admissioncheck_controller").
76+
Complete(r)
77+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
"github.com/google/go-cmp/cmp"
21+
corev1 "k8s.io/api/core/v1"
22+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
"k8s.io/apimachinery/pkg/runtime"
24+
"k8s.io/apimachinery/pkg/types"
25+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
26+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
27+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
28+
kueue "sigs.k8s.io/kueue/apis/kueue/v1beta1"
29+
"testing"
30+
utiltesting "tpu-slice-controller/internal/util/testing"
31+
)
32+
33+
func TestAdmissionCheckReconciler(t *testing.T) {
34+
baseAdmissionCheckName := "ac"
35+
baseGeneration := int64(1)
36+
baseRequest := types.NamespacedName{Name: baseAdmissionCheckName, Namespace: corev1.NamespaceDefault}
37+
baseAdmissionCheckWrapper := utiltesting.MakeAdmissionCheck(baseAdmissionCheckName).
38+
Generation(baseGeneration).
39+
ControllerName(SliceControllerName)
40+
41+
testCases := map[string]struct {
42+
request types.NamespacedName
43+
admissionCheck *kueue.AdmissionCheck
44+
wantAdmissionChecks []kueue.AdmissionCheck
45+
wantErr error
46+
}{
47+
"unrelated check": {
48+
request: baseRequest,
49+
admissionCheck: baseAdmissionCheckWrapper.Clone().ControllerName("other-controller").Obj(),
50+
wantAdmissionChecks: []kueue.AdmissionCheck{
51+
*baseAdmissionCheckWrapper.Clone().ControllerName("other-controller").Obj(),
52+
},
53+
},
54+
"should set Active status": {
55+
request: baseRequest,
56+
admissionCheck: baseAdmissionCheckWrapper.DeepCopy(),
57+
wantAdmissionChecks: []kueue.AdmissionCheck{
58+
*baseAdmissionCheckWrapper.Clone().
59+
Condition(metav1.Condition{
60+
Type: kueue.AdmissionCheckActive,
61+
Status: metav1.ConditionTrue,
62+
Reason: "Active",
63+
Message: "The admission check is active",
64+
ObservedGeneration: baseGeneration,
65+
}).
66+
Obj(),
67+
},
68+
},
69+
}
70+
for name, tc := range testCases {
71+
t.Run(name, func(t *testing.T) {
72+
scheme := runtime.NewScheme()
73+
utilruntime.Must(kueue.AddToScheme(scheme))
74+
utilruntime.Must(kueue.AddToScheme(scheme))
75+
76+
clientBuilder := fake.NewClientBuilder().WithScheme(scheme)
77+
78+
if tc.admissionCheck != nil {
79+
clientBuilder = clientBuilder.WithObjects(tc.admissionCheck)
80+
}
81+
82+
kClient := clientBuilder.Build()
83+
reconciler := NewAdmissionCheckReconciler(kClient)
84+
85+
ctx, _ := utiltesting.ContextWithLog(t)
86+
87+
_, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: tc.request})
88+
if diff := cmp.Diff(tc.wantErr, err); diff != "" {
89+
t.Errorf("Error after reconcile (-want,+got):\n%s", diff)
90+
}
91+
})
92+
}
93+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
func SetupControllers(mgr ctrl.Manager) (string, error) {
24+
wlRec := NewWorkloadReconciler(mgr.GetClient())
25+
if err := wlRec.SetupWithManager(mgr); err != nil {
26+
return "Workload", err
27+
}
28+
acRec := NewAdmissionCheckReconciler(mgr.GetClient())
29+
if err := acRec.SetupWithManager(mgr); err != nil {
30+
return "AdmissionCheck", err
31+
}
32+
return "", nil
33+
}

0 commit comments

Comments
 (0)