kubernetes: Kubelet didn't respect --resolv-conf when resolv.conf is empty or full of comments

What happened?

When I passing --resolv-conf to kubelet, it did not respect the option, and still copy the /etc/resolv.conf to the pod.

What makes it special is that the resolv.conf provided by myself is empty.

https://kubernetes.io/docs/reference/command-line-tools-reference/kubelet/

After skimming the code, I found the logic here is problematic:

https://github.com/kubernetes/kubernetes/blob/v1.28.2/pkg/kubelet/network/dns/dns.go#L225-L274

Because resolv.conf is empty, an empty array and no error is returned. Then, if the dnsPolicy is default (that is the default config for CoreDNS deployment), the podDNSType turns to be podDNSHost:

https://github.com/kubernetes/kubernetes/blob/v1.28.2/pkg/kubelet/network/dns/dns.go#L316-L317

Then, containerd will get an empty dns_config, so it will copy /etc/resolv.conf from the host to the container:

https://github.com/containerd/containerd/blob/v1.7.6/pkg/cri/server/sandbox_run_linux.go#L282-L287

Finally, CoreDNS reports forwarding loop error.

What did you expect to happen?

Kubelet should refuse to use the wrong empty resolv.conf to create a pod, and complains with errors.

How can we reproduce it (as minimally and precisely as possible)?

  1. Pass --resolv-conf to kubelet, and make sure the specified resolv.conf file is empty.
  2. Check the output of CoreDNS, and its /etc/resolv.conf.

Anything else we need to know?

No response

Kubernetes version

k8s v1.28.2 containerd v1.7.6

Cloud provider

None

OS version

Any major Linux distribution.

Install tools

Container runtime (CRI) and version (if applicable)

Related plugins (CNI, CSI, …) and versions (if applicable)

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 24 (19 by maintainers)

Most upvoted comments

This is a fun bug, but not that hard to comprehend, I think.

Forget whether it’s a “good idea” or not.

Is it valid to pass an empty file to --resolv-conf ? If yes, should it mean the same as not passing that flag or should it mean “no config”.

I think it IS valid and it SHOULD mean “no config”. What really happens is that kubelet gets empty slices (but no error, so not ambiguous). When it creates the DNSConfig message, it appears empty, because it is comprised entirely of repeated fields with no instances. That is ambiguous - it could mean “no data” or “not specified”. It’s unclear to me if we can tell the difference between those? @samuelkarp @SergeyKanzhelev @ndixita - do you know? PodSandboxConfig.GetDnsConfig() can return nil.

Containerd further conflates those two by parsing into a string, and assuming "" means “not specified”. Even if it WAS specified!

Then, containerd “helpfully” decides that, if you don’t specify any contents, it should copy the host’s resolv.conf.

I am not 100% confident in CRI semantics - is it valid for CRI to NOT populate the PodSandboxConfig.DNSConfig message? Or is that effectively required? If a CRI caller does not specify PodSandboxConfig.DNSConfig, is the CORRECT behavior to go look at the host? That seems like over-stepping, to me. If I wanted a DNS config, I would have sent it. Agree or disagree?

So IMO, the fix here is either (in my pref order):

  1. containerd should not fall back on host’s resolv.conf AT ALL
  2. containerd should differentiate “not provided” from “provided and empty” (PodSandboxConfig.GetDnsConfig() returned nil)
  3. we should add a “this struct intentionally left blank” marker to DNSConfig, so containerd can do the above

So I think this very much is a SIG Node issue.

/sig node

As to the question of how this problem was discovered…why are you setting the kubelet to an empty resolv.conf rather than just configuring the specific Pods in question?

Thank you @roman-kiselenko and thanks @aojea for the hint! ❤️

It is suggesting to change the behavior to stop the pod creation

Kubelet should refuse to use the wrong empty resolv.conf to create a pod, and complains with errors.