Skip to content

Commit 6997fbd

Browse files
committed
Fix incorrect validation on the kubelet
1 parent e2eed70 commit 6997fbd

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

pkg/kubelet/kubelet_pods.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,8 +575,8 @@ func (kl *Kubelet) GeneratePodHostNameAndDomain(pod *v1.Pod) (string, string, er
575575

576576
if utilfeature.DefaultFeatureGate.Enabled(features.HostnameOverride) && pod.Spec.HostnameOverride != nil {
577577
hostname := *pod.Spec.HostnameOverride
578-
if msgs := utilvalidation.IsDNS1123Label(hostname); len(msgs) != 0 {
579-
return "", "", fmt.Errorf("pod HostnameOverride %q is not a valid DNS label: %s", hostname, strings.Join(msgs, ";"))
578+
if msgs := utilvalidation.IsDNS1123Subdomain(hostname); len(msgs) != 0 {
579+
return "", "", fmt.Errorf("pod HostnameOverride %q is not a valid DNS subdomain: %s", hostname, strings.Join(msgs, ";"))
580580
}
581581
truncatedHostname, err := truncatePodHostnameIfNeeded(pod.Name, hostname)
582582
if err != nil {

pkg/kubelet/kubelet_pods_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7468,7 +7468,15 @@ func TestGeneratePodHostNameAndDomain(t *testing.T) {
74687468
podHostnameOverride: ptr.To("Invalid-Hostname-!"),
74697469
featureGateEnabled: true,
74707470
expectError: true,
7471-
errorContains: "pod HostnameOverride \"Invalid-Hostname-!\" is not a valid DNS label",
7471+
errorContains: "pod HostnameOverride \"Invalid-Hostname-!\" is not a valid DNS subdomain",
7472+
},
7473+
{
7474+
name: "HostnameOverride - enabled - overrides all - valid DNS hostname",
7475+
podName: "test-pod",
7476+
podHostnameOverride: ptr.To("valid.hostname"),
7477+
expectedHostname: "valid.hostname",
7478+
featureGateEnabled: true,
7479+
errorContains: "",
74727480
},
74737481
{
74747482
name: "HostnameOverride - disabled - is ignored",

test/e2e/common/node/pod_hostnameoverride.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ var _ = SIGDescribe("Override hostname of Pod", framework.WithFeatureGate(featur
5656
f := framework.NewDefaultFramework("hostfqdn")
5757
f.NamespacePodSecurityLevel = admissionapi.LevelBaseline
5858

59+
ginkgo.It("a pod has hostnameOverride field with value that is a valid DNS subdomain.", func(ctx context.Context) {
60+
pod := newTestPod(f.Namespace.Name)
61+
hostnameOverride := "override.example.host"
62+
pod.Spec.HostnameOverride = &hostnameOverride
63+
output := []string{fmt.Sprintf("%s;%s;", hostnameOverride, hostnameOverride)}
64+
e2eoutput.TestContainerOutput(ctx, f, "hostnameOverride overrides hostname", pod, 0, output)
65+
})
66+
5967
ginkgo.It("a pod with hostname and hostnameOverride fields will have hostnameOverride as hostname", func(ctx context.Context) {
6068
pod := newTestPod(f.Namespace.Name)
6169
hostname := "custom-host"

0 commit comments

Comments
 (0)