options_test.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  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. "context"
  16. "fmt"
  17. "io/ioutil"
  18. "net/http"
  19. "net/http/httptest"
  20. "os"
  21. "path/filepath"
  22. "testing"
  23. "time"
  24. "github.com/google/go-cmp/cmp"
  25. "github.com/stretchr/testify/assert"
  26. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  27. "k8s.io/apimachinery/pkg/runtime"
  28. apiserveroptions "k8s.io/apiserver/pkg/server/options"
  29. componentbaseconfig "k8s.io/component-base/config"
  30. kubeschedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"
  31. )
  32. func TestSchedulerOptions(t *testing.T) {
  33. // temp dir
  34. tmpDir, err := ioutil.TempDir("", "scheduler-options")
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. defer os.RemoveAll(tmpDir)
  39. // record the username requests were made with
  40. username := ""
  41. // https server
  42. server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  43. username, _, _ = req.BasicAuth()
  44. if username == "" {
  45. username = "none, tls"
  46. }
  47. w.WriteHeader(200)
  48. w.Write([]byte(`ok`))
  49. }))
  50. defer server.Close()
  51. // http server
  52. insecureserver := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  53. username, _, _ = req.BasicAuth()
  54. if username == "" {
  55. username = "none, http"
  56. }
  57. w.WriteHeader(200)
  58. w.Write([]byte(`ok`))
  59. }))
  60. defer insecureserver.Close()
  61. // config file and kubeconfig
  62. configFile := filepath.Join(tmpDir, "scheduler.yaml")
  63. configKubeconfig := filepath.Join(tmpDir, "config.kubeconfig")
  64. if err := ioutil.WriteFile(configFile, []byte(fmt.Sprintf(`
  65. apiVersion: kubescheduler.config.k8s.io/v1alpha2
  66. kind: KubeSchedulerConfiguration
  67. clientConnection:
  68. kubeconfig: "%s"
  69. leaderElection:
  70. leaderElect: true`, configKubeconfig)), os.FileMode(0600)); err != nil {
  71. t.Fatal(err)
  72. }
  73. if err := ioutil.WriteFile(configKubeconfig, []byte(fmt.Sprintf(`
  74. apiVersion: v1
  75. kind: Config
  76. clusters:
  77. - cluster:
  78. insecure-skip-tls-verify: true
  79. server: %s
  80. name: default
  81. contexts:
  82. - context:
  83. cluster: default
  84. user: default
  85. name: default
  86. current-context: default
  87. users:
  88. - name: default
  89. user:
  90. username: config
  91. `, server.URL)), os.FileMode(0600)); err != nil {
  92. t.Fatal(err)
  93. }
  94. oldConfigFile := filepath.Join(tmpDir, "scheduler_old.yaml")
  95. if err := ioutil.WriteFile(oldConfigFile, []byte(fmt.Sprintf(`
  96. apiVersion: componentconfig/v1alpha1
  97. kind: KubeSchedulerConfiguration
  98. clientConnection:
  99. kubeconfig: "%s"
  100. leaderElection:
  101. leaderElect: true`, configKubeconfig)), os.FileMode(0600)); err != nil {
  102. t.Fatal(err)
  103. }
  104. unknownVersionConfig := filepath.Join(tmpDir, "scheduler_invalid_wrong_api_version.yaml")
  105. if err := ioutil.WriteFile(unknownVersionConfig, []byte(fmt.Sprintf(`
  106. apiVersion: kubescheduler.config.k8s.io/unknown
  107. kind: KubeSchedulerConfiguration
  108. clientConnection:
  109. kubeconfig: "%s"
  110. leaderElection:
  111. leaderElect: true`, configKubeconfig)), os.FileMode(0600)); err != nil {
  112. t.Fatal(err)
  113. }
  114. noVersionConfig := filepath.Join(tmpDir, "scheduler_invalid_no_version.yaml")
  115. if err := ioutil.WriteFile(noVersionConfig, []byte(fmt.Sprintf(`
  116. kind: KubeSchedulerConfiguration
  117. clientConnection:
  118. kubeconfig: "%s"
  119. leaderElection:
  120. leaderElect: true`, configKubeconfig)), os.FileMode(0600)); err != nil {
  121. t.Fatal(err)
  122. }
  123. v1alpha1Config := filepath.Join(tmpDir, "kubeconfig_v1alpha1.yaml")
  124. if err := ioutil.WriteFile(v1alpha1Config, []byte(fmt.Sprintf(`
  125. apiVersion: kubescheduler.config.k8s.io/v1alpha1
  126. kind: KubeSchedulerConfiguration
  127. schedulerName: "my-old-scheduler"
  128. clientConnection:
  129. kubeconfig: "%s"
  130. leaderElection:
  131. leaderElect: true
  132. hardPodAffinitySymmetricWeight: 3`, configKubeconfig)), os.FileMode(0600)); err != nil {
  133. t.Fatal(err)
  134. }
  135. unknownFieldConfigLenient := filepath.Join(tmpDir, "scheduler_invalid_unknown_field_lenient.yaml")
  136. if err := ioutil.WriteFile(unknownFieldConfigLenient, []byte(fmt.Sprintf(`
  137. apiVersion: kubescheduler.config.k8s.io/v1alpha1
  138. kind: KubeSchedulerConfiguration
  139. clientConnection:
  140. kubeconfig: "%s"
  141. leaderElection:
  142. leaderElect: true
  143. foo: bar`, configKubeconfig)), os.FileMode(0600)); err != nil {
  144. t.Fatal(err)
  145. }
  146. unknownFieldConfig := filepath.Join(tmpDir, "scheduler_invalid_unknown_field.yaml")
  147. if err := ioutil.WriteFile(unknownFieldConfig, []byte(fmt.Sprintf(`
  148. apiVersion: kubescheduler.config.k8s.io/v1alpha2
  149. kind: KubeSchedulerConfiguration
  150. clientConnection:
  151. kubeconfig: "%s"
  152. leaderElection:
  153. leaderElect: true
  154. foo: bar`, configKubeconfig)), os.FileMode(0600)); err != nil {
  155. t.Fatal(err)
  156. }
  157. duplicateFieldConfigLenient := filepath.Join(tmpDir, "scheduler_invalid_duplicate_fields_lenient.yaml")
  158. if err := ioutil.WriteFile(duplicateFieldConfigLenient, []byte(fmt.Sprintf(`
  159. apiVersion: kubescheduler.config.k8s.io/v1alpha1
  160. kind: KubeSchedulerConfiguration
  161. clientConnection:
  162. kubeconfig: "%s"
  163. leaderElection:
  164. leaderElect: true
  165. leaderElect: false`, configKubeconfig)), os.FileMode(0600)); err != nil {
  166. t.Fatal(err)
  167. }
  168. duplicateFieldConfig := filepath.Join(tmpDir, "scheduler_invalid_duplicate_fields.yaml")
  169. if err := ioutil.WriteFile(duplicateFieldConfig, []byte(fmt.Sprintf(`
  170. apiVersion: kubescheduler.config.k8s.io/v1alpha2
  171. kind: KubeSchedulerConfiguration
  172. clientConnection:
  173. kubeconfig: "%s"
  174. leaderElection:
  175. leaderElect: true
  176. leaderElect: false`, configKubeconfig)), os.FileMode(0600)); err != nil {
  177. t.Fatal(err)
  178. }
  179. // flag-specified kubeconfig
  180. flagKubeconfig := filepath.Join(tmpDir, "flag.kubeconfig")
  181. if err := ioutil.WriteFile(flagKubeconfig, []byte(fmt.Sprintf(`
  182. apiVersion: v1
  183. kind: Config
  184. clusters:
  185. - cluster:
  186. insecure-skip-tls-verify: true
  187. server: %s
  188. name: default
  189. contexts:
  190. - context:
  191. cluster: default
  192. user: default
  193. name: default
  194. current-context: default
  195. users:
  196. - name: default
  197. user:
  198. username: flag
  199. `, server.URL)), os.FileMode(0600)); err != nil {
  200. t.Fatal(err)
  201. }
  202. // plugin config
  203. pluginconfigFile := filepath.Join(tmpDir, "plugin.yaml")
  204. if err := ioutil.WriteFile(pluginconfigFile, []byte(fmt.Sprintf(`
  205. apiVersion: kubescheduler.config.k8s.io/v1alpha2
  206. kind: KubeSchedulerConfiguration
  207. clientConnection:
  208. kubeconfig: "%s"
  209. profiles:
  210. - plugins:
  211. reserve:
  212. enabled:
  213. - name: foo
  214. - name: bar
  215. disabled:
  216. - name: baz
  217. preBind:
  218. enabled:
  219. - name: foo
  220. disabled:
  221. - name: baz
  222. pluginConfig:
  223. - name: foo
  224. `, configKubeconfig)), os.FileMode(0600)); err != nil {
  225. t.Fatal(err)
  226. }
  227. // v1alpha1 postfilter plugin config
  228. postfilterPluginConfigFile := filepath.Join(tmpDir, "v1alpha1_postfilter_plugin.yaml")
  229. if err := ioutil.WriteFile(postfilterPluginConfigFile, []byte(fmt.Sprintf(`
  230. apiVersion: kubescheduler.config.k8s.io/v1alpha1
  231. kind: KubeSchedulerConfiguration
  232. clientConnection:
  233. kubeconfig: "%s"
  234. plugins:
  235. postFilter:
  236. enabled:
  237. - name: foo
  238. - name: bar
  239. disabled:
  240. - name: baz
  241. `, configKubeconfig)), os.FileMode(0600)); err != nil {
  242. t.Fatal(err)
  243. }
  244. // Insulate this test from picking up in-cluster config when run inside a pod
  245. // We can't assume we have permissions to write to /var/run/secrets/... from a unit test to mock in-cluster config for testing
  246. originalHost := os.Getenv("KUBERNETES_SERVICE_HOST")
  247. if len(originalHost) > 0 {
  248. os.Setenv("KUBERNETES_SERVICE_HOST", "")
  249. defer os.Setenv("KUBERNETES_SERVICE_HOST", originalHost)
  250. }
  251. defaultSource := "DefaultProvider"
  252. defaultBindTimeoutSeconds := int64(600)
  253. defaultPodInitialBackoffSeconds := int64(1)
  254. defaultPodMaxBackoffSeconds := int64(10)
  255. defaultPercentageOfNodesToScore := int32(0)
  256. testcases := []struct {
  257. name string
  258. options *Options
  259. expectedUsername string
  260. expectedError string
  261. expectedConfig kubeschedulerconfig.KubeSchedulerConfiguration
  262. checkErrFn func(err error) bool
  263. }{
  264. {
  265. name: "config file",
  266. options: &Options{
  267. ConfigFile: configFile,
  268. ComponentConfig: func() kubeschedulerconfig.KubeSchedulerConfiguration {
  269. cfg, err := newDefaultComponentConfig()
  270. if err != nil {
  271. t.Fatal(err)
  272. }
  273. return *cfg
  274. }(),
  275. SecureServing: (&apiserveroptions.SecureServingOptions{
  276. ServerCert: apiserveroptions.GeneratableKeyCert{
  277. CertDirectory: "/a/b/c",
  278. PairName: "kube-scheduler",
  279. },
  280. HTTP2MaxStreamsPerConnection: 47,
  281. }).WithLoopback(),
  282. Authentication: &apiserveroptions.DelegatingAuthenticationOptions{
  283. CacheTTL: 10 * time.Second,
  284. ClientCert: apiserveroptions.ClientCertAuthenticationOptions{},
  285. RequestHeader: apiserveroptions.RequestHeaderAuthenticationOptions{
  286. UsernameHeaders: []string{"x-remote-user"},
  287. GroupHeaders: []string{"x-remote-group"},
  288. ExtraHeaderPrefixes: []string{"x-remote-extra-"},
  289. },
  290. RemoteKubeConfigFileOptional: true,
  291. },
  292. Authorization: &apiserveroptions.DelegatingAuthorizationOptions{
  293. AllowCacheTTL: 10 * time.Second,
  294. DenyCacheTTL: 10 * time.Second,
  295. RemoteKubeConfigFileOptional: true,
  296. AlwaysAllowPaths: []string{"/healthz"}, // note: this does not match /healthz/ or /healthz/*
  297. },
  298. },
  299. expectedUsername: "config",
  300. expectedConfig: kubeschedulerconfig.KubeSchedulerConfiguration{
  301. AlgorithmSource: kubeschedulerconfig.SchedulerAlgorithmSource{Provider: &defaultSource},
  302. HealthzBindAddress: "0.0.0.0:10251",
  303. MetricsBindAddress: "0.0.0.0:10251",
  304. DebuggingConfiguration: componentbaseconfig.DebuggingConfiguration{
  305. EnableProfiling: true,
  306. EnableContentionProfiling: true,
  307. },
  308. LeaderElection: kubeschedulerconfig.KubeSchedulerLeaderElectionConfiguration{
  309. LeaderElectionConfiguration: componentbaseconfig.LeaderElectionConfiguration{
  310. LeaderElect: true,
  311. LeaseDuration: metav1.Duration{Duration: 15 * time.Second},
  312. RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
  313. RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
  314. ResourceLock: "endpointsleases",
  315. ResourceNamespace: "kube-system",
  316. ResourceName: "kube-scheduler",
  317. },
  318. },
  319. ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
  320. Kubeconfig: configKubeconfig,
  321. QPS: 50,
  322. Burst: 100,
  323. ContentType: "application/vnd.kubernetes.protobuf",
  324. },
  325. PercentageOfNodesToScore: defaultPercentageOfNodesToScore,
  326. BindTimeoutSeconds: defaultBindTimeoutSeconds,
  327. PodInitialBackoffSeconds: defaultPodInitialBackoffSeconds,
  328. PodMaxBackoffSeconds: defaultPodMaxBackoffSeconds,
  329. Profiles: []kubeschedulerconfig.KubeSchedulerProfile{
  330. {SchedulerName: "default-scheduler"},
  331. },
  332. },
  333. },
  334. {
  335. name: "config file in componentconfig/v1alpha1",
  336. options: &Options{
  337. ConfigFile: oldConfigFile,
  338. ComponentConfig: func() kubeschedulerconfig.KubeSchedulerConfiguration {
  339. cfg, err := newDefaultComponentConfig()
  340. if err != nil {
  341. t.Fatal(err)
  342. }
  343. return *cfg
  344. }(),
  345. },
  346. expectedError: "no kind \"KubeSchedulerConfiguration\" is registered for version \"componentconfig/v1alpha1\"",
  347. },
  348. {
  349. name: "unknown version kubescheduler.config.k8s.io/unknown",
  350. options: &Options{ConfigFile: unknownVersionConfig},
  351. expectedError: "no kind \"KubeSchedulerConfiguration\" is registered for version \"kubescheduler.config.k8s.io/unknown\"",
  352. },
  353. {
  354. name: "config file with no version",
  355. options: &Options{ConfigFile: noVersionConfig},
  356. expectedError: "Object 'apiVersion' is missing",
  357. },
  358. {
  359. name: "kubeconfig flag",
  360. options: &Options{
  361. ComponentConfig: func() kubeschedulerconfig.KubeSchedulerConfiguration {
  362. cfg, _ := newDefaultComponentConfig()
  363. cfg.ClientConnection.Kubeconfig = flagKubeconfig
  364. return *cfg
  365. }(),
  366. SecureServing: (&apiserveroptions.SecureServingOptions{
  367. ServerCert: apiserveroptions.GeneratableKeyCert{
  368. CertDirectory: "/a/b/c",
  369. PairName: "kube-scheduler",
  370. },
  371. HTTP2MaxStreamsPerConnection: 47,
  372. }).WithLoopback(),
  373. Authentication: &apiserveroptions.DelegatingAuthenticationOptions{
  374. CacheTTL: 10 * time.Second,
  375. ClientCert: apiserveroptions.ClientCertAuthenticationOptions{},
  376. RequestHeader: apiserveroptions.RequestHeaderAuthenticationOptions{
  377. UsernameHeaders: []string{"x-remote-user"},
  378. GroupHeaders: []string{"x-remote-group"},
  379. ExtraHeaderPrefixes: []string{"x-remote-extra-"},
  380. },
  381. RemoteKubeConfigFileOptional: true,
  382. },
  383. Authorization: &apiserveroptions.DelegatingAuthorizationOptions{
  384. AllowCacheTTL: 10 * time.Second,
  385. DenyCacheTTL: 10 * time.Second,
  386. RemoteKubeConfigFileOptional: true,
  387. AlwaysAllowPaths: []string{"/healthz"}, // note: this does not match /healthz/ or /healthz/*
  388. },
  389. },
  390. expectedUsername: "flag",
  391. expectedConfig: kubeschedulerconfig.KubeSchedulerConfiguration{
  392. AlgorithmSource: kubeschedulerconfig.SchedulerAlgorithmSource{Provider: &defaultSource},
  393. HealthzBindAddress: "", // defaults empty when not running from config file
  394. MetricsBindAddress: "", // defaults empty when not running from config file
  395. DebuggingConfiguration: componentbaseconfig.DebuggingConfiguration{
  396. EnableProfiling: true,
  397. EnableContentionProfiling: true,
  398. },
  399. LeaderElection: kubeschedulerconfig.KubeSchedulerLeaderElectionConfiguration{
  400. LeaderElectionConfiguration: componentbaseconfig.LeaderElectionConfiguration{
  401. LeaderElect: true,
  402. LeaseDuration: metav1.Duration{Duration: 15 * time.Second},
  403. RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
  404. RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
  405. ResourceLock: "endpointsleases",
  406. ResourceNamespace: "kube-system",
  407. ResourceName: "kube-scheduler",
  408. },
  409. },
  410. ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
  411. Kubeconfig: flagKubeconfig,
  412. QPS: 50,
  413. Burst: 100,
  414. ContentType: "application/vnd.kubernetes.protobuf",
  415. },
  416. PercentageOfNodesToScore: defaultPercentageOfNodesToScore,
  417. BindTimeoutSeconds: defaultBindTimeoutSeconds,
  418. PodInitialBackoffSeconds: defaultPodInitialBackoffSeconds,
  419. PodMaxBackoffSeconds: defaultPodMaxBackoffSeconds,
  420. Profiles: []kubeschedulerconfig.KubeSchedulerProfile{
  421. {SchedulerName: "default-scheduler"},
  422. },
  423. },
  424. },
  425. {
  426. name: "overridden master",
  427. options: &Options{
  428. ComponentConfig: func() kubeschedulerconfig.KubeSchedulerConfiguration {
  429. cfg, _ := newDefaultComponentConfig()
  430. cfg.ClientConnection.Kubeconfig = flagKubeconfig
  431. return *cfg
  432. }(),
  433. Master: insecureserver.URL,
  434. SecureServing: (&apiserveroptions.SecureServingOptions{
  435. ServerCert: apiserveroptions.GeneratableKeyCert{
  436. CertDirectory: "/a/b/c",
  437. PairName: "kube-scheduler",
  438. },
  439. HTTP2MaxStreamsPerConnection: 47,
  440. }).WithLoopback(),
  441. Authentication: &apiserveroptions.DelegatingAuthenticationOptions{
  442. CacheTTL: 10 * time.Second,
  443. RequestHeader: apiserveroptions.RequestHeaderAuthenticationOptions{
  444. UsernameHeaders: []string{"x-remote-user"},
  445. GroupHeaders: []string{"x-remote-group"},
  446. ExtraHeaderPrefixes: []string{"x-remote-extra-"},
  447. },
  448. RemoteKubeConfigFileOptional: true,
  449. },
  450. Authorization: &apiserveroptions.DelegatingAuthorizationOptions{
  451. AllowCacheTTL: 10 * time.Second,
  452. DenyCacheTTL: 10 * time.Second,
  453. RemoteKubeConfigFileOptional: true,
  454. AlwaysAllowPaths: []string{"/healthz"}, // note: this does not match /healthz/ or /healthz/*
  455. },
  456. },
  457. expectedConfig: kubeschedulerconfig.KubeSchedulerConfiguration{
  458. AlgorithmSource: kubeschedulerconfig.SchedulerAlgorithmSource{Provider: &defaultSource},
  459. HealthzBindAddress: "", // defaults empty when not running from config file
  460. MetricsBindAddress: "", // defaults empty when not running from config file
  461. DebuggingConfiguration: componentbaseconfig.DebuggingConfiguration{
  462. EnableProfiling: true,
  463. EnableContentionProfiling: true,
  464. },
  465. LeaderElection: kubeschedulerconfig.KubeSchedulerLeaderElectionConfiguration{
  466. LeaderElectionConfiguration: componentbaseconfig.LeaderElectionConfiguration{
  467. LeaderElect: true,
  468. LeaseDuration: metav1.Duration{Duration: 15 * time.Second},
  469. RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
  470. RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
  471. ResourceLock: "endpointsleases",
  472. ResourceNamespace: "kube-system",
  473. ResourceName: "kube-scheduler",
  474. },
  475. },
  476. ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
  477. Kubeconfig: flagKubeconfig,
  478. QPS: 50,
  479. Burst: 100,
  480. ContentType: "application/vnd.kubernetes.protobuf",
  481. },
  482. PercentageOfNodesToScore: defaultPercentageOfNodesToScore,
  483. BindTimeoutSeconds: defaultBindTimeoutSeconds,
  484. PodInitialBackoffSeconds: defaultPodInitialBackoffSeconds,
  485. PodMaxBackoffSeconds: defaultPodMaxBackoffSeconds,
  486. Profiles: []kubeschedulerconfig.KubeSchedulerProfile{
  487. {SchedulerName: "default-scheduler"},
  488. },
  489. },
  490. expectedUsername: "none, http",
  491. },
  492. {
  493. name: "plugin config",
  494. options: &Options{
  495. ConfigFile: pluginconfigFile,
  496. },
  497. expectedUsername: "config",
  498. expectedConfig: kubeschedulerconfig.KubeSchedulerConfiguration{
  499. AlgorithmSource: kubeschedulerconfig.SchedulerAlgorithmSource{Provider: &defaultSource},
  500. HealthzBindAddress: "0.0.0.0:10251",
  501. MetricsBindAddress: "0.0.0.0:10251",
  502. DebuggingConfiguration: componentbaseconfig.DebuggingConfiguration{
  503. EnableProfiling: true,
  504. EnableContentionProfiling: true,
  505. },
  506. LeaderElection: kubeschedulerconfig.KubeSchedulerLeaderElectionConfiguration{
  507. LeaderElectionConfiguration: componentbaseconfig.LeaderElectionConfiguration{
  508. LeaderElect: true,
  509. LeaseDuration: metav1.Duration{Duration: 15 * time.Second},
  510. RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
  511. RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
  512. ResourceLock: "endpointsleases",
  513. ResourceNamespace: "kube-system",
  514. ResourceName: "kube-scheduler",
  515. },
  516. },
  517. ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
  518. Kubeconfig: configKubeconfig,
  519. QPS: 50,
  520. Burst: 100,
  521. ContentType: "application/vnd.kubernetes.protobuf",
  522. },
  523. PercentageOfNodesToScore: defaultPercentageOfNodesToScore,
  524. BindTimeoutSeconds: defaultBindTimeoutSeconds,
  525. PodInitialBackoffSeconds: defaultPodInitialBackoffSeconds,
  526. PodMaxBackoffSeconds: defaultPodMaxBackoffSeconds,
  527. Profiles: []kubeschedulerconfig.KubeSchedulerProfile{
  528. {
  529. SchedulerName: "default-scheduler",
  530. Plugins: &kubeschedulerconfig.Plugins{
  531. Reserve: &kubeschedulerconfig.PluginSet{
  532. Enabled: []kubeschedulerconfig.Plugin{
  533. {
  534. Name: "foo",
  535. },
  536. {
  537. Name: "bar",
  538. },
  539. },
  540. Disabled: []kubeschedulerconfig.Plugin{
  541. {
  542. Name: "baz",
  543. },
  544. },
  545. },
  546. PreBind: &kubeschedulerconfig.PluginSet{
  547. Enabled: []kubeschedulerconfig.Plugin{
  548. {
  549. Name: "foo",
  550. },
  551. },
  552. Disabled: []kubeschedulerconfig.Plugin{
  553. {
  554. Name: "baz",
  555. },
  556. },
  557. },
  558. },
  559. PluginConfig: []kubeschedulerconfig.PluginConfig{
  560. {
  561. Name: "foo",
  562. Args: runtime.Unknown{},
  563. },
  564. },
  565. },
  566. },
  567. },
  568. },
  569. {
  570. name: "v1alpha1 postfilter plugin config",
  571. options: &Options{
  572. ConfigFile: postfilterPluginConfigFile,
  573. },
  574. expectedUsername: "config",
  575. expectedConfig: kubeschedulerconfig.KubeSchedulerConfiguration{
  576. AlgorithmSource: kubeschedulerconfig.SchedulerAlgorithmSource{Provider: &defaultSource},
  577. HealthzBindAddress: "0.0.0.0:10251",
  578. MetricsBindAddress: "0.0.0.0:10251",
  579. DebuggingConfiguration: componentbaseconfig.DebuggingConfiguration{
  580. EnableProfiling: true,
  581. EnableContentionProfiling: true,
  582. },
  583. LeaderElection: kubeschedulerconfig.KubeSchedulerLeaderElectionConfiguration{
  584. LeaderElectionConfiguration: componentbaseconfig.LeaderElectionConfiguration{
  585. LeaderElect: true,
  586. LeaseDuration: metav1.Duration{Duration: 15 * time.Second},
  587. RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
  588. RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
  589. ResourceLock: "endpointsleases",
  590. ResourceNamespace: "kube-system",
  591. ResourceName: "kube-scheduler",
  592. },
  593. },
  594. ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
  595. Kubeconfig: configKubeconfig,
  596. QPS: 50,
  597. Burst: 100,
  598. ContentType: "application/vnd.kubernetes.protobuf",
  599. },
  600. PercentageOfNodesToScore: defaultPercentageOfNodesToScore,
  601. BindTimeoutSeconds: defaultBindTimeoutSeconds,
  602. PodInitialBackoffSeconds: defaultPodInitialBackoffSeconds,
  603. PodMaxBackoffSeconds: defaultPodMaxBackoffSeconds,
  604. Profiles: []kubeschedulerconfig.KubeSchedulerProfile{
  605. {
  606. SchedulerName: "default-scheduler",
  607. Plugins: &kubeschedulerconfig.Plugins{
  608. PreScore: &kubeschedulerconfig.PluginSet{
  609. Enabled: []kubeschedulerconfig.Plugin{
  610. {
  611. Name: "foo",
  612. },
  613. {
  614. Name: "bar",
  615. },
  616. },
  617. Disabled: []kubeschedulerconfig.Plugin{
  618. {
  619. Name: "baz",
  620. },
  621. },
  622. },
  623. },
  624. },
  625. },
  626. },
  627. },
  628. {
  629. name: "no config",
  630. options: &Options{},
  631. expectedError: "no configuration has been provided",
  632. },
  633. {
  634. name: "Deprecated HardPodAffinitySymmetricWeight",
  635. options: &Options{
  636. ComponentConfig: func() kubeschedulerconfig.KubeSchedulerConfiguration {
  637. cfg, _ := newDefaultComponentConfig()
  638. cfg.ClientConnection.Kubeconfig = flagKubeconfig
  639. return *cfg
  640. }(),
  641. Deprecated: &DeprecatedOptions{
  642. HardPodAffinitySymmetricWeight: 5,
  643. },
  644. },
  645. expectedUsername: "flag",
  646. expectedConfig: kubeschedulerconfig.KubeSchedulerConfiguration{
  647. AlgorithmSource: kubeschedulerconfig.SchedulerAlgorithmSource{Provider: &defaultSource},
  648. DebuggingConfiguration: componentbaseconfig.DebuggingConfiguration{
  649. EnableProfiling: true,
  650. EnableContentionProfiling: true,
  651. },
  652. LeaderElection: kubeschedulerconfig.KubeSchedulerLeaderElectionConfiguration{
  653. LeaderElectionConfiguration: componentbaseconfig.LeaderElectionConfiguration{
  654. LeaderElect: true,
  655. LeaseDuration: metav1.Duration{Duration: 15 * time.Second},
  656. RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
  657. RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
  658. ResourceLock: "endpointsleases",
  659. ResourceNamespace: "kube-system",
  660. ResourceName: "kube-scheduler",
  661. },
  662. },
  663. ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
  664. Kubeconfig: flagKubeconfig,
  665. QPS: 50,
  666. Burst: 100,
  667. ContentType: "application/vnd.kubernetes.protobuf",
  668. },
  669. PercentageOfNodesToScore: defaultPercentageOfNodesToScore,
  670. BindTimeoutSeconds: defaultBindTimeoutSeconds,
  671. PodInitialBackoffSeconds: defaultPodInitialBackoffSeconds,
  672. PodMaxBackoffSeconds: defaultPodMaxBackoffSeconds,
  673. Profiles: []kubeschedulerconfig.KubeSchedulerProfile{
  674. {
  675. SchedulerName: "default-scheduler",
  676. PluginConfig: []kubeschedulerconfig.PluginConfig{
  677. {
  678. Name: "InterPodAffinity",
  679. Args: runtime.Unknown{
  680. Raw: []byte(`{"hardPodAffinityWeight":5}`),
  681. },
  682. },
  683. },
  684. },
  685. },
  686. },
  687. },
  688. {
  689. name: "Deprecated SchedulerName flag",
  690. options: &Options{
  691. ComponentConfig: func() kubeschedulerconfig.KubeSchedulerConfiguration {
  692. cfg, _ := newDefaultComponentConfig()
  693. cfg.ClientConnection.Kubeconfig = flagKubeconfig
  694. return *cfg
  695. }(),
  696. Deprecated: &DeprecatedOptions{
  697. SchedulerName: "my-nice-scheduler",
  698. HardPodAffinitySymmetricWeight: 1,
  699. },
  700. },
  701. expectedUsername: "flag",
  702. expectedConfig: kubeschedulerconfig.KubeSchedulerConfiguration{
  703. AlgorithmSource: kubeschedulerconfig.SchedulerAlgorithmSource{Provider: &defaultSource},
  704. DebuggingConfiguration: componentbaseconfig.DebuggingConfiguration{
  705. EnableProfiling: true,
  706. EnableContentionProfiling: true,
  707. },
  708. LeaderElection: kubeschedulerconfig.KubeSchedulerLeaderElectionConfiguration{
  709. LeaderElectionConfiguration: componentbaseconfig.LeaderElectionConfiguration{
  710. LeaderElect: true,
  711. LeaseDuration: metav1.Duration{Duration: 15 * time.Second},
  712. RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
  713. RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
  714. ResourceLock: "endpointsleases",
  715. ResourceNamespace: "kube-system",
  716. ResourceName: "kube-scheduler",
  717. },
  718. },
  719. ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
  720. Kubeconfig: flagKubeconfig,
  721. QPS: 50,
  722. Burst: 100,
  723. ContentType: "application/vnd.kubernetes.protobuf",
  724. },
  725. PercentageOfNodesToScore: defaultPercentageOfNodesToScore,
  726. BindTimeoutSeconds: defaultBindTimeoutSeconds,
  727. PodInitialBackoffSeconds: defaultPodInitialBackoffSeconds,
  728. PodMaxBackoffSeconds: defaultPodMaxBackoffSeconds,
  729. Profiles: []kubeschedulerconfig.KubeSchedulerProfile{
  730. {SchedulerName: "my-nice-scheduler"},
  731. },
  732. },
  733. },
  734. {
  735. name: "v1alpha1 config with SchedulerName and HardPodAffinitySymmetricWeight",
  736. options: &Options{
  737. ConfigFile: v1alpha1Config,
  738. },
  739. expectedUsername: "config",
  740. expectedConfig: kubeschedulerconfig.KubeSchedulerConfiguration{
  741. AlgorithmSource: kubeschedulerconfig.SchedulerAlgorithmSource{Provider: &defaultSource},
  742. HealthzBindAddress: "0.0.0.0:10251",
  743. MetricsBindAddress: "0.0.0.0:10251",
  744. DebuggingConfiguration: componentbaseconfig.DebuggingConfiguration{
  745. EnableProfiling: true,
  746. EnableContentionProfiling: true,
  747. },
  748. LeaderElection: kubeschedulerconfig.KubeSchedulerLeaderElectionConfiguration{
  749. LeaderElectionConfiguration: componentbaseconfig.LeaderElectionConfiguration{
  750. LeaderElect: true,
  751. LeaseDuration: metav1.Duration{Duration: 15 * time.Second},
  752. RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
  753. RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
  754. ResourceLock: "endpointsleases",
  755. ResourceNamespace: "kube-system",
  756. ResourceName: "kube-scheduler",
  757. },
  758. },
  759. ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
  760. Kubeconfig: configKubeconfig,
  761. QPS: 50,
  762. Burst: 100,
  763. ContentType: "application/vnd.kubernetes.protobuf",
  764. },
  765. PercentageOfNodesToScore: defaultPercentageOfNodesToScore,
  766. BindTimeoutSeconds: defaultBindTimeoutSeconds,
  767. PodInitialBackoffSeconds: defaultPodInitialBackoffSeconds,
  768. PodMaxBackoffSeconds: defaultPodMaxBackoffSeconds,
  769. Profiles: []kubeschedulerconfig.KubeSchedulerProfile{
  770. {
  771. SchedulerName: "my-old-scheduler",
  772. PluginConfig: []kubeschedulerconfig.PluginConfig{
  773. {
  774. Name: "InterPodAffinity",
  775. Args: runtime.Unknown{
  776. Raw: []byte(`{"hardPodAffinityWeight":3}`),
  777. },
  778. },
  779. },
  780. },
  781. },
  782. },
  783. },
  784. {
  785. name: "unknown field lenient (v1alpha1)",
  786. options: &Options{
  787. ConfigFile: unknownFieldConfigLenient,
  788. },
  789. expectedUsername: "config",
  790. expectedConfig: kubeschedulerconfig.KubeSchedulerConfiguration{
  791. AlgorithmSource: kubeschedulerconfig.SchedulerAlgorithmSource{Provider: &defaultSource},
  792. HealthzBindAddress: "0.0.0.0:10251",
  793. MetricsBindAddress: "0.0.0.0:10251",
  794. DebuggingConfiguration: componentbaseconfig.DebuggingConfiguration{
  795. EnableProfiling: true,
  796. EnableContentionProfiling: true,
  797. },
  798. LeaderElection: kubeschedulerconfig.KubeSchedulerLeaderElectionConfiguration{
  799. LeaderElectionConfiguration: componentbaseconfig.LeaderElectionConfiguration{
  800. LeaderElect: true,
  801. LeaseDuration: metav1.Duration{Duration: 15 * time.Second},
  802. RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
  803. RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
  804. ResourceLock: "endpointsleases",
  805. ResourceNamespace: "kube-system",
  806. ResourceName: "kube-scheduler",
  807. },
  808. },
  809. ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
  810. Kubeconfig: configKubeconfig,
  811. QPS: 50,
  812. Burst: 100,
  813. ContentType: "application/vnd.kubernetes.protobuf",
  814. },
  815. PercentageOfNodesToScore: defaultPercentageOfNodesToScore,
  816. BindTimeoutSeconds: defaultBindTimeoutSeconds,
  817. PodInitialBackoffSeconds: defaultPodInitialBackoffSeconds,
  818. PodMaxBackoffSeconds: defaultPodMaxBackoffSeconds,
  819. Profiles: []kubeschedulerconfig.KubeSchedulerProfile{
  820. {SchedulerName: "default-scheduler"},
  821. },
  822. },
  823. },
  824. {
  825. name: "unknown field",
  826. options: &Options{
  827. ConfigFile: unknownFieldConfig,
  828. },
  829. expectedError: "found unknown field: foo",
  830. checkErrFn: runtime.IsStrictDecodingError,
  831. },
  832. {
  833. name: "duplicate fields lenient (v1alpha1)",
  834. options: &Options{
  835. ConfigFile: duplicateFieldConfigLenient,
  836. },
  837. expectedUsername: "config",
  838. expectedConfig: kubeschedulerconfig.KubeSchedulerConfiguration{
  839. AlgorithmSource: kubeschedulerconfig.SchedulerAlgorithmSource{Provider: &defaultSource},
  840. HealthzBindAddress: "0.0.0.0:10251",
  841. MetricsBindAddress: "0.0.0.0:10251",
  842. DebuggingConfiguration: componentbaseconfig.DebuggingConfiguration{
  843. EnableProfiling: true,
  844. EnableContentionProfiling: true,
  845. },
  846. LeaderElection: kubeschedulerconfig.KubeSchedulerLeaderElectionConfiguration{
  847. LeaderElectionConfiguration: componentbaseconfig.LeaderElectionConfiguration{
  848. LeaderElect: false,
  849. LeaseDuration: metav1.Duration{Duration: 15 * time.Second},
  850. RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
  851. RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
  852. ResourceLock: "endpointsleases",
  853. ResourceNamespace: "kube-system",
  854. ResourceName: "kube-scheduler",
  855. },
  856. },
  857. ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
  858. Kubeconfig: configKubeconfig,
  859. QPS: 50,
  860. Burst: 100,
  861. ContentType: "application/vnd.kubernetes.protobuf",
  862. },
  863. PercentageOfNodesToScore: defaultPercentageOfNodesToScore,
  864. BindTimeoutSeconds: defaultBindTimeoutSeconds,
  865. PodInitialBackoffSeconds: defaultPodInitialBackoffSeconds,
  866. PodMaxBackoffSeconds: defaultPodMaxBackoffSeconds,
  867. Profiles: []kubeschedulerconfig.KubeSchedulerProfile{
  868. {SchedulerName: "default-scheduler"},
  869. },
  870. },
  871. },
  872. {
  873. name: "duplicate fields",
  874. options: &Options{
  875. ConfigFile: duplicateFieldConfig,
  876. },
  877. expectedError: `key "leaderElect" already set`,
  878. checkErrFn: runtime.IsStrictDecodingError,
  879. },
  880. }
  881. for _, tc := range testcases {
  882. t.Run(tc.name, func(t *testing.T) {
  883. // create the config
  884. config, err := tc.options.Config()
  885. // handle errors
  886. if err != nil {
  887. if tc.expectedError != "" || tc.checkErrFn != nil {
  888. if tc.expectedError != "" {
  889. assert.Contains(t, err.Error(), tc.expectedError)
  890. }
  891. if tc.checkErrFn != nil {
  892. assert.True(t, tc.checkErrFn(err), "got error: %v", err)
  893. }
  894. return
  895. }
  896. assert.NoError(t, err)
  897. return
  898. }
  899. if diff := cmp.Diff(tc.expectedConfig, config.ComponentConfig); diff != "" {
  900. t.Errorf("incorrect config (-want, +got):\n%s", diff)
  901. }
  902. // ensure we have a client
  903. if config.Client == nil {
  904. t.Error("unexpected nil client")
  905. return
  906. }
  907. // test the client talks to the endpoint we expect with the credentials we expect
  908. username = ""
  909. _, err = config.Client.Discovery().RESTClient().Get().AbsPath("/").DoRaw(context.TODO())
  910. if err != nil {
  911. t.Error(err)
  912. return
  913. }
  914. if username != tc.expectedUsername {
  915. t.Errorf("expected server call with user %q, got %q", tc.expectedUsername, username)
  916. }
  917. })
  918. }
  919. }