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

fix: Remove namespaceSelector from webhook definition #879

Merged
merged 1 commit into from
Feb 21, 2024

Conversation

akrejcir
Copy link
Collaborator

@akrejcir akrejcir commented Feb 9, 2024

What this PR does / why we need it:
The ValidatingWebhookConfiguration created by OLM can have namespaceSelector field set. We don't want it, because the webhook checks that there is only one SSP resource in the cluster.

Which issue(s) this PR fixes:
Jira: https://issues.redhat.com/browse/CNV-36636

Release note:

None

Copy link

openshift-ci bot commented Feb 9, 2024

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@kubevirt-bot kubevirt-bot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. release-note-none Denotes a PR that doesn't merit a release note. dco-signoff: yes Indicates the PR's author has DCO signed all their commits. size/L labels Feb 9, 2024
@akrejcir akrejcir force-pushed the webhook-namespace-selector branch 2 times, most recently from d1f74be to abaf479 Compare February 9, 2024 09:12
@akrejcir akrejcir marked this pull request as ready for review February 9, 2024 09:12
@kubevirt-bot kubevirt-bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Feb 9, 2024
@akrejcir
Copy link
Collaborator Author

akrejcir commented Feb 9, 2024

/cc @0xFelix @jcanocan

@@ -53,6 +66,65 @@ func Setup(mgr ctrl.Manager) error {
Complete()
}

// The OLM limits the webhook scope to the namespaces that are defined in the OperatorGroup
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that an issue for core KubeVirt too? How do we avoid the creation of multiple KubeVirt CRs when deployed via HCO? Does it use the same approach?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not. SSP ValidatingWebhookConfiguration is created by OLM. The webhook for Kubevirt resource is created by virt-operator and it only checks deletion, not create or update.

webhooks/ssp_webhook.go Outdated Show resolved Hide resolved
webhooks/ssp_webhook.go Outdated Show resolved Hide resolved
webhooks/ssp_webhook.go Outdated Show resolved Hide resolved
tests/webhook_test.go Outdated Show resolved Hide resolved
@akrejcir
Copy link
Collaborator Author

akrejcir commented Feb 15, 2024

I've changed the logic completely. It uses a controller now.
@0xFelix @jcanocan can please you take another look?

// that watches ValidatingWebhookConfiguration created by OLM,
// and removes any namespaceSelector defined in it.
//
// The OLM limits the webhook scope to the namespaces that are defined in the OperatorGroup
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw. is this a bug than should be opened on OLM?

Copy link
Collaborator Author

@akrejcir akrejcir Feb 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not an OLM bug. Our downstream CSV manifest has intentionally set installModes field to OwnNamespace and SingleNamespace. We want to limit the scope of our operators.

SSP webhook is special compared to regular webhooks, that it should also prevent the creation of SSP CRs in other namespaces, so it needs to be triggered if these objects are created in different namespace.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand, but isn't that a valid use case the we should be able to map in OLM?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know. It looks like a design choice that should be discussed with OLM team.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be able to investigate it? Do other repos in the kubevirt org have the same issue (e.g. HCO)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HCO has the same issue. My previous revision of this PR was copied from: https://github.com/kubevirt/hyperconverged-cluster-operator/blob/7ac82e83ad975bf369f7ad1425936047ae892eba/pkg/webhooks/setup.go#L71

Kubevirt does not have a webhook that checks Kubevirt resource, and I'm not sure about CDI.

controllers/webhook_controller.go Outdated Show resolved Hide resolved
Comment on lines +93 to +119
It("should not remove namespaceSelector from non SSP webhook", func() {
webhookConfig.Webhooks[0].Rules[0].APIGroups = []string{"non-ssp-api-group"}

Expect(fakeClient.Create(context.Background(), webhookConfig)).To(Succeed())

_, err := testController.Reconcile(context.Background(), testRequest)
Expect(err).ToNot(HaveOccurred())

updatedConfig := &admissionv1.ValidatingWebhookConfiguration{}
Expect(fakeClient.Get(context.Background(), client.ObjectKeyFromObject(webhookConfig), updatedConfig)).To(Succeed())

Expect(updatedConfig).To(Equal(webhookConfig))
})

It("should not remove namespaceSelector from webhook without labels", func() {
webhookConfig.Labels = nil

Expect(fakeClient.Create(context.Background(), webhookConfig)).To(Succeed())

_, err := testController.Reconcile(context.Background(), testRequest)
Expect(err).ToNot(HaveOccurred())

updatedConfig := &admissionv1.ValidatingWebhookConfiguration{}
Expect(fakeClient.Get(context.Background(), client.ObjectKeyFromObject(webhookConfig), updatedConfig)).To(Succeed())

Expect(updatedConfig).To(Equal(webhookConfig))
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maye put these two into a table?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer not to. It is only 2 tests. If there were more similar test cases, then I would do it.

@akrejcir akrejcir force-pushed the webhook-namespace-selector branch 2 times, most recently from 3878186 to c30f13a Compare February 19, 2024 10:47
The ValidatingWebhookConfiguration created by OLM can have
namespaceSelector field set. We don't want that, because
the webhook checks that there is only one SSP resource
in the cluster.

This commit adds a controller that watches
ValidatingWebhookConfigurations and removes the namespaceSelector,
if the object has a specific label.

Signed-off-by: Andrej Krejcir <akrejcir@redhat.com>
Copy link

sonarcloud bot commented Feb 19, 2024

Quality Gate Passed Quality Gate passed

Issues
0 New issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@akrejcir
Copy link
Collaborator Author

/retest

1 similar comment
@akrejcir
Copy link
Collaborator Author

/retest

Copy link
Member

@0xFelix 0xFelix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/approve

Comment on lines +75 to +77
if changed := updateWebhookConfiguration(webhookConfig); !changed {
return reconcile.Result{}, nil
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit

Suggested change
if changed := updateWebhookConfiguration(webhookConfig); !changed {
return reconcile.Result{}, nil
}
if !updateWebhookConfiguration(webhookConfig) {
return reconcile.Result{}, nil
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really small. I don't want to retrigger CI because of it.

// that watches ValidatingWebhookConfiguration created by OLM,
// and removes any namespaceSelector defined in it.
//
// The OLM limits the webhook scope to the namespaces that are defined in the OperatorGroup
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be able to investigate it? Do other repos in the kubevirt org have the same issue (e.g. HCO)?

@kubevirt-bot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: 0xFelix

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubevirt-bot kubevirt-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Feb 20, 2024
@akrejcir
Copy link
Collaborator Author

@jcanocan can you please take another look?

@jcanocan
Copy link
Contributor

/lgtm

@kubevirt-bot kubevirt-bot added the lgtm Indicates that a PR is ready to be merged. label Feb 21, 2024
@kubevirt-bot kubevirt-bot merged commit 2ca6001 into kubevirt:main Feb 21, 2024
12 checks passed
@akrejcir akrejcir deleted the webhook-namespace-selector branch February 21, 2024 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. dco-signoff: yes Indicates the PR's author has DCO signed all their commits. lgtm Indicates that a PR is ready to be merged. release-note-none Denotes a PR that doesn't merit a release note. size/L
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants