Posted community wiki answer for better visibility. Feel free to expand it.
EDIT:
The workaround presented by the original poster (@moluzhui):
At present, I provide ValidatingWebhookConfiguration in chart/template in advance and write it through .Files.Get
As stated in the official documentation:
Note: When using clientConfig.service, the server cert must be valid for <svc_name>.<svc_namespace>.svc.
The namespace name is required - this is how DNS in Kubernetes works - by using service and namespace name.
However, there is a good article which presents best practices of managing TLS certificates for Kubernetes Admission Webhooks - 5 Ways of Managing TLS Certificates for your Kubernetes Admission Webhooks. Maybe some of them will be useful to you and will be solution for your issue:
- for helm - use Certificator project and Helm Hooks - it automatically patches
caBundle field
- setup init container to create a certificate and provide CA bundle to the API server
- generate certificate with cert-manager CA Injector and inject them to WebhookConfiguration
You can also set up URL with a location of the webhook, where you don't have to use caBundle:
Expects the TLS certificate to be verified using system trust roots, so does not specify a caBundle.
Answering your comment:
Well, then I can only use multiple DNS(1,2,3...) to preset the name space that may be deployed. Does this affect efficiency?
Probably depends how many namespaces you want to deploy, but for sure it is not good practice.
Another solution from the comment (thanks to @JWhy user):
You may create another service at a predictable location (i.e. in a specific namespace) and link that to your actual service in the less predictable namespace. See stackoverflow.com/a/44329470/763875