Skip to content

Commit

Permalink
DSC, DSCI: add validating webhook (opendatahub-io#711)
Browse files Browse the repository at this point in the history
Jira: https://issues.redhat.com/browse/RHOAIENG-4268

This patch reverts

5288015 ("Revert "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 <[email protected]>

* 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 <[email protected]>

* 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 <[email protected]>

* 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 <[email protected]>

---------

Signed-off-by: Yauheni Kaliuta <[email protected]>

chore(webhook): (opendatahub-io#870)

- add testcase on DSCI
- remove kubebuilder marker not needed
- remove checks on instance number in existing controllers
- re-generate bundle
- we do not act on update but we keep it on webhook for now

Signed-off-by: Wen Zhou <[email protected]>

fix uncommented tests/e2e/dsc_creation_test.go with a line from
9be146f ("chore(lint): updates to latest version (opendatahub-io#1074)")

Signed-off-by: Yauheni Kaliuta <[email protected]>

webhook: partial: upgrade: controller-runtime and code change accordingly

Partial application of already applied

03c1abc ("upgrade: controller-runtime and code change accordingly (opendatahub-io#1189)")

Webhook related changes.

Signed-off-by: Yauheni Kaliuta <[email protected]>

tests: e2e: fix requireInstalled to check actuall list emptiness

Partial backport of

6acf1db ("chore: update golangci-lint to v1.60.2, fix misleading test (opendatahub-io#1195)")

Signed-off-by: Yauheni Kaliuta <[email protected]>
  • Loading branch information
ykaliuta committed Sep 2, 2024
1 parent 08493b3 commit 47334de
Show file tree
Hide file tree
Showing 17 changed files with 582 additions and 123 deletions.
4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,7 @@ endef

.PHONY: manifests
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
# TODO: enable below when we do webhook
# $(CONTROLLER_GEN) rbac:roleName=controller-manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
$(CONTROLLER_GEN) rbac:roleName=controller-manager-role crd:ignoreUnexportedFields=true paths="./..." output:crd:artifacts:config=config/crd/bases
$(CONTROLLER_GEN) rbac:roleName=controller-manager-role crd:ignoreUnexportedFields=true webhook paths="./..." output:crd:artifacts:config=config/crd/bases
$(call fetch-external-crds,github.com/openshift/api,route/v1)
$(call fetch-external-crds,github.com/openshift/api,user/v1)

Expand Down
6 changes: 6 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ resources:
kind: DSCInitialization
path: github.com/opendatahub-io/opendatahub-operator/v2/apis/dscinitialization/v1
version: v1
webhooks:
validation: true
webhookVersion: v1
- api:
crdVersion: v1
namespaced: false
Expand All @@ -30,4 +33,7 @@ resources:
kind: DataScienceCluster
path: github.com/opendatahub-io/opendatahub-operator/v2/apis/datasciencecluster/v1
version: v1
webhooks:
validation: true
webhookVersion: v1
version: "3"
38 changes: 37 additions & 1 deletion bundle/manifests/rhods-operator.clusterserviceversion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ metadata:
categories: AI/Machine Learning, Big Data
certified: "False"
containerImage: quay.io/opendatahub/opendatahub-operator:v2.0.0
createdAt: "2024-07-24T19:29:46Z"
createdAt: "2024-08-01T14:26:51Z"
features.operators.openshift.io/disconnected: "true"
features.operators.openshift.io/fips-compliant: "false"
features.operators.openshift.io/proxy-aware: "false"
Expand Down Expand Up @@ -1729,6 +1729,10 @@ spec:
initialDelaySeconds: 15
periodSeconds: 20
name: manager
ports:
- containerPort: 9443
name: webhook-server
protocol: TCP
readinessProbe:
httpGet:
path: /readyz
Expand All @@ -1747,10 +1751,19 @@ spec:
capabilities:
drop:
- ALL
volumeMounts:
- mountPath: /tmp/k8s-webhook-server/serving-certs
name: cert
readOnly: true
securityContext:
runAsNonRoot: true
serviceAccountName: redhat-ods-operator-controller-manager
terminationGracePeriodSeconds: 10
volumes:
- name: cert
secret:
defaultMode: 420
secretName: redhat-ods-operator-controller-webhook-cert
strategy: deployment
installModes:
- supported: false
Expand Down Expand Up @@ -1779,3 +1792,26 @@ spec:
provider:
name: Red Hat
version: 2.12.0
webhookdefinitions:
- admissionReviewVersions:
- v1
containerPort: 443
deploymentName: redhat-ods-operator-controller-manager
failurePolicy: Fail
generateName: operator.opendatahub.io
rules:
- apiGroups:
- datasciencecluster.opendatahub.io
- dscinitialization.opendatahub.io
apiVersions:
- v1
operations:
- CREATE
- UPDATE
resources:
- datascienceclusters
- dscinitializations
sideEffects: None
targetPort: 9443
type: ValidatingAdmissionWebhook
webhookPath: /validate-opendatahub-io-v1
5 changes: 4 additions & 1 deletion config/default/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ resources:
- ../manager
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
#- ../webhook
- ../webhook
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
#- ../certmanager
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
Expand All @@ -37,6 +37,7 @@ resources:

# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
# Moved below to patches
#- manager_webhook_patch.yaml

# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'.
Expand Down Expand Up @@ -76,3 +77,5 @@ resources:

patches:
- path: manager_auth_proxy_patch.yaml
# [WEBHOOK]
- path: manager_webhook_patch.yaml
23 changes: 23 additions & 0 deletions config/default/manager_webhook_patch.yaml
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: redhat-ods-operator-controller-webhook-cert
9 changes: 9 additions & 0 deletions config/webhook/kustomization.yaml
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
25 changes: 25 additions & 0 deletions config/webhook/kustomizeconfig.yaml
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
29 changes: 29 additions & 0 deletions config/webhook/manifests.yaml
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
22 changes: 22 additions & 0 deletions config/webhook/service.yaml
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: rhods-operator
app.kubernetes.io/part-of: rhods-operator
app.kubernetes.io/managed-by: kustomize
name: webhook-service
namespace: system
annotations:
service.beta.openshift.io/serving-cert-secret-name: redhat-ods-operator-controller-webhook-cert
spec:
ports:
- port: 443
protocol: TCP
targetPort: 9443
selector:
control-plane: controller-manager
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ func (r *CertConfigmapGeneratorReconciler) Reconcile(ctx context.Context, req ct
return ctrl.Result{}, nil
case 1:
dsciInstance = &dsciInstances.Items[0]
default:
message := "only one instance of DSCInitialization object is allowed"
return ctrl.Result{}, errors.New(message)
}

if skipApplyTrustCAConfig(dsciInstance.Spec.TrustedCABundle) {
Expand Down
22 changes: 1 addition & 21 deletions controllers/datasciencecluster/datasciencecluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,6 @@ func (r *DataScienceClusterReconciler) Reconcile(ctx context.Context, req ctrl.R
return reconcile.Result{Requeue: true}, nil
}

if len(instances.Items) > 1 {
message := fmt.Sprintf("only one instance of DataScienceCluster object is allowed. Update existing instance %s", req.Name)
err := errors.New(message)
_ = r.reportError(err, instance, message)

_, _ = status.UpdateWithRetry(ctx, r.Client, instance, func(saved *dscv1.DataScienceCluster) {
status.SetErrorCondition(&saved.Status.Conditions, status.DuplicateDataScienceCluster, message)
saved.Status.Phase = status.PhaseError
})

return ctrl.Result{}, err
}

// Verify a valid DSCInitialization instance is created
dsciInstances := &dsciv1.DSCInitializationList{}
err = r.Client.List(ctx, dsciInstances)
Expand All @@ -167,7 +154,7 @@ func (r *DataScienceClusterReconciler) Reconcile(ctx context.Context, req ctrl.R
}

// Update phase to error state if DataScienceCluster is created without valid DSCInitialization
switch len(dsciInstances.Items) {
switch len(dsciInstances.Items) { // only handle number as 0 or 1, others won't be existed since webhook block creation
case 0:
reason := status.ReconcileFailed
message := "Failed to get a valid DSCInitialization instance, please create a DSCI instance"
Expand All @@ -186,13 +173,6 @@ func (r *DataScienceClusterReconciler) Reconcile(ctx context.Context, req ctrl.R
case 1:
dscInitializationSpec := dsciInstances.Items[0].Spec
dscInitializationSpec.DeepCopyInto(r.DataScienceCluster.DSCISpec)
default:
message := "only one instance of DSCInitialization object is allowed"
_, _ = status.UpdateWithRetry(ctx, r.Client, instance, func(saved *dscv1.DataScienceCluster) {
status.SetErrorCondition(&saved.Status.Conditions, status.DuplicateDSCInitialization, message)
saved.Status.Phase = status.PhaseError
})
return ctrl.Result{}, errors.New(message)
}

if instance.ObjectMeta.DeletionTimestamp.IsZero() {
Expand Down
26 changes: 1 addition & 25 deletions controllers/dscinitialization/dscinitialization_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ package dscinitialization

import (
"context"
"errors"
"fmt"
"path/filepath"
"reflect"

Expand Down Expand Up @@ -94,33 +92,11 @@ func (r *DSCInitializationReconciler) Reconcile(ctx context.Context, req ctrl.Re
}

var instance *dsciv1.DSCInitialization
switch {
switch { // only handle number as 0 or 1, others won't be existed since webhook block creation
case len(instances.Items) == 0:
return ctrl.Result{}, nil
case len(instances.Items) == 1:
instance = &instances.Items[0]
case len(instances.Items) > 1:
// find out the one by created timestamp and use it as the default one
earliestDSCI := &instances.Items[0]
for _, instance := range instances.Items {
currentDSCI := instance
if currentDSCI.CreationTimestamp.Before(&earliestDSCI.CreationTimestamp) {
earliestDSCI = &currentDSCI
}
}
message := fmt.Sprintf("only one instance of DSCInitialization object is allowed. Please delete other instances than %s", earliestDSCI.Name)
// update all instances Message and Status
for _, deletionInstance := range instances.Items {
deletionInstance := deletionInstance
if deletionInstance.Name != earliestDSCI.Name {
_, _ = status.UpdateWithRetry(ctx, r.Client, &deletionInstance, func(saved *dsciv1.DSCInitialization) {
status.SetErrorCondition(&saved.Status.Conditions, status.DuplicateDSCInitialization, message)
saved.Status.Phase = status.PhaseError
})
}
}

return ctrl.Result{}, errors.New(message)
}

if instance.ObjectMeta.DeletionTimestamp.IsZero() {
Expand Down
18 changes: 0 additions & 18 deletions controllers/dscinitialization/dscinitialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,24 +166,6 @@ var _ = Describe("DataScienceCluster initialization", func() {

Context("Handling existing resources", func() {
AfterEach(cleanupResources)
const applicationName = "default-dsci"

It("Should not have more than one DSCI instance in the cluster", func(ctx context.Context) {

anotherApplicationName := "default2"
// given
desiredDsci := createDSCI(applicationName, operatorv1.Managed, operatorv1.Managed, monitoringNamespace)
Expect(k8sClient.Create(context.Background(), desiredDsci)).Should(Succeed())
// when
desiredDsci2 := createDSCI(anotherApplicationName, operatorv1.Managed, operatorv1.Managed, monitoringNamespace)
// then
Eventually(dscInitializationIsReady(anotherApplicationName, workingNamespace, desiredDsci2)).
WithContext(ctx).
WithTimeout(timeout).
WithPolling(interval).
Should(BeFalse())
})

It("Should not update rolebinding if it exists", func(ctx context.Context) {
applicationName := envtestutil.AppendRandomNameTo("rolebinding-test")

Expand Down
Loading

0 comments on commit 47334de

Please sign in to comment.