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

Add webhook validation for empty database and rabbitmq #33

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

raukadah
Copy link
Contributor

@raukadah raukadah commented Jan 8, 2025

databaseInstance and rabbitMqClusterName are required fields. If an user specify databaseInstance and rabbitMqClusterName field as empty string. The webhook should fail it saying as there cannot be empty.

This pr adds the validations for the same.

Copy link

openshift-ci bot commented Jan 8, 2025

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

Copy link

openshift-ci bot commented Jan 8, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please ask for approval from raukadah. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found 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

@abays
Copy link

abays commented Jan 8, 2025

Another option is to make these fields pointers and then use the kubebuilder required validation for these. Though I also see some places where databaseInstance and rabbitMqClusterName use kubebuilder validation without using a pointer string [1][2], which seems like it wouldn't work. That is because the defaulting webhooks will set the field to the empty string, and then the kubebuilder validation will be "tricked" into thinking the user explicitly set a value in this manner -- this is a classic problem. Using a pointer prevents this because the zero-value of nil remains intact throughout the webhook processing and thus will properly be treated as a empty value during subsequent kubebuilder validation.

[1] https://github.com/openstack-k8s-operators/keystone-operator/blob/16c3ed8e549f8591dd17c94fcd165620c8a5444f/api/v1beta1/keystoneapi_types.go#L60-L64
[2] https://github.com/openstack-k8s-operators/keystone-operator/blob/16c3ed8e549f8591dd17c94fcd165620c8a5444f/api/v1beta1/keystoneapi_types.go#L177-L181

databaseInstance and rabbitMqClusterName are required fields.
If an user specify databaseInstance and rabbitMqClusterName field
as empty string. The webhook should fail it saying as there cannot be
empty.

This pr adds the validations for the same.

Signed-off-by: Chandan Kumar (raukadah) <[email protected]>
@raukadah raukadah force-pushed the required_webhook_validation branch from d7009d7 to aa71a31 Compare January 9, 2025 15:24
@raukadah
Copy link
Contributor Author

raukadah commented Jan 9, 2025

Another option is to make these fields pointers and then use the kubebuilder required validation for these. Though I also see some places where databaseInstance and rabbitMqClusterName use kubebuilder validation without using a pointer string [1][2], which seems like it wouldn't work. That is because the defaulting webhooks will set the field to the empty string, and then the kubebuilder validation will be "tricked" into thinking the user explicitly set a value in this manner -- this is a classic problem. Using a pointer prevents this because the zero-value of nil remains intact throughout the webhook processing and thus will properly be treated as a empty value during subsequent kubebuilder validation.

[1] https://github.com/openstack-k8s-operators/keystone-operator/blob/16c3ed8e549f8591dd17c94fcd165620c8a5444f/api/v1beta1/keystoneapi_types.go#L60-L64 [2] https://github.com/openstack-k8s-operators/keystone-operator/blob/16c3ed8e549f8591dd17c94fcd165620c8a5444f/api/v1beta1/keystoneapi_types.go#L177-L181

Thank you for the suggestion @abays , I have added them in existing patch. But while running tests, I am hitting following error:

chandankumar@raukadahlaptop:~/projects/watcher-operator$ make test
test -f go.work || GOTOOLCHAIN=go1.21.0 go work init
go work use .
go work use ./api
go work sync
test -s /home/chandankumar/projects/watcher-operator/bin/controller-gen && /home/chandankumar/projects/watcher-operator/bin/controller-gen --version | grep -q v0.14.0 || \
GOBIN=/home/chandankumar/projects/watcher-operator/bin go install sigs.k8s.io/controller-tools/cmd/[email protected]
/home/chandankumar/projects/watcher-operator/bin/controller-gen rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases && \
 	rm -f api/bases/* && cp -a config/crd/bases api/
/home/chandankumar/projects/watcher-operator/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
go fmt ./...
go vet ./...
# github.com/openstack-k8s-operators/watcher-operator/tests/functional
vet: tests/functional/watcher_controller_test.go:199:6: cannot use GetWatcher(watcherTest.Instance).Spec.DatabaseInstance (variable of type *string) as string value in argument to mariadb.CreateDBService
make: *** [Makefile:127: vet] Error 1

I might have missed something in this change. Need your suggestion here, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants