|
| 1 | +/* |
| 2 | +Copyright 2022 The Kubernetes Authors. |
| 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 runtime |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "net/http" |
| 22 | + |
| 23 | + "github.com/pkg/errors" |
| 24 | + ctrl "sigs.k8s.io/controller-runtime" |
| 25 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/webhook" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 28 | + |
| 29 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 30 | + runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" |
| 31 | + "sigs.k8s.io/cluster-api/feature" |
| 32 | + "sigs.k8s.io/cluster-api/internal/hooks" |
| 33 | + runtimecatalog "sigs.k8s.io/cluster-api/internal/runtime/catalog" |
| 34 | +) |
| 35 | + |
| 36 | +func (w *ClusterDeleteHandler) SetupWebhookWithManager(mgr ctrl.Manager) error { |
| 37 | + mgr.GetWebhookServer().Register("/delete-hook-cluster-x-k8s-io-v1beta1-cluster", &webhook.Admission{ |
| 38 | + Handler: w, |
| 39 | + }) |
| 40 | + return nil |
| 41 | +} |
| 42 | + |
| 43 | +// +kubebuilder:webhook:verbs=delete,path=/delete-hook-cluster-x-k8s-io-v1beta1-cluster,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=cluster.x-k8s.io,resources=clusters,versions=v1beta1,name=delete-hook.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1 |
| 44 | + |
| 45 | +type ClusterDeleteHandler struct { |
| 46 | + Client client.Client |
| 47 | + decoder *admission.Decoder |
| 48 | +} |
| 49 | + |
| 50 | +// Handle will capture the intent to eventually call the BeforeClusterDelete hook by adding it to the pending hooks list. |
| 51 | +func (w *ClusterDeleteHandler) Handle(ctx context.Context, req admission.Request) admission.Response { |
| 52 | + // If RuntimeSDK or ClusterTopology feature gate is not enabled then do nothing and simply pass. |
| 53 | + if !feature.Gates.Enabled(feature.RuntimeSDK) || feature.Gates.Enabled(feature.ClusterTopology) { |
| 54 | + return admission.Allowed("") |
| 55 | + } |
| 56 | + |
| 57 | + cluster := &clusterv1.Cluster{} |
| 58 | + err := w.decoder.Decode(req, cluster) |
| 59 | + if err != nil { |
| 60 | + return admission.Errored(http.StatusBadRequest, errors.Wrapf(err, "failed to decode Cluster resource")) |
| 61 | + } |
| 62 | + // If the Cluster is not deleted then there is nothing to do. |
| 63 | + if cluster.DeletionTimestamp.IsZero() { |
| 64 | + return admission.Allowed("") |
| 65 | + } |
| 66 | + |
| 67 | + // If the Cluster is not a managed topology then there is nothing to do. |
| 68 | + if cluster.Spec.Topology == nil { |
| 69 | + return admission.Allowed("") |
| 70 | + } |
| 71 | + |
| 72 | + if err := hooks.MarkAsPending(ctx, w.Client, cluster, runtimehooksv1.BeforeClusterDelete); err != nil { |
| 73 | + return admission.Errored(http.StatusInternalServerError, errors.Wrapf(err, "failed to mark %s hook as pending for Cluster %s/%s", runtimecatalog.HookName(runtimehooksv1.BeforeClusterDelete), cluster.Namespace, cluster.Name)) |
| 74 | + } |
| 75 | + |
| 76 | + return admission.Allowed("") |
| 77 | +} |
| 78 | + |
| 79 | +// InjectDecoder injects the decoder. |
| 80 | +// ClusterDeleteHandler implements admission.DecoderInjector. |
| 81 | +// A decoder will be automatically injected. |
| 82 | +func (w *ClusterDeleteHandler) InjectDecoder(d *admission.Decoder) error { |
| 83 | + w.decoder = d |
| 84 | + return nil |
| 85 | +} |
0 commit comments