mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 02:41:25 +00:00
Added ability for Docker containers to set usage of dns settings along with hostNetwork is true
Introduced chages: 1. Re-writing of the resolv.conf file generated by docker. Cluster dns settings aren't passed anymore to docker api in all cases, not only for pods with host network: the resolver conf will be overwritten after infra-container creation to override docker's behaviour. 2. Added new one dnsPolicy - 'ClusterFirstWithHostNet', so now there are: - ClusterFirstWithHostNet - use dns settings in all cases, i.e. with hostNet=true as well - ClusterFirst - use dns settings unless hostNetwork is true - Default Fixes #17406
This commit is contained in:
parent
c7b53794d0
commit
fc8a37ec86
@ -39466,7 +39466,7 @@
|
||||
}
|
||||
},
|
||||
"dnsPolicy": {
|
||||
"description": "Set DNS policy for containers within the pod. One of 'ClusterFirst' or 'Default'. Defaults to \"ClusterFirst\".",
|
||||
"description": "Set DNS policy for containers within the pod. One of 'ClusterFirstWithHostNet', 'ClusterFirst' or 'Default'. Defaults to \"ClusterFirst\". To have DNS options set along with hostNetwork, you have to specify DNS policy explicitly to 'ClusterFirstWithHostNet'.",
|
||||
"type": "string"
|
||||
},
|
||||
"hostIPC": {
|
||||
|
@ -1303,7 +1303,7 @@
|
||||
},
|
||||
"dnsPolicy": {
|
||||
"type": "string",
|
||||
"description": "Set DNS policy for containers within the pod. One of 'ClusterFirst' or 'Default'. Defaults to \"ClusterFirst\"."
|
||||
"description": "Set DNS policy for containers within the pod. One of 'ClusterFirstWithHostNet', 'ClusterFirst' or 'Default'. Defaults to \"ClusterFirst\". To have DNS options set along with hostNetwork, you have to specify DNS policy explicitly to 'ClusterFirstWithHostNet'."
|
||||
},
|
||||
"nodeSelector": {
|
||||
"type": "object",
|
||||
|
@ -2542,7 +2542,7 @@
|
||||
},
|
||||
"dnsPolicy": {
|
||||
"type": "string",
|
||||
"description": "Set DNS policy for containers within the pod. One of 'ClusterFirst' or 'Default'. Defaults to \"ClusterFirst\"."
|
||||
"description": "Set DNS policy for containers within the pod. One of 'ClusterFirstWithHostNet', 'ClusterFirst' or 'Default'. Defaults to \"ClusterFirst\". To have DNS options set along with hostNetwork, you have to specify DNS policy explicitly to 'ClusterFirstWithHostNet'."
|
||||
},
|
||||
"nodeSelector": {
|
||||
"type": "object",
|
||||
|
@ -1325,7 +1325,7 @@
|
||||
},
|
||||
"dnsPolicy": {
|
||||
"type": "string",
|
||||
"description": "Set DNS policy for containers within the pod. One of 'ClusterFirst' or 'Default'. Defaults to \"ClusterFirst\"."
|
||||
"description": "Set DNS policy for containers within the pod. One of 'ClusterFirstWithHostNet', 'ClusterFirst' or 'Default'. Defaults to \"ClusterFirst\". To have DNS options set along with hostNetwork, you have to specify DNS policy explicitly to 'ClusterFirstWithHostNet'."
|
||||
},
|
||||
"nodeSelector": {
|
||||
"type": "object",
|
||||
|
@ -6789,7 +6789,7 @@
|
||||
},
|
||||
"dnsPolicy": {
|
||||
"type": "string",
|
||||
"description": "Set DNS policy for containers within the pod. One of 'ClusterFirst' or 'Default'. Defaults to \"ClusterFirst\"."
|
||||
"description": "Set DNS policy for containers within the pod. One of 'ClusterFirstWithHostNet', 'ClusterFirst' or 'Default'. Defaults to \"ClusterFirst\". To have DNS options set along with hostNetwork, you have to specify DNS policy explicitly to 'ClusterFirstWithHostNet'."
|
||||
},
|
||||
"nodeSelector": {
|
||||
"type": "object",
|
||||
|
@ -18628,7 +18628,7 @@
|
||||
},
|
||||
"dnsPolicy": {
|
||||
"type": "string",
|
||||
"description": "Set DNS policy for containers within the pod. One of 'ClusterFirst' or 'Default'. Defaults to \"ClusterFirst\"."
|
||||
"description": "Set DNS policy for containers within the pod. One of 'ClusterFirstWithHostNet', 'ClusterFirst' or 'Default'. Defaults to \"ClusterFirst\". To have DNS options set along with hostNetwork, you have to specify DNS policy explicitly to 'ClusterFirstWithHostNet'."
|
||||
},
|
||||
"nodeSelector": {
|
||||
"type": "object",
|
||||
|
@ -3568,7 +3568,7 @@ The StatefulSet guarantees that a given network identity will always map to the
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="tableblock halign-left valign-top"><p class="tableblock">dnsPolicy</p></td>
|
||||
<td class="tableblock halign-left valign-top"><p class="tableblock">Set DNS policy for containers within the pod. One of <em>ClusterFirst</em> or <em>Default</em>. Defaults to "ClusterFirst".</p></td>
|
||||
<td class="tableblock halign-left valign-top"><p class="tableblock">Set DNS policy for containers within the pod. One of <em>ClusterFirstWithHostNet</em>, <em>ClusterFirst</em> or <em>Default</em>. Defaults to "ClusterFirst". To have DNS options set along with hostNetwork, you have to specify DNS policy explicitly to <em>ClusterFirstWithHostNet</em>.</p></td>
|
||||
<td class="tableblock halign-left valign-top"><p class="tableblock">false</p></td>
|
||||
<td class="tableblock halign-left valign-top"><p class="tableblock">string</p></td>
|
||||
<td class="tableblock halign-left valign-top"></td>
|
||||
@ -6152,7 +6152,7 @@ Examples:<br>
|
||||
</div>
|
||||
<div id="footer">
|
||||
<div id="footer-text">
|
||||
Last updated 2017-03-01 11:37:39 UTC
|
||||
Last updated 2017-03-01 17:05:47 UTC
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
@ -4821,7 +4821,7 @@ Populated by the system when a graceful deletion is requested. Read-only. More i
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="tableblock halign-left valign-top"><p class="tableblock">dnsPolicy</p></td>
|
||||
<td class="tableblock halign-left valign-top"><p class="tableblock">Set DNS policy for containers within the pod. One of <em>ClusterFirst</em> or <em>Default</em>. Defaults to "ClusterFirst".</p></td>
|
||||
<td class="tableblock halign-left valign-top"><p class="tableblock">Set DNS policy for containers within the pod. One of <em>ClusterFirstWithHostNet</em>, <em>ClusterFirst</em> or <em>Default</em>. Defaults to "ClusterFirst". To have DNS options set along with hostNetwork, you have to specify DNS policy explicitly to <em>ClusterFirstWithHostNet</em>.</p></td>
|
||||
<td class="tableblock halign-left valign-top"><p class="tableblock">false</p></td>
|
||||
<td class="tableblock halign-left valign-top"><p class="tableblock">string</p></td>
|
||||
<td class="tableblock halign-left valign-top"></td>
|
||||
|
@ -4359,7 +4359,7 @@ Populated by the system when a graceful deletion is requested. Read-only. More i
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="tableblock halign-left valign-top"><p class="tableblock">dnsPolicy</p></td>
|
||||
<td class="tableblock halign-left valign-top"><p class="tableblock">Set DNS policy for containers within the pod. One of <em>ClusterFirst</em> or <em>Default</em>. Defaults to "ClusterFirst".</p></td>
|
||||
<td class="tableblock halign-left valign-top"><p class="tableblock">Set DNS policy for containers within the pod. One of <em>ClusterFirstWithHostNet</em>, <em>ClusterFirst</em> or <em>Default</em>. Defaults to "ClusterFirst". To have DNS options set along with hostNetwork, you have to specify DNS policy explicitly to <em>ClusterFirstWithHostNet</em>.</p></td>
|
||||
<td class="tableblock halign-left valign-top"><p class="tableblock">false</p></td>
|
||||
<td class="tableblock halign-left valign-top"><p class="tableblock">string</p></td>
|
||||
<td class="tableblock halign-left valign-top"></td>
|
||||
|
@ -5086,7 +5086,7 @@ The resulting set of endpoints can be viewed as:<br>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="tableblock halign-left valign-top"><p class="tableblock">dnsPolicy</p></td>
|
||||
<td class="tableblock halign-left valign-top"><p class="tableblock">Set DNS policy for containers within the pod. One of <em>ClusterFirst</em> or <em>Default</em>. Defaults to "ClusterFirst".</p></td>
|
||||
<td class="tableblock halign-left valign-top"><p class="tableblock">Set DNS policy for containers within the pod. One of <em>ClusterFirstWithHostNet</em>, <em>ClusterFirst</em> or <em>Default</em>. Defaults to "ClusterFirst". To have DNS options set along with hostNetwork, you have to specify DNS policy explicitly to <em>ClusterFirstWithHostNet</em>.</p></td>
|
||||
<td class="tableblock halign-left valign-top"><p class="tableblock">false</p></td>
|
||||
<td class="tableblock halign-left valign-top"><p class="tableblock">string</p></td>
|
||||
<td class="tableblock halign-left valign-top"></td>
|
||||
|
@ -12626,7 +12626,7 @@
|
||||
}
|
||||
},
|
||||
"dnsPolicy": {
|
||||
"description": "Set DNS policy for containers within the pod. One of 'ClusterFirst' or 'Default'. Defaults to \"ClusterFirst\".",
|
||||
"description": "Set DNS policy for containers within the pod. One of 'ClusterFirstWithHostNet', 'ClusterFirst' or 'Default'. Defaults to \"ClusterFirst\". To have DNS options set along with hostNetwork, you have to specify DNS policy explicitly to 'ClusterFirstWithHostNet'.",
|
||||
"type": "string"
|
||||
},
|
||||
"hostIPC": {
|
||||
|
@ -4595,7 +4595,7 @@
|
||||
},
|
||||
"dnsPolicy": {
|
||||
"type": "string",
|
||||
"description": "Set DNS policy for containers within the pod. One of 'ClusterFirst' or 'Default'. Defaults to \"ClusterFirst\"."
|
||||
"description": "Set DNS policy for containers within the pod. One of 'ClusterFirstWithHostNet', 'ClusterFirst' or 'Default'. Defaults to \"ClusterFirst\". To have DNS options set along with hostNetwork, you have to specify DNS policy explicitly to 'ClusterFirstWithHostNet'."
|
||||
},
|
||||
"nodeSelector": {
|
||||
"type": "object",
|
||||
|
@ -1679,9 +1679,14 @@ type PodList struct {
|
||||
type DNSPolicy string
|
||||
|
||||
const (
|
||||
// DNSClusterFirstWithHostNet indicates that the pod should use cluster DNS
|
||||
// first, if it is available, then fall back on the default
|
||||
// (as determined by kubelet) DNS settings.
|
||||
DNSClusterFirstWithHostNet DNSPolicy = "ClusterFirstWithHostNet"
|
||||
|
||||
// DNSClusterFirst indicates that the pod should use cluster DNS
|
||||
// first, if it is available, then fall back on the default (as
|
||||
// determined by kubelet) DNS settings.
|
||||
// first unless hostNetwork is true, if it is available, then
|
||||
// fall back on the default (as determined by kubelet) DNS settings.
|
||||
DNSClusterFirst DNSPolicy = "ClusterFirst"
|
||||
|
||||
// DNSDefault indicates that the pod should use the default (as
|
||||
|
@ -2607,8 +2607,9 @@ message PodSpec {
|
||||
optional int64 activeDeadlineSeconds = 5;
|
||||
|
||||
// Set DNS policy for containers within the pod.
|
||||
// One of 'ClusterFirst' or 'Default'.
|
||||
// One of 'ClusterFirstWithHostNet', 'ClusterFirst' or 'Default'.
|
||||
// Defaults to "ClusterFirst".
|
||||
// To have DNS options set along with hostNetwork, you have to specify DNS policy explicitly to 'ClusterFirstWithHostNet'.
|
||||
// +optional
|
||||
optional string dnsPolicy = 6;
|
||||
|
||||
|
@ -1888,9 +1888,14 @@ const (
|
||||
type DNSPolicy string
|
||||
|
||||
const (
|
||||
// DNSClusterFirstWithHostNet indicates that the pod should use cluster DNS
|
||||
// first, if it is available, then fall back on the default
|
||||
// (as determined by kubelet) DNS settings.
|
||||
DNSClusterFirstWithHostNet DNSPolicy = "ClusterFirstWithHostNet"
|
||||
|
||||
// DNSClusterFirst indicates that the pod should use cluster DNS
|
||||
// first, if it is available, then fall back on the default (as
|
||||
// determined by kubelet) DNS settings.
|
||||
// first unless hostNetwork is true, if it is available, then
|
||||
// fall back on the default (as determined by kubelet) DNS settings.
|
||||
DNSClusterFirst DNSPolicy = "ClusterFirst"
|
||||
|
||||
// DNSDefault indicates that the pod should use the default (as
|
||||
@ -2245,8 +2250,9 @@ type PodSpec struct {
|
||||
// +optional
|
||||
ActiveDeadlineSeconds *int64 `json:"activeDeadlineSeconds,omitempty" protobuf:"varint,5,opt,name=activeDeadlineSeconds"`
|
||||
// Set DNS policy for containers within the pod.
|
||||
// One of 'ClusterFirst' or 'Default'.
|
||||
// One of 'ClusterFirstWithHostNet', 'ClusterFirst' or 'Default'.
|
||||
// Defaults to "ClusterFirst".
|
||||
// To have DNS options set along with hostNetwork, you have to specify DNS policy explicitly to 'ClusterFirstWithHostNet'.
|
||||
// +optional
|
||||
DNSPolicy DNSPolicy `json:"dnsPolicy,omitempty" protobuf:"bytes,6,opt,name=dnsPolicy,casttype=DNSPolicy"`
|
||||
// NodeSelector is a selector which must be true for the pod to fit on a node.
|
||||
|
@ -1321,7 +1321,7 @@ var map_PodSpec = map[string]string{
|
||||
"restartPolicy": "Restart policy for all containers within the pod. One of Always, OnFailure, Never. Default to Always. More info: http://kubernetes.io/docs/user-guide/pod-states#restartpolicy",
|
||||
"terminationGracePeriodSeconds": "Optional duration in seconds the pod needs to terminate gracefully. May be decreased in delete request. Value must be non-negative integer. The value zero indicates delete immediately. If this value is nil, the default grace period will be used instead. The grace period is the duration in seconds after the processes running in the pod are sent a termination signal and the time when the processes are forcibly halted with a kill signal. Set this value longer than the expected cleanup time for your process. Defaults to 30 seconds.",
|
||||
"activeDeadlineSeconds": "Optional duration in seconds the pod may be active on the node relative to StartTime before the system will actively try to mark it failed and kill associated containers. Value must be a positive integer.",
|
||||
"dnsPolicy": "Set DNS policy for containers within the pod. One of 'ClusterFirst' or 'Default'. Defaults to \"ClusterFirst\".",
|
||||
"dnsPolicy": "Set DNS policy for containers within the pod. One of 'ClusterFirstWithHostNet', 'ClusterFirst' or 'Default'. Defaults to \"ClusterFirst\". To have DNS options set along with hostNetwork, you have to specify DNS policy explicitly to 'ClusterFirstWithHostNet'.",
|
||||
"nodeSelector": "NodeSelector is a selector which must be true for the pod to fit on a node. Selector which must match a node's labels for the pod to be scheduled on that node. More info: http://kubernetes.io/docs/user-guide/node-selection/README",
|
||||
"serviceAccountName": "ServiceAccountName is the name of the ServiceAccount to use to run this pod. More info: http://releases.k8s.io/HEAD/docs/design/service_accounts.md",
|
||||
"serviceAccount": "DeprecatedServiceAccount is a depreciated alias for ServiceAccountName. Deprecated: Use serviceAccountName instead.",
|
||||
|
@ -1788,12 +1788,12 @@ func validateRestartPolicy(restartPolicy *api.RestartPolicy, fldPath *field.Path
|
||||
func validateDNSPolicy(dnsPolicy *api.DNSPolicy, fldPath *field.Path) field.ErrorList {
|
||||
allErrors := field.ErrorList{}
|
||||
switch *dnsPolicy {
|
||||
case api.DNSClusterFirst, api.DNSDefault:
|
||||
case api.DNSClusterFirstWithHostNet, api.DNSClusterFirst, api.DNSDefault:
|
||||
break
|
||||
case "":
|
||||
allErrors = append(allErrors, field.Required(fldPath, ""))
|
||||
default:
|
||||
validValues := []string{string(api.DNSClusterFirst), string(api.DNSDefault)}
|
||||
validValues := []string{string(api.DNSClusterFirstWithHostNet), string(api.DNSClusterFirst), string(api.DNSDefault)}
|
||||
allErrors = append(allErrors, field.NotSupported(fldPath, dnsPolicy, validValues))
|
||||
}
|
||||
return allErrors
|
||||
|
@ -6491,7 +6491,7 @@ func GetOpenAPIDefinitions(ref openapi.ReferenceCallback) map[string]openapi.Ope
|
||||
},
|
||||
"dnsPolicy": {
|
||||
SchemaProps: spec.SchemaProps{
|
||||
Description: "Set DNS policy for containers within the pod. One of 'ClusterFirst' or 'Default'. Defaults to \"ClusterFirst\".",
|
||||
Description: "Set DNS policy for containers within the pod. One of 'ClusterFirstWithHostNet', 'ClusterFirst' or 'Default'. Defaults to \"ClusterFirst\". To have DNS options set along with hostNetwork, you have to specify DNS policy explicitly to 'ClusterFirstWithHostNet'.",
|
||||
Type: []string{"string"},
|
||||
Format: "",
|
||||
},
|
||||
|
@ -49,8 +49,8 @@ type HandlerRunner interface {
|
||||
// RuntimeHelper wraps kubelet to make container runtime
|
||||
// able to get necessary informations like the RunContainerOptions, DNS settings.
|
||||
type RuntimeHelper interface {
|
||||
GenerateRunContainerOptions(pod *v1.Pod, container *v1.Container, podIP string) (*RunContainerOptions, error)
|
||||
GetClusterDNS(pod *v1.Pod) (dnsServers []string, dnsSearches []string, err error)
|
||||
GenerateRunContainerOptions(pod *v1.Pod, container *v1.Container, podIP string) (contOpts *RunContainerOptions, useClusterFirstPolicy bool, err error)
|
||||
GetClusterDNS(pod *v1.Pod) (dnsServers []string, dnsSearches []string, useClusterFirstPolicy bool, err error)
|
||||
// GetPodCgroupParent returns the the CgroupName identifer, and its literal cgroupfs form on the host
|
||||
// of a pod.
|
||||
GetPodCgroupParent(pod *v1.Pod) (cm.CgroupName, string)
|
||||
|
@ -33,20 +33,20 @@ type FakeRuntimeHelper struct {
|
||||
Err error
|
||||
}
|
||||
|
||||
func (f *FakeRuntimeHelper) GenerateRunContainerOptions(pod *v1.Pod, container *v1.Container, podIP string) (*kubecontainer.RunContainerOptions, error) {
|
||||
func (f *FakeRuntimeHelper) GenerateRunContainerOptions(pod *v1.Pod, container *v1.Container, podIP string) (*kubecontainer.RunContainerOptions, bool, error) {
|
||||
var opts kubecontainer.RunContainerOptions
|
||||
if len(container.TerminationMessagePath) != 0 {
|
||||
opts.PodContainerDir = f.PodContainerDir
|
||||
}
|
||||
return &opts, nil
|
||||
return &opts, false, nil
|
||||
}
|
||||
|
||||
func (f *FakeRuntimeHelper) GetPodCgroupParent(pod *v1.Pod) (cm.CgroupName, string) {
|
||||
return "", ""
|
||||
}
|
||||
|
||||
func (f *FakeRuntimeHelper) GetClusterDNS(pod *v1.Pod) ([]string, []string, error) {
|
||||
return f.DNSServers, f.DNSSearches, f.Err
|
||||
func (f *FakeRuntimeHelper) GetClusterDNS(pod *v1.Pod) ([]string, []string, bool, error) {
|
||||
return f.DNSServers, f.DNSSearches, false, f.Err
|
||||
}
|
||||
|
||||
// This is not used by docker runtime.
|
||||
|
@ -858,6 +858,8 @@ func (dm *DockerManager) runContainer(
|
||||
|
||||
// setInfraContainerNetworkConfig sets the network configuration for the infra-container. We only set network configuration for infra-container, all
|
||||
// the user containers will share the same network namespace with infra-container.
|
||||
// NOTE: cluster dns settings aren't passed anymore to docker api in all cases, not only for pods with host network:
|
||||
// the resolver conf will be overwritten after infra-container creation to override docker's behaviour
|
||||
func setInfraContainerNetworkConfig(pod *v1.Pod, netMode string, opts *kubecontainer.RunContainerOptions, dockerOpts *dockertypes.ContainerCreateConfig) {
|
||||
exposedPorts, portBindings := makePortsAndBindings(opts.PortMappings)
|
||||
dockerOpts.Config.ExposedPorts = exposedPorts
|
||||
@ -865,12 +867,6 @@ func setInfraContainerNetworkConfig(pod *v1.Pod, netMode string, opts *kubeconta
|
||||
|
||||
if netMode != namespaceModeHost {
|
||||
dockerOpts.Config.Hostname = opts.Hostname
|
||||
if len(opts.DNS) > 0 {
|
||||
dockerOpts.HostConfig.DNS = opts.DNS
|
||||
}
|
||||
if len(opts.DNSSearch) > 0 {
|
||||
dockerOpts.HostConfig.DNSSearch = opts.DNSSearch
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1759,7 +1755,7 @@ func (dm *DockerManager) runContainerInPod(pod *v1.Pod, container *v1.Container,
|
||||
glog.V(5).Infof("Generating ref for container %s: %#v", container.Name, ref)
|
||||
}
|
||||
|
||||
opts, err := dm.runtimeHelper.GenerateRunContainerOptions(pod, container, podIP)
|
||||
opts, useClusterFirstPolicy, err := dm.runtimeHelper.GenerateRunContainerOptions(pod, container, podIP)
|
||||
if err != nil {
|
||||
return kubecontainer.ContainerID{}, fmt.Errorf("GenerateRunContainerOptions: %v", err)
|
||||
}
|
||||
@ -1813,14 +1809,15 @@ func (dm *DockerManager) runContainerInPod(pod *v1.Pod, container *v1.Container,
|
||||
return kubecontainer.ContainerID{}, err
|
||||
}
|
||||
|
||||
// The addNDotsOption call appends the ndots option to the resolv.conf file generated by docker.
|
||||
// Re-write resolv.conf file generated by docker.
|
||||
// NOTE: cluster dns settings aren't passed anymore to docker api in all cases, not only for pods with host network:
|
||||
// the resolver conf will be overwritten after infra-container creation to override docker's behaviour
|
||||
// This resolv.conf file is shared by all containers of the same pod, and needs to be modified only once per pod.
|
||||
// we modify it when the pause container is created since it is the first container created in the pod since it holds
|
||||
// the networking namespace.
|
||||
if container.Name == PodInfraContainerName && utsMode != namespaceModeHost {
|
||||
err = addNDotsOption(containerInfo.ResolvConfPath)
|
||||
if err != nil {
|
||||
return kubecontainer.ContainerID{}, fmt.Errorf("addNDotsOption: %v", err)
|
||||
if container.Name == PodInfraContainerName {
|
||||
if err := rewriteResolvFile(containerInfo.ResolvConfPath, opts.DNS, opts.DNSSearch, useClusterFirstPolicy); err != nil {
|
||||
return kubecontainer.ContainerID{}, err
|
||||
}
|
||||
}
|
||||
|
||||
@ -1885,7 +1882,7 @@ func (dm *DockerManager) checkDockerAPIVersion(expectedVersion string) (int, err
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func addNDotsOption(resolvFilePath string) error {
|
||||
func rewriteResolvFile(resolvFilePath string, dns []string, dnsSearch []string, useClusterFirstPolicy bool) error {
|
||||
if len(resolvFilePath) == 0 {
|
||||
glog.Errorf("ResolvConfPath is empty.")
|
||||
return nil
|
||||
@ -1895,23 +1892,42 @@ func addNDotsOption(resolvFilePath string) error {
|
||||
return fmt.Errorf("ResolvConfPath %q does not exist", resolvFilePath)
|
||||
}
|
||||
|
||||
glog.V(4).Infof("DNS ResolvConfPath exists: %s. Will attempt to add ndots option: %s", resolvFilePath, ndotsDNSOption)
|
||||
var resolvFileContent []string
|
||||
|
||||
if err := appendToFile(resolvFilePath, ndotsDNSOption); err != nil {
|
||||
glog.Errorf("resolv.conf could not be updated: %v", err)
|
||||
return err
|
||||
for _, srv := range dns {
|
||||
resolvFileContent = append(resolvFileContent, "nameserver "+srv)
|
||||
}
|
||||
|
||||
if len(dnsSearch) > 0 {
|
||||
resolvFileContent = append(resolvFileContent, "search "+strings.Join(dnsSearch, " "))
|
||||
}
|
||||
|
||||
if len(resolvFileContent) > 0 {
|
||||
if useClusterFirstPolicy {
|
||||
resolvFileContent = append(resolvFileContent, ndotsDNSOption)
|
||||
}
|
||||
|
||||
resolvFileContentStr := strings.Join(resolvFileContent, "\n")
|
||||
resolvFileContentStr += "\n"
|
||||
|
||||
glog.V(4).Infof("Will attempt to re-write config file %s with: \n%s", resolvFilePath, resolvFileContent)
|
||||
if err := rewriteFile(resolvFilePath, resolvFileContentStr); err != nil {
|
||||
glog.Errorf("resolv.conf could not be updated: %v", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func appendToFile(filePath, stringToAppend string) error {
|
||||
f, err := os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY, 0644)
|
||||
func rewriteFile(filePath, stringToWrite string) error {
|
||||
f, err := os.OpenFile(filePath, os.O_TRUNC|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
_, err = f.WriteString(stringToAppend)
|
||||
_, err = f.WriteString(stringToWrite)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -1268,22 +1268,22 @@ func (kl *Kubelet) GetKubeClient() clientset.Interface {
|
||||
|
||||
// GetClusterDNS returns a list of the DNS servers and a list of the DNS search
|
||||
// domains of the cluster.
|
||||
func (kl *Kubelet) GetClusterDNS(pod *v1.Pod) ([]string, []string, error) {
|
||||
func (kl *Kubelet) GetClusterDNS(pod *v1.Pod) ([]string, []string, bool, error) {
|
||||
var hostDNS, hostSearch []string
|
||||
// Get host DNS settings
|
||||
if kl.resolverConfig != "" {
|
||||
f, err := os.Open(kl.resolverConfig)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, nil, false, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
hostDNS, hostSearch, err = kl.parseResolvConf(f)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, nil, false, err
|
||||
}
|
||||
}
|
||||
useClusterFirstPolicy := pod.Spec.DNSPolicy == v1.DNSClusterFirst
|
||||
useClusterFirstPolicy := ((pod.Spec.DNSPolicy == v1.DNSClusterFirst && !kubecontainer.IsHostNetworkPod(pod)) || pod.Spec.DNSPolicy == v1.DNSClusterFirstWithHostNet)
|
||||
if useClusterFirstPolicy && len(kl.clusterDNS) == 0 {
|
||||
// clusterDNS is not known.
|
||||
// pod with ClusterDNSFirst Policy cannot be created
|
||||
@ -1309,7 +1309,7 @@ func (kl *Kubelet) GetClusterDNS(pod *v1.Pod) ([]string, []string, error) {
|
||||
} else {
|
||||
hostSearch = kl.formDNSSearchForDNSDefault(hostSearch, pod)
|
||||
}
|
||||
return hostDNS, hostSearch, nil
|
||||
return hostDNS, hostSearch, useClusterFirstPolicy, nil
|
||||
}
|
||||
|
||||
// for a pod with DNSClusterFirst policy, the cluster DNS server is the only nameserver configured for
|
||||
@ -1321,7 +1321,7 @@ func (kl *Kubelet) GetClusterDNS(pod *v1.Pod) ([]string, []string, error) {
|
||||
}
|
||||
dnsSearch := kl.formDNSSearch(hostSearch, pod)
|
||||
|
||||
return dns, dnsSearch, nil
|
||||
return dns, dnsSearch, useClusterFirstPolicy, nil
|
||||
}
|
||||
|
||||
// syncPod is the transaction script for the sync of a single pod.
|
||||
|
@ -274,13 +274,14 @@ func (kl *Kubelet) GetPodCgroupParent(pod *v1.Pod) (cm.CgroupName, string) {
|
||||
|
||||
// GenerateRunContainerOptions generates the RunContainerOptions, which can be used by
|
||||
// the container runtime to set parameters for launching a container.
|
||||
func (kl *Kubelet) GenerateRunContainerOptions(pod *v1.Pod, container *v1.Container, podIP string) (*kubecontainer.RunContainerOptions, error) {
|
||||
func (kl *Kubelet) GenerateRunContainerOptions(pod *v1.Pod, container *v1.Container, podIP string) (*kubecontainer.RunContainerOptions, bool, error) {
|
||||
var err error
|
||||
useClusterFirstPolicy := false
|
||||
_, cgroupParent := kl.GetPodCgroupParent(pod)
|
||||
opts := &kubecontainer.RunContainerOptions{CgroupParent: cgroupParent}
|
||||
hostname, hostDomainName, err := kl.GeneratePodHostNameAndDomain(pod)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, false, err
|
||||
}
|
||||
opts.Hostname = hostname
|
||||
podName := volumehelper.GetUniquePodName(pod)
|
||||
@ -290,16 +291,16 @@ func (kl *Kubelet) GenerateRunContainerOptions(pod *v1.Pod, container *v1.Contai
|
||||
// TODO(random-liu): Move following convert functions into pkg/kubelet/container
|
||||
opts.Devices, err = kl.makeDevices(pod, container)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
opts.Mounts, err = makeMounts(pod, kl.getPodDir(pod.UID), container, hostname, hostDomainName, podIP, volumes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, false, err
|
||||
}
|
||||
opts.Envs, err = kl.makeEnvironmentVariables(pod, container, podIP)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
// Disabling adding TerminationMessagePath on Windows as these files would be mounted as docker volume and
|
||||
@ -313,9 +314,9 @@ func (kl *Kubelet) GenerateRunContainerOptions(pod *v1.Pod, container *v1.Contai
|
||||
}
|
||||
}
|
||||
|
||||
opts.DNS, opts.DNSSearch, err = kl.GetClusterDNS(pod)
|
||||
opts.DNS, opts.DNSSearch, useClusterFirstPolicy, err = kl.GetClusterDNS(pod)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
// only do this check if the experimental behavior is enabled, otherwise allow it to default to false
|
||||
@ -323,7 +324,7 @@ func (kl *Kubelet) GenerateRunContainerOptions(pod *v1.Pod, container *v1.Contai
|
||||
opts.EnableHostUserNamespace = kl.enableHostUserNamespace(pod)
|
||||
}
|
||||
|
||||
return opts, nil
|
||||
return opts, useClusterFirstPolicy, nil
|
||||
}
|
||||
|
||||
var masterServices = sets.NewString("kubernetes")
|
||||
|
@ -176,14 +176,17 @@ func TestGenerateRunContainerOptions_DNSConfigurationParams(t *testing.T) {
|
||||
kubelet.clusterDomain = "kubernetes.io"
|
||||
kubelet.clusterDNS = []net.IP{net.ParseIP(clusterNS)}
|
||||
|
||||
pods := newTestPods(2)
|
||||
pods[0].Spec.DNSPolicy = v1.DNSClusterFirst
|
||||
pods[1].Spec.DNSPolicy = v1.DNSDefault
|
||||
pods := newTestPods(4)
|
||||
pods[0].Spec.DNSPolicy = v1.DNSClusterFirstWithHostNet
|
||||
pods[1].Spec.DNSPolicy = v1.DNSClusterFirst
|
||||
pods[2].Spec.DNSPolicy = v1.DNSClusterFirst
|
||||
pods[2].Spec.HostNetwork = false
|
||||
pods[3].Spec.DNSPolicy = v1.DNSDefault
|
||||
|
||||
options := make([]*kubecontainer.RunContainerOptions, 2)
|
||||
options := make([]*kubecontainer.RunContainerOptions, 4)
|
||||
for i, pod := range pods {
|
||||
var err error
|
||||
options[i], err = kubelet.GenerateRunContainerOptions(pod, &v1.Container{}, "")
|
||||
options[i], _, err = kubelet.GenerateRunContainerOptions(pod, &v1.Container{}, "")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to generate container options: %v", err)
|
||||
}
|
||||
@ -200,11 +203,23 @@ func TestGenerateRunContainerOptions_DNSConfigurationParams(t *testing.T) {
|
||||
if len(options[1].DNSSearch) != 1 || options[1].DNSSearch[0] != "." {
|
||||
t.Errorf("expected search \".\", got %+v", options[1].DNSSearch)
|
||||
}
|
||||
if len(options[2].DNS) != 1 || options[2].DNS[0] != clusterNS {
|
||||
t.Errorf("expected nameserver %s, got %+v", clusterNS, options[2].DNS)
|
||||
}
|
||||
if len(options[2].DNSSearch) == 0 || options[2].DNSSearch[0] != ".svc."+kubelet.clusterDomain {
|
||||
t.Errorf("expected search %s, got %+v", ".svc."+kubelet.clusterDomain, options[2].DNSSearch)
|
||||
}
|
||||
if len(options[3].DNS) != 1 || options[3].DNS[0] != "127.0.0.1" {
|
||||
t.Errorf("expected nameserver 127.0.0.1, got %+v", options[3].DNS)
|
||||
}
|
||||
if len(options[3].DNSSearch) != 1 || options[3].DNSSearch[0] != "." {
|
||||
t.Errorf("expected search \".\", got %+v", options[3].DNSSearch)
|
||||
}
|
||||
|
||||
kubelet.resolverConfig = "/etc/resolv.conf"
|
||||
for i, pod := range pods {
|
||||
var err error
|
||||
options[i], err = kubelet.GenerateRunContainerOptions(pod, &v1.Container{}, "")
|
||||
options[i], _, err = kubelet.GenerateRunContainerOptions(pod, &v1.Container{}, "")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to generate container options: %v", err)
|
||||
}
|
||||
@ -224,6 +239,16 @@ func TestGenerateRunContainerOptions_DNSConfigurationParams(t *testing.T) {
|
||||
} else if options[0].DNSSearch[0] != ".svc."+kubelet.clusterDomain {
|
||||
t.Errorf("expected domain %s, got %s", ".svc."+kubelet.clusterDomain, options[0].DNSSearch)
|
||||
}
|
||||
if len(options[2].DNS) != 1 {
|
||||
t.Errorf("expected cluster nameserver only, got %+v", options[2].DNS)
|
||||
} else if options[2].DNS[0] != clusterNS {
|
||||
t.Errorf("expected nameserver %s, got %v", clusterNS, options[2].DNS[0])
|
||||
}
|
||||
if len(options[2].DNSSearch) != expLength {
|
||||
t.Errorf("expected prepend of cluster domain, got %+v", options[2].DNSSearch)
|
||||
} else if options[2].DNSSearch[0] != ".svc."+kubelet.clusterDomain {
|
||||
t.Errorf("expected domain %s, got %s", ".svc."+kubelet.clusterDomain, options[0].DNSSearch)
|
||||
}
|
||||
}
|
||||
|
||||
type testServiceLister struct {
|
||||
|
@ -132,7 +132,7 @@ func (m *kubeGenericRuntimeManager) startContainer(podSandboxID string, podSandb
|
||||
|
||||
// generateContainerConfig generates container config for kubelet runtime v1.
|
||||
func (m *kubeGenericRuntimeManager) generateContainerConfig(container *v1.Container, pod *v1.Pod, restartCount int, podIP, imageRef string) (*runtimeapi.ContainerConfig, error) {
|
||||
opts, err := m.runtimeHelper.GenerateRunContainerOptions(pod, container, podIP)
|
||||
opts, _, err := m.runtimeHelper.GenerateRunContainerOptions(pod, container, podIP)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -75,7 +75,7 @@ func (m *kubeGenericRuntimeManager) generatePodSandboxConfig(pod *v1.Pod, attemp
|
||||
}
|
||||
|
||||
if !kubecontainer.IsHostNetworkPod(pod) {
|
||||
dnsServers, dnsSearches, err := m.runtimeHelper.GetClusterDNS(pod)
|
||||
dnsServers, dnsSearches, _, err := m.runtimeHelper.GetClusterDNS(pod)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -810,7 +810,7 @@ func (r *Runtime) newAppcRuntimeApp(pod *v1.Pod, podIP string, c v1.Container, r
|
||||
}
|
||||
|
||||
// TODO: determine how this should be handled for rkt
|
||||
opts, err := r.runtimeHelper.GenerateRunContainerOptions(pod, &c, podIP)
|
||||
opts, _, err := r.runtimeHelper.GenerateRunContainerOptions(pod, &c, podIP)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -1009,7 +1009,7 @@ func (r *Runtime) generateRunCommand(pod *v1.Pod, uuid, netnsName string) (strin
|
||||
}
|
||||
} else {
|
||||
// Setup DNS.
|
||||
dnsServers, dnsSearches, err := r.runtimeHelper.GetClusterDNS(pod)
|
||||
dnsServers, dnsSearches, _, err := r.runtimeHelper.GetClusterDNS(pod)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -1679,9 +1679,14 @@ type PodList struct {
|
||||
type DNSPolicy string
|
||||
|
||||
const (
|
||||
// DNSClusterFirstWithHostNet indicates that the pod should use cluster DNS
|
||||
// first, if it is available, then fall back on the default
|
||||
// (as determined by kubelet) DNS settings.
|
||||
DNSClusterFirstWithHostNet DNSPolicy = "ClusterFirstWithHostNet"
|
||||
|
||||
// DNSClusterFirst indicates that the pod should use cluster DNS
|
||||
// first, if it is available, then fall back on the default (as
|
||||
// determined by kubelet) DNS settings.
|
||||
// first unless hostNetwork is true, if it is available, then
|
||||
// fall back on the default (as determined by kubelet) DNS settings.
|
||||
DNSClusterFirst DNSPolicy = "ClusterFirst"
|
||||
|
||||
// DNSDefault indicates that the pod should use the default (as
|
||||
|
@ -2607,8 +2607,9 @@ message PodSpec {
|
||||
optional int64 activeDeadlineSeconds = 5;
|
||||
|
||||
// Set DNS policy for containers within the pod.
|
||||
// One of 'ClusterFirst' or 'Default'.
|
||||
// One of 'ClusterFirstWithHostNet', 'ClusterFirst' or 'Default'.
|
||||
// Defaults to "ClusterFirst".
|
||||
// To have DNS options set along with hostNetwork, you have to specify DNS policy explicitly to 'ClusterFirstWithHostNet'.
|
||||
// +optional
|
||||
optional string dnsPolicy = 6;
|
||||
|
||||
|
@ -1888,9 +1888,14 @@ const (
|
||||
type DNSPolicy string
|
||||
|
||||
const (
|
||||
// DNSClusterFirstWithHostNet indicates that the pod should use cluster DNS
|
||||
// first, if it is available, then fall back on the default
|
||||
// (as determined by kubelet) DNS settings.
|
||||
DNSClusterFirstWithHostNet DNSPolicy = "ClusterFirstWithHostNet"
|
||||
|
||||
// DNSClusterFirst indicates that the pod should use cluster DNS
|
||||
// first, if it is available, then fall back on the default (as
|
||||
// determined by kubelet) DNS settings.
|
||||
// first unless hostNetwork is true, if it is available, then
|
||||
// fall back on the default (as determined by kubelet) DNS settings.
|
||||
DNSClusterFirst DNSPolicy = "ClusterFirst"
|
||||
|
||||
// DNSDefault indicates that the pod should use the default (as
|
||||
@ -2245,8 +2250,9 @@ type PodSpec struct {
|
||||
// +optional
|
||||
ActiveDeadlineSeconds *int64 `json:"activeDeadlineSeconds,omitempty" protobuf:"varint,5,opt,name=activeDeadlineSeconds"`
|
||||
// Set DNS policy for containers within the pod.
|
||||
// One of 'ClusterFirst' or 'Default'.
|
||||
// One of 'ClusterFirstWithHostNet', 'ClusterFirst' or 'Default'.
|
||||
// Defaults to "ClusterFirst".
|
||||
// To have DNS options set along with hostNetwork, you have to specify DNS policy explicitly to 'ClusterFirstWithHostNet'.
|
||||
// +optional
|
||||
DNSPolicy DNSPolicy `json:"dnsPolicy,omitempty" protobuf:"bytes,6,opt,name=dnsPolicy,casttype=DNSPolicy"`
|
||||
// NodeSelector is a selector which must be true for the pod to fit on a node.
|
||||
|
@ -1321,7 +1321,7 @@ var map_PodSpec = map[string]string{
|
||||
"restartPolicy": "Restart policy for all containers within the pod. One of Always, OnFailure, Never. Default to Always. More info: http://kubernetes.io/docs/user-guide/pod-states#restartpolicy",
|
||||
"terminationGracePeriodSeconds": "Optional duration in seconds the pod needs to terminate gracefully. May be decreased in delete request. Value must be non-negative integer. The value zero indicates delete immediately. If this value is nil, the default grace period will be used instead. The grace period is the duration in seconds after the processes running in the pod are sent a termination signal and the time when the processes are forcibly halted with a kill signal. Set this value longer than the expected cleanup time for your process. Defaults to 30 seconds.",
|
||||
"activeDeadlineSeconds": "Optional duration in seconds the pod may be active on the node relative to StartTime before the system will actively try to mark it failed and kill associated containers. Value must be a positive integer.",
|
||||
"dnsPolicy": "Set DNS policy for containers within the pod. One of 'ClusterFirst' or 'Default'. Defaults to \"ClusterFirst\".",
|
||||
"dnsPolicy": "Set DNS policy for containers within the pod. One of 'ClusterFirstWithHostNet', 'ClusterFirst' or 'Default'. Defaults to \"ClusterFirst\". To have DNS options set along with hostNetwork, you have to specify DNS policy explicitly to 'ClusterFirstWithHostNet'.",
|
||||
"nodeSelector": "NodeSelector is a selector which must be true for the pod to fit on a node. Selector which must match a node's labels for the pod to be scheduled on that node. More info: http://kubernetes.io/docs/user-guide/node-selection/README",
|
||||
"serviceAccountName": "ServiceAccountName is the name of the ServiceAccount to use to run this pod. More info: http://releases.k8s.io/HEAD/docs/design/service_accounts.md",
|
||||
"serviceAccount": "DeprecatedServiceAccount is a depreciated alias for ServiceAccountName. Deprecated: Use serviceAccountName instead.",
|
||||
|
Loading…
Reference in New Issue
Block a user