|
| 1 | +/* |
| 2 | +Copyright 2024. |
| 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 controllers |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + awv1beta2 "github.com/project-codeflare/appwrapper/api/v1beta2" |
| 24 | + |
| 25 | + "k8s.io/apimachinery/pkg/runtime" |
| 26 | + ctrl "sigs.k8s.io/controller-runtime" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/webhook" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 29 | +) |
| 30 | + |
| 31 | +// webhook configuration |
| 32 | +//+kubebuilder:webhook:path=/mutate-workload-codeflare-dev-v1beta2-appwrapper,mutating=true,failurePolicy=fail,sideEffects=None,groups=workload.codeflare.dev,resources=appwrappers,verbs=create,versions=v1beta2,name=mappwrapper.kb.io,admissionReviewVersions=v1 |
| 33 | +//+kubebuilder:webhook:path=/validate-workload-codeflare-dev-v1beta2-appwrapper,mutating=false,failurePolicy=fail,sideEffects=None,groups=workload.codeflare.dev,resources=appwrappers,verbs=create;update,versions=v1beta2,name=vappwrapper.kb.io,admissionReviewVersions=v1 |
| 34 | + |
| 35 | +// permissions needed by the "real" Webhook in the appwrapper project to enable SubjectAccessReview |
| 36 | +//+kubebuilder:rbac:groups=authorization.k8s.io,resources=subjectaccessreviews,verbs=create |
| 37 | +//+kubebuilder:rbac:groups=apiextensions.k8s.io,resources=customresourcedefinitions,verbs=list |
| 38 | + |
| 39 | +type appwrapperWebhook struct { |
| 40 | + externalController bool |
| 41 | +} |
| 42 | + |
| 43 | +var _ webhook.CustomDefaulter = &appwrapperWebhook{} |
| 44 | + |
| 45 | +func (w *appwrapperWebhook) Default(ctx context.Context, obj runtime.Object) error { |
| 46 | + return nil |
| 47 | +} |
| 48 | + |
| 49 | +var _ webhook.CustomValidator = &appwrapperWebhook{} |
| 50 | + |
| 51 | +func (w *appwrapperWebhook) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { |
| 52 | + if w.externalController { |
| 53 | + return nil, nil |
| 54 | + } else { |
| 55 | + return nil, fmt.Errorf("AppWrappers disabled by CodeFlare operator configuration") |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func (w *appwrapperWebhook) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { |
| 60 | + return nil, nil |
| 61 | +} |
| 62 | + |
| 63 | +func (w *appwrapperWebhook) ValidateDelete(context.Context, runtime.Object) (admission.Warnings, error) { |
| 64 | + return nil, nil |
| 65 | +} |
| 66 | + |
| 67 | +func SetupMockAppWrapperWebhooks(mgr ctrl.Manager, externalController bool) error { |
| 68 | + wh := &appwrapperWebhook{externalController: externalController} |
| 69 | + return ctrl.NewWebhookManagedBy(mgr). |
| 70 | + For(&awv1beta2.AppWrapper{}). |
| 71 | + WithDefaulter(wh). |
| 72 | + WithValidator(wh). |
| 73 | + Complete() |
| 74 | +} |
0 commit comments