forked from opendatahub-io/opendatahub-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DSC, DSCI: add validating webhook (opendatahub-io#711)
* webhook: add initial skeleton Originally it was generated with ```operator-sdk create webhook --group datasciencecluster --version v1 --kind DataScienceCluster --programmatic-validation``` but webhook.Validator interface (like described in the kubebuilder book[1]) does not work well for the purpose of the webhook due to needs to access openshift cluster (client.Client) to check existing instances of DSC. So, direct implementation of Handler was done inspired by [2] and odh-notebooks implementation [3]. Move it from api package closer to controllers as in [3] as well since it's not DataScienceCluster or DSCInitialization extention anymore. Amend webhook_suite_test.go's path to configs accordingly. Fix linter issues in webhook_suite_test.go: - disable ssl check; - move to package webhook_test certmanager files removed too due to usage of OpenShift service serving certificates[4] (see also service.beta.openshift.io/inject-cabundle annotation in config/webhook/kustomization.yaml). Add webhook generation to `make manifests` target so webhook/manifests.yaml is generated with it. Since DSCI creation now requires webhook it should be delayed after manager started. Move it to a closure and add it to the manager for run with Add() API. It requires explicit declaration of the interface variable otherwise complains about type mismatch for the function literal. [1] https://book.kubebuilder.io/cronjob-tutorial/webhook-implementation [2] https://book-v1.book.kubebuilder.io/beyond_basics/sample_webhook.html [3] https://github.com/opendatahub-io/kubeflow/blob/v1.7-branch/components/odh-notebook-controller/controllers/notebook_webhook.go [4] https://docs.openshift.com/container-platform/4.9/security/certificates/service-serving-certificate.html Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com> * webhook: implement one instance enforcing The webhook is written with the idea to handle both Create and Update requests (configured in config/webhook/manifests.yaml), but at the moment only duplication check on Create is implemented. Implements the logic which is done now on reconcile time [1] (same for DSCI). It checks for 0 instances, 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> * tests: add tests to check duplication blocking Add both envtest and e2e tests of a second DataScienceCluster instance creation blocking. envtest's one is a part of webhook test suite. e2e: Add `name` parameter to setupDSCInstance() function to reuse it. Use require.Error() as the assertion, shorter and more straight logic than implementing it in the test itself. Add e2e test to check DSCInitialization similar way. Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com> * tests: e2e: refactor duplication tests in more abstract way Factor out common code using Unstructured/List objects. Change structure to remind more prepare/action/assert. Use "require" features when appropriate. Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com> --------- Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com> (cherry picked from commit fb93e5c)
- Loading branch information
1 parent
1935074
commit d4d242b
Showing
16 changed files
with
593 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
bundle/manifests/opendatahub-operator-webhook-service_v1_service.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
annotations: | ||
service.beta.openshift.io/inject-cabundle: "true" | ||
service.beta.openshift.io/serving-cert-secret-name: opendatahub-operator-controller-webhook-cert | ||
labels: | ||
app.kubernetes.io/component: webhook | ||
app.kubernetes.io/created-by: opendatahub-operator | ||
app.kubernetes.io/instance: webhook-service | ||
app.kubernetes.io/managed-by: kustomize | ||
app.kubernetes.io/name: service | ||
app.kubernetes.io/part-of: opendatahub-operator | ||
name: opendatahub-operator-webhook-service | ||
spec: | ||
ports: | ||
- port: 443 | ||
protocol: TCP | ||
targetPort: 9443 | ||
selector: | ||
control-plane: controller-manager | ||
status: | ||
loadBalancer: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: controller-manager | ||
namespace: system | ||
spec: | ||
template: | ||
spec: | ||
containers: | ||
- name: manager | ||
ports: | ||
- containerPort: 9443 | ||
name: webhook-server | ||
protocol: TCP | ||
volumeMounts: | ||
- mountPath: /tmp/k8s-webhook-server/serving-certs | ||
name: cert | ||
readOnly: true | ||
volumes: | ||
- name: cert | ||
secret: | ||
defaultMode: 420 | ||
secretName: opendatahub-operator-controller-webhook-cert |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
resources: | ||
- manifests.yaml | ||
- service.yaml | ||
|
||
commonAnnotations: | ||
service.beta.openshift.io/inject-cabundle: "true" | ||
|
||
configurations: | ||
- kustomizeconfig.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# the following config is for teaching kustomize where to look at when substituting vars. | ||
# It requires kustomize v2.1.0 or newer to work properly. | ||
nameReference: | ||
- kind: Service | ||
version: v1 | ||
fieldSpecs: | ||
- kind: MutatingWebhookConfiguration | ||
group: admissionregistration.k8s.io | ||
path: webhooks/clientConfig/service/name | ||
- kind: ValidatingWebhookConfiguration | ||
group: admissionregistration.k8s.io | ||
path: webhooks/clientConfig/service/name | ||
|
||
namespace: | ||
- kind: MutatingWebhookConfiguration | ||
group: admissionregistration.k8s.io | ||
path: webhooks/clientConfig/service/namespace | ||
create: true | ||
- kind: ValidatingWebhookConfiguration | ||
group: admissionregistration.k8s.io | ||
path: webhooks/clientConfig/service/namespace | ||
create: true | ||
|
||
varReference: | ||
- path: metadata/annotations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
--- | ||
apiVersion: admissionregistration.k8s.io/v1 | ||
kind: ValidatingWebhookConfiguration | ||
metadata: | ||
creationTimestamp: null | ||
name: validating-webhook-configuration | ||
webhooks: | ||
- admissionReviewVersions: | ||
- v1 | ||
clientConfig: | ||
service: | ||
name: webhook-service | ||
namespace: system | ||
path: /validate-opendatahub-io-v1 | ||
failurePolicy: Fail | ||
name: operator.opendatahub.io | ||
rules: | ||
- apiGroups: | ||
- datasciencecluster.opendatahub.io | ||
- dscinitialization.opendatahub.io | ||
apiVersions: | ||
- v1 | ||
operations: | ||
- CREATE | ||
- UPDATE | ||
resources: | ||
- datascienceclusters | ||
- dscinitializations | ||
sideEffects: None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
labels: | ||
app.kubernetes.io/name: service | ||
app.kubernetes.io/instance: webhook-service | ||
app.kubernetes.io/component: webhook | ||
app.kubernetes.io/created-by: opendatahub-operator | ||
app.kubernetes.io/part-of: opendatahub-operator | ||
app.kubernetes.io/managed-by: kustomize | ||
name: webhook-service | ||
namespace: system | ||
annotations: | ||
service.beta.openshift.io/serving-cert-secret-name: opendatahub-operator-controller-webhook-cert | ||
spec: | ||
ports: | ||
- port: 443 | ||
protocol: TCP | ||
targetPort: 9443 | ||
selector: | ||
control-plane: controller-manager |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
Copyright 2023. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package webhook | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
admissionv1 "k8s.io/api/admission/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
) | ||
|
||
var log = ctrl.Log.WithName("odh-controller-webhook") | ||
|
||
//+kubebuilder:webhook:path=/validate-opendatahub-io-v1,mutating=false,failurePolicy=fail,sideEffects=None,groups=datasciencecluster.opendatahub.io;dscinitialization.opendatahub.io,resources=datascienceclusters;dscinitializations,verbs=create;update,versions=v1,name=operator.opendatahub.io,admissionReviewVersions=v1 | ||
//nolint:lll | ||
|
||
type OpenDataHubWebhook struct { | ||
client client.Client | ||
decoder *admission.Decoder | ||
} | ||
|
||
func (w *OpenDataHubWebhook) SetupWithManager(mgr ctrl.Manager) { | ||
hookServer := mgr.GetWebhookServer() | ||
odhWebhook := &webhook.Admission{ | ||
Handler: w, | ||
} | ||
hookServer.Register("/validate-opendatahub-io-v1", odhWebhook) | ||
} | ||
|
||
func (w *OpenDataHubWebhook) InjectDecoder(d *admission.Decoder) error { | ||
w.decoder = d | ||
return nil | ||
} | ||
|
||
func (w *OpenDataHubWebhook) InjectClient(c client.Client) error { | ||
w.client = c | ||
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)) | ||
} | ||
|
||
switch req.Kind.Kind { | ||
case "DataScienceCluster": | ||
case "DSCInitialization": | ||
default: | ||
log.Info("Got wrong kind", "kind", req.Kind.Kind) | ||
return admission.Errored(http.StatusBadRequest, nil) | ||
} | ||
|
||
gvk := schema.GroupVersionKind{ | ||
Group: req.Kind.Group, | ||
Version: req.Kind.Version, | ||
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)) | ||
} | ||
|
||
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) | ||
log.Info(msg) | ||
return admission.Allowed(msg) | ||
} | ||
|
||
resp = w.checkDupCreation(ctx, req) | ||
if !resp.Allowed { | ||
return resp | ||
} | ||
|
||
return admission.Allowed(fmt.Sprintf("%s allowed", req.Kind.Kind)) | ||
} |
Oops, something went wrong.