|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one or more |
| 2 | +// contributor license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright ownership. |
| 4 | +// The ASF licenses this file to You under the Apache License, Version 2.0 |
| 5 | +// (the "License"); you may not use this file except in compliance with |
| 6 | +// the License. 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 | +package v1 |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + |
| 22 | + k8serrors "k8s.io/apimachinery/pkg/api/errors" |
| 23 | + "k8s.io/apimachinery/pkg/runtime" |
| 24 | + ctrl "sigs.k8s.io/controller-runtime" |
| 25 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 26 | + logf "sigs.k8s.io/controller-runtime/pkg/log" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/webhook" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 29 | + gatewaynetworkingk8siov1 "sigs.k8s.io/gateway-api/apis/v1" |
| 30 | + |
| 31 | + v1alpha1 "github.com/apache/apisix-ingress-controller/api/v1alpha1" |
| 32 | + "github.com/apache/apisix-ingress-controller/internal/controller/config" |
| 33 | + internaltypes "github.com/apache/apisix-ingress-controller/internal/types" |
| 34 | +) |
| 35 | + |
| 36 | +// nolint:unused |
| 37 | +// log is for logging in this package. |
| 38 | +var gatewaylog = logf.Log.WithName("gateway-resource") |
| 39 | + |
| 40 | +// SetupGatewayWebhookWithManager registers the webhook for Gateway in the manager. |
| 41 | +func SetupGatewayWebhookWithManager(mgr ctrl.Manager) error { |
| 42 | + return ctrl.NewWebhookManagedBy(mgr).For(&gatewaynetworkingk8siov1.Gateway{}). |
| 43 | + WithValidator(&GatewayCustomValidator{Client: mgr.GetClient()}). |
| 44 | + Complete() |
| 45 | +} |
| 46 | + |
| 47 | +// NOTE: The 'path' attribute must follow a specific pattern and should not be modified directly here. |
| 48 | +// Modifying the path for an invalid path can cause API server errors; failing to locate the webhook. |
| 49 | +// +kubebuilder:webhook:path=/validate-gateway-networking-k8s-io-v1-gateway,mutating=false,failurePolicy=fail,sideEffects=None,groups=gateway.networking.k8s.io,resources=gateways,verbs=create;update,versions=v1,name=vgateway-v1.kb.io,admissionReviewVersions=v1 |
| 50 | + |
| 51 | +// GatewayCustomValidator struct is responsible for validating the Gateway resource |
| 52 | +// when it is created, updated, or deleted. |
| 53 | +// |
| 54 | +// NOTE: The +kubebuilder:object:generate=false marker prevents controller-gen from generating DeepCopy methods, |
| 55 | +// as this struct is used only for temporary operations and does not need to be deeply copied. |
| 56 | +type GatewayCustomValidator struct { |
| 57 | + Client client.Client |
| 58 | +} |
| 59 | + |
| 60 | +var _ webhook.CustomValidator = &GatewayCustomValidator{} |
| 61 | + |
| 62 | +// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type Gateway. |
| 63 | +func (v *GatewayCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { |
| 64 | + gateway, ok := obj.(*gatewaynetworkingk8siov1.Gateway) |
| 65 | + if !ok { |
| 66 | + return nil, fmt.Errorf("expected a Gateway object but got %T", obj) |
| 67 | + } |
| 68 | + gatewaylog.Info("Validation for Gateway upon creation", "name", gateway.GetName()) |
| 69 | + |
| 70 | + warnings := v.warnIfMissingGatewayProxyForGateway(ctx, gateway) |
| 71 | + |
| 72 | + return warnings, nil |
| 73 | +} |
| 74 | + |
| 75 | +// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type Gateway. |
| 76 | +func (v *GatewayCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { |
| 77 | + gateway, ok := newObj.(*gatewaynetworkingk8siov1.Gateway) |
| 78 | + if !ok { |
| 79 | + return nil, fmt.Errorf("expected a Gateway object for the newObj but got %T", newObj) |
| 80 | + } |
| 81 | + gatewaylog.Info("Validation for Gateway upon update", "name", gateway.GetName()) |
| 82 | + |
| 83 | + warnings := v.warnIfMissingGatewayProxyForGateway(ctx, gateway) |
| 84 | + |
| 85 | + return warnings, nil |
| 86 | +} |
| 87 | + |
| 88 | +// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type Gateway. |
| 89 | +func (v *GatewayCustomValidator) ValidateDelete(_ context.Context, obj runtime.Object) (admission.Warnings, error) { |
| 90 | + return nil, nil |
| 91 | +} |
| 92 | + |
| 93 | +func (v *GatewayCustomValidator) warnIfMissingGatewayProxyForGateway(ctx context.Context, gateway *gatewaynetworkingk8siov1.Gateway) admission.Warnings { |
| 94 | + var warnings admission.Warnings |
| 95 | + |
| 96 | + // get gateway class |
| 97 | + gatewayClass := &gatewaynetworkingk8siov1.GatewayClass{} |
| 98 | + if err := v.Client.Get(ctx, client.ObjectKey{Name: string(gateway.Spec.GatewayClassName)}, gatewayClass); err != nil { |
| 99 | + gatewaylog.Error(err, "failed to get gateway class", "gateway", gateway.GetName(), "gatewayclass", gateway.Spec.GatewayClassName) |
| 100 | + return nil |
| 101 | + } |
| 102 | + // match controller |
| 103 | + if string(gatewayClass.Spec.ControllerName) != config.ControllerConfig.ControllerName { |
| 104 | + return nil |
| 105 | + } |
| 106 | + |
| 107 | + infra := gateway.Spec.Infrastructure |
| 108 | + if infra == nil || infra.ParametersRef == nil { |
| 109 | + return nil |
| 110 | + } |
| 111 | + ref := infra.ParametersRef |
| 112 | + if string(ref.Group) != v1alpha1.GroupVersion.Group || string(ref.Kind) != internaltypes.KindGatewayProxy { |
| 113 | + return nil |
| 114 | + } |
| 115 | + |
| 116 | + ns := gateway.GetNamespace() |
| 117 | + name := ref.Name |
| 118 | + |
| 119 | + var gp v1alpha1.GatewayProxy |
| 120 | + if err := v.Client.Get(ctx, client.ObjectKey{Namespace: ns, Name: name}, &gp); err != nil { |
| 121 | + if k8serrors.IsNotFound(err) { |
| 122 | + msg := fmt.Sprintf("Referenced GatewayProxy '%s/%s' not found.", ns, name) |
| 123 | + warnings = append(warnings, msg) |
| 124 | + gatewaylog.Info("Gateway references missing GatewayProxy", "gateway", gateway.GetName(), "namespace", ns, "gatewayproxy", name) |
| 125 | + } else { |
| 126 | + gatewaylog.Error(err, "failed to resolve GatewayProxy for Gateway", "gateway", gateway.GetName(), "namespace", ns, "gatewayproxy", name) |
| 127 | + } |
| 128 | + } |
| 129 | + return warnings |
| 130 | +} |
0 commit comments