Skip to content

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

@zhixiongdu027

Description

@zhixiongdu027

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions