insecure_serving_test.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 options
  14. import (
  15. "fmt"
  16. "net"
  17. "strconv"
  18. "testing"
  19. "k8s.io/apimachinery/pkg/util/rand"
  20. apiserveroptions "k8s.io/apiserver/pkg/server/options"
  21. schedulerappconfig "k8s.io/kubernetes/cmd/kube-scheduler/app/config"
  22. kubeschedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"
  23. )
  24. func TestOptions_ApplyTo(t *testing.T) {
  25. tests := []struct {
  26. name string
  27. options Options
  28. configLoaded bool
  29. expectHealthzBindAddress, expectMetricsBindAddress string
  30. expectInsecureServingAddress, expectInsecureMetricsServingAddress string
  31. expectInsecureServingPort, expectInsecureMetricsServingPort int
  32. wantErr bool
  33. }{
  34. {
  35. name: "no config, zero port",
  36. options: Options{
  37. ComponentConfig: kubeschedulerconfig.KubeSchedulerConfiguration{
  38. HealthzBindAddress: "1.2.3.4:1234",
  39. MetricsBindAddress: "1.2.3.4:1234",
  40. },
  41. CombinedInsecureServing: &CombinedInsecureServingOptions{
  42. Healthz: (&apiserveroptions.DeprecatedInsecureServingOptions{}).WithLoopback(),
  43. Metrics: (&apiserveroptions.DeprecatedInsecureServingOptions{}).WithLoopback(),
  44. BindPort: 0,
  45. },
  46. },
  47. configLoaded: false,
  48. },
  49. {
  50. name: "config loaded, non-nil healthz",
  51. options: Options{
  52. ComponentConfig: kubeschedulerconfig.KubeSchedulerConfiguration{
  53. HealthzBindAddress: "1.2.3.4:1234",
  54. MetricsBindAddress: "1.2.3.4:1234",
  55. },
  56. CombinedInsecureServing: &CombinedInsecureServingOptions{
  57. Healthz: (&apiserveroptions.DeprecatedInsecureServingOptions{}).WithLoopback(),
  58. BindPort: 0,
  59. },
  60. },
  61. configLoaded: true,
  62. expectHealthzBindAddress: "1.2.3.4:1234",
  63. expectInsecureServingPort: 1234,
  64. expectInsecureServingAddress: "1.2.3.4",
  65. },
  66. {
  67. name: "config loaded, non-nil metrics",
  68. options: Options{
  69. ComponentConfig: kubeschedulerconfig.KubeSchedulerConfiguration{
  70. HealthzBindAddress: "1.2.3.4:1234",
  71. MetricsBindAddress: "1.2.3.4:1234",
  72. },
  73. CombinedInsecureServing: &CombinedInsecureServingOptions{
  74. Metrics: (&apiserveroptions.DeprecatedInsecureServingOptions{}).WithLoopback(),
  75. BindPort: 0,
  76. },
  77. },
  78. configLoaded: true,
  79. expectMetricsBindAddress: "1.2.3.4:1234",
  80. expectInsecureMetricsServingPort: 1234,
  81. expectInsecureMetricsServingAddress: "1.2.3.4",
  82. },
  83. {
  84. name: "config loaded, all set, zero BindPort",
  85. options: Options{
  86. ComponentConfig: kubeschedulerconfig.KubeSchedulerConfiguration{
  87. HealthzBindAddress: "1.2.3.4:1234",
  88. MetricsBindAddress: "1.2.3.4:1234",
  89. },
  90. CombinedInsecureServing: &CombinedInsecureServingOptions{
  91. Healthz: (&apiserveroptions.DeprecatedInsecureServingOptions{}).WithLoopback(),
  92. Metrics: (&apiserveroptions.DeprecatedInsecureServingOptions{}).WithLoopback(),
  93. BindPort: 0,
  94. },
  95. },
  96. configLoaded: true,
  97. expectHealthzBindAddress: "1.2.3.4:1234",
  98. expectInsecureServingPort: 1234,
  99. expectInsecureServingAddress: "1.2.3.4",
  100. expectMetricsBindAddress: "1.2.3.4:1234",
  101. },
  102. {
  103. name: "config loaded, all set, different addresses",
  104. options: Options{
  105. ComponentConfig: kubeschedulerconfig.KubeSchedulerConfiguration{
  106. HealthzBindAddress: "1.2.3.4:1234",
  107. MetricsBindAddress: "1.2.3.4:1235",
  108. },
  109. CombinedInsecureServing: &CombinedInsecureServingOptions{
  110. Healthz: (&apiserveroptions.DeprecatedInsecureServingOptions{}).WithLoopback(),
  111. Metrics: (&apiserveroptions.DeprecatedInsecureServingOptions{}).WithLoopback(),
  112. BindPort: 0,
  113. },
  114. },
  115. configLoaded: true,
  116. expectHealthzBindAddress: "1.2.3.4:1234",
  117. expectInsecureServingPort: 1234,
  118. expectInsecureServingAddress: "1.2.3.4",
  119. expectMetricsBindAddress: "1.2.3.4:1235",
  120. expectInsecureMetricsServingPort: 1235,
  121. expectInsecureMetricsServingAddress: "1.2.3.4",
  122. },
  123. {
  124. name: "no config, all set, port passed",
  125. options: Options{
  126. ComponentConfig: kubeschedulerconfig.KubeSchedulerConfiguration{
  127. HealthzBindAddress: "1.2.3.4:1234",
  128. MetricsBindAddress: "1.2.3.4:1234",
  129. },
  130. CombinedInsecureServing: &CombinedInsecureServingOptions{
  131. Healthz: (&apiserveroptions.DeprecatedInsecureServingOptions{}).WithLoopback(),
  132. Metrics: (&apiserveroptions.DeprecatedInsecureServingOptions{}).WithLoopback(),
  133. BindPort: 1236,
  134. BindAddress: "1.2.3.4",
  135. },
  136. },
  137. configLoaded: false,
  138. expectHealthzBindAddress: "1.2.3.4:1236",
  139. expectInsecureServingPort: 1236,
  140. expectInsecureServingAddress: "1.2.3.4",
  141. expectMetricsBindAddress: "1.2.3.4:1236",
  142. },
  143. {
  144. name: "no config, all set, address passed",
  145. options: Options{
  146. ComponentConfig: kubeschedulerconfig.KubeSchedulerConfiguration{
  147. HealthzBindAddress: "1.2.3.4:1234",
  148. MetricsBindAddress: "1.2.3.4:1234",
  149. },
  150. CombinedInsecureServing: &CombinedInsecureServingOptions{
  151. Healthz: (&apiserveroptions.DeprecatedInsecureServingOptions{}).WithLoopback(),
  152. Metrics: (&apiserveroptions.DeprecatedInsecureServingOptions{}).WithLoopback(),
  153. BindAddress: "2.3.4.5",
  154. BindPort: 1234,
  155. },
  156. },
  157. configLoaded: false,
  158. expectHealthzBindAddress: "2.3.4.5:1234",
  159. expectInsecureServingPort: 1234,
  160. expectInsecureServingAddress: "2.3.4.5",
  161. expectMetricsBindAddress: "2.3.4.5:1234",
  162. },
  163. {
  164. name: "no config, all set, zero port passed",
  165. options: Options{
  166. ComponentConfig: kubeschedulerconfig.KubeSchedulerConfiguration{
  167. HealthzBindAddress: "1.2.3.4:1234",
  168. MetricsBindAddress: "1.2.3.4:1234",
  169. },
  170. CombinedInsecureServing: &CombinedInsecureServingOptions{
  171. Healthz: (&apiserveroptions.DeprecatedInsecureServingOptions{}).WithLoopback(),
  172. Metrics: (&apiserveroptions.DeprecatedInsecureServingOptions{}).WithLoopback(),
  173. BindAddress: "2.3.4.5",
  174. BindPort: 0,
  175. },
  176. },
  177. configLoaded: false,
  178. },
  179. }
  180. for i, tt := range tests {
  181. t.Run(fmt.Sprintf("%d-%s", i, tt.name), func(t *testing.T) {
  182. c := schedulerappconfig.Config{
  183. ComponentConfig: tt.options.ComponentConfig,
  184. }
  185. if tt.options.CombinedInsecureServing != nil {
  186. if tt.options.CombinedInsecureServing.Healthz != nil {
  187. tt.options.CombinedInsecureServing.Healthz.ListenFunc = createMockListener
  188. }
  189. if tt.options.CombinedInsecureServing.Metrics != nil {
  190. tt.options.CombinedInsecureServing.Metrics.ListenFunc = createMockListener
  191. }
  192. }
  193. if tt.configLoaded {
  194. if err := tt.options.CombinedInsecureServing.ApplyToFromLoadedConfig(&c, &c.ComponentConfig); (err != nil) != tt.wantErr {
  195. t.Fatalf("%d - Options.ApplyTo() error = %v, wantErr %v", i, err, tt.wantErr)
  196. }
  197. } else {
  198. if err := tt.options.CombinedInsecureServing.ApplyTo(&c, &c.ComponentConfig); (err != nil) != tt.wantErr {
  199. t.Fatalf("%d - Options.ApplyTo() error = %v, wantErr %v", i, err, tt.wantErr)
  200. }
  201. }
  202. if got, expect := c.ComponentConfig.HealthzBindAddress, tt.expectHealthzBindAddress; got != expect {
  203. t.Errorf("%d - expected HealthzBindAddress %q, got %q", i, expect, got)
  204. }
  205. if got, expect := c.ComponentConfig.MetricsBindAddress, tt.expectMetricsBindAddress; got != expect {
  206. t.Errorf("%d - expected MetricsBindAddress %q, got %q", i, expect, got)
  207. }
  208. if got, expect := c.InsecureServing != nil, tt.expectInsecureServingPort != 0; got != expect {
  209. t.Errorf("%d - expected InsecureServing != nil to be %v, got %v", i, expect, got)
  210. } else if c.InsecureServing != nil {
  211. if got, expect := c.InsecureServing.Listener.(*mockListener).address, tt.expectInsecureServingAddress; got != expect {
  212. t.Errorf("%d - expected healthz address %q, got %q", i, expect, got)
  213. }
  214. if got, expect := c.InsecureServing.Listener.(*mockListener).port, tt.expectInsecureServingPort; got != expect {
  215. t.Errorf("%d - expected healthz port %v, got %v", i, expect, got)
  216. }
  217. }
  218. if got, expect := c.InsecureMetricsServing != nil, tt.expectInsecureMetricsServingPort != 0; got != expect {
  219. t.Errorf("%d - expected Metrics != nil to be %v, got %v", i, expect, got)
  220. } else if c.InsecureMetricsServing != nil {
  221. if got, expect := c.InsecureMetricsServing.Listener.(*mockListener).address, tt.expectInsecureMetricsServingAddress; got != expect {
  222. t.Errorf("%d - expected metrics address %q, got %q", i, expect, got)
  223. }
  224. if got, expect := c.InsecureMetricsServing.Listener.(*mockListener).port, tt.expectInsecureMetricsServingPort; got != expect {
  225. t.Errorf("%d - expected metrics port %v, got %v", i, expect, got)
  226. }
  227. }
  228. })
  229. }
  230. }
  231. type mockListener struct {
  232. address string
  233. port int
  234. }
  235. func createMockListener(network, addr string) (net.Listener, int, error) {
  236. host, portInt, err := splitHostIntPort(addr)
  237. if err != nil {
  238. return nil, 0, err
  239. }
  240. if portInt == 0 {
  241. portInt = rand.IntnRange(1, 32767)
  242. }
  243. return &mockListener{host, portInt}, portInt, nil
  244. }
  245. func (l *mockListener) Accept() (net.Conn, error) { return nil, nil }
  246. func (l *mockListener) Close() error { return nil }
  247. func (l *mockListener) Addr() net.Addr {
  248. return mockAddr(net.JoinHostPort(l.address, strconv.Itoa(l.port)))
  249. }
  250. type mockAddr string
  251. func (a mockAddr) Network() string { return "tcp" }
  252. func (a mockAddr) String() string { return string(a) }