Skip to content

Commit ce75c5a

Browse files
committed
Validating webhooks created for NIMCache and NIMService: ValidateUpdate and ValidateCreate. Helm deployment configured as well. Addressed linting, public functions, and git issues.
Signed-off-by: Aryan <gorwadearyan@gmail.com>
1 parent cb182d6 commit ce75c5a

27 files changed

Lines changed: 2372 additions & 2 deletions

PROJECT

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
domain: nvidia.com
66
layout:
77
- go.kubebuilder.io/v4
8+
multigroup: true
89
projectName: k8s-nim-operator
910
repo: github.com/NVIDIA/k8s-nim-operator
1011
resources:
@@ -17,6 +18,9 @@ resources:
1718
kind: NIMService
1819
path: github.com/NVIDIA/k8s-nim-operator/api/apps/v1alpha1
1920
version: v1alpha1
21+
webhooks:
22+
validation: true
23+
webhookVersion: v1
2024
- api:
2125
crdVersion: v1
2226
namespaced: true
@@ -26,6 +30,9 @@ resources:
2630
kind: NIMCache
2731
path: github.com/NVIDIA/k8s-nim-operator/api/apps/v1alpha1
2832
version: v1alpha1
33+
webhooks:
34+
validation: true
35+
webhookVersion: v1
2936
- api:
3037
crdVersion: v1
3138
namespaced: true

cmd/main.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"crypto/tls"
2121
"flag"
2222
"os"
23+
"strconv"
2324

2425
kservev1beta1 "github.com/kserve/kserve/pkg/apis/serving/v1beta1"
2526
monitoring "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
@@ -47,6 +48,7 @@ import (
4748
"github.com/NVIDIA/k8s-nim-operator/internal/controller/platform/kserve"
4849
"github.com/NVIDIA/k8s-nim-operator/internal/controller/platform/standalone"
4950
"github.com/NVIDIA/k8s-nim-operator/internal/render"
51+
webhookappsv1alpha1 "github.com/NVIDIA/k8s-nim-operator/internal/webhook/apps/v1alpha1"
5052
// +kubebuilder:scaffold:imports
5153
)
5254

@@ -256,6 +258,29 @@ func main() {
256258
os.Exit(1)
257259
}
258260

261+
// nolint:goconst
262+
// Parse ENABLE_WEBHOOKS environment variable once as a boolean.
263+
var enableWebhooks bool
264+
if val, ok := os.LookupEnv("ENABLE_WEBHOOKS"); ok {
265+
var err error
266+
enableWebhooks, err = strconv.ParseBool(val)
267+
if err != nil {
268+
setupLog.Error(err, "invalid value for ENABLE_WEBHOOKS, expected boolean")
269+
os.Exit(1)
270+
}
271+
}
272+
273+
if enableWebhooks {
274+
if err := webhookappsv1alpha1.SetupNIMCacheWebhookWithManager(mgr); err != nil {
275+
setupLog.Error(err, "unable to create webhook", "webhook", "NIMCache")
276+
os.Exit(1)
277+
}
278+
279+
if err := webhookappsv1alpha1.SetupNIMServiceWebhookWithManager(mgr); err != nil {
280+
setupLog.Error(err, "unable to create webhook", "webhook", "NIMService")
281+
os.Exit(1)
282+
}
283+
}
259284
// +kubebuilder:scaffold:builder
260285

261286
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# The following manifests contain a self-signed issuer CR and a metrics certificate CR.
2+
# More document can be found at https://docs.cert-manager.io
3+
apiVersion: cert-manager.io/v1
4+
kind: Certificate
5+
metadata:
6+
labels:
7+
app.kubernetes.io/name: k8s-nim-operator
8+
app.kubernetes.io/managed-by: kustomize
9+
name: metrics-certs # this name should match the one appeared in kustomizeconfig.yaml
10+
namespace: system
11+
spec:
12+
dnsNames:
13+
# SERVICE_NAME and SERVICE_NAMESPACE will be substituted by kustomize
14+
# replacements in the config/default/kustomization.yaml file.
15+
- SERVICE_NAME.SERVICE_NAMESPACE.svc
16+
- SERVICE_NAME.SERVICE_NAMESPACE.svc.cluster.local
17+
issuerRef:
18+
kind: Issuer
19+
name: selfsigned-issuer
20+
secretName: metrics-server-cert
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# The following manifests contain a self-signed issuer CR and a certificate CR.
2+
# More document can be found at https://docs.cert-manager.io
3+
apiVersion: cert-manager.io/v1
4+
kind: Certificate
5+
metadata:
6+
labels:
7+
app.kubernetes.io/name: k8s-nim-operator
8+
app.kubernetes.io/managed-by: kustomize
9+
name: serving-cert # this name should match the one appeared in kustomizeconfig.yaml
10+
namespace: system
11+
spec:
12+
# SERVICE_NAME and SERVICE_NAMESPACE will be substituted by kustomize
13+
# replacements in the config/default/kustomization.yaml file.
14+
dnsNames:
15+
- SERVICE_NAME.SERVICE_NAMESPACE.svc
16+
- SERVICE_NAME.SERVICE_NAMESPACE.svc.cluster.local
17+
issuerRef:
18+
kind: Issuer
19+
name: selfsigned-issuer
20+
secretName: webhook-server-cert

