mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 04:33:26 +00:00
More unit test for configurable pod resolv.conf
This commit is contained in:
parent
44a64edc54
commit
9e5e0c6a59
@ -18,7 +18,9 @@ package dns
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -487,64 +489,134 @@ func TestGetPodDNSCustom(t *testing.T) {
|
|||||||
UID: types.UID("testNode"),
|
UID: types.UID("testNode"),
|
||||||
Namespace: "",
|
Namespace: "",
|
||||||
}
|
}
|
||||||
clusterNS := "203.0.113.1"
|
|
||||||
|
testPodNamespace := "testNS"
|
||||||
|
testClusterNameserver := "10.0.0.10"
|
||||||
testClusterDNSDomain := "kubernetes.io"
|
testClusterDNSDomain := "kubernetes.io"
|
||||||
testClusterDNS := []net.IP{net.ParseIP(clusterNS)}
|
testSvcDomain := fmt.Sprintf("svc.%s", testClusterDNSDomain)
|
||||||
testOptionValue := "3"
|
testNsSvcDomain := fmt.Sprintf("%s.svc.%s", testPodNamespace, testClusterDNSDomain)
|
||||||
|
testNdotsOptionValue := "3"
|
||||||
|
testHostNameserver := "8.8.8.8"
|
||||||
|
testHostDomain := "host.domain"
|
||||||
|
|
||||||
configurer := NewConfigurer(recorder, nodeRef, nil, testClusterDNS, testClusterDNSDomain, "")
|
testPod := &v1.Pod{
|
||||||
|
|
||||||
pod := &v1.Pod{
|
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
UID: "",
|
Name: "test_pod",
|
||||||
Name: "test_pod",
|
Namespace: testPodNamespace,
|
||||||
Namespace: "testNS",
|
|
||||||
Annotations: map[string]string{},
|
|
||||||
},
|
},
|
||||||
Spec: v1.PodSpec{
|
|
||||||
DNSPolicy: v1.DNSClusterFirst,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
clusterFirstDNSConfig, err := configurer.GetPodDNS(pod)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Preparing clusterFirstDNSConfig: GetPodDNS(%v), unexpected error: %v", pod, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Overwrite DNSPolicy for testing.
|
resolvConfContent := []byte(fmt.Sprintf("nameserver %s\nsearch %s\n", testHostNameserver, testHostDomain))
|
||||||
pod.Spec.DNSPolicy = v1.DNSNone
|
tmpfile, err := ioutil.TempFile("", "tmpResolvConf")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.Remove(tmpfile.Name())
|
||||||
|
if _, err := tmpfile.Write(resolvConfContent); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := tmpfile.Close(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
configurer := NewConfigurer(recorder, nodeRef, nil, []net.IP{net.ParseIP(testClusterNameserver)}, testClusterDNSDomain, tmpfile.Name())
|
||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
desc string
|
desc string
|
||||||
customPodDNSFeatureGate bool
|
customPodDNSFeatureGate bool
|
||||||
|
hostnetwork bool
|
||||||
|
dnsPolicy v1.DNSPolicy
|
||||||
dnsConfig *v1.PodDNSConfig
|
dnsConfig *v1.PodDNSConfig
|
||||||
expectedDNSConfig *runtimeapi.DNSConfig
|
expectedDNSConfig *runtimeapi.DNSConfig
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
desc: "feature gate is disabled, DNSNone should fallback to DNSClusterFirst",
|
desc: "feature gate is disabled, DNSNone should fallback to DNSClusterFirst",
|
||||||
expectedDNSConfig: clusterFirstDNSConfig,
|
dnsPolicy: v1.DNSNone,
|
||||||
|
expectedDNSConfig: &runtimeapi.DNSConfig{
|
||||||
|
Servers: []string{testClusterNameserver},
|
||||||
|
Searches: []string{testNsSvcDomain, testSvcDomain, testClusterDNSDomain, testHostDomain},
|
||||||
|
Options: []string{"ndots:5"},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "feature gate is enabled, DNSNone without DNSConfig should have empty DNS settings",
|
desc: "feature gate is enabled, DNSNone without DNSConfig should have empty DNS settings",
|
||||||
customPodDNSFeatureGate: true,
|
customPodDNSFeatureGate: true,
|
||||||
|
dnsPolicy: v1.DNSNone,
|
||||||
expectedDNSConfig: &runtimeapi.DNSConfig{},
|
expectedDNSConfig: &runtimeapi.DNSConfig{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "feature gate is enabled, DNSNone with DNSConfig should have a merged DNS settings",
|
desc: "feature gate is enabled, DNSNone with DNSConfig should have a merged DNS settings",
|
||||||
customPodDNSFeatureGate: true,
|
customPodDNSFeatureGate: true,
|
||||||
|
dnsPolicy: v1.DNSNone,
|
||||||
dnsConfig: &v1.PodDNSConfig{
|
dnsConfig: &v1.PodDNSConfig{
|
||||||
Nameservers: []string{"10.0.0.10"},
|
Nameservers: []string{"203.0.113.1"},
|
||||||
Searches: []string{"my.domain", "second.domain"},
|
Searches: []string{"my.domain", "second.domain"},
|
||||||
Options: []v1.PodDNSConfigOption{
|
Options: []v1.PodDNSConfigOption{
|
||||||
{Name: "ndots", Value: &testOptionValue},
|
{Name: "ndots", Value: &testNdotsOptionValue},
|
||||||
{Name: "debug"},
|
{Name: "debug"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expectedDNSConfig: &runtimeapi.DNSConfig{
|
expectedDNSConfig: &runtimeapi.DNSConfig{
|
||||||
Servers: []string{"10.0.0.10"},
|
Servers: []string{"203.0.113.1"},
|
||||||
Searches: []string{"my.domain", "second.domain"},
|
Searches: []string{"my.domain", "second.domain"},
|
||||||
Options: []string{"ndots:3", "debug"},
|
Options: []string{"ndots:3", "debug"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
desc: "feature gate is enabled, DNSClusterFirst with DNSConfig should have a merged DNS settings",
|
||||||
|
customPodDNSFeatureGate: true,
|
||||||
|
dnsPolicy: v1.DNSClusterFirst,
|
||||||
|
dnsConfig: &v1.PodDNSConfig{
|
||||||
|
Nameservers: []string{"10.0.0.11"},
|
||||||
|
Searches: []string{"my.domain"},
|
||||||
|
Options: []v1.PodDNSConfigOption{
|
||||||
|
{Name: "ndots", Value: &testNdotsOptionValue},
|
||||||
|
{Name: "debug"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectedDNSConfig: &runtimeapi.DNSConfig{
|
||||||
|
Servers: []string{testClusterNameserver, "10.0.0.11"},
|
||||||
|
Searches: []string{testNsSvcDomain, testSvcDomain, testClusterDNSDomain, testHostDomain, "my.domain"},
|
||||||
|
Options: []string{"ndots:3", "debug"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "feature gate is enabled, DNSClusterFirstWithHostNet with DNSConfig should have a merged DNS settings",
|
||||||
|
customPodDNSFeatureGate: true,
|
||||||
|
hostnetwork: true,
|
||||||
|
dnsPolicy: v1.DNSClusterFirstWithHostNet,
|
||||||
|
dnsConfig: &v1.PodDNSConfig{
|
||||||
|
Nameservers: []string{"10.0.0.11"},
|
||||||
|
Searches: []string{"my.domain"},
|
||||||
|
Options: []v1.PodDNSConfigOption{
|
||||||
|
{Name: "ndots", Value: &testNdotsOptionValue},
|
||||||
|
{Name: "debug"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectedDNSConfig: &runtimeapi.DNSConfig{
|
||||||
|
Servers: []string{testClusterNameserver, "10.0.0.11"},
|
||||||
|
Searches: []string{testNsSvcDomain, testSvcDomain, testClusterDNSDomain, testHostDomain, "my.domain"},
|
||||||
|
Options: []string{"ndots:3", "debug"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "feature gate is enabled, DNSDefault with DNSConfig should have a merged DNS settings",
|
||||||
|
customPodDNSFeatureGate: true,
|
||||||
|
dnsPolicy: v1.DNSDefault,
|
||||||
|
dnsConfig: &v1.PodDNSConfig{
|
||||||
|
Nameservers: []string{"10.0.0.11"},
|
||||||
|
Searches: []string{"my.domain"},
|
||||||
|
Options: []v1.PodDNSConfigOption{
|
||||||
|
{Name: "ndots", Value: &testNdotsOptionValue},
|
||||||
|
{Name: "debug"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectedDNSConfig: &runtimeapi.DNSConfig{
|
||||||
|
Servers: []string{testHostNameserver, "10.0.0.11"},
|
||||||
|
Searches: []string{testHostDomain, "my.domain"},
|
||||||
|
Options: []string{"ndots:3", "debug"},
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
@ -552,14 +624,16 @@ func TestGetPodDNSCustom(t *testing.T) {
|
|||||||
t.Errorf("Failed to set CustomPodDNS feature gate: %v", err)
|
t.Errorf("Failed to set CustomPodDNS feature gate: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
pod.Spec.DNSConfig = tc.dnsConfig
|
testPod.Spec.HostNetwork = tc.hostnetwork
|
||||||
|
testPod.Spec.DNSConfig = tc.dnsConfig
|
||||||
|
testPod.Spec.DNSPolicy = tc.dnsPolicy
|
||||||
|
|
||||||
resDNSConfig, err := configurer.GetPodDNS(pod)
|
resDNSConfig, err := configurer.GetPodDNS(testPod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("%s: GetPodDNS(%v), unexpected error: %v", tc.desc, pod, err)
|
t.Errorf("%s: GetPodDNS(%v), unexpected error: %v", tc.desc, testPod, err)
|
||||||
}
|
}
|
||||||
if !dnsConfigsAreEqual(resDNSConfig, tc.expectedDNSConfig) {
|
if !dnsConfigsAreEqual(resDNSConfig, tc.expectedDNSConfig) {
|
||||||
t.Errorf("%s: GetPodDNS(%v)=%v, want %v", tc.desc, pod, resDNSConfig, tc.expectedDNSConfig)
|
t.Errorf("%s: GetPodDNS(%v)=%v, want %v", tc.desc, testPod, resDNSConfig, tc.expectedDNSConfig)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user