kubernetes-sigs / kubectl-validate

Apache License 2.0
121 stars 31 forks source link

the validator fails when only generatedName is set #121

Open richardvflux opened 2 months ago

richardvflux commented 2 months ago

What happened?

With a job, we set only the "generatedName" value as recommended by Argo https://argo-cd.readthedocs.io/en/stable/user-guide/resource_hooks/#using-a-hook-to-send-a-slack-message - however on rendered validation, it fails because there is no name. Having no name is valid it appears if generatedName is set https://kubernetes.io/docs/reference/using-api/api-concepts/#generated-values

What did you expect to happen?

validation to pass

How can we reproduce it (as minimally and precisely as possible)?

use the sample Argo resource as an example

Anything else we need to know?

nope!

Kubernetes version

```console $ kubectl version Client Version: v1.29.1 ```
ewon78 commented 1 month ago

It seems it's caused by the rest/create.go which make the name initialized earlier in the create process。(https://github.com/kubernetes/kubernetes/commit/af9d36f9310bc3c21791bf17470a667b690573b0)

 if len(objectMeta.GetGenerateName()) > 0 && len(objectMeta.GetName()) == 0 {
        return errors.NewInternalError(fmt.Errorf("metadata.name was not generated"))
  }

A straight forward fix will be to generate name before invoking rest.BeforeCreate in validator.go. I'll open a PR.

ewon78 commented 1 month ago

/assign

juliantrue commented 1 week ago

Currently facing this issue as well. Thanks for your PR