mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-08 03:33:56 +00:00
Consolidate DNS codes in kubelet pkg
This commit is contained in:
parent
a82460d772
commit
386d1b61bd
@ -47,7 +47,7 @@ type HandlerRunner interface {
|
|||||||
// able to get necessary informations like the RunContainerOptions, DNS settings, Host IP.
|
// able to get necessary informations like the RunContainerOptions, DNS settings, Host IP.
|
||||||
type RuntimeHelper interface {
|
type RuntimeHelper interface {
|
||||||
GenerateRunContainerOptions(pod *v1.Pod, container *v1.Container, podIP string) (contOpts *RunContainerOptions, err error)
|
GenerateRunContainerOptions(pod *v1.Pod, container *v1.Container, podIP string) (contOpts *RunContainerOptions, err error)
|
||||||
GetClusterDNS(pod *v1.Pod) (dnsServers []string, dnsSearches []string, dnsOptions []string, useClusterFirstPolicy bool, err error)
|
GetPodDNS(pod *v1.Pod) (dnsConfig *runtimeapi.DNSConfig, err error)
|
||||||
// GetPodCgroupParent returns the CgroupName identifer, and its literal cgroupfs form on the host
|
// GetPodCgroupParent returns the CgroupName identifer, and its literal cgroupfs form on the host
|
||||||
// of a pod.
|
// of a pod.
|
||||||
GetPodCgroupParent(pod *v1.Pod) string
|
GetPodCgroupParent(pod *v1.Pod) string
|
||||||
|
@ -14,6 +14,7 @@ go_library(
|
|||||||
importpath = "k8s.io/kubernetes/pkg/kubelet/container/testing",
|
importpath = "k8s.io/kubernetes/pkg/kubelet/container/testing",
|
||||||
visibility = ["//visibility:public"],
|
visibility = ["//visibility:public"],
|
||||||
deps = [
|
deps = [
|
||||||
|
"//pkg/kubelet/apis/cri/v1alpha1/runtime:go_default_library",
|
||||||
"//pkg/kubelet/container:go_default_library",
|
"//pkg/kubelet/container:go_default_library",
|
||||||
"//pkg/volume:go_default_library",
|
"//pkg/volume:go_default_library",
|
||||||
"//vendor/github.com/golang/mock/gomock:go_default_library",
|
"//vendor/github.com/golang/mock/gomock:go_default_library",
|
||||||
|
@ -19,6 +19,7 @@ package testing
|
|||||||
import (
|
import (
|
||||||
"k8s.io/api/core/v1"
|
"k8s.io/api/core/v1"
|
||||||
kubetypes "k8s.io/apimachinery/pkg/types"
|
kubetypes "k8s.io/apimachinery/pkg/types"
|
||||||
|
runtimeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
|
||||||
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -26,6 +27,7 @@ import (
|
|||||||
type FakeRuntimeHelper struct {
|
type FakeRuntimeHelper struct {
|
||||||
DNSServers []string
|
DNSServers []string
|
||||||
DNSSearches []string
|
DNSSearches []string
|
||||||
|
DNSOptions []string
|
||||||
HostName string
|
HostName string
|
||||||
HostDomain string
|
HostDomain string
|
||||||
PodContainerDir string
|
PodContainerDir string
|
||||||
@ -44,8 +46,11 @@ func (f *FakeRuntimeHelper) GetPodCgroupParent(pod *v1.Pod) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FakeRuntimeHelper) GetClusterDNS(pod *v1.Pod) ([]string, []string, []string, bool, error) {
|
func (f *FakeRuntimeHelper) GetPodDNS(pod *v1.Pod) (*runtimeapi.DNSConfig, error) {
|
||||||
return f.DNSServers, f.DNSSearches, nil, false, f.Err
|
return &runtimeapi.DNSConfig{
|
||||||
|
Servers: f.DNSServers,
|
||||||
|
Searches: f.DNSSearches,
|
||||||
|
Options: f.DNSOptions}, f.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is not used by docker runtime.
|
// This is not used by docker runtime.
|
||||||
|
@ -22,6 +22,7 @@ import (
|
|||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"k8s.io/api/core/v1"
|
"k8s.io/api/core/v1"
|
||||||
clientset "k8s.io/client-go/kubernetes"
|
clientset "k8s.io/client-go/kubernetes"
|
||||||
|
runtimeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
|
||||||
"k8s.io/kubernetes/pkg/kubelet/apis/kubeletconfig"
|
"k8s.io/kubernetes/pkg/kubelet/apis/kubeletconfig"
|
||||||
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
||||||
"k8s.io/kubernetes/pkg/kubelet/network"
|
"k8s.io/kubernetes/pkg/kubelet/network"
|
||||||
@ -281,10 +282,9 @@ func getIPTablesMark(bit int) string {
|
|||||||
return fmt.Sprintf("%#08x/%#08x", value, value)
|
return fmt.Sprintf("%#08x/%#08x", value, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetClusterDNS returns a list of the DNS servers, a list of the DNS search
|
// GetPodDNS returns DNS setttings for the pod.
|
||||||
// domains of the cluster, and a list of resolv.conf options.
|
|
||||||
// This function is defined in kubecontainer.RuntimeHelper interface so we
|
// This function is defined in kubecontainer.RuntimeHelper interface so we
|
||||||
// have to implement it.
|
// have to implement it.
|
||||||
func (kl *Kubelet) GetClusterDNS(pod *v1.Pod) ([]string, []string, []string, bool, error) {
|
func (kl *Kubelet) GetPodDNS(pod *v1.Pod) (*runtimeapi.DNSConfig, error) {
|
||||||
return kl.dnsConfigurer.GetClusterDNS(pod)
|
return kl.dnsConfigurer.GetPodDNS(pod)
|
||||||
}
|
}
|
||||||
|
@ -41,11 +41,6 @@ const (
|
|||||||
minQuotaPeriod = 1000
|
minQuotaPeriod = 1000
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
// The default dns opt strings
|
|
||||||
defaultDNSOptions = []string{"ndots:5"}
|
|
||||||
)
|
|
||||||
|
|
||||||
type podsByID []*kubecontainer.Pod
|
type podsByID []*kubecontainer.Pod
|
||||||
|
|
||||||
func (b podsByID) Len() int { return len(b) }
|
func (b podsByID) Len() int { return len(b) }
|
||||||
|
@ -74,19 +74,11 @@ func (m *kubeGenericRuntimeManager) generatePodSandboxConfig(pod *v1.Pod, attemp
|
|||||||
Annotations: newPodAnnotations(pod),
|
Annotations: newPodAnnotations(pod),
|
||||||
}
|
}
|
||||||
|
|
||||||
dnsServers, dnsSearches, dnsOptions, useClusterFirstPolicy, err := m.runtimeHelper.GetClusterDNS(pod)
|
dnsConfig, err := m.runtimeHelper.GetPodDNS(pod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
podSandboxConfig.DnsConfig = &runtimeapi.DNSConfig{
|
podSandboxConfig.DnsConfig = dnsConfig
|
||||||
Servers: dnsServers,
|
|
||||||
Searches: dnsSearches,
|
|
||||||
Options: dnsOptions,
|
|
||||||
}
|
|
||||||
|
|
||||||
if useClusterFirstPolicy {
|
|
||||||
podSandboxConfig.DnsConfig.Options = defaultDNSOptions
|
|
||||||
}
|
|
||||||
|
|
||||||
if !kubecontainer.IsHostNetworkPod(pod) {
|
if !kubecontainer.IsHostNetworkPod(pod) {
|
||||||
// TODO: Add domain support in new runtime interface
|
// TODO: Add domain support in new runtime interface
|
||||||
|
@ -6,6 +6,7 @@ go_library(
|
|||||||
importpath = "k8s.io/kubernetes/pkg/kubelet/network/dns",
|
importpath = "k8s.io/kubernetes/pkg/kubelet/network/dns",
|
||||||
visibility = ["//visibility:public"],
|
visibility = ["//visibility:public"],
|
||||||
deps = [
|
deps = [
|
||||||
|
"//pkg/kubelet/apis/cri/v1alpha1/runtime:go_default_library",
|
||||||
"//pkg/kubelet/container:go_default_library",
|
"//pkg/kubelet/container:go_default_library",
|
||||||
"//pkg/kubelet/util/format:go_default_library",
|
"//pkg/kubelet/util/format:go_default_library",
|
||||||
"//vendor/github.com/golang/glog:go_default_library",
|
"//vendor/github.com/golang/glog:go_default_library",
|
||||||
|
@ -27,12 +27,18 @@ import (
|
|||||||
|
|
||||||
"k8s.io/api/core/v1"
|
"k8s.io/api/core/v1"
|
||||||
"k8s.io/client-go/tools/record"
|
"k8s.io/client-go/tools/record"
|
||||||
|
runtimeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
|
||||||
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
||||||
"k8s.io/kubernetes/pkg/kubelet/util/format"
|
"k8s.io/kubernetes/pkg/kubelet/util/format"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// The default dns opt strings.
|
||||||
|
defaultDNSOptions = []string{"ndots:5"}
|
||||||
|
)
|
||||||
|
|
||||||
// Configurer is used for setting up DNS resolver configuration when launching pods.
|
// Configurer is used for setting up DNS resolver configuration when launching pods.
|
||||||
type Configurer struct {
|
type Configurer struct {
|
||||||
recorder record.EventRecorder
|
recorder record.EventRecorder
|
||||||
@ -219,22 +225,20 @@ func parseResolvConf(reader io.Reader) (nameservers []string, searches []string,
|
|||||||
return nameservers, searches, options, nil
|
return nameservers, searches, options, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetClusterDNS returns a list of the DNS servers, a list of the DNS search
|
// GetPodDNS returns DNS setttings for the pod.
|
||||||
// domains of the cluster, and a list of resolv.conf options.
|
func (c *Configurer) GetPodDNS(pod *v1.Pod) (*runtimeapi.DNSConfig, error) {
|
||||||
// TODO: This should return a struct.
|
|
||||||
func (c *Configurer) GetClusterDNS(pod *v1.Pod) ([]string, []string, []string, bool, error) {
|
|
||||||
var hostDNS, hostSearch, hostOptions []string
|
var hostDNS, hostSearch, hostOptions []string
|
||||||
// Get host DNS settings
|
// Get host DNS settings
|
||||||
if c.ResolverConfig != "" {
|
if c.ResolverConfig != "" {
|
||||||
f, err := os.Open(c.ResolverConfig)
|
f, err := os.Open(c.ResolverConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, nil, false, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
hostDNS, hostSearch, hostOptions, err = parseResolvConf(f)
|
hostDNS, hostSearch, hostOptions, err = parseResolvConf(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, nil, false, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
useClusterFirstPolicy := ((pod.Spec.DNSPolicy == v1.DNSClusterFirst && !kubecontainer.IsHostNetworkPod(pod)) || pod.Spec.DNSPolicy == v1.DNSClusterFirstWithHostNet)
|
useClusterFirstPolicy := ((pod.Spec.DNSPolicy == v1.DNSClusterFirst && !kubecontainer.IsHostNetworkPod(pod)) || pod.Spec.DNSPolicy == v1.DNSClusterFirstWithHostNet)
|
||||||
@ -268,7 +272,10 @@ func (c *Configurer) GetClusterDNS(pod *v1.Pod) ([]string, []string, []string, b
|
|||||||
} else {
|
} else {
|
||||||
hostSearch = c.formDNSSearchForDNSDefault(hostSearch, pod)
|
hostSearch = c.formDNSSearchForDNSDefault(hostSearch, pod)
|
||||||
}
|
}
|
||||||
return hostDNS, hostSearch, hostOptions, useClusterFirstPolicy, nil
|
return &runtimeapi.DNSConfig{
|
||||||
|
Servers: hostDNS,
|
||||||
|
Searches: hostSearch,
|
||||||
|
Options: hostOptions}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// for a pod with DNSClusterFirst policy, the cluster DNS server is the only nameserver configured for
|
// for a pod with DNSClusterFirst policy, the cluster DNS server is the only nameserver configured for
|
||||||
@ -280,7 +287,10 @@ func (c *Configurer) GetClusterDNS(pod *v1.Pod) ([]string, []string, []string, b
|
|||||||
}
|
}
|
||||||
dnsSearch := c.formDNSSearch(hostSearch, pod)
|
dnsSearch := c.formDNSSearch(hostSearch, pod)
|
||||||
|
|
||||||
return dns, dnsSearch, hostOptions, useClusterFirstPolicy, nil
|
return &runtimeapi.DNSConfig{
|
||||||
|
Servers: dns,
|
||||||
|
Searches: dnsSearch,
|
||||||
|
Options: defaultDNSOptions}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetupDNSinContainerizedMounter replaces the nameserver in containerized-mounter's rootfs/etc/resolve.conf with kubelet.ClusterDNS
|
// SetupDNSinContainerizedMounter replaces the nameserver in containerized-mounter's rootfs/etc/resolve.conf with kubelet.ClusterDNS
|
||||||
|
@ -152,7 +152,7 @@ func TestComposeDNSSearch(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetClusterDNS(t *testing.T) {
|
func TestGetPodDNS(t *testing.T) {
|
||||||
recorder := record.NewFakeRecorder(20)
|
recorder := record.NewFakeRecorder(20)
|
||||||
nodeRef := &v1.ObjectReference{
|
nodeRef := &v1.ObjectReference{
|
||||||
Kind: "Node",
|
Kind: "Node",
|
||||||
@ -179,10 +179,11 @@ func TestGetClusterDNS(t *testing.T) {
|
|||||||
}, 4)
|
}, 4)
|
||||||
for i, pod := range pods {
|
for i, pod := range pods {
|
||||||
var err error
|
var err error
|
||||||
options[i].DNS, options[i].DNSSearch, _, _, err = configurer.GetClusterDNS(pod)
|
dnsConfig, err := configurer.GetPodDNS(pod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to generate container options: %v", err)
|
t.Fatalf("failed to generate container options: %v", err)
|
||||||
}
|
}
|
||||||
|
options[i].DNS, options[i].DNSSearch = dnsConfig.Servers, dnsConfig.Searches
|
||||||
}
|
}
|
||||||
if len(options[0].DNS) != 1 || options[0].DNS[0] != clusterNS {
|
if len(options[0].DNS) != 1 || options[0].DNS[0] != clusterNS {
|
||||||
t.Errorf("expected nameserver %s, got %+v", clusterNS, options[0].DNS)
|
t.Errorf("expected nameserver %s, got %+v", clusterNS, options[0].DNS)
|
||||||
@ -213,10 +214,11 @@ func TestGetClusterDNS(t *testing.T) {
|
|||||||
configurer = NewConfigurer(recorder, nodeRef, nil, testClusterDNS, testClusterDNSDomain, testResolverConfig)
|
configurer = NewConfigurer(recorder, nodeRef, nil, testClusterDNS, testClusterDNSDomain, testResolverConfig)
|
||||||
for i, pod := range pods {
|
for i, pod := range pods {
|
||||||
var err error
|
var err error
|
||||||
options[i].DNS, options[i].DNSSearch, _, _, err = configurer.GetClusterDNS(pod)
|
dnsConfig, err := configurer.GetPodDNS(pod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to generate container options: %v", err)
|
t.Fatalf("failed to generate container options: %v", err)
|
||||||
}
|
}
|
||||||
|
options[i].DNS, options[i].DNSSearch = dnsConfig.Servers, dnsConfig.Searches
|
||||||
}
|
}
|
||||||
t.Logf("nameservers %+v", options[1].DNS)
|
t.Logf("nameservers %+v", options[1].DNS)
|
||||||
if len(options[0].DNS) != 1 {
|
if len(options[0].DNS) != 1 {
|
||||||
|
@ -1041,17 +1041,17 @@ func (r *Runtime) generateRunCommand(pod *v1.Pod, uuid, networkNamespaceID strin
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Setup DNS.
|
// Setup DNS.
|
||||||
dnsServers, dnsSearches, _, _, err := r.runtimeHelper.GetClusterDNS(pod)
|
dnsConfig, err := r.runtimeHelper.GetPodDNS(pod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
for _, server := range dnsServers {
|
for _, server := range dnsConfig.Servers {
|
||||||
runPrepared = append(runPrepared, fmt.Sprintf("--dns=%s", server))
|
runPrepared = append(runPrepared, fmt.Sprintf("--dns=%s", server))
|
||||||
}
|
}
|
||||||
for _, search := range dnsSearches {
|
for _, search := range dnsConfig.Searches {
|
||||||
runPrepared = append(runPrepared, fmt.Sprintf("--dns-search=%s", search))
|
runPrepared = append(runPrepared, fmt.Sprintf("--dns-search=%s", search))
|
||||||
}
|
}
|
||||||
if len(dnsServers) > 0 || len(dnsSearches) > 0 {
|
if len(dnsConfig.Servers) > 0 || len(dnsConfig.Searches) > 0 {
|
||||||
runPrepared = append(runPrepared, fmt.Sprintf("--dns-opt=%s", defaultDNSOption))
|
runPrepared = append(runPrepared, fmt.Sprintf("--dns-opt=%s", defaultDNSOption))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user