Skip to content

Commit

Permalink
DSC, DSCI: webhook: implement one instance enforcing
Browse files Browse the repository at this point in the history
The webhook handles only Create requests (configured in
config/webhook/manifests.yaml). Implements the logic which is done
now on reconcile time [1] (same for DSCI).

It checks for 0, not 1, since when the webhook is running the object
has not been created yet. Means if it's 1 then it handles request to
create a second one.

It could be probably possible to use generics but does not make a
lot of sense for such a simple case.

Closes: opendatahub-io#693

[1] https://github.com/opendatahub-io/opendatahub-operator/blob/incubation/controllers/datasciencecluster/datasciencecluster_controller.go#L98

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
  • Loading branch information
ykaliuta committed Nov 15, 2023
1 parent b0a7f4d commit c1521b0
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion controllers/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,41 @@ func (w *OpenDataHubWebhook) InjectClient(c client.Client) error {
}

func (w *OpenDataHubWebhook) Handle(ctx context.Context, req admission.Request) admission.Response {
return admission.ValidationResponse(true, "")
var listLen int
var err error

if req.Operation != admissionv1.Create {
msg := fmt.Sprintf("ODH skipping %v request", req.Operation)
log.Info(msg)
return admission.Allowed(msg)
}

switch req.Kind.Kind {
case "DataScienceCluster":
instances := &dsc.DataScienceClusterList{}
err = w.client.List(ctx, instances)
listLen = len(instances.Items)

case "DSCInitialization":
instances := &dsci.DSCInitializationList{}
err = w.client.List(ctx, instances)
listLen = len(instances.Items)

default:
log.Info("Got wrong kind %s", req.Kind.Kind)
return admission.Errored(http.StatusBadRequest, nil)
}

if err != nil {
return admission.Errored(http.StatusBadRequest, err)
}

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

msg := fmt.Sprintf("%s allowed", req.Kind.Kind)
return admission.Allowed(msg)
}

0 comments on commit c1521b0

Please sign in to comment.