|
| 1 | +/* |
| 2 | +Copyright 2026. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package v1alpha1 |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + agentv1alpha1 "github.com/kagenti/operator/api/v1alpha1" |
| 24 | + "k8s.io/apimachinery/pkg/runtime" |
| 25 | + ctrl "sigs.k8s.io/controller-runtime" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 28 | +) |
| 29 | + |
| 30 | +var agentruntimelog = ctrl.Log.WithName("agentruntime-webhook") |
| 31 | + |
| 32 | +func SetupAgentRuntimeWebhookWithManager(mgr ctrl.Manager) error { |
| 33 | + return ctrl.NewWebhookManagedBy(mgr). |
| 34 | + For(&agentv1alpha1.AgentRuntime{}). |
| 35 | + WithValidator(&AgentRuntimeValidator{Reader: mgr.GetAPIReader()}). |
| 36 | + Complete() |
| 37 | +} |
| 38 | + |
| 39 | +// +kubebuilder:webhook:path=/validate-agent-kagenti-dev-v1alpha1-agentruntime,mutating=false,failurePolicy=fail,sideEffects=None,groups=agent.kagenti.dev,resources=agentruntimes,verbs=create;update,versions=v1alpha1,name=vagentruntime.kb.io,admissionReviewVersions=v1 |
| 40 | + |
| 41 | +type AgentRuntimeValidator struct { |
| 42 | + // Reader is an uncached client for authoritative reads from the API server. |
| 43 | + // Used for duplicate targetRef checks during admission. Nil-safe: the check |
| 44 | + // is skipped when Reader is nil (e.g., in unit tests without a real API server). |
| 45 | + Reader client.Reader |
| 46 | +} |
| 47 | + |
| 48 | +func (v *AgentRuntimeValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { |
| 49 | + rt, ok := obj.(*agentv1alpha1.AgentRuntime) |
| 50 | + if !ok { |
| 51 | + return nil, fmt.Errorf("expected an AgentRuntime but got a %T", obj) |
| 52 | + } |
| 53 | + |
| 54 | + agentruntimelog.Info("validate create", "name", rt.Name) |
| 55 | + |
| 56 | + if err := v.checkDuplicateTargetRef(ctx, rt); err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + |
| 60 | + return nil, nil |
| 61 | +} |
| 62 | + |
| 63 | +func (v *AgentRuntimeValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { |
| 64 | + rt, ok := newObj.(*agentv1alpha1.AgentRuntime) |
| 65 | + if !ok { |
| 66 | + return nil, fmt.Errorf("expected an AgentRuntime but got a %T", newObj) |
| 67 | + } |
| 68 | + |
| 69 | + agentruntimelog.Info("validate update", "name", rt.Name) |
| 70 | + |
| 71 | + if err := v.checkDuplicateTargetRef(ctx, rt); err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + |
| 75 | + return nil, nil |
| 76 | +} |
| 77 | + |
| 78 | +func (v *AgentRuntimeValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { |
| 79 | + rt, ok := obj.(*agentv1alpha1.AgentRuntime) |
| 80 | + if !ok { |
| 81 | + return nil, fmt.Errorf("expected an AgentRuntime but got a %T", obj) |
| 82 | + } |
| 83 | + |
| 84 | + agentruntimelog.Info("validate delete", "name", rt.Name) |
| 85 | + |
| 86 | + return nil, nil |
| 87 | +} |
| 88 | + |
| 89 | +// checkDuplicateTargetRef rejects creation/update if another AgentRuntime already |
| 90 | +// targets the same workload (apiVersion + kind + name) in the same namespace. |
| 91 | +func (v *AgentRuntimeValidator) checkDuplicateTargetRef(ctx context.Context, rt *agentv1alpha1.AgentRuntime) error { |
| 92 | + if v.Reader == nil { |
| 93 | + return nil |
| 94 | + } |
| 95 | + |
| 96 | + ref := rt.Spec.TargetRef |
| 97 | + |
| 98 | + rtList := &agentv1alpha1.AgentRuntimeList{} |
| 99 | + // fail-open: allow creation if we can't verify uniqueness |
| 100 | + if err := v.Reader.List(ctx, rtList, client.InNamespace(rt.Namespace)); err != nil { |
| 101 | + agentruntimelog.Error(err, "failed to list AgentRuntimes for duplicate check") |
| 102 | + return nil |
| 103 | + } |
| 104 | + |
| 105 | + for i := range rtList.Items { |
| 106 | + existing := &rtList.Items[i] |
| 107 | + if existing.Name == rt.Name { |
| 108 | + continue |
| 109 | + } |
| 110 | + if existing.Spec.TargetRef.APIVersion == ref.APIVersion && |
| 111 | + existing.Spec.TargetRef.Kind == ref.Kind && |
| 112 | + existing.Spec.TargetRef.Name == ref.Name { |
| 113 | + return fmt.Errorf( |
| 114 | + "an AgentRuntime already targets %s %s in namespace %s: %s", |
| 115 | + ref.Kind, ref.Name, rt.Namespace, existing.Name, |
| 116 | + ) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return nil |
| 121 | +} |
0 commit comments