Skip to content

Commit 4d07047

Browse files
committed
VWC logic now moved to operator code for both OLM and Helm.
Signed-off-by: Aryan <[email protected]>
1 parent 3ff2e94 commit 4d07047

File tree

7 files changed

+248
-48
lines changed

7 files changed

+248
-48
lines changed

bundle/manifests/k8s-nim-operator.clusterserviceversion.yaml

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,18 @@ spec:
13451345
- watch
13461346
- create
13471347
- delete
1348+
- apiGroups:
1349+
- admissionregistration.k8s.io
1350+
resources:
1351+
- validatingwebhookconfigurations
1352+
verbs:
1353+
- get
1354+
- list
1355+
- watch
1356+
- create
1357+
- update
1358+
- patch
1359+
- delete
13481360
- apiGroups:
13491361
- gateway.networking.k8s.io
13501362
resources:
@@ -1357,6 +1369,7 @@ spec:
13571369
- patch
13581370
- update
13591371
- watch
1372+
- admissionregistration.k8s.io
13601373
deployments:
13611374
- name: k8s-nim-operator
13621375
spec:
@@ -1413,6 +1426,8 @@ spec:
14131426
fieldPath: metadata.namespace
14141427
- name: ENABLE_WEBHOOKS
14151428
value: "true"
1429+
- name: OPERATOR_NAME_PREFIX
1430+
value: "k8s-nim-operator"
14161431
image: 'ghcr.io/nvidia/k8s-nim-operator:main'
14171432
imagePullPolicy: Always
14181433
livenessProbe:
@@ -1426,6 +1441,10 @@ spec:
14261441
successThreshold: 1
14271442
timeoutSeconds: 1
14281443
name: manager
1444+
volumeMounts:
1445+
- name: cert
1446+
mountPath: /tmp/k8s-webhook-server/serving-certs
1447+
readOnly: true
14291448
readinessProbe:
14301449
failureThreshold: 3
14311450
httpGet:
@@ -1447,6 +1466,11 @@ spec:
14471466
allowPrivilegeEscalation: false
14481467
terminationMessagePath: /dev/termination-log
14491468
terminationMessagePolicy: File
1469+
volumes:
1470+
- name: cert
1471+
secret:
1472+
secretName: k8s-nim-operator-webhook-server-cert
1473+
defaultMode: 420
14501474
dnsPolicy: ClusterFirst
14511475
imagePullSecrets: []
14521476
restartPolicy: Always
@@ -1469,46 +1493,4 @@ spec:
14691493
- type: MultiNamespace
14701494
supported: false
14711495
- type: AllNamespaces
1472-
supported: true
1473-
webhookdefinitions:
1474-
- type: ValidatingAdmissionWebhook
1475-
admissionReviewVersions:
1476-
- v1
1477-
containerPort: 9443
1478-
targetPort: 9443
1479-
deploymentName: k8s-nim-operator
1480-
failurePolicy: Fail
1481-
generateName: vnimcache-v1alpha1.kb.io
1482-
rules:
1483-
- apiGroups:
1484-
- apps.nvidia.com
1485-
apiVersions:
1486-
- v1alpha1
1487-
operations:
1488-
- CREATE
1489-
- UPDATE
1490-
resources:
1491-
- nimcaches
1492-
sideEffects: None
1493-
webhookPath: /validate-apps-nvidia-com-v1alpha1-nimcache
1494-
- type: ValidatingAdmissionWebhook
1495-
admissionReviewVersions:
1496-
- v1
1497-
containerPort: 9443
1498-
targetPort: 9443
1499-
deploymentName: k8s-nim-operator
1500-
failurePolicy: Fail
1501-
generateName: vnimservice-v1alpha1.kb.io
1502-
rules:
1503-
- apiGroups:
1504-
- apps.nvidia.com
1505-
apiVersions:
1506-
- v1alpha1
1507-
operations:
1508-
- CREATE
1509-
- UPDATE
1510-
resources:
1511-
- nimservices
1512-
sideEffects: None
1513-
webhookPath: /validate-apps-nvidia-com-v1alpha1-nimservice
1514-
1496+
supported: true
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: k8s-nim-operator-webhook-service
5+
labels:
6+
app.kubernetes.io/name: k8s-nim-operator
7+
app.kubernetes.io/instance: nim-operator
8+
control-plane: controller-manager
9+
annotations:
10+
service.beta.openshift.io/serving-cert-secret-name: k8s-nim-operator-webhook-server-cert
11+
spec:
12+
selector:
13+
app.kubernetes.io/name: k8s-nim-operator
14+
app.kubernetes.io/instance: nim-operator
15+
control-plane: controller-manager
16+
ports:
17+
- port: 443
18+
targetPort: 9443
19+
protocol: TCP

cmd/main.go

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package main
1818

