options_test.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. Copyright 2017 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. "net"
  16. "reflect"
  17. "testing"
  18. "time"
  19. "github.com/spf13/pflag"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/util/diff"
  22. apiserveroptions "k8s.io/apiserver/pkg/server/options"
  23. componentbaseconfig "k8s.io/component-base/config"
  24. cmoptions "k8s.io/kubernetes/cmd/controller-manager/app/options"
  25. kubectrlmgrconfig "k8s.io/kubernetes/pkg/controller/apis/config"
  26. serviceconfig "k8s.io/kubernetes/pkg/controller/service/config"
  27. )
  28. func TestDefaultFlags(t *testing.T) {
  29. s, _ := NewCloudControllerManagerOptions()
  30. expected := &CloudControllerManagerOptions{
  31. Generic: &cmoptions.GenericControllerManagerConfigurationOptions{
  32. GenericControllerManagerConfiguration: &kubectrlmgrconfig.GenericControllerManagerConfiguration{
  33. Port: DefaultInsecureCloudControllerManagerPort, // Note: InsecureServingOptions.ApplyTo will write the flag value back into the component config
  34. Address: "0.0.0.0", // Note: InsecureServingOptions.ApplyTo will write the flag value back into the component config
  35. MinResyncPeriod: metav1.Duration{Duration: 12 * time.Hour},
  36. ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
  37. ContentType: "application/vnd.kubernetes.protobuf",
  38. QPS: 20.0,
  39. Burst: 30,
  40. },
  41. ControllerStartInterval: metav1.Duration{Duration: 0},
  42. LeaderElection: componentbaseconfig.LeaderElectionConfiguration{
  43. ResourceLock: "endpoints",
  44. LeaderElect: true,
  45. LeaseDuration: metav1.Duration{Duration: 15 * time.Second},
  46. RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
  47. RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
  48. },
  49. Controllers: []string{"*"},
  50. },
  51. Debugging: &cmoptions.DebuggingOptions{
  52. DebuggingConfiguration: &componentbaseconfig.DebuggingConfiguration{
  53. EnableContentionProfiling: false,
  54. },
  55. },
  56. },
  57. KubeCloudShared: &cmoptions.KubeCloudSharedOptions{
  58. KubeCloudSharedConfiguration: &kubectrlmgrconfig.KubeCloudSharedConfiguration{
  59. RouteReconciliationPeriod: metav1.Duration{Duration: 10 * time.Second},
  60. NodeMonitorPeriod: metav1.Duration{Duration: 5 * time.Second},
  61. ClusterName: "kubernetes",
  62. ClusterCIDR: "",
  63. AllocateNodeCIDRs: false,
  64. CIDRAllocatorType: "",
  65. ConfigureCloudRoutes: true,
  66. },
  67. CloudProvider: &cmoptions.CloudProviderOptions{
  68. CloudProviderConfiguration: &kubectrlmgrconfig.CloudProviderConfiguration{
  69. Name: "",
  70. CloudConfigFile: "",
  71. },
  72. },
  73. },
  74. ServiceController: &cmoptions.ServiceControllerOptions{
  75. ServiceControllerConfiguration: &serviceconfig.ServiceControllerConfiguration{
  76. ConcurrentServiceSyncs: 1,
  77. },
  78. },
  79. SecureServing: (&apiserveroptions.SecureServingOptions{
  80. BindPort: 10258,
  81. BindAddress: net.ParseIP("0.0.0.0"),
  82. ServerCert: apiserveroptions.GeneratableKeyCert{
  83. CertDirectory: "",
  84. PairName: "cloud-controller-manager",
  85. },
  86. HTTP2MaxStreamsPerConnection: 0,
  87. }).WithLoopback(),
  88. InsecureServing: (&apiserveroptions.DeprecatedInsecureServingOptions{
  89. BindAddress: net.ParseIP("0.0.0.0"),
  90. BindPort: int(0),
  91. BindNetwork: "tcp",
  92. }).WithLoopback(),
  93. Authentication: &apiserveroptions.DelegatingAuthenticationOptions{
  94. CacheTTL: 10 * time.Second,
  95. ClientCert: apiserveroptions.ClientCertAuthenticationOptions{},
  96. RequestHeader: apiserveroptions.RequestHeaderAuthenticationOptions{
  97. UsernameHeaders: []string{"x-remote-user"},
  98. GroupHeaders: []string{"x-remote-group"},
  99. ExtraHeaderPrefixes: []string{"x-remote-extra-"},
  100. },
  101. RemoteKubeConfigFileOptional: true,
  102. },
  103. Authorization: &apiserveroptions.DelegatingAuthorizationOptions{
  104. AllowCacheTTL: 10 * time.Second,
  105. DenyCacheTTL: 10 * time.Second,
  106. RemoteKubeConfigFileOptional: true,
  107. AlwaysAllowPaths: []string{"/healthz"}, // note: this does not match /healthz/ or
  108. },
  109. Kubeconfig: "",
  110. Master: "",
  111. NodeStatusUpdateFrequency: metav1.Duration{Duration: 5 * time.Minute},
  112. }
  113. if !reflect.DeepEqual(expected, s) {
  114. t.Errorf("Got different run options than expected.\nDifference detected on:\n%s", diff.ObjectReflectDiff(expected, s))
  115. }
  116. }
  117. func TestAddFlags(t *testing.T) {
  118. fs := pflag.NewFlagSet("addflagstest", pflag.ContinueOnError)
  119. s, _ := NewCloudControllerManagerOptions()
  120. for _, f := range s.Flags([]string{""}, []string{""}).FlagSets {
  121. fs.AddFlagSet(f)
  122. }
  123. args := []string{
  124. "--address=192.168.4.10",
  125. "--allocate-node-cidrs=true",
  126. "--bind-address=192.168.4.21",
  127. "--cert-dir=/a/b/c",
  128. "--cloud-config=/cloud-config",
  129. "--cloud-provider=gce",
  130. "--cluster-cidr=1.2.3.4/24",
  131. "--cluster-name=k8s",
  132. "--configure-cloud-routes=false",
  133. "--contention-profiling=true",
  134. "--controller-start-interval=2m",
  135. "--controllers=foo,bar",
  136. "--http2-max-streams-per-connection=47",
  137. "--kube-api-burst=100",
  138. "--kube-api-content-type=application/vnd.kubernetes.protobuf",
  139. "--kube-api-qps=50.0",
  140. "--kubeconfig=/kubeconfig",
  141. "--leader-elect=false",
  142. "--leader-elect-lease-duration=30s",
  143. "--leader-elect-renew-deadline=15s",
  144. "--leader-elect-resource-lock=configmap",
  145. "--leader-elect-retry-period=5s",
  146. "--master=192.168.4.20",
  147. "--min-resync-period=100m",
  148. "--node-status-update-frequency=10m",
  149. "--port=10000",
  150. "--profiling=false",
  151. "--route-reconciliation-period=30s",
  152. "--secure-port=10001",
  153. "--use-service-account-credentials=false",
  154. }
  155. fs.Parse(args)
  156. expected := &CloudControllerManagerOptions{
  157. Generic: &cmoptions.GenericControllerManagerConfigurationOptions{
  158. GenericControllerManagerConfiguration: &kubectrlmgrconfig.GenericControllerManagerConfiguration{
  159. Port: DefaultInsecureCloudControllerManagerPort, // Note: InsecureServingOptions.ApplyTo will write the flag value back into the component config
  160. Address: "0.0.0.0", // Note: InsecureServingOptions.ApplyTo will write the flag value back into the component config
  161. MinResyncPeriod: metav1.Duration{Duration: 100 * time.Minute},
  162. ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
  163. ContentType: "application/vnd.kubernetes.protobuf",
  164. QPS: 50.0,
  165. Burst: 100,
  166. },
  167. ControllerStartInterval: metav1.Duration{Duration: 2 * time.Minute},
  168. LeaderElection: componentbaseconfig.LeaderElectionConfiguration{
  169. ResourceLock: "configmap",
  170. LeaderElect: false,
  171. LeaseDuration: metav1.Duration{Duration: 30 * time.Second},
  172. RenewDeadline: metav1.Duration{Duration: 15 * time.Second},
  173. RetryPeriod: metav1.Duration{Duration: 5 * time.Second},
  174. },
  175. Controllers: []string{"foo", "bar"},
  176. },
  177. Debugging: &cmoptions.DebuggingOptions{
  178. DebuggingConfiguration: &componentbaseconfig.DebuggingConfiguration{
  179. EnableContentionProfiling: true,
  180. },
  181. },
  182. },
  183. KubeCloudShared: &cmoptions.KubeCloudSharedOptions{
  184. KubeCloudSharedConfiguration: &kubectrlmgrconfig.KubeCloudSharedConfiguration{
  185. RouteReconciliationPeriod: metav1.Duration{Duration: 30 * time.Second},
  186. NodeMonitorPeriod: metav1.Duration{Duration: 5 * time.Second},
  187. ClusterName: "k8s",
  188. ClusterCIDR: "1.2.3.4/24",
  189. AllocateNodeCIDRs: true,
  190. CIDRAllocatorType: "RangeAllocator",
  191. ConfigureCloudRoutes: false,
  192. },
  193. CloudProvider: &cmoptions.CloudProviderOptions{
  194. CloudProviderConfiguration: &kubectrlmgrconfig.CloudProviderConfiguration{
  195. Name: "gce",
  196. CloudConfigFile: "/cloud-config",
  197. },
  198. },
  199. },
  200. ServiceController: &cmoptions.ServiceControllerOptions{
  201. ServiceControllerConfiguration: &serviceconfig.ServiceControllerConfiguration{
  202. ConcurrentServiceSyncs: 1,
  203. },
  204. },
  205. SecureServing: (&apiserveroptions.SecureServingOptions{
  206. BindPort: 10001,
  207. BindAddress: net.ParseIP("192.168.4.21"),
  208. ServerCert: apiserveroptions.GeneratableKeyCert{
  209. CertDirectory: "/a/b/c",
  210. PairName: "cloud-controller-manager",
  211. },
  212. HTTP2MaxStreamsPerConnection: 47,
  213. }).WithLoopback(),
  214. InsecureServing: (&apiserveroptions.DeprecatedInsecureServingOptions{
  215. BindAddress: net.ParseIP("192.168.4.10"),
  216. BindPort: int(10000),
  217. BindNetwork: "tcp",
  218. }).WithLoopback(),
  219. Authentication: &apiserveroptions.DelegatingAuthenticationOptions{
  220. CacheTTL: 10 * time.Second,
  221. ClientCert: apiserveroptions.ClientCertAuthenticationOptions{},
  222. RequestHeader: apiserveroptions.RequestHeaderAuthenticationOptions{
  223. UsernameHeaders: []string{"x-remote-user"},
  224. GroupHeaders: []string{"x-remote-group"},
  225. ExtraHeaderPrefixes: []string{"x-remote-extra-"},
  226. },
  227. RemoteKubeConfigFileOptional: true,
  228. },
  229. Authorization: &apiserveroptions.DelegatingAuthorizationOptions{
  230. AllowCacheTTL: 10 * time.Second,
  231. DenyCacheTTL: 10 * time.Second,
  232. RemoteKubeConfigFileOptional: true,
  233. AlwaysAllowPaths: []string{"/healthz"}, // note: this does not match /healthz/ or
  234. },
  235. Kubeconfig: "/kubeconfig",
  236. Master: "192.168.4.20",
  237. NodeStatusUpdateFrequency: metav1.Duration{Duration: 10 * time.Minute},
  238. }
  239. if !reflect.DeepEqual(expected, s) {
  240. t.Errorf("Got different run options than expected.\nDifference detected on:\n%s", diff.ObjectReflectDiff(expected, s))
  241. }
  242. }