config/certmanager/issuer.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# The following manifest contains a self-signed issuer CR.
2+
# More information can be found at https://docs.cert-manager.io
3+
# WARNING: Targets CertManager v1.0. Check https://cert-manager.io/docs/installation/upgrading/ for breaking changes.
4+
apiVersion: cert-manager.io/v1
5+
kind: Issuer
6+
metadata:
7+
labels:
8+
app.kubernetes.io/name: k8s-nim-operator
9+
app.kubernetes.io/managed-by: kustomize
10+
name: selfsigned-issuer
11+
namespace: system
12+
spec:
13+
selfSigned: {}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
resources:
2+
- issuer.yaml
3+
- certificate-webhook.yaml
4+
- certificate-metrics.yaml
5+
6+
configurations:
7+
- kustomizeconfig.yaml
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# This configuration is for teaching kustomize how to update name ref substitution
2+
nameReference:
3+
- kind: Issuer
4+
group: cert-manager.io
5+
fieldSpecs:
6+
- kind: Certificate
7+
group: cert-manager.io
8+
path: spec/issuerRef/name
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# This patch ensures the webhook certificates are properly mounted in the manager container.
2+
# It configures the necessary arguments, volumes, volume mounts, and container ports.
3+
4+
# Add the --webhook-cert-path argument for configuring the webhook certificate path
5+
- op: add
6+
path: /spec/template/spec/containers/0/args/-
7+
value: --webhook-cert-path=/tmp/k8s-webhook-server/serving-certs
8+
9+
# Add the volumeMount for the webhook certificates
10+
- op: add
11+
path: /spec/template/spec/containers/0/volumeMounts/-
12+
value:
13+
mountPath: /tmp/k8s-webhook-server/serving-certs
14+
name: webhook-certs
15+
readOnly: true
16+
17+
# Add the port configuration for the webhook server
18+
- op: add
19+
path: /spec/template/spec/containers/0/ports/-
20+
value:
21+
containerPort: 9443
22+
name: webhook-server
23+
protocol: TCP
24+
25+
# Add the volume configuration for the webhook certificates
26+
- op: add
27+
path: /spec/template/spec/volumes/-
28+
value:
29+
name: webhook-certs
30+
secret:
31+
secretName: webhook-server-cert

config/manager/kustomization.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ kind: Kustomization
55
images:
66
- name: controller
77
newName: nvcr.io/nvidia/cloud-native/nim-operator
8-
newTag: v1.0.0
8+
newTag: v1.0.0
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# This NetworkPolicy allows ingress traffic to your webhook server running
2+
# as part of the controller-manager from specific namespaces and pods. CR(s) which uses webhooks
3+
# will only work when applied in namespaces labeled with 'webhook: enabled'
4+
apiVersion: networking.k8s.io/v1
5+
kind: NetworkPolicy
6+
metadata:
7+
labels:
8+
app.kubernetes.io/name: k8s-nim-operator
9+
app.kubernetes.io/managed-by: kustomize
10+
name: allow-webhook-traffic
11+
namespace: system
12+
spec:
13+
podSelector:
14+
matchLabels:
15+
control-plane: controller-manager
16+
app.kubernetes.io/name: k8s-nim-operator
17+
policyTypes:
18+
- Ingress
19+
ingress:
20+
# This allows ingress traffic from any namespace with the label webhook: enabled
21+
- from:
22+
- namespaceSelector:
23+
matchLabels:
24+
webhook: enabled # Only from namespaces with this label
25+
ports:
26+
- port: 443
27+
protocol: TCP

0 commit comments

Comments
 (0)