github.com/redhat-appstudio/release-service@v0.0.0-20240507045911-a8558ef3422a/api/v1alpha1/webhooks/releaseplanadmission/webhook.go (about) 1 /* 2 Copyright 2022. 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 releaseplanadmission 18 19 import ( 20 "context" 21 "fmt" 22 "github.com/go-logr/logr" 23 "github.com/redhat-appstudio/release-service/api/v1alpha1" 24 "github.com/redhat-appstudio/release-service/metadata" 25 "k8s.io/apimachinery/pkg/runtime" 26 ctrl "sigs.k8s.io/controller-runtime" 27 "sigs.k8s.io/controller-runtime/pkg/client" 28 "sigs.k8s.io/controller-runtime/pkg/webhook/admission" 29 ) 30 31 // Webhook describes the data structure for the bar webhook 32 type Webhook struct { 33 client client.Client 34 log logr.Logger 35 } 36 37 // Default implements webhook.Defaulter so a webhook will be registered for the type. 38 func (w *Webhook) Default(ctx context.Context, obj runtime.Object) error { 39 releasePlanAdmission := obj.(*v1alpha1.ReleasePlanAdmission) 40 41 if _, found := releasePlanAdmission.GetLabels()[metadata.AutoReleaseLabel]; !found { 42 if releasePlanAdmission.Labels == nil { 43 releasePlanAdmission.Labels = map[string]string{ 44 metadata.AutoReleaseLabel: "true", 45 } 46 } 47 } 48 49 return nil 50 } 51 52 // +kubebuilder:webhook:path=/mutate-appstudio-redhat-com-v1alpha1-releaseplanadmission,mutating=true,failurePolicy=fail,sideEffects=None,groups=appstudio.redhat.com,resources=releaseplanadmissions,verbs=create,versions=v1alpha1,name=mreleaseplanadmission.kb.io,admissionReviewVersions=v1 53 // +kubebuilder:webhook:path=/validate-appstudio-redhat-com-v1alpha1-releaseplanadmission,mutating=false,failurePolicy=fail,sideEffects=None,groups=appstudio.redhat.com,resources=releaseplanadmissions,verbs=create;update,versions=v1alpha1,name=vreleaseplanadmission.kb.io,admissionReviewVersions=v1 54 55 // Register registers the webhook with the passed manager and log. 56 func (w *Webhook) Register(mgr ctrl.Manager, log *logr.Logger) error { 57 w.client = mgr.GetClient() 58 w.log = log.WithName("releasePlanAdmission") 59 60 return ctrl.NewWebhookManagedBy(mgr). 61 For(&v1alpha1.ReleasePlanAdmission{}). 62 WithDefaulter(w). 63 WithValidator(w). 64 Complete() 65 } 66 67 // ValidateCreate implements webhook.Validator so a webhook will be registered for the type. 68 func (w *Webhook) ValidateCreate(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) { 69 return w.validateAutoReleaseLabel(obj) 70 } 71 72 // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. 73 func (w *Webhook) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (warnings admission.Warnings, err error) { 74 return w.validateAutoReleaseLabel(newObj) 75 } 76 77 // ValidateDelete implements webhook.Validator so a webhook will be registered for the type. 78 func (w *Webhook) ValidateDelete(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) { 79 return nil, nil 80 } 81 82 // validateAutoReleaseLabel throws an error if the auto-release label value is set to anything besides true or false. 83 func (w *Webhook) validateAutoReleaseLabel(obj runtime.Object) (warnings admission.Warnings, err error) { 84 releasePlanAdmission := obj.(*v1alpha1.ReleasePlanAdmission) 85 86 if value, found := releasePlanAdmission.GetLabels()[metadata.AutoReleaseLabel]; found { 87 if value != "true" && value != "false" { 88 return nil, fmt.Errorf("'%s' label can only be set to true or false", metadata.AutoReleaseLabel) 89 } 90 } 91 return nil, nil 92 }