Skip to content

Commit 2e0aeb1

Browse files
authored
kaap-560: Update the byoh controller validating webhook to allow deletion of byohost object when byoMachine does not exists in the cluster (#34)
Co-authored-by: Vaibhav Dubewar <vdubewar@platform9.com>
1 parent 742d6cf commit 2e0aeb1

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

apis/infrastructure/v1beta1/byohost_webhook.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"strings"
1111

1212
v1 "k8s.io/api/admission/v1"
13+
apierrors "k8s.io/apimachinery/pkg/api/errors"
14+
"sigs.k8s.io/controller-runtime/pkg/client"
1315
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
1416
)
1517

@@ -18,7 +20,7 @@ import (
1820
// +k8s:deepcopy-gen=false
1921
// ByoHostValidator validates ByoHosts
2022
type ByoHostValidator struct {
21-
// Client client.Client
23+
Client client.Client
2224
decoder *admission.Decoder
2325
}
2426

@@ -34,7 +36,7 @@ func (v *ByoHostValidator) Handle(ctx context.Context, req admission.Request) ad
3436
case v1.Create, v1.Update:
3537
response = v.handleCreateUpdate(&req)
3638
case v1.Delete:
37-
response = v.handleDelete(&req)
39+
response = v.handleDelete(ctx, &req)
3840
default:
3941
response = admission.Allowed("")
4042
}
@@ -61,13 +63,29 @@ func (v *ByoHostValidator) handleCreateUpdate(req *admission.Request) admission.
6163
return admission.Allowed("")
6264
}
6365

64-
func (v *ByoHostValidator) handleDelete(req *admission.Request) admission.Response {
66+
func (v *ByoHostValidator) handleDelete(ctx context.Context, req *admission.Request) admission.Response {
6567
byoHost := &ByoHost{}
6668
err := v.decoder.DecodeRaw(req.OldObject, byoHost)
6769
if err != nil {
6870
return admission.Errored(http.StatusBadRequest, err)
6971
}
7072
if byoHost.Status.MachineRef != nil {
73+
// allow webhook to delete ByoHost when MachineRef is assigned but respective byoMachine doesn't exist
74+
byoMachine := byoHost.Status.MachineRef.Name
75+
76+
// Fetch the ByoMachine instance
77+
byoMachineObj := &ByoMachine{}
78+
err = v.Client.Get(ctx, client.ObjectKey{
79+
Name: byoMachine,
80+
Namespace: byoHost.Namespace,
81+
}, byoMachineObj)
82+
if err != nil {
83+
if apierrors.IsNotFound(err) {
84+
return admission.Allowed("")
85+
}
86+
return admission.Denied("cannot delete ByoHost when byomachine exists")
87+
}
88+
7189
return admission.Denied("cannot delete ByoHost when MachineRef is assigned")
7290
}
7391
return admission.Allowed("")

main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,9 @@ func main() {
145145
os.Exit(1)
146146
}
147147

148-
mgr.GetWebhookServer().Register("/validate-infrastructure-cluster-x-k8s-io-v1beta1-byohost", &webhook.Admission{Handler: &infrastructurev1beta1.ByoHostValidator{}})
148+
mgr.GetWebhookServer().Register("/validate-infrastructure-cluster-x-k8s-io-v1beta1-byohost", &webhook.Admission{Handler: &infrastructurev1beta1.ByoHostValidator{
149+
Client: mgr.GetClient(),
150+
}})
149151

150152
if err = (&byohcontrollers.BootstrapKubeconfigReconciler{
151153
Client: mgr.GetClient(),

0 commit comments

Comments
 (0)