Fix incorrect validation on the kubelet

This commit is contained in:
HirazawaUi
2025-07-28 23:03:46 +08:00
parent e2eed70a29
commit 6997fbd1ed
3 changed files with 19 additions and 3 deletions

View File

@@ -575,8 +575,8 @@ func (kl *Kubelet) GeneratePodHostNameAndDomain(pod *v1.Pod) (string, string, er
if utilfeature.DefaultFeatureGate.Enabled(features.HostnameOverride) && pod.Spec.HostnameOverride != nil {
hostname := *pod.Spec.HostnameOverride
if msgs := utilvalidation.IsDNS1123Label(hostname); len(msgs) != 0 {
return "", "", fmt.Errorf("pod HostnameOverride %q is not a valid DNS label: %s", hostname, strings.Join(msgs, ";"))
if msgs := utilvalidation.IsDNS1123Subdomain(hostname); len(msgs) != 0 {
return "", "", fmt.Errorf("pod HostnameOverride %q is not a valid DNS subdomain: %s", hostname, strings.Join(msgs, ";"))
}
truncatedHostname, err := truncatePodHostnameIfNeeded(pod.Name, hostname)
if err != nil {

View File

@@ -7468,7 +7468,15 @@ func TestGeneratePodHostNameAndDomain(t *testing.T) {
podHostnameOverride: ptr.To("Invalid-Hostname-!"),
featureGateEnabled: true,
expectError: true,
errorContains: "pod HostnameOverride \"Invalid-Hostname-!\" is not a valid DNS label",
errorContains: "pod HostnameOverride \"Invalid-Hostname-!\" is not a valid DNS subdomain",
},
{
name: "HostnameOverride - enabled - overrides all - valid DNS hostname",
podName: "test-pod",
podHostnameOverride: ptr.To("valid.hostname"),
expectedHostname: "valid.hostname",
featureGateEnabled: true,
errorContains: "",
},
{
name: "HostnameOverride - disabled - is ignored",

View File

@@ -56,6 +56,14 @@ var _ = SIGDescribe("Override hostname of Pod", framework.WithFeatureGate(featur
f := framework.NewDefaultFramework("hostfqdn")
f.NamespacePodSecurityLevel = admissionapi.LevelBaseline
ginkgo.It("a pod has hostnameOverride field with value that is a valid DNS subdomain.", func(ctx context.Context) {
pod := newTestPod(f.Namespace.Name)
hostnameOverride := "override.example.host"
pod.Spec.HostnameOverride = &hostnameOverride
output := []string{fmt.Sprintf("%s;%s;", hostnameOverride, hostnameOverride)}
e2eoutput.TestContainerOutput(ctx, f, "hostnameOverride overrides hostname", pod, 0, output)
})
ginkgo.It("a pod with hostname and hostnameOverride fields will have hostnameOverride as hostname", func(ctx context.Context) {
pod := newTestPod(f.Namespace.Name)
hostname := "custom-host"