Skip to content

Commit

Permalink
webhook: refactor with switch and helpers
Browse files Browse the repository at this point in the history
Prepare to implement deletion check.

- Use switch to select supported requests, easier to add Delete then.
- Remove misleading messages for the allowed case, the last one only
  is in use.
- Use helpers to count and to compare, there will be the same logic
  for deletion check.
- Tune allowed message.
- Use go style case with several variants instead of C-style.

Signed-off-by: Yauheni Kaliuta <[email protected]>
  • Loading branch information
ykaliuta committed Jun 24, 2024
1 parent 9be146f commit 74a525b
Showing 1 changed file with 34 additions and 24 deletions.
58 changes: 34 additions & 24 deletions controllers/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,33 @@ func (w *OpenDataHubWebhook) InjectClient(c client.Client) error {
return nil
}

func (w *OpenDataHubWebhook) checkDupCreation(ctx context.Context, req admission.Request) admission.Response {
if req.Operation != admissionv1.Create {
return admission.Allowed(fmt.Sprintf("duplication check: skipping %v request", req.Operation))
func countObjects(ctx context.Context, cli client.Client, gvk schema.GroupVersionKind) (int, error) {
list := &unstructured.UnstructuredList{}
list.SetGroupVersionKind(gvk)

if err := cli.List(ctx, list); err != nil {
return 0, err
}

return len(list.Items), nil
}

func denyCountGtZero(ctx context.Context, cli client.Client, gvk schema.GroupVersionKind, msg string) admission.Response {
count, err := countObjects(ctx, cli, gvk)
if err != nil {
return admission.Errored(http.StatusBadRequest, err)
}

if count > 0 {
return admission.Denied(msg)
}

return admission.Allowed("")
}

func (w *OpenDataHubWebhook) checkDupCreation(ctx context.Context, req admission.Request) admission.Response {
switch req.Kind.Kind {
case "DataScienceCluster":
case "DSCInitialization":
case "DataScienceCluster", "DSCInitialization":
default:
log.Info("Got wrong kind", "kind", req.Kind.Kind)
return admission.Errored(http.StatusBadRequest, nil)
Expand All @@ -77,35 +96,26 @@ func (w *OpenDataHubWebhook) checkDupCreation(ctx context.Context, req admission
Kind: req.Kind.Kind,
}

list := &unstructured.UnstructuredList{}
list.SetGroupVersionKind(gvk)

if err := w.client.List(ctx, list); err != nil {
return admission.Errored(http.StatusBadRequest, err)
}

// if len == 1 now creation of #2 is being handled
if len(list.Items) > 0 {
return admission.Denied(fmt.Sprintf("Only one instance of %s object is allowed", req.Kind.Kind))
}

return admission.Allowed(fmt.Sprintf("%s duplication check passed", req.Kind.Kind))
// if count == 1 now creation of #2 is being handled
return denyCountGtZero(ctx, w.client, gvk,
fmt.Sprintf("Only one instance of %s object is allowed", req.Kind.Kind))
}

func (w *OpenDataHubWebhook) Handle(ctx context.Context, req admission.Request) admission.Response {
var resp admission.Response

// Handle only Create and Update
if req.Operation == admissionv1.Delete || req.Operation == admissionv1.Connect {
msg := fmt.Sprintf("ODH skipping %v request", req.Operation)
switch req.Operation {
case admissionv1.Create:
resp = w.checkDupCreation(ctx, req)
default:
msg := fmt.Sprintf("Webhook skipping %v request", req.Operation)
log.Info(msg)
return admission.Allowed(msg)
resp = admission.Allowed("")
}

resp = w.checkDupCreation(ctx, req)
if !resp.Allowed {
return resp
}

return admission.Allowed(fmt.Sprintf("%s allowed", req.Kind.Kind))
return admission.Allowed(fmt.Sprintf("Operation %s on %s allowed", req.Operation, req.Kind.Kind))
}

0 comments on commit 74a525b

Please sign in to comment.