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

Check if the other resources referenced by an object exist within a webhook #2968

Closed
zhixiongdu027 opened this issue Sep 29, 2024 · 4 comments

Comments

@zhixiongdu027
Copy link

zhixiongdu027 commented Sep 29, 2024

I want to check if the other resources referenced by an object exist within a webhook. Could someone please help me complete the logic in the following code example? Thank you very much.

// SetupWebhookWithManager will setup the manager to manage the webhooks
func (r *Pool) SetupWebhookWithManager(mgr ctrl.Manager) error {
	return ctrl.NewWebhookManagedBy(mgr).
		For(r).
		Complete()
}

var _ webhook.Validator = &Pool{}

func (r *Pool) Validate() error {
	var allErrs field.ErrorList

	model := r.Spec.Model

	/*
		check model is exist here;
	*/

	if len(allErrs) == 0 {
		return nil
	}

	return apierrors.NewInvalid(schema.GroupKind{Group: GroupVersion.Group, Kind: "Pool"}, r.Name, allErrs)
}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *Pool) ValidateCreate() (admission.Warnings, error) {
	poollog.Info("validate create", "name", r.Name)

	// TODO(user): fill in your validation logic upon object creation.
	return nil, r.Validate()
}

Tasks

No tasks being tracked yet.
@sbueringer
Copy link
Member

You can implement a CustomValidator and then set a Client on the struct that implements it. Here's an example https://github.com/kubernetes-sigs/cluster-api/blob/main/internal/webhooks/cluster.go#L75

@sbueringer
Copy link
Member

/transfer controller-runtime

@k8s-ci-robot k8s-ci-robot transferred this issue from kubernetes-sigs/controller-tools Sep 30, 2024
@zhixiongdu027
Copy link
Author

I will try,Many tks

@sbueringer
Copy link
Member

Please also note that Validator has been removed and won't exist in the next CR release (xref #2877)

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

No branches or pull requests

2 participants