flags_test.go 7.4 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. v1 "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 serviceIsActiveFunc(_ string) (bool, error) {
  78. return true, nil
  79. }
  80. func serviceIsNotActiveFunc(_ string) (bool, error) {
  81. return false, 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. Taints: []v1.Taint{ // This should be ignored as registerTaintsUsingFlags is false
  95. {
  96. Key: "foo",
  97. Value: "bar",
  98. Effect: "baz",
  99. },
  100. },
  101. },
  102. execer: errCgroupExecer,
  103. isServiceActiveFunc: serviceIsNotActiveFunc,
  104. },
  105. expected: map[string]string{
  106. "network-plugin": "cni",
  107. },
  108. },
  109. {
  110. name: "hostname override from NodeRegistrationOptions.Name",
  111. opts: kubeletFlagsOpts{
  112. nodeRegOpts: &kubeadmapi.NodeRegistrationOptions{
  113. CRISocket: "/var/run/dockershim.sock",
  114. Name: "override-name",
  115. },
  116. execer: errCgroupExecer,
  117. isServiceActiveFunc: serviceIsNotActiveFunc,
  118. },
  119. expected: map[string]string{
  120. "network-plugin": "cni",
  121. "hostname-override": "override-name",
  122. },
  123. },
  124. {
  125. name: "hostname override from NodeRegistrationOptions.KubeletExtraArgs",
  126. opts: kubeletFlagsOpts{
  127. nodeRegOpts: &kubeadmapi.NodeRegistrationOptions{
  128. CRISocket: "/var/run/dockershim.sock",
  129. KubeletExtraArgs: map[string]string{"hostname-override": "override-name"},
  130. },
  131. execer: errCgroupExecer,
  132. isServiceActiveFunc: serviceIsNotActiveFunc,
  133. },
  134. expected: map[string]string{
  135. "network-plugin": "cni",
  136. "hostname-override": "override-name",
  137. },
  138. },
  139. {
  140. name: "systemd cgroup driver",
  141. opts: kubeletFlagsOpts{
  142. nodeRegOpts: &kubeadmapi.NodeRegistrationOptions{
  143. CRISocket: "/var/run/dockershim.sock",
  144. },
  145. execer: systemdCgroupExecer,
  146. isServiceActiveFunc: serviceIsNotActiveFunc,
  147. },
  148. expected: map[string]string{
  149. "network-plugin": "cni",
  150. "cgroup-driver": "systemd",
  151. },
  152. },
  153. {
  154. name: "cgroupfs cgroup driver",
  155. opts: kubeletFlagsOpts{
  156. nodeRegOpts: &kubeadmapi.NodeRegistrationOptions{
  157. CRISocket: "/var/run/dockershim.sock",
  158. },
  159. execer: cgroupfsCgroupExecer,
  160. isServiceActiveFunc: serviceIsNotActiveFunc,
  161. },
  162. expected: map[string]string{
  163. "network-plugin": "cni",
  164. "cgroup-driver": "cgroupfs",
  165. },
  166. },
  167. {
  168. name: "external CRI runtime",
  169. opts: kubeletFlagsOpts{
  170. nodeRegOpts: &kubeadmapi.NodeRegistrationOptions{
  171. CRISocket: "/var/run/containerd.sock",
  172. },
  173. execer: cgroupfsCgroupExecer,
  174. isServiceActiveFunc: serviceIsNotActiveFunc,
  175. },
  176. expected: map[string]string{
  177. "container-runtime": "remote",
  178. "container-runtime-endpoint": "/var/run/containerd.sock",
  179. },
  180. },
  181. {
  182. name: "register with taints",
  183. opts: kubeletFlagsOpts{
  184. nodeRegOpts: &kubeadmapi.NodeRegistrationOptions{
  185. CRISocket: "/var/run/containerd.sock",
  186. Taints: []v1.Taint{
  187. {
  188. Key: "foo",
  189. Value: "bar",
  190. Effect: "baz",
  191. },
  192. {
  193. Key: "key",
  194. Value: "val",
  195. Effect: "eff",
  196. },
  197. },
  198. },
  199. registerTaintsUsingFlags: true,
  200. execer: cgroupfsCgroupExecer,
  201. isServiceActiveFunc: serviceIsNotActiveFunc,
  202. },
  203. expected: map[string]string{
  204. "container-runtime": "remote",
  205. "container-runtime-endpoint": "/var/run/containerd.sock",
  206. "register-with-taints": "foo=bar:baz,key=val:eff",
  207. },
  208. },
  209. {
  210. name: "systemd-resolved running",
  211. opts: kubeletFlagsOpts{
  212. nodeRegOpts: &kubeadmapi.NodeRegistrationOptions{
  213. CRISocket: "/var/run/containerd.sock",
  214. },
  215. execer: cgroupfsCgroupExecer,
  216. isServiceActiveFunc: serviceIsActiveFunc,
  217. },
  218. expected: map[string]string{
  219. "container-runtime": "remote",
  220. "container-runtime-endpoint": "/var/run/containerd.sock",
  221. "resolv-conf": "/run/systemd/resolve/resolv.conf",
  222. },
  223. },
  224. {
  225. name: "pause image is set",
  226. opts: kubeletFlagsOpts{
  227. nodeRegOpts: &kubeadmapi.NodeRegistrationOptions{
  228. CRISocket: "/var/run/dockershim.sock",
  229. },
  230. pauseImage: "gcr.io/pause:3.2",
  231. execer: cgroupfsCgroupExecer,
  232. isServiceActiveFunc: serviceIsNotActiveFunc,
  233. },
  234. expected: map[string]string{
  235. "network-plugin": "cni",
  236. "cgroup-driver": "cgroupfs",
  237. "pod-infra-container-image": "gcr.io/pause:3.2",
  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. }