server_test.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. Copyright 2015 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 app
  14. import (
  15. "fmt"
  16. "runtime"
  17. "strings"
  18. "testing"
  19. "github.com/stretchr/testify/assert"
  20. "k8s.io/kubernetes/cmd/kube-proxy/app/options"
  21. "k8s.io/kubernetes/pkg/api"
  22. "k8s.io/kubernetes/pkg/apis/componentconfig"
  23. "k8s.io/kubernetes/pkg/util/iptables"
  24. )
  25. type fakeNodeInterface struct {
  26. node api.Node
  27. }
  28. func (fake *fakeNodeInterface) Get(hostname string) (*api.Node, error) {
  29. return &fake.node, nil
  30. }
  31. type fakeIptablesVersioner struct {
  32. version string // what to return
  33. err error // what to return
  34. }
  35. func (fake *fakeIptablesVersioner) GetVersion() (string, error) {
  36. return fake.version, fake.err
  37. }
  38. type fakeKernelCompatTester struct {
  39. ok bool
  40. }
  41. func (fake *fakeKernelCompatTester) IsCompatible() error {
  42. if !fake.ok {
  43. return fmt.Errorf("error")
  44. }
  45. return nil
  46. }
  47. func Test_getProxyMode(t *testing.T) {
  48. if runtime.GOOS != "linux" {
  49. t.Skip("skipping on non-Linux")
  50. }
  51. var cases = []struct {
  52. flag string
  53. annotationKey string
  54. annotationVal string
  55. iptablesVersion string
  56. kernelCompat bool
  57. iptablesError error
  58. expected string
  59. }{
  60. { // flag says userspace
  61. flag: "userspace",
  62. expected: proxyModeUserspace,
  63. },
  64. { // flag says iptables, error detecting version
  65. flag: "iptables",
  66. iptablesError: fmt.Errorf("oops!"),
  67. expected: proxyModeUserspace,
  68. },
  69. { // flag says iptables, version too low
  70. flag: "iptables",
  71. iptablesVersion: "0.0.0",
  72. expected: proxyModeUserspace,
  73. },
  74. { // flag says iptables, version ok, kernel not compatible
  75. flag: "iptables",
  76. iptablesVersion: iptables.MinCheckVersion,
  77. kernelCompat: false,
  78. expected: proxyModeUserspace,
  79. },
  80. { // flag says iptables, version ok, kernel is compatible
  81. flag: "iptables",
  82. iptablesVersion: iptables.MinCheckVersion,
  83. kernelCompat: true,
  84. expected: proxyModeIptables,
  85. },
  86. { // detect, error
  87. flag: "",
  88. iptablesError: fmt.Errorf("oops!"),
  89. expected: proxyModeUserspace,
  90. },
  91. { // detect, version too low
  92. flag: "",
  93. iptablesVersion: "0.0.0",
  94. expected: proxyModeUserspace,
  95. },
  96. { // detect, version ok, kernel not compatible
  97. flag: "",
  98. iptablesVersion: iptables.MinCheckVersion,
  99. kernelCompat: false,
  100. expected: proxyModeUserspace,
  101. },
  102. { // detect, version ok, kernel is compatible
  103. flag: "",
  104. iptablesVersion: iptables.MinCheckVersion,
  105. kernelCompat: true,
  106. expected: proxyModeIptables,
  107. },
  108. { // annotation says userspace
  109. flag: "",
  110. annotationKey: "net.experimental.kubernetes.io/proxy-mode",
  111. annotationVal: "userspace",
  112. expected: proxyModeUserspace,
  113. },
  114. { // annotation says iptables, error detecting
  115. flag: "",
  116. annotationKey: "net.experimental.kubernetes.io/proxy-mode",
  117. annotationVal: "iptables",
  118. iptablesError: fmt.Errorf("oops!"),
  119. expected: proxyModeUserspace,
  120. },
  121. { // annotation says iptables, version too low
  122. flag: "",
  123. annotationKey: "net.experimental.kubernetes.io/proxy-mode",
  124. annotationVal: "iptables",
  125. iptablesVersion: "0.0.0",
  126. expected: proxyModeUserspace,
  127. },
  128. { // annotation says iptables, version ok, kernel not compatible
  129. flag: "",
  130. annotationKey: "net.experimental.kubernetes.io/proxy-mode",
  131. annotationVal: "iptables",
  132. iptablesVersion: iptables.MinCheckVersion,
  133. kernelCompat: false,
  134. expected: proxyModeUserspace,
  135. },
  136. { // annotation says iptables, version ok, kernel is compatible
  137. flag: "",
  138. annotationKey: "net.experimental.kubernetes.io/proxy-mode",
  139. annotationVal: "iptables",
  140. iptablesVersion: iptables.MinCheckVersion,
  141. kernelCompat: true,
  142. expected: proxyModeIptables,
  143. },
  144. { // annotation says something else, version ok
  145. flag: "",
  146. annotationKey: "net.experimental.kubernetes.io/proxy-mode",
  147. annotationVal: "other",
  148. iptablesVersion: iptables.MinCheckVersion,
  149. kernelCompat: true,
  150. expected: proxyModeIptables,
  151. },
  152. { // annotation says nothing, version ok
  153. flag: "",
  154. annotationKey: "net.experimental.kubernetes.io/proxy-mode",
  155. annotationVal: "",
  156. iptablesVersion: iptables.MinCheckVersion,
  157. kernelCompat: true,
  158. expected: proxyModeIptables,
  159. },
  160. { // annotation says userspace
  161. flag: "",
  162. annotationKey: "net.beta.kubernetes.io/proxy-mode",
  163. annotationVal: "userspace",
  164. expected: proxyModeUserspace,
  165. },
  166. { // annotation says iptables, error detecting
  167. flag: "",
  168. annotationKey: "net.beta.kubernetes.io/proxy-mode",
  169. annotationVal: "iptables",
  170. iptablesError: fmt.Errorf("oops!"),
  171. expected: proxyModeUserspace,
  172. },
  173. { // annotation says iptables, version too low
  174. flag: "",
  175. annotationKey: "net.beta.kubernetes.io/proxy-mode",
  176. annotationVal: "iptables",
  177. iptablesVersion: "0.0.0",
  178. expected: proxyModeUserspace,
  179. },
  180. { // annotation says iptables, version ok, kernel not compatible
  181. flag: "",
  182. annotationKey: "net.beta.kubernetes.io/proxy-mode",
  183. annotationVal: "iptables",
  184. iptablesVersion: iptables.MinCheckVersion,
  185. kernelCompat: false,
  186. expected: proxyModeUserspace,
  187. },
  188. { // annotation says iptables, version ok, kernel is compatible
  189. flag: "",
  190. annotationKey: "net.beta.kubernetes.io/proxy-mode",
  191. annotationVal: "iptables",
  192. iptablesVersion: iptables.MinCheckVersion,
  193. kernelCompat: true,
  194. expected: proxyModeIptables,
  195. },
  196. { // annotation says something else, version ok
  197. flag: "",
  198. annotationKey: "net.beta.kubernetes.io/proxy-mode",
  199. annotationVal: "other",
  200. iptablesVersion: iptables.MinCheckVersion,
  201. kernelCompat: true,
  202. expected: proxyModeIptables,
  203. },
  204. { // annotation says nothing, version ok
  205. flag: "",
  206. annotationKey: "net.beta.kubernetes.io/proxy-mode",
  207. annotationVal: "",
  208. iptablesVersion: iptables.MinCheckVersion,
  209. kernelCompat: true,
  210. expected: proxyModeIptables,
  211. },
  212. { // flag says userspace, annotation disagrees
  213. flag: "userspace",
  214. annotationKey: "net.experimental.kubernetes.io/proxy-mode",
  215. annotationVal: "iptables",
  216. iptablesVersion: iptables.MinCheckVersion,
  217. expected: proxyModeUserspace,
  218. },
  219. { // flag says iptables, annotation disagrees
  220. flag: "iptables",
  221. annotationKey: "net.experimental.kubernetes.io/proxy-mode",
  222. annotationVal: "userspace",
  223. iptablesVersion: iptables.MinCheckVersion,
  224. kernelCompat: true,
  225. expected: proxyModeIptables,
  226. },
  227. { // flag says userspace, annotation disagrees
  228. flag: "userspace",
  229. annotationKey: "net.beta.kubernetes.io/proxy-mode",
  230. annotationVal: "iptables",
  231. iptablesVersion: iptables.MinCheckVersion,
  232. expected: proxyModeUserspace,
  233. },
  234. { // flag says iptables, annotation disagrees
  235. flag: "iptables",
  236. annotationKey: "net.beta.kubernetes.io/proxy-mode",
  237. annotationVal: "userspace",
  238. iptablesVersion: iptables.MinCheckVersion,
  239. kernelCompat: true,
  240. expected: proxyModeIptables,
  241. },
  242. }
  243. for i, c := range cases {
  244. getter := &fakeNodeInterface{}
  245. getter.node.Annotations = map[string]string{c.annotationKey: c.annotationVal}
  246. versioner := &fakeIptablesVersioner{c.iptablesVersion, c.iptablesError}
  247. kcompater := &fakeKernelCompatTester{c.kernelCompat}
  248. r := getProxyMode(c.flag, getter, "host", versioner, kcompater)
  249. if r != c.expected {
  250. t.Errorf("Case[%d] Expected %q, got %q", i, c.expected, r)
  251. }
  252. }
  253. }
  254. // This test verifies that Proxy Server does not crash that means
  255. // Config and iptinterface are not nil when CleanupAndExit is true.
  256. // To avoid proxy crash: https://github.com/kubernetes/kubernetes/pull/14736
  257. func TestProxyServerWithCleanupAndExit(t *testing.T) {
  258. // creates default config
  259. config := options.NewProxyConfig()
  260. // sets CleanupAndExit manually
  261. config.CleanupAndExit = true
  262. // creates new proxy server
  263. proxyserver, err := NewProxyServerDefault(config)
  264. // verifies that nothing is nill except error
  265. assert.Nil(t, err)
  266. assert.NotNil(t, proxyserver)
  267. assert.NotNil(t, proxyserver.Config)
  268. assert.NotNil(t, proxyserver.IptInterface)
  269. }
  270. func TestGetConntrackMax(t *testing.T) {
  271. ncores := runtime.NumCPU()
  272. testCases := []struct {
  273. config componentconfig.KubeProxyConfiguration
  274. expected int
  275. err string
  276. }{
  277. {
  278. config: componentconfig.KubeProxyConfiguration{},
  279. expected: 0,
  280. },
  281. {
  282. config: componentconfig.KubeProxyConfiguration{
  283. ConntrackMax: 12345,
  284. },
  285. expected: 12345,
  286. },
  287. {
  288. config: componentconfig.KubeProxyConfiguration{
  289. ConntrackMax: 12345,
  290. ConntrackMaxPerCore: 67890,
  291. },
  292. expected: -1,
  293. err: "mutually exclusive",
  294. },
  295. {
  296. config: componentconfig.KubeProxyConfiguration{
  297. ConntrackMaxPerCore: 67890, // use this if other is 0
  298. },
  299. expected: 67890 * ncores,
  300. },
  301. }
  302. for i, tc := range testCases {
  303. cfg := options.ProxyServerConfig{KubeProxyConfiguration: tc.config}
  304. x, e := getConntrackMax(&cfg)
  305. if e != nil {
  306. if tc.err == "" {
  307. t.Errorf("[%d] unexpected error: %v", i, e)
  308. } else if !strings.Contains(e.Error(), tc.err) {
  309. t.Errorf("[%d] expected an error containing %q: %v", i, tc.err, e)
  310. }
  311. } else if x != tc.expected {
  312. t.Errorf("[%d] expected %d, got %d", i, tc.expected, x)
  313. }
  314. }
  315. }