Files
kubevela/docs/WEBHOOK_DEBUGGING.md
Ayush Kumar 36f217e258 Feat: webhook reject unknown cr outputs (#6932)
* feat: implement output resource existence validation in component, trait, and policy definitions

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>
Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

feat: add validation tests for ComponentDefinition and TraitDefinition outputs

- Implement tests for ComponentDefinition with non-existent CRDs in outputs, ensuring they are rejected.
- Add tests for valid outputs in ComponentDefinition, confirming acceptance.
- Include tests for mixed valid and non-K8s outputs in ComponentDefinition, verifying they pass validation.
- Test handling of empty outputs in ComponentDefinition, ensuring they are accepted.
- Introduce tests for invalid apiVersion formats in ComponentDefinition, confirming rejection.
- Add tests for TraitDefinition with mixed valid and invalid outputs, ensuring proper rejection.
- Create YAML manifests for valid and invalid ComponentDefinitions and TraitDefinitions to support e2e tests.
- Ensure comprehensive coverage of edge cases in output validation logic.

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>
Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

fix: handle errors in resource validation for component, trait, and policy definitions

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>
Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

fix: improve error handling in Go module tidy and resource validation

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>
Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

feat: add webhook debugging setup and validation tests for ComponentDefinition and TraitDefinition

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>
Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

feat: add VS Code launch configuration for debugging webhook validation

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>
Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

refactor: streamline error handling in Go module tidy and remove obsolete test manifests

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>
Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

feat: add mock context support for CUE template compilation

Signed-off-by: Reetika Malhotra <malhotra.reetika25@gmail.com>
Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>
Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

feat: enhance validation for WorkflowStepDefinition resources and improve output resource checks

Signed-off-by: viskumar <viskumar@guidewire.com>
Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>
Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

feat: implement resource validation for CUE templates and add unit tests

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>
Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

feat: enhance logging and validation for component, policy, and trait definitions

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>
Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

feat: improve error handling and logging in validation handlers for component, policy, trait, and workflow step definitions

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>
Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

Remove testUnknownResource folder from repository

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>
Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

feat: implement structured logging for validation handlers and remove deprecated request_logger

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>
Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

feat: enhance structured logging and error handling in admission validation handlers

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>
Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

feat: improve logging messages in validating handlers for better clarity

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>
Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

feat: refactor logging field definitions for consistency and improve error handling in resource validation

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>
Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

chore: add license header to invalid_resource_check.go and invalid_resource_check_test.go

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>
Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

feat: enhance validation tests for WorkflowStepDefinition and improve error messages

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>
Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

feat: add e2e-test-local target for k3d cluster setup and webhook validation

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>
Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

feat: add webhook configuration for workflow step definitions with validation rules

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

feat: update e2e-test-local configuration and improve Ingress API version compatibility

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

feat: add installation of FluxCD CRDs in pre-hook to prevent webhook validation errors

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

feat: add ValidateResourcesExist feature gate and enhance resource validation in webhook handlers

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

feat: enhance resource validation in e2e tests and improve addon definition checks

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

feat: enhance addon definition detection by using owner references for validation

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

feat: add ValidateResourcesExist feature gate and implement webhook validation for resource existence

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

feat: update Ingress API version to v1 and adjust service references in tests

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

chore: remove webhook test commands and related YAML files from makefiles and tests

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

chore: remove architecture section from webhook debugging guide

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

feat: update webhook setup script with k3d host gateway IP note and improve cluster creation logic

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

* Fix: Correct path in Ingress resource definition in template tests

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

* Chore: add empty line to re-trigger failing workflow

Signed-off-by: Vaibhav Agrawal <vaibhav.agrawal0096@gmail.com>

* Chore: remove space to re-trigger workflow

Signed-off-by: Chaitanya Reddy Onteddu <co@guidewire.com>

---------

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>
Signed-off-by: Vaibhav Agrawal <vaibhav.agrawal0096@gmail.com>
Signed-off-by: Chaitanya Reddy Onteddu <co@guidewire.com>
Co-authored-by: Chaitanya Reddy Onteddu <chaitanyareddy0702@gmail.com>
Co-authored-by: Amit Singh <amisingh@guidewire.com>
2025-09-30 09:30:53 -07:00

6.3 KiB

KubeVela Webhook Debugging Guide

This guide explains how to debug KubeVela webhook validation locally, particularly for the feature that validates ComponentDefinitions, TraitDefinitions, and PolicyDefinitions to ensure they don't reference non-existent CRDs.

Overview

The webhook validation feature checks that CUE templates in definitions only reference Kubernetes resources that exist on the cluster. This prevents runtime errors when non-existent CRDs are referenced.

Prerequisites

  • Docker Desktop or similar container runtime
  • k3d for local Kubernetes clusters
  • VS Code with Go extension
  • kubectl configured
  • openssl for certificate generation

Quick Start

# 1. Complete setup (cluster + CRDs + webhook)
make webhook-debug-setup

# 2. Start VS Code debugger
# Press F5 and select "Debug Webhook Validation"

Detailed Setup Steps

1. Environment Setup

# Create k3d cluster
make k3d-create

# Install KubeVela CRDs
make manifests
kubectl apply -f charts/vela-core/crds/

2. Webhook Certificate Setup

The webhook requires TLS certificates with proper Subject Alternative Names (SANs) for IP addresses.

# Generate certificates and create Kubernetes secret
make webhook-setup

This creates:

  • CA certificate and key
  • Server certificate with IP SANs (127.0.0.1, Docker internal IP, local machine IP)
  • Kubernetes Secret webhook-server-cert in vela-system namespace
  • ValidatingWebhookConfiguration pointing to local debugger

3. Start Debugger in VS Code

VS Code Launch Configuration

If you're using VSCode add this configuration to .vscode/launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug Webhook Validation",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "program": "${workspaceFolder}/cmd/core",
            "args": [
                "--log-debug=true",
                "--metrics-addr=:8080",
                "--enable-leader-election=false",
                "--use-webhook=true",
                "--webhook-port=9445",
                "--webhook-cert-dir=${workspaceFolder}/k8s-webhook-server/serving-certs"
            ],
            "env": {
                "KUBECONFIG": "${env:HOME}/.kube/config",
                "POD_NAMESPACE": "vela-system"
            },
            "showLog": false,
            "console": "integratedTerminal"
        }
    ]
}

