|
| 1 | +// Copyright 2023 Google LLC |
| 2 | +// |
| 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 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package webhook |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + |
| 21 | + "github.com/google/kube-startup-cpu-boost/api/v1alpha1" |
| 22 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 23 | + "k8s.io/apimachinery/pkg/runtime" |
| 24 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 25 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 26 | + "k8s.io/klog/v2" |
| 27 | + ctrl "sigs.k8s.io/controller-runtime" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/webhook" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 30 | +) |
| 31 | + |
| 32 | +type StartupCPUBoostWebhook struct{} |
| 33 | + |
| 34 | +var _ webhook.CustomValidator = &StartupCPUBoostWebhook{} |
| 35 | + |
| 36 | +func setupWebhookForStartupCPUBoost(mgr ctrl.Manager) error { |
| 37 | + return ctrl.NewWebhookManagedBy(mgr). |
| 38 | + For(&v1alpha1.StartupCPUBoost{}). |
| 39 | + WithValidator(&StartupCPUBoostWebhook{}). |
| 40 | + Complete() |
| 41 | +} |
| 42 | + |
| 43 | +// +kubebuilder:webhook:path=/validate-autoscaling-x-k8s-io-v1alpha1-startupcpuboost,mutating=false,failurePolicy=fail,sideEffects=None,groups=autoscaling.x-k8s.io,resources=startupcpuboosts,verbs=create;update,versions=v1alpha1,name=vstartupcpuboost.autoscaling.x-k8s.io,admissionReviewVersions=v1 |
| 44 | + |
| 45 | +// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type |
| 46 | +func (w *StartupCPUBoostWebhook) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { |
| 47 | + boost := obj.(*v1alpha1.StartupCPUBoost) |
| 48 | + log := ctrl.LoggerFrom(ctx).WithName("startupcpuboost-webhook") |
| 49 | + log.V(5).Info("validating create", "startupcpuboost", klog.KObj(boost)) |
| 50 | + return nil, validate(boost) |
| 51 | +} |
| 52 | + |
| 53 | +// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type |
| 54 | +func (w *StartupCPUBoostWebhook) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { |
| 55 | + boost := newObj.(*v1alpha1.StartupCPUBoost) |
| 56 | + log := ctrl.LoggerFrom(ctx).WithName("startupcpuboost-webhook") |
| 57 | + log.V(5).Info("validating update", "startupcpuboost", klog.KObj(boost)) |
| 58 | + return nil, validate(boost) |
| 59 | +} |
| 60 | + |
| 61 | +// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type |
| 62 | +func (w *StartupCPUBoostWebhook) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { |
| 63 | + return nil, nil |
| 64 | +} |
| 65 | + |
| 66 | +// validate verifies if Startup CPU Boost is valid. This is programmatic |
| 67 | +// validation on a top of declarative API validation |
| 68 | +func validate(boost *v1alpha1.StartupCPUBoost) error { |
| 69 | + var allErrs field.ErrorList |
| 70 | + if err := validateDurationPolicy(boost.Spec.DurationPolicy); err != nil { |
| 71 | + allErrs = append(allErrs, err) |
| 72 | + } |
| 73 | + if len(allErrs) > 0 { |
| 74 | + return apierrors.NewInvalid( |
| 75 | + schema.GroupKind{Group: "autoscaling.x-k8s.io", Kind: "StartupCPUBoost"}, |
| 76 | + boost.Name, allErrs) |
| 77 | + } |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +func validateDurationPolicy(policy v1alpha1.DurationPolicy) *field.Error { |
| 82 | + var cnt int |
| 83 | + fldPath := field.NewPath("spec").Child("") |
| 84 | + if policy.Fixed != nil { |
| 85 | + cnt++ |
| 86 | + } |
| 87 | + if policy.PodCondition != nil { |
| 88 | + cnt++ |
| 89 | + } |
| 90 | + if cnt != 1 { |
| 91 | + err := errors.New("one type of duration policy should be defined") |
| 92 | + return field.Invalid(fldPath, policy, err.Error()) |
| 93 | + } |
| 94 | + return nil |
| 95 | +} |
0 commit comments