Skip to content

Add validating admission webhook to verify opaque configs - #461

Merged
klueska merged 5 commits into
kubernetes-sigs:mainfrom
klueska:validating-webhook
Aug 21, 2025
Merged

klueska merged 5 commits into
kubernetes-sigs:mainfrom
klueska:validating-webhook

Conversation

@klueska

@klueska klueska commented Aug 12, 2025 •

Copy link
Copy Markdown
Contributor

This is an adaptation of the following from the example DRA driver:
kubernetes-sigs/dra-example-driver#75

It is customized to validate the opaque configs for all of the supported types in the DRA driver for GPUs.

It has also been updated to:

  1. Work across all of the v1, v1beta1, and v1beta2 resource APIs.
  2. Add secret mode and custom issuer support for webhook TLS certificates

Fixes: #461

Example configurations for webhook TLS certificate management

# Example 1: Using cert-manager with self-signed certificates (default)
webhook:
  enabled: true
  tls:
    mode: "cert-manager"
    certManager:
      issuerType: "selfsigned"

---
# Example 2: Using cert-manager with a ClusterIssuer (e.g., Let's Encrypt)
webhook:
  enabled: true
  tls:
    mode: "cert-manager"
    certManager:
      issuerType: "clusterissuer"
      issuerName: "letsencrypt-prod"  # Your ClusterIssuer name
      dnsNames:
        - "webhook.example.com"  # Additional DNS names if needed

---
# Example 3: Using cert-manager with a namespaced Issuer
webhook:
  enabled: true
  tls:
    mode: "cert-manager"
    certManager:
      issuerType: "issuer"
      issuerName: "my-ca-issuer"  # Your Issuer name in the same namespace

---
# Example 4: Using a user-provided secret with TLS certificates
webhook:
  enabled: true
  tls:
    mode: "secret"
    secret:
      name: "my-webhook-tls-secret"  # Secret containing tls.crt and tls.key
      namespace: "default"  # Optional: namespace of the secret (defaults to driver namespace)

---
# Example 5: Using a user-provided secret from a different namespace
webhook:
  enabled: true
  tls:
    mode: "secret"
    secret:
      name: "webhook-tls-secret"
      namespace: "certificates"  # Different namespace where the secret exists

A test script that can be run (from the root of the repo) to make sure things render as expected

#!/bin/bash

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CHART_DIR="$SCRIPT_DIR/deployments/helm/nvidia-dra-driver-gpu"

echo "Testing TLS configuration options..."

# Test 1: Default cert-manager configuration
echo "Test 1: Default cert-manager configuration"
helm template test-release "$CHART_DIR" \
  --set webhook.enabled=true \
  --set webhook.tls.mode=cert-manager \
  --set webhook.tls.certManager.issuerType=selfsigned \
  --set allowDefaultNamespace=true \
  --set resources.gpus.enabled=false \
  --dry-run > /dev/null
echo "✓ Default cert-manager configuration passed"

# Test 2: Cert-manager with ClusterIssuer
echo "Test 2: Cert-manager with ClusterIssuer"
helm template test-release "$CHART_DIR" \
  --set webhook.enabled=true \
  --set webhook.tls.mode=cert-manager \
  --set webhook.tls.certManager.issuerType=clusterissuer \
  --set webhook.tls.certManager.issuerName=letsencrypt-prod \
  --set allowDefaultNamespace=true \
  --set resources.gpus.enabled=false \
  --dry-run > /dev/null
echo "✓ Cert-manager with ClusterIssuer passed"

# Test 3: Secret mode
echo "Test 3: Secret mode"
helm template test-release "$CHART_DIR" \
  --set webhook.enabled=true \
  --set webhook.tls.mode=secret \
  --set webhook.tls.secret.name=my-tls-secret \
  --set allowDefaultNamespace=true \
  --set resources.gpus.enabled=false \
  --dry-run > /dev/null
echo "✓ Secret mode passed"

# Test 4: Secret mode with different namespace
echo "Test 4: Secret mode with different namespace"
helm template test-release "$CHART_DIR" \
  --set webhook.enabled=true \
  --set webhook.tls.mode=secret \
  --set webhook.tls.secret.name=my-tls-secret \
  --set webhook.tls.secret.namespace=certificates \
  --set allowDefaultNamespace=true \
  --set resources.gpus.enabled=false \
  --dry-run > /dev/null
echo "✓ Secret mode with different namespace passed"

# Test 5: Invalid mode (should fail)
echo "Test 5: Invalid mode (should fail)"
if helm template test-release "$CHART_DIR" \
  --set webhook.enabled=true \
  --set webhook.tls.mode=invalid \
  --set allowDefaultNamespace=true \
  --set resources.gpus.enabled=false \
  --dry-run > /dev/null 2>&1; then
  echo "✗ Invalid mode should have failed"
  exit 1
else
  echo "✓ Invalid mode correctly failed"
fi

# Test 6: Missing secret name (should fail)
echo "Test 6: Missing secret name (should fail)"
if helm template test-release "$CHART_DIR" \
  --set webhook.enabled=true \
  --set webhook.tls.mode=secret \
  --set allowDefaultNamespace=true \
  --set resources.gpus.enabled=false \
  --dry-run > /dev/null 2>&1; then
  echo "✗ Missing secret name should have failed"
  exit 1
else
  echo "✓ Missing secret name correctly failed"
fi