Set Breakpoints

Recommended breakpoint locations:

  • pkg/webhook/core.oam.dev/v1beta1/componentdefinition/validating_handler.go - ComponentDefinition handler
  • pkg/webhook/core.oam.dev/v1beta1/traitdefinition/validating_handler.go - TraitDefinition handler
  • pkg/webhook/core.oam.dev/v1beta1/policydefinition/validating_handler.go - PolicyDefinition handler
  • pkg/webhook/core.oam.dev/v1beta1/workflowstepdefinition/workflowstep_validating_handler.go - WorkflowDefinition handler

Launch Debugger

  1. Open VS Code
  2. Press F5 or go to Run → Start Debugging
  3. Select "Debug Webhook Validation" configuration
  4. Wait for webhook server to start (look for message about port 9445)

The debugger configuration includes:

  • --use-webhook=true - Enables webhook server
  • --webhook-port=9445 - Port for webhook server
  • --webhook-cert-dir - Path to certificates
  • POD_NAMESPACE=vela-system - Required for finding the secret

Make Targets Reference

Target Description
make webhook-help Show webhook debugging help
make webhook-debug-setup Complete setup (cluster + CRDs + webhook)
make k3d-create Create k3d cluster
make k3d-delete Delete k3d cluster
make webhook-setup Setup certificates and webhook configuration
make webhook-clean Clean up webhook environment

Troubleshooting

Connection Refused Error

If you get "connection refused" errors:

  1. Ensure the debugger is running in VS Code
  2. Check that port 9445 is not blocked by firewall
  3. Verify the webhook server started (check VS Code console)

TLS Certificate Errors

If you get certificate validation errors:

  1. Regenerate certificates: make webhook-setup
  2. Restart the debugger
  3. Ensure IP addresses in certificates match your setup

Webhook Not Triggering

If the webhook doesn't trigger:

  1. Check ValidatingWebhookConfiguration: kubectl get validatingwebhookconfiguration
  2. Verify the webhook URL matches your debugger's IP
  3. Check namespace is correct (vela-system)

Secret Not Found

If you see "Wait webhook secret" messages:

  1. Ensure the secret exists: kubectl get secret webhook-server-cert -n vela-system
  2. Recreate if needed: make webhook-setup

How It Works

  1. Certificate Generation: Creates TLS certificates with proper SANs for local IPs
  2. Secret Creation: Stores certificates in Kubernetes secret
  3. Webhook Configuration: Creates ValidatingWebhookConfiguration pointing to local debugger
  4. Debugger Startup: VS Code starts the webhook server on port 9445
  5. Validation: When definitions are applied, Kubernetes calls the webhook
  6. Debugging: Breakpoints allow stepping through validation logic

Files and Components

  • Script: hack/debug-webhook-setup.sh - Main setup script
  • Makefile: makefiles/develop.mk - Make targets for debugging
  • VS Code Config: .vscode/launch.json - Debugger configuration
  • Test Files: test/webhook-*.yaml - Test manifests
  • Validation Logic: pkg/webhook/utils/utils.go - Core validation implementation
  • Handlers: pkg/webhook/core.oam.dev/v1beta1/*/validating_handler.go - Resource handlers

Clean Up

# Clean up webhook setup
make webhook-clean

# Delete k3d cluster
make k3d-delete

Tips

  1. Always start the debugger before testing - The webhook configuration points to your local machine
  2. Use breakpoints wisely - Too many breakpoints can cause timeouts
  3. Check logs - VS Code Debug Console shows detailed logs
  4. Test both valid and invalid cases - Ensures validation works correctly
  5. Keep certificates updated - Regenerate if IPs change