mirror of
https://github.com/kubevela/kubevela.git
synced 2026-02-14 10:00:06 +00:00
* 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>
174 lines
5.1 KiB
Go
174 lines
5.1 KiB
Go
/*
|
|
Copyright 2024 The KubeVela Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
// Package logging provides structured logging utilities for KubeVela webhooks
|
|
// with focus on request traceability and observability.
|
|
package logging
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/go-logr/logr"
|
|
"sigs.k8s.io/controller-runtime/pkg/log"
|
|
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
|
|
)
|
|
|
|
// Structured logging field keys - consistent across all handlers for observability
|
|
const (
|
|
// Core traceability fields
|
|
FieldRequestID = "requestID" // Unique identifier for request correlation
|
|
FieldOperation = "operation" // Webhook operation (CREATE/UPDATE/DELETE)
|
|
FieldHandler = "handler" // Handler processing the request
|
|
FieldStep = "step" // Current processing step
|
|
FieldDuration = "durationMs" // Operation duration in milliseconds
|
|
|
|
// Resource identification
|
|
FieldName = "name" // Resource name
|
|
FieldNamespace = "namespace" // Resource namespace
|
|
FieldKind = "kind" // Resource kind
|
|
FieldGeneration = "generation" // Resource generation
|
|
|
|
// User context
|
|
FieldUserName = "user" // User making the request
|
|
|
|
// Error tracking
|
|
FieldError = "error" // Error indicator
|
|
FieldSuccess = "success" // Success indicator
|
|
)
|
|
|
|
// contextKey for storing values in context
|
|
type contextKey struct{ name string }
|
|
|
|
var (
|
|
requestIDKey = contextKey{name: "requestID"}
|
|
loggerKey = contextKey{name: "logger"}
|
|
)
|
|
|
|
// Logger wraps logr.Logger with structured logging methods
|
|
type Logger struct {
|
|
logr.Logger
|
|
}
|
|
|
|
// WithValues adds key-value pairs to the logger
|
|
func (l Logger) WithValues(keysAndValues ...interface{}) Logger {
|
|
return Logger{Logger: l.Logger.WithValues(keysAndValues...)}
|
|
}
|
|
|
|
// New creates a new Logger
|
|
func New() Logger {
|
|
return Logger{Logger: log.Log}
|
|
}
|
|
|
|
// WithContext returns a Logger from context or creates a new one
|
|
func WithContext(ctx context.Context) Logger {
|
|
if logger, ok := ctx.Value(loggerKey).(Logger); ok {
|
|
return logger
|
|
}
|
|
return New()
|
|
}
|
|
|
|
// IntoContext stores the Logger in context
|
|
func (l Logger) IntoContext(ctx context.Context) context.Context {
|
|
return context.WithValue(ctx, loggerKey, l)
|
|
}
|
|
|
|
// WithRequestID stores request ID in context
|
|
func WithRequestID(ctx context.Context, requestID string) context.Context {
|
|
if requestID == "" {
|
|
return ctx
|
|
}
|
|
return context.WithValue(ctx, requestIDKey, requestID)
|
|
}
|
|
|
|
// RequestIDFrom retrieves request ID from context
|
|
func RequestIDFrom(ctx context.Context) (string, bool) {
|
|
id, ok := ctx.Value(requestIDKey).(string)
|
|
return id, ok && id != ""
|
|
}
|
|
|
|
// NewHandlerLogger creates a logger for webhook handlers with full request context
|
|
func NewHandlerLogger(ctx context.Context, req admission.Request, handlerName string) Logger {
|
|
logger := New()
|
|
|
|
// Use admission UID as request ID for correlation
|
|
requestID := string(req.UID)
|
|
if rid, ok := RequestIDFrom(ctx); ok && rid != "" {
|
|
requestID = rid
|
|
}
|
|
|
|
// Build structured log with essential fields for observability
|
|
logger = logger.WithValues(
|
|
FieldRequestID, requestID,
|
|
FieldHandler, handlerName,
|
|
FieldOperation, req.Operation,
|
|
FieldKind, req.Kind.Kind,
|
|
FieldName, req.Name,
|
|
FieldNamespace, req.Namespace,
|
|
FieldUserName, req.UserInfo.Username,
|
|
)
|
|
|
|
return logger
|
|
}
|
|
|
|
// Helper methods that return the logger with values added
|
|
// These don't log directly, so the actual logging call site is preserved
|
|
|
|
// WithStep adds a step field to the logger
|
|
func (l Logger) WithStep(step string) Logger {
|
|
return l.WithValues(FieldStep, step)
|
|
}
|
|
|
|
// WithSuccess adds success and duration fields to the logger
|
|
func (l Logger) WithSuccess(success bool, startTime ...time.Time) Logger {
|
|
logger := l.WithValues(FieldSuccess, success)
|
|
if len(startTime) > 0 {
|
|
duration := time.Since(startTime[0])
|
|
logger = logger.WithValues(FieldDuration, duration.Milliseconds())
|
|
}
|
|
return logger
|
|
}
|
|
|
|
// WithError adds error context to the logger
|
|
func (l Logger) WithError(err error) Logger {
|
|
return l.WithValues(FieldError, err.Error(), FieldSuccess, false)
|
|
}
|
|
|
|
// V returns a logger with verbosity level (0=info, 1=debug, 2=trace)
|
|
func (l Logger) V(level int) Logger {
|
|
return Logger{Logger: l.Logger.V(level)}
|
|
}
|
|
|
|
// Debug logs debug message (verbosity 1)
|
|
func (l Logger) Debug(msg string, keysAndValues ...interface{}) {
|
|
l.Logger.V(1).Info(msg, keysAndValues...)
|
|
}
|
|
|
|
// Trace logs trace message (verbosity 2)
|
|
func (l Logger) Trace(msg string, keysAndValues ...interface{}) {
|
|
l.Logger.V(2).Info(msg, keysAndValues...)
|
|
}
|
|
|
|
// Info logs info message
|
|
func (l Logger) Info(msg string, keysAndValues ...interface{}) {
|
|
l.Logger.Info(msg, keysAndValues...)
|
|
}
|
|
|
|
// Error logs error message
|
|
func (l Logger) Error(err error, msg string, keysAndValues ...interface{}) {
|
|
l.Logger.Error(err, msg, keysAndValues...)
|
|
}
|