Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webhook: refactor with switch and helpers #1076

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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("No logic check by webhook is applied on %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))
}
Loading