|
| 1 | +/* |
| 2 | + Copyright 2025 The KubeFleet Authors. |
| 3 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + you may not use this file except in compliance with the License. |
| 5 | + You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + Unless required by applicable law or agreed to in writing, software |
| 8 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + See the License for the specific language governing permissions and |
| 11 | + limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +// Package clusterresourceplacementdisruptionbudget provides a validating webhook for the clusterresourceplacementdisruptionbudget custom resource in the KubeFleet API group. |
| 15 | +package clusterresourceplacementdisruptionbudget |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "net/http" |
| 21 | + |
| 22 | + k8serrors "k8s.io/apimachinery/pkg/api/errors" |
| 23 | + "k8s.io/apimachinery/pkg/types" |
| 24 | + "k8s.io/klog/v2" |
| 25 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/manager" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/webhook" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 29 | + |
| 30 | + fleetv1beta1 "github.com/kubefleet-dev/kubefleet/apis/placement/v1beta1" |
| 31 | + "github.com/kubefleet-dev/kubefleet/pkg/utils" |
| 32 | + "github.com/kubefleet-dev/kubefleet/pkg/utils/validator" |
| 33 | +) |
| 34 | + |
| 35 | +var ( |
| 36 | + // ValidationPath is the webhook service path which admission requests are routed to for validating clusterresourceplacementdisruptionbudget resources. |
| 37 | + ValidationPath = fmt.Sprintf(utils.ValidationPathFmt, fleetv1beta1.GroupVersion.Group, fleetv1beta1.GroupVersion.Version, "clusterresourceplacementdisruptionbudget") |
| 38 | +) |
| 39 | + |
| 40 | +type clusterResourcePlacementDisruptionBudgetValidator struct { |
| 41 | + client client.Client |
| 42 | + decoder webhook.AdmissionDecoder |
| 43 | +} |
| 44 | + |
| 45 | +// Add registers the webhook for K8s bulit-in object types. |
| 46 | +func Add(mgr manager.Manager) error { |
| 47 | + hookServer := mgr.GetWebhookServer() |
| 48 | + hookServer.Register(ValidationPath, &webhook.Admission{Handler: &clusterResourcePlacementDisruptionBudgetValidator{mgr.GetClient(), admission.NewDecoder(mgr.GetScheme())}}) |
| 49 | + return nil |
| 50 | +} |
| 51 | + |
| 52 | +// Handle clusterResourcePlacementDisruptionBudgetValidator checks to see if resource override is valid. |
| 53 | +func (v *clusterResourcePlacementDisruptionBudgetValidator) Handle(ctx context.Context, req admission.Request) admission.Response { |
| 54 | + var db fleetv1beta1.ClusterResourcePlacementDisruptionBudget |
| 55 | + klog.V(2).InfoS("Validating webhook handling cluster resource placement disruption budget", "operation", req.Operation, "clusterResourcePlacementDisruptionBudget", req.Name) |
| 56 | + if err := v.decoder.Decode(req, &db); err != nil { |
| 57 | + klog.ErrorS(err, "Failed to decode cluster resource placement disruption budget object for validating fields", "userName", req.UserInfo.Username, "groups", req.UserInfo.Groups, "clusterResourcePlacementDisruptionBudget", req.Name) |
| 58 | + return admission.Errored(http.StatusBadRequest, err) |
| 59 | + } |
| 60 | + |
| 61 | + // Get the corresponding ClusterResourcePlacement object |
| 62 | + var crp fleetv1beta1.ClusterResourcePlacement |
| 63 | + if err := v.client.Get(ctx, types.NamespacedName{Name: db.Name}, &crp); err != nil { |
| 64 | + if k8serrors.IsNotFound(err) { |
| 65 | + klog.V(2).InfoS("The corresponding ClusterResourcePlacement object does not exist", "clusterResourcePlacementDisruptionBudget", db.Name, "clusterResourcePlacement", db.Name) |
| 66 | + return admission.Allowed("Associated clusterResourcePlacement object for clusterResourcePlacementDisruptionBudget is not found") |
| 67 | + } |
| 68 | + return admission.Errored(http.StatusBadRequest, fmt.Errorf("failed to get clusterResourcePlacement %s for clusterResourcePlacementDisruptionBudget %s: %w", db.Name, db.Name, err)) |
| 69 | + } |
| 70 | + |
| 71 | + if err := validator.ValidateClusterResourcePlacementDisruptionBudget(&db, &crp); err != nil { |
| 72 | + klog.V(2).ErrorS(err, "ClusterResourcePlacementDisruptionBudget has invalid fields, request is denied", "operation", req.Operation, "clusterResourcePlacementDisruptionBudget", db.Name) |
| 73 | + return admission.Denied(err.Error()) |
| 74 | + } |
| 75 | + |
| 76 | + klog.V(2).InfoS("ClusterResourcePlacementDisruptionBudget has valid fields", "clusterResourcePlacementDisruptionBudget", db.Name) |
| 77 | + return admission.Allowed("clusterResourcePlacementDisruptionBudget has valid fields") |
| 78 | +} |
0 commit comments