# Test 7: Missing issuer name for non-selfsigned (should fail)
echo "Test 7: Missing issuer name for non-selfsigned (should fail)"
if helm template test-release "$CHART_DIR" \
  --set webhook.enabled=true \
  --set webhook.tls.mode=cert-manager \
  --set webhook.tls.certManager.issuerType=clusterissuer \
  --set allowDefaultNamespace=true \
  --set resources.gpus.enabled=false \
  --dry-run > /dev/null 2>&1; then
  echo "✗ Missing issuer name should have failed"
  exit 1
else
  echo "✓ Missing issuer name correctly failed"
fi

echo "All tests passed! ✓"

@copy-pr-bot

copy-pr-bot Bot commented Aug 12, 2025

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@klueska
klueska force-pushed the validating-webhook branch 4 times, most recently from 1e6ea3d to 4610207 Compare August 13, 2025 08:21
@klueska klueska added this to the v25.8.0 milestone Aug 13, 2025
Comment thread cmd/webhook/main.go
contentType := r.Header.Get("Content-Type")
if contentType != "application/json" {
msg := fmt.Sprintf("contentType=%s, expected application/json", contentType)
klog.Error(msg)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's log with info or warning level.

(from the perspective of this request handler here that's not an error -- just an HTTP request coming in that this handler is not supposed to handle)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can look into as followup

Comment thread cmd/webhook/main.go
if r.Body != nil {
data, err := io.ReadAll(r.Body)
if err != nil {
klog.Error(err)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also here: not an error from the handler's perspective

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will change these as an add-on commit, because I tried to keep the diff in the first commit as minimal to kubernetes-sigs/dra-example-driver#75 as possible.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From sync discussion: I am happy with doing any logging-around-HTTP-request-lifecycle consolidations in a follow-up.

Comment thread cmd/webhook/main.go
return
}

klog.V(2).Infof("handling request: %s", body)

@jgehrcke jgehrcke Aug 13, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense to not log the entire request body, but at most N bytes.

For N, I often pick something like 100 or 500 or something like that depending on context -- that shows everything of interest in a sane case. This is for not accidentally logging e.g. a megabyte of data in case something goes pearshaped. 🍐

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include in the follow-up as well?

@klueska klueska self-assigned this Aug 13, 2025
@klueska klueska added the feature issue/PR that proposes a new feature or functionality label Aug 13, 2025
Comment thread cmd/webhook/main_test.go Outdated
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

@@ -0,0 +1,106 @@
/*
* Copyright 2024 NVIDIA CORPORATION.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to use the same license headers as the other newly added files

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was in a commit that merged as part of another PR

@@ -0,0 +1,417 @@
/*
* Copyright 2024 NVIDIA CORPORATION.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as previous

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was in a commit that merged as part of another PR

@@ -0,0 +1,255 @@
/*
* Copyright 2024 NVIDIA CORPORATION.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as previous

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was in a commit that merged as part of another PR

Comment thread pkg/flags/featuregates.go
@@ -0,0 +1,54 @@
/*
* Copyright 2024 NVIDIA CORPORATION.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as previous

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was in a commit that merged as part of another PR

labels:
{{- include "nvidia-dra-driver-gpu.labels" . | nindent 4 }}
spec:
replicas: 1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we make the number of replicas configurable? I think it's good to have redundancy for the webhook especially when running this at scale

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in latest revision

livenessProbe:
failureThreshold: 5
httpGet:
path: /readyz

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious; I wonder where that trailing z convention comes from.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure. I remember it even during my days at google on all of their internal endpoints so I assume its carried over from there.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my mind, the z in readyz is the same as the z in lolz, so it looks a tiny bit silly. But that's ok.

# Additional DNS names for the certificate
dnsNames: []
secret:
# Name of the secret containing tls.crt and tls.key

@jgehrcke jgehrcke Aug 19, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When done properly, there should be documentation about the (various types of) input format(s) expected (for both the public key and the private key files). This is about content encoding as well as key types and strengths.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and about the feature gates and their stages.

@klueska

klueska commented Aug 19, 2025

Copy link
Copy Markdown
Contributor Author

@jgehrcke and @tariq1890 new version pushed and all questions answered. PTAL

@klueska
klueska force-pushed the validating-webhook branch 2 times, most recently from 3670276 to 6db96fd Compare August 20, 2025 21:13
Comment thread cmd/webhook/main.go Outdated
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"

configapi "github.com/NVIDIA/k8s-dra-driver-gpu/api/nvidia.com/resource/v1beta1"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious, why use configapi as the import alias here? Wouldn't resourceapi be more intuitive?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name resourceapi is already taken (4 lines up).
Regardless, I always use nvapi for this, so changing this to that is the right thing to do.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

This is a straight copy of this PR
kubernetes-sigs/dra-example-driver#75, with
minimal changes to make it work in this repo.

Signed-off-by: Kevin Klues <kklues@nvidia.com>
Signed-off-by: Kevin Klues <kklues@nvidia.com>
Signed-off-by: Kevin Klues <kklues@nvidia.com>
Signed-off-by: Kevin Klues <kklues@nvidia.com>
Previously, the webhook only supported cert-manager with a self-signed
issuer for TLS certificate generation and management. With this patch,
users can now provide their own TLS certificates via secret mode or use
custom cert-manager issuers (issuer/clusterissuer) with specified issuer
names.

Signed-off-by: Kevin Klues <kklues@nvidia.com>
@klueska
klueska force-pushed the validating-webhook branch from 6db96fd to 152c683 Compare August 21, 2025 09:22

@jgehrcke jgehrcke left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

As discussed out-of-band, it will be worthwhile 'productionizing' the HTTP request lifecycle (in particular w.r.t. logging and error handling) a bit, and also invest into reference docs for the various input parameters users can provide. We'll do that later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature issue/PR that proposes a new feature or functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants