flags_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package kubelet
  14. import (
  15. "context"
  16. "io"
  17. "reflect"
  18. "strings"
  19. "testing"
  20. "github.com/pkg/errors"
  21. "k8s.io/api/core/v1"
  22. kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
  23. "k8s.io/utils/exec"
  24. )
  25. type fakeCmd struct {
  26. b []byte
  27. err error
  28. }
  29. func (f fakeCmd) Run() error { return f.err }
  30. func (f fakeCmd) CombinedOutput() ([]byte, error) { return f.b, f.err }
  31. func (f fakeCmd) Output() ([]byte, error) { return f.b, f.err }
  32. func (f fakeCmd) SetDir(dir string) {}
  33. func (f fakeCmd) SetStdin(in io.Reader) {}
  34. func (f fakeCmd) SetStdout(out io.Writer) {}
  35. func (f fakeCmd) SetStderr(out io.Writer) {}
  36. func (f fakeCmd) SetEnv([]string) {}
  37. func (f fakeCmd) Stop() {}
  38. func (f fakeCmd) Start() error { return nil }
  39. func (f fakeCmd) Wait() error { return nil }
  40. func (f fakeCmd) StdoutPipe() (io.ReadCloser, error) { return nil, nil }
  41. func (f fakeCmd) StderrPipe() (io.ReadCloser, error) { return nil, nil }
  42. type fakeExecer struct {
  43. ioMap map[string]fakeCmd
  44. }
  45. func (f fakeExecer) Command(cmd string, args ...string) exec.Cmd {
  46. cmds := []string{cmd}
  47. cmds = append(cmds, args...)
  48. return f.ioMap[strings.Join(cmds, " ")]
  49. }
  50. func (f fakeExecer) CommandContext(ctx context.Context, cmd string, args ...string) exec.Cmd {
  51. return f.Command(cmd, args...)
  52. }
  53. func (f fakeExecer) LookPath(file string) (string, error) { return "", errors.New("unknown binary") }
  54. var (
  55. systemdCgroupExecer = fakeExecer{
  56. ioMap: map[string]fakeCmd{
  57. "docker info -f {{.CgroupDriver}}": {
  58. b: []byte(`systemd`),
  59. },
  60. },
  61. }
  62. cgroupfsCgroupExecer = fakeExecer{
  63. ioMap: map[string]fakeCmd{
  64. "docker info -f {{.CgroupDriver}}": {
  65. b: []byte(`cgroupfs`),
  66. },
  67. },
  68. }
  69. errCgroupExecer = fakeExecer{
  70. ioMap: map[string]fakeCmd{
  71. "docker info -f {{.CgroupDriver}}": {
  72. err: errors.New("no such binary: docker"),
  73. },
  74. },
  75. }
  76. )
  77. func binaryRunningPidOfFunc(_ string) ([]int, error) {
  78. return []int{1, 2, 3}, nil
  79. }
  80. func binaryNotRunningPidOfFunc(_ string) ([]int, error) {
  81. return []int{}, nil
  82. }
  83. func TestBuildKubeletArgMap(t *testing.T) {
  84. tests := []struct {
  85. name string
  86. opts kubeletFlagsOpts
  87. expected map[string]string
  88. }{
  89. {
  90. name: "the simplest case",
  91. opts: kubeletFlagsOpts{
  92. nodeRegOpts: &kubeadmapi.NodeRegistrationOptions{
  93. CRISocket: "/var/run/dockershim.sock",
  94. Name: "foo",
  95. Taints: []v1.Taint{ // This should be ignored as registerTaintsUsingFlags is false
  96. {
  97. Key: "foo",
  98. Value: "bar",
  99. Effect: "baz",
  100. },
  101. },
  102. },
  103. execer: errCgroupExecer,
  104. pidOfFunc: binaryNotRunningPidOfFunc,
  105. defaultHostname: "foo",
  106. },
  107. expected: map[string]string{
  108. "network-plugin": "cni",
  109. },
  110. },
  111. {
  112. name: "nodeRegOpts.Name != default hostname",
  113. opts: kubeletFlagsOpts{
  114. nodeRegOpts: &kubeadmapi.NodeRegistrationOptions{
  115. CRISocket: "/var/run/dockershim.sock",
  116. Name: "override-name",
  117. },
  118. execer: errCgroupExecer,
  119. pidOfFunc: binaryNotRunningPidOfFunc,
  120. defaultHostname: "default",
  121. },
  122. expected: map[string]string{
  123. "network-plugin": "cni",
  124. "hostname-override": "override-name",
  125. },
  126. },
  127. {
  128. name: "systemd cgroup driver",
  129. opts: kubeletFlagsOpts{
  130. nodeRegOpts: &kubeadmapi.NodeRegistrationOptions{
  131. CRISocket: "/var/run/dockershim.sock",
  132. Name: "foo",
  133. },
  134. execer: systemdCgroupExecer,
  135. pidOfFunc: binaryNotRunningPidOfFunc,
  136. defaultHostname: "foo",
  137. },
  138. expected: map[string]string{
  139. "network-plugin": "cni",
  140. "cgroup-driver": "systemd",
  141. },
  142. },
  143. {
  144. name: "cgroupfs cgroup driver",
  145. opts: kubeletFlagsOpts{
  146. nodeRegOpts: &kubeadmapi.NodeRegistrationOptions{
  147. CRISocket: "/var/run/dockershim.sock",
  148. Name: "foo",
  149. },
  150. execer: cgroupfsCgroupExecer,
  151. pidOfFunc: binaryNotRunningPidOfFunc,
  152. defaultHostname: "foo",
  153. },
  154. expected: map[string]string{
  155. "network-plugin": "cni",
  156. "cgroup-driver": "cgroupfs",
  157. },
  158. },
  159. {
  160. name: "external CRI runtime",
  161. opts: kubeletFlagsOpts{
  162. nodeRegOpts: &kubeadmapi.NodeRegistrationOptions{
  163. CRISocket: "/var/run/containerd.sock",
  164. Name: "foo",
  165. },
  166. execer: cgroupfsCgroupExecer,
  167. pidOfFunc: binaryNotRunningPidOfFunc,
  168. defaultHostname: "foo",
  169. },
  170. expected: map[string]string{
  171. "container-runtime": "remote",
  172. "container-runtime-endpoint": "/var/run/containerd.sock",
  173. },
  174. },
  175. {
  176. name: "register with taints",
  177. opts: kubeletFlagsOpts{
  178. nodeRegOpts: &kubeadmapi.NodeRegistrationOptions{
  179. CRISocket: "/var/run/containerd.sock",
  180. Name: "foo",
  181. Taints: []v1.Taint{
  182. {
  183. Key: "foo",
  184. Value: "bar",
  185. Effect: "baz",
  186. },
  187. {
  188. Key: "key",
  189. Value: "val",
  190. Effect: "eff",
  191. },
  192. },
  193. },
  194. registerTaintsUsingFlags: true,
  195. execer: cgroupfsCgroupExecer,
  196. pidOfFunc: binaryNotRunningPidOfFunc,
  197. defaultHostname: "foo",
  198. },
  199. expected: map[string]string{
  200. "container-runtime": "remote",
  201. "container-runtime-endpoint": "/var/run/containerd.sock",
  202. "register-with-taints": "foo=bar:baz,key=val:eff",
  203. },
  204. },
  205. {
  206. name: "systemd-resolved running",
  207. opts: kubeletFlagsOpts{
  208. nodeRegOpts: &kubeadmapi.NodeRegistrationOptions{
  209. CRISocket: "/var/run/containerd.sock",
  210. Name: "foo",
  211. },
  212. execer: cgroupfsCgroupExecer,
  213. pidOfFunc: binaryRunningPidOfFunc,
  214. defaultHostname: "foo",
  215. },
  216. expected: map[string]string{
  217. "container-runtime": "remote",
  218. "container-runtime-endpoint": "/var/run/containerd.sock",
  219. "resolv-conf": "/run/systemd/resolve/resolv.conf",
  220. },
  221. },
  222. {
  223. name: "pause image is set",
  224. opts: kubeletFlagsOpts{
  225. nodeRegOpts: &kubeadmapi.NodeRegistrationOptions{
  226. CRISocket: "/var/run/dockershim.sock",
  227. Name: "foo",
  228. },
  229. pauseImage: "gcr.io/pause:3.1",
  230. execer: cgroupfsCgroupExecer,
  231. pidOfFunc: binaryNotRunningPidOfFunc,
  232. defaultHostname: "foo",
  233. },
  234. expected: map[string]string{
  235. "network-plugin": "cni",
  236. "cgroup-driver": "cgroupfs",
  237. "pod-infra-container-image": "gcr.io/pause:3.1",
  238. },
  239. },
  240. }
  241. for _, test := range tests {
  242. t.Run(test.name, func(t *testing.T) {
  243. actual := buildKubeletArgMap(test.opts)
  244. if !reflect.DeepEqual(actual, test.expected) {
  245. t.Errorf(
  246. "failed buildKubeletArgMap:\n\texpected: %v\n\t actual: %v",
  247. test.expected,
  248. actual,
  249. )
  250. }
  251. })
  252. }
  253. }