1919
import (
20+
"context"
2021
"crypto/tls"
2122
"flag"
2223
"os"
@@ -34,6 +35,7 @@ import (
3435
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3536
_ "k8s.io/client-go/plugin/pkg/client/auth"
3637
ctrl "sigs.k8s.io/controller-runtime"
38+
crclient "sigs.k8s.io/controller-runtime/pkg/client"
3739
"sigs.k8s.io/controller-runtime/pkg/healthz"
3840
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3941
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
@@ -45,7 +47,9 @@ import (
4547

4648
appsv1alpha1 "github.com/NVIDIA/k8s-nim-operator/api/apps/v1alpha1"
4749
"github.com/NVIDIA/k8s-nim-operator/internal/conditions"
50+
"github.com/NVIDIA/k8s-nim-operator/internal/config"
4851
"github.com/NVIDIA/k8s-nim-operator/internal/controller"
52+
"github.com/NVIDIA/k8s-nim-operator/internal/k8sutil"
4953
"github.com/NVIDIA/k8s-nim-operator/internal/render"
5054
webhookappsv1alpha1 "github.com/NVIDIA/k8s-nim-operator/internal/webhook/apps/v1alpha1"
5155
// +kubebuilder:scaffold:imports
@@ -264,17 +268,38 @@ func main() {
264268

265269
// nolint:goconst
266270
// Parse ENABLE_WEBHOOKS environment variable once as a boolean.
267-
var enableWebhooks bool
268271
if val, ok := os.LookupEnv("ENABLE_WEBHOOKS"); ok {
269272
var err error
270-
enableWebhooks, err = strconv.ParseBool(val)
273+
enableWebhooks, err := strconv.ParseBool(val)
271274
if err != nil {
272275
setupLog.Error(err, "invalid value for ENABLE_WEBHOOKS, expected boolean")
273276
os.Exit(1)
274277
}
278+
config.EnableWebhooks = enableWebhooks
279+
}
280+
if val, ok := os.LookupEnv("OPERATOR_NAME_PREFIX"); ok {
281+
config.OperatorNamePrefix = val
282+
}
283+
if val, ok := os.LookupEnv("OPERATOR_NAMESPACE"); ok {
284+
config.OperatorNamespace = val
275285
}
276286

277-
if enableWebhooks {
287+
cfg := ctrl.GetConfigOrDie()
288+
liveClient, err := crclient.New(cfg, crclient.Options{Scheme: scheme})
289+
if err != nil {
290+
setupLog.Error(err, "unable to construct live client")
291+
os.Exit(1)
292+
}
293+
ctx := context.Background()
294+
orch, err := k8sutil.GetOrchestratorType(ctx, liveClient) // uses direct REST calls
295+
if err != nil {
296+
setupLog.Error(err, "failed to detect orchestrator type")
297+
os.Exit(1)
298+
}
299+
config.OrchestratorType = orch
300+
setupLog.Info("detected orchestrator", "type", orch)
301+
302+
if config.EnableWebhooks {
278303
if err := webhookappsv1alpha1.SetupNIMCacheWebhookWithManager(mgr); err != nil {
279304
setupLog.Error(err, "unable to create webhook", "webhook", "NIMCache")
280305
os.Exit(1)
@@ -284,6 +309,17 @@ func main() {
284309
setupLog.Error(err, "unable to create webhook", "webhook", "NIMService")
285310
os.Exit(1)
286311
}
312+
// Set up cluster-level ValidatingWebhookConfiguration.
313+
if err := webhookappsv1alpha1.EnsureValidatingWebhook(
314+
context.TODO(),
315+
mgr.GetAPIReader(),
316+
mgr.GetClient(),
317+
config.OperatorNamespace,
318+
config.OperatorNamePrefix,
319+
); err != nil {
320+
setupLog.Error(err, "unable to ensure ValidatingWebhookConfiguration")
321+
os.Exit(1)
322+
}
287323
}
288324
// +kubebuilder:scaffold:builder
289325

deployments/helm/k8s-nim-operator/templates/deployment.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ spec:
5151
valueFrom:
5252
fieldRef:
5353
fieldPath: metadata.namespace
54+
- name: OPERATOR_NAME_PREFIX
55+
value: {{ include "k8s-nim-operator.fullname" . }}
5456
- name: ENABLE_WEBHOOKS
5557
value: "{{ .Values.operator.admissionController.enabled }}"
5658
livenessProbe:

deployments/helm/k8s-nim-operator/templates/manager-rbac.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,8 +582,10 @@ rules:
582582
- get
583583
- list
584584
- watch
585-
- patch
585+
- create
586586
- update
587+
- patch
588+
- delete
587589
- apiGroups:
588590
- gateway.networking.k8s.io
589591
resources:
@@ -596,7 +598,6 @@ rules:
596598
- patch
597599
- update
598600
- watch
599-
600601
---
601602
apiVersion: rbac.authorization.k8s.io/v1
602603
kind: ClusterRoleBinding

internal/config/config.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package config
2+
3+
import "github.com/NVIDIA/k8s-nim-operator/internal/k8sutil"
4+
5+
var (
6+
EnableWebhooks bool
7+
OperatorNamePrefix string
8+
OperatorNamespace string
9+
OrchestratorType k8sutil.OrchestratorType
10+
)

0 commit comments

Comments
 (0)