mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-30 06:54:01 +00:00
Merge pull request #55912 from MrHohn/kubelet-dns-cleanup
Automatic merge from submit-queue (batch tested with PRs 54837, 55970, 55912, 55898, 52977). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. Consolidate DNS codes in kubelet pkg **What this PR does / why we need it**: Follow up of https://github.com/kubernetes/kubernetes/pull/55651. Ref https://github.com/kubernetes/kubernetes/pull/55651#discussion_r151042281. This PRs consolidate DNS related codes in kubelet so that it could be self-contained. There is no actual code changes again. **Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*: Ref https://github.com/kubernetes/kubernetes/issues/55451 **Special notes for your reviewer**: cc @bowei @thockin **Release note**: ```release-note NONE ```
This commit is contained in:
commit
a74ffbb521
@ -47,7 +47,7 @@ type HandlerRunner interface {
|
||||
// able to get necessary informations like the RunContainerOptions, DNS settings, Host IP.
|
||||
type RuntimeHelper interface {
|
||||
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
|
||||
// of a pod.
|
||||
GetPodCgroupParent(pod *v1.Pod) string
|
||||
|
@ -14,6 +14,7 @@ go_library(
|
||||
importpath = "k8s.io/kubernetes/pkg/kubelet/container/testing",
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//pkg/kubelet/apis/cri/v1alpha1/runtime:go_default_library",
|
||||
"//pkg/kubelet/container:go_default_library",
|
||||
"//pkg/volume:go_default_library",
|
||||
"//vendor/github.com/golang/mock/gomock:go_default_library",
|
||||
|
@ -19,6 +19,7 @@ package testing
|
||||
import (
|
||||
"k8s.io/api/core/v1"
|
||||
kubetypes "k8s.io/apimachinery/pkg/types"
|
||||
runtimeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
|
||||
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
||||
)
|
||||
|
||||
@ -26,6 +27,7 @@ import (
|
||||
type FakeRuntimeHelper struct {
|
||||
DNSServers []string
|
||||
DNSSearches []string
|
||||
DNSOptions []string
|
||||
HostName string
|
||||
HostDomain string
|
||||
PodContainerDir string
|
||||
@ -44,8 +46,11 @@ func (f *FakeRuntimeHelper) GetPodCgroupParent(pod *v1.Pod) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (f *FakeRuntimeHelper) GetClusterDNS(pod *v1.Pod) ([]string, []string, []string, bool, error) {
|
||||
return f.DNSServers, f.DNSSearches, nil, false, f.Err
|
||||
func (f *FakeRuntimeHelper) GetPodDNS(pod *v1.Pod) (*runtimeapi.DNSConfig, error) {
|
||||
return &runtimeapi.DNSConfig{
|
||||
Servers: f.DNSServers,
|
||||
Searches: f.DNSSearches,
|
||||
Options: f.DNSOptions}, f.Err
|
||||
}
|
||||
|
||||
// This is not used by docker runtime.
|
||||
|
@ -22,6 +22,7 @@ import (
|
||||
"github.com/golang/glog"
|
||||
"k8s.io/api/core/v1"
|
||||
clientset "k8s.io/client-go/kubernetes"
|
||||
runtimeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
|
||||
"k8s.io/kubernetes/pkg/kubelet/apis/kubeletconfig"
|
||||
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
||||
"k8s.io/kubernetes/pkg/kubelet/network"
|
||||
@ -281,10 +282,9 @@ func getIPTablesMark(bit int) string {
|
||||
return fmt.Sprintf("%#08x/%#08x", value, value)
|
||||
}
|
||||
|
||||
// GetClusterDNS returns a list of the DNS servers, a list of the DNS search
|
||||
// domains of the cluster, and a list of resolv.conf options.
|
||||
// GetPodDNS returns DNS setttings for the pod.
|
||||
// This function is defined in kubecontainer.RuntimeHelper interface so we
|
||||
// have to implement it.
|
||||
func (kl *Kubelet) GetClusterDNS(pod *v1.Pod) ([]string, []string, []string, bool, error) {
|
||||
return kl.dnsConfigurer.GetClusterDNS(pod)
|
||||
func (kl *Kubelet) GetPodDNS(pod *v1.Pod) (*runtimeapi.DNSConfig, error) {
|
||||
return kl.dnsConfigurer.GetPodDNS(pod)
|
||||
}
|
||||
|
@ -41,11 +41,6 @@ const (
|
||||
minQuotaPeriod = 1000
|
||||
)
|
||||
|
||||
var (
|
||||
// The default dns opt strings
|
||||
defaultDNSOptions = []string{"ndots:5"}
|
||||
)
|
||||
|
||||
type podsByID []*kubecontainer.Pod
|
||||
|
||||
func (b podsByID) Len() int { return len(b) }
|
||||
|
@ -74,19 +74,11 @@ func (m *kubeGenericRuntimeManager) generatePodSandboxConfig(pod *v1.Pod, attemp
|
||||
Annotations: newPodAnnotations(pod),
|
||||
}
|
||||
|
||||
dnsServers, dnsSearches, dnsOptions, useClusterFirstPolicy, err := m.runtimeHelper.GetClusterDNS(pod)
|
||||
dnsConfig, err := m.runtimeHelper.GetPodDNS(pod)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
podSandboxConfig.DnsConfig = &runtimeapi.DNSConfig{
|
||||
Servers: dnsServers,
|
||||
Searches: dnsSearches,
|
||||
Options: dnsOptions,
|
||||
}
|
||||
|
||||
if useClusterFirstPolicy {
|
||||
podSandboxConfig.DnsConfig.Options = defaultDNSOptions
|
||||
}
|
||||
podSandboxConfig.DnsConfig = dnsConfig
|
||||
|
||||
if !kubecontainer.IsHostNetworkPod(pod) {
|
||||
// TODO: Add domain support in new runtime interface
|
||||
|
@ -6,6 +6,7 @@ go_library(
|
||||
importpath = "k8s.io/kubernetes/pkg/kubelet/network/dns",
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//pkg/kubelet/apis/cri/v1alpha1/runtime:go_default_library",
|
||||
"//pkg/kubelet/container:go_default_library",
|
||||
"//pkg/kubelet/util/format:go_default_library",
|
||||
"//vendor/github.com/golang/glog:go_default_library",
|
||||
|
@ -27,12 +27,18 @@ import (
|
||||
|
||||
"k8s.io/api/core/v1"
|
||||
"k8s.io/client-go/tools/record"
|
||||
runtimeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
|
||||
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
||||
"k8s.io/kubernetes/pkg/kubelet/util/format"
|
||||
|
||||
"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.
|
||||
type Configurer struct {
|
||||
recorder record.EventRecorder
|
||||
@ -219,22 +225,20 @@ func parseResolvConf(reader io.Reader) (nameservers []string, searches []string,
|
||||
return nameservers, searches, options, nil
|
||||
}
|
||||
|
||||
// GetClusterDNS returns a list of the DNS servers, a list of the DNS search
|
||||
// domains of the cluster, and a list of resolv.conf options.
|
||||
// TODO: This should return a struct.
|
||||
func (c *Configurer) GetClusterDNS(pod *v1.Pod) ([]string, []string, []string, bool, error) {
|
||||
// GetPodDNS returns DNS setttings for the pod.
|
||||
func (c *Configurer) GetPodDNS(pod *v1.Pod) (*runtimeapi.DNSConfig, error) {
|
||||
var hostDNS, hostSearch, hostOptions []string
|
||||
// Get host DNS settings
|
||||
if c.ResolverConfig != "" {
|
||||
f, err := os.Open(c.ResolverConfig)
|
||||
if err != nil {
|
||||
return nil, nil, nil, false, err
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
hostDNS, hostSearch, hostOptions, err = parseResolvConf(f)
|
||||
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)
|
||||
@ -268,7 +272,10 @@ func (c *Configurer) GetClusterDNS(pod *v1.Pod) ([]string, []string, []string, b
|
||||
} else {
|
||||
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
|
||||
@ -280,7 +287,10 @@ func (c *Configurer) GetClusterDNS(pod *v1.Pod) ([]string, []string, []string, b
|
||||
}
|
||||
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
|
||||
|
@ -152,7 +152,7 @@ func TestComposeDNSSearch(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetClusterDNS(t *testing.T) {
|
||||
func TestGetPodDNS(t *testing.T) {
|
||||
recorder := record.NewFakeRecorder(20)
|
||||
nodeRef := &v1.ObjectReference{
|
||||
Kind: "Node",
|
||||
@ -179,10 +179,11 @@ func TestGetClusterDNS(t *testing.T) {
|
||||
}, 4)
|
||||
for i, pod := range pods {
|
||||
var err error
|
||||
options[i].DNS, options[i].DNSSearch, _, _, err = configurer.GetClusterDNS(pod)
|
||||
dnsConfig, err := configurer.GetPodDNS(pod)
|
||||
if err != nil {
|
||||
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 {
|
||||
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)
|
||||
for i, pod := range pods {
|
||||
var err error
|
||||
options[i].DNS, options[i].DNSSearch, _, _, err = configurer.GetClusterDNS(pod)
|
||||
dnsConfig, err := configurer.GetPodDNS(pod)
|
||||
if err != nil {
|
||||
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)
|
||||
if len(options[0].DNS) != 1 {
|
||||
|
@ -1041,17 +1041,17 @@ func (r *Runtime) generateRunCommand(pod *v1.Pod, uuid, networkNamespaceID strin
|
||||
}
|
||||
} else {
|
||||
// Setup DNS.
|
||||
dnsServers, dnsSearches, _, _, err := r.runtimeHelper.GetClusterDNS(pod)
|
||||
dnsConfig, err := r.runtimeHelper.GetPodDNS(pod)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, server := range dnsServers {
|
||||
for _, server := range dnsConfig.Servers {
|
||||
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))
|
||||
}
|
||||
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))
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user