/
pod_hostnamefqdn.go
143 lines (126 loc) · 5.4 KB
/
pod_hostnamefqdn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/* This test check that setHostnameAsFQDN PodSpec field works as
* expected.
*/
package node
import (
"crypto/rand"
"fmt"
"math/big"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubernetes/test/e2e/framework"
imageutils "k8s.io/kubernetes/test/utils/image"
"github.com/onsi/ginkgo"
)
func generatePodName(base string) string {
id, err := rand.Int(rand.Reader, big.NewInt(214748))
if err != nil {
return base
}
return fmt.Sprintf("%s-%d", base, id)
}
func testPod() *v1.Pod {
podName := generatePodName("hostfqdn")
pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podName,
Labels: map[string]string{"name": podName},
Annotations: map[string]string{},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "test-container",
Image: imageutils.GetE2EImage(imageutils.BusyBox),
},
},
RestartPolicy: v1.RestartPolicyNever,
},
}
return pod
}
var _ = SIGDescribe("Hostname of Pod [Feature:SetHostnameAsFQDN]", func() {
f := framework.NewDefaultFramework("hostfqdn")
/*
Release: v1.19
Testname: Create Pod without fully qualified domain name (FQDN)
Description: A Pod that does not define the subdomain field in it spec, does not have FQDN.
*/
ginkgo.It("a pod without subdomain field does not have FQDN [Feature:SetHostnameAsFQDN]", func() {
pod := testPod()
pod.Spec.Containers[0].Command = []string{"sh", "-c", "echo $(hostname)';'$(hostname -f)';'"}
output := []string{fmt.Sprintf("%s;%s;", pod.ObjectMeta.Name, pod.ObjectMeta.Name)}
// Create Pod
f.TestContainerOutput("shotname only", pod, 0, output)
})
/*
Release: v1.19
Testname: Create Pod without FQDN, setHostnameAsFQDN field set to true
Description: A Pod that does not define the subdomain field in it spec, does not have FQDN.
Hence, SetHostnameAsFQDN feature has no effect.
*/
ginkgo.It("a pod without FQDN is not affected by SetHostnameAsFQDN field [Feature:SetHostnameAsFQDN]", func() {
pod := testPod()
// Setting setHostnameAsFQDN field to true should have no effect.
setHostnameAsFQDN := true
pod.Spec.SetHostnameAsFQDN = &setHostnameAsFQDN
pod.Spec.Containers[0].Command = []string{"sh", "-c", "echo $(hostname)';'$(hostname -f)';'"}
output := []string{fmt.Sprintf("%s;%s;", pod.ObjectMeta.Name, pod.ObjectMeta.Name)}
// Create Pod
f.TestContainerOutput("shotname only", pod, 0, output)
})
/*
Release: v1.19
Testname: Create Pod with FQDN, setHostnameAsFQDN field not defined.
Description: A Pod that defines the subdomain field in it spec has FQDN.
hostname command returns shortname (pod name in this case), and hostname -f returns FQDN.
*/
ginkgo.It("a pod with subdomain field has FQDN, hostname is shortname [Feature:SetHostnameAsFQDN]", func() {
pod := testPod()
pod.Spec.Containers[0].Command = []string{"sh", "-c", "echo $(hostname)';'$(hostname -f)';'"}
subdomain := "t"
// Set PodSpec subdomain field to generate FQDN for pod
pod.Spec.Subdomain = subdomain
// Expected Pod FQDN
hostFQDN := fmt.Sprintf("%s.%s.%s.svc.%s", pod.ObjectMeta.Name, subdomain, f.Namespace.Name, framework.TestContext.ClusterDNSDomain)
output := []string{fmt.Sprintf("%s;%s;", pod.ObjectMeta.Name, hostFQDN)}
// Create Pod
f.TestContainerOutput("shotname and fqdn", pod, 0, output)
})
/*
Release: v1.19
Testname: Create Pod with FQDN, setHostnameAsFQDN field set to true.
Description: A Pod that defines the subdomain field in it spec has FQDN. When setHostnameAsFQDN: true, the
hostname is set to be the FQDN. In this case, both commands hostname and hostname -f return the FQDN of the Pod.
*/
ginkgo.It("a pod with subdomain field has FQDN, when setHostnameAsFQDN is set to true, the FQDN is set as hostname [Feature:SetHostnameAsFQDN]", func() {
pod := testPod()
pod.Spec.Containers[0].Command = []string{"sh", "-c", "echo $(hostname)';'$(hostname -f)';'"}
subdomain := "t"
// Set PodSpec subdomain field to generate FQDN for pod
pod.Spec.Subdomain = subdomain
// Set PodSpec setHostnameAsFQDN to set FQDN as hostname
setHostnameAsFQDN := true
pod.Spec.SetHostnameAsFQDN = &setHostnameAsFQDN
// Expected Pod FQDN
hostFQDN := fmt.Sprintf("%s.%s.%s.svc.%s", pod.ObjectMeta.Name, subdomain, f.Namespace.Name, framework.TestContext.ClusterDNSDomain)
// Fail if FQDN is longer than 64 characters, otherwise the Pod will remain pending until test timeout.
// In Linux, 64 characters is the limit of the hostname kernel field, which this test sets to the pod FQDN.
framework.ExpectEqual(len(hostFQDN) < 65, true, fmt.Sprintf("The FQDN of the Pod cannot be longer than 64 characters, requested %s which is %d characters long.", hostFQDN, len(hostFQDN)))
output := []string{fmt.Sprintf("%s;%s;", hostFQDN, hostFQDN)}
// Create Pod
f.TestContainerOutput("fqdn and fqdn", pod, 0, output)
})
})