options_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. "sort"
  18. "testing"
  19. "time"
  20. "github.com/spf13/pflag"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/util/diff"
  23. apiserveroptions "k8s.io/apiserver/pkg/server/options"
  24. componentbaseconfig "k8s.io/component-base/config"
  25. cmoptions "k8s.io/kubernetes/cmd/controller-manager/app/options"
  26. kubectrlmgrconfig "k8s.io/kubernetes/pkg/controller/apis/config"
  27. csrsigningconfig "k8s.io/kubernetes/pkg/controller/certificates/signer/config"
  28. daemonconfig "k8s.io/kubernetes/pkg/controller/daemon/config"
  29. deploymentconfig "k8s.io/kubernetes/pkg/controller/deployment/config"
  30. endpointconfig "k8s.io/kubernetes/pkg/controller/endpoint/config"
  31. garbagecollectorconfig "k8s.io/kubernetes/pkg/controller/garbagecollector/config"
  32. jobconfig "k8s.io/kubernetes/pkg/controller/job/config"
  33. namespaceconfig "k8s.io/kubernetes/pkg/controller/namespace/config"
  34. nodeipamconfig "k8s.io/kubernetes/pkg/controller/nodeipam/config"
  35. nodelifecycleconfig "k8s.io/kubernetes/pkg/controller/nodelifecycle/config"
  36. poautosclerconfig "k8s.io/kubernetes/pkg/controller/podautoscaler/config"
  37. podgcconfig "k8s.io/kubernetes/pkg/controller/podgc/config"
  38. replicasetconfig "k8s.io/kubernetes/pkg/controller/replicaset/config"
  39. replicationconfig "k8s.io/kubernetes/pkg/controller/replication/config"
  40. resourcequotaconfig "k8s.io/kubernetes/pkg/controller/resourcequota/config"
  41. serviceconfig "k8s.io/kubernetes/pkg/controller/service/config"
  42. serviceaccountconfig "k8s.io/kubernetes/pkg/controller/serviceaccount/config"
  43. ttlafterfinishedconfig "k8s.io/kubernetes/pkg/controller/ttlafterfinished/config"
  44. attachdetachconfig "k8s.io/kubernetes/pkg/controller/volume/attachdetach/config"
  45. persistentvolumeconfig "k8s.io/kubernetes/pkg/controller/volume/persistentvolume/config"
  46. )
  47. func TestAddFlags(t *testing.T) {
  48. fs := pflag.NewFlagSet("addflagstest", pflag.ContinueOnError)
  49. s, _ := NewKubeControllerManagerOptions()
  50. for _, f := range s.Flags([]string{""}, []string{""}).FlagSets {
  51. fs.AddFlagSet(f)
  52. }
  53. args := []string{
  54. "--address=192.168.4.10",
  55. "--allocate-node-cidrs=true",
  56. "--attach-detach-reconcile-sync-period=30s",
  57. "--cidr-allocator-type=CloudAllocator",
  58. "--cloud-config=/cloud-config",
  59. "--cloud-provider=gce",
  60. "--cluster-cidr=1.2.3.4/24",
  61. "--cluster-name=k8s",
  62. "--cluster-signing-cert-file=/cluster-signing-cert",
  63. "--cluster-signing-key-file=/cluster-signing-key",
  64. "--concurrent-deployment-syncs=10",
  65. "--concurrent-endpoint-syncs=10",
  66. "--concurrent-gc-syncs=30",
  67. "--concurrent-namespace-syncs=20",
  68. "--concurrent-replicaset-syncs=10",
  69. "--concurrent-resource-quota-syncs=10",
  70. "--concurrent-service-syncs=2",
  71. "--concurrent-serviceaccount-token-syncs=10",
  72. "--concurrent_rc_syncs=10",
  73. "--configure-cloud-routes=false",
  74. "--contention-profiling=true",
  75. "--controller-start-interval=2m",
  76. "--controllers=foo,bar",
  77. "--deployment-controller-sync-period=45s",
  78. "--disable-attach-detach-reconcile-sync=true",
  79. "--enable-dynamic-provisioning=false",
  80. "--enable-garbage-collector=false",
  81. "--enable-hostpath-provisioner=true",
  82. "--enable-taint-manager=false",
  83. "--experimental-cluster-signing-duration=10h",
  84. "--flex-volume-plugin-dir=/flex-volume-plugin",
  85. "--horizontal-pod-autoscaler-downscale-delay=2m",
  86. "--horizontal-pod-autoscaler-sync-period=45s",
  87. "--horizontal-pod-autoscaler-upscale-delay=1m",
  88. "--horizontal-pod-autoscaler-downscale-stabilization=3m",
  89. "--horizontal-pod-autoscaler-cpu-initialization-period=90s",
  90. "--horizontal-pod-autoscaler-initial-readiness-delay=50s",
  91. "--http2-max-streams-per-connection=47",
  92. "--kube-api-burst=100",
  93. "--kube-api-content-type=application/json",
  94. "--kube-api-qps=50.0",
  95. "--kubeconfig=/kubeconfig",
  96. "--large-cluster-size-threshold=100",
  97. "--leader-elect=false",
  98. "--leader-elect-lease-duration=30s",
  99. "--leader-elect-renew-deadline=15s",
  100. "--leader-elect-resource-lock=configmap",
  101. "--leader-elect-retry-period=5s",
  102. "--master=192.168.4.20",
  103. "--min-resync-period=8h",
  104. "--namespace-sync-period=10m",
  105. "--node-cidr-mask-size=48",
  106. "--node-eviction-rate=0.2",
  107. "--node-monitor-grace-period=30s",
  108. "--node-monitor-period=10s",
  109. "--node-startup-grace-period=30s",
  110. "--pod-eviction-timeout=2m",
  111. "--port=10000",
  112. "--profiling=false",
  113. "--pv-recycler-increment-timeout-nfs=45",
  114. "--pv-recycler-minimum-timeout-hostpath=45",
  115. "--pv-recycler-minimum-timeout-nfs=200",
  116. "--pv-recycler-timeout-increment-hostpath=45",
  117. "--pvclaimbinder-sync-period=30s",
  118. "--resource-quota-sync-period=10m",
  119. "--route-reconciliation-period=30s",
  120. "--secondary-node-eviction-rate=0.05",
  121. "--service-account-private-key-file=/service-account-private-key",
  122. "--terminated-pod-gc-threshold=12000",
  123. "--unhealthy-zone-threshold=0.6",
  124. "--use-service-account-credentials=true",
  125. "--cert-dir=/a/b/c",
  126. "--bind-address=192.168.4.21",
  127. "--secure-port=10001",
  128. "--concurrent-ttl-after-finished-syncs=8",
  129. }
  130. fs.Parse(args)
  131. // Sort GCIgnoredResources because it's built from a map, which means the
  132. // insertion order is random.
  133. sort.Sort(sortedGCIgnoredResources(s.GarbageCollectorController.GCIgnoredResources))
  134. expected := &KubeControllerManagerOptions{
  135. Generic: &cmoptions.GenericControllerManagerConfigurationOptions{
  136. GenericControllerManagerConfiguration: &kubectrlmgrconfig.GenericControllerManagerConfiguration{
  137. Port: 10252, // Note: InsecureServingOptions.ApplyTo will write the flag value back into the component config
  138. Address: "0.0.0.0", // Note: InsecureServingOptions.ApplyTo will write the flag value back into the component config
  139. MinResyncPeriod: metav1.Duration{Duration: 8 * time.Hour},
  140. ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
  141. ContentType: "application/json",
  142. QPS: 50.0,
  143. Burst: 100,
  144. },
  145. ControllerStartInterval: metav1.Duration{Duration: 2 * time.Minute},
  146. LeaderElection: componentbaseconfig.LeaderElectionConfiguration{
  147. ResourceLock: "configmap",
  148. LeaderElect: false,
  149. LeaseDuration: metav1.Duration{Duration: 30 * time.Second},
  150. RenewDeadline: metav1.Duration{Duration: 15 * time.Second},
  151. RetryPeriod: metav1.Duration{Duration: 5 * time.Second},
  152. },
  153. Controllers: []string{"foo", "bar"},
  154. },
  155. Debugging: &cmoptions.DebuggingOptions{
  156. DebuggingConfiguration: &componentbaseconfig.DebuggingConfiguration{
  157. EnableProfiling: false,
  158. EnableContentionProfiling: true,
  159. },
  160. },
  161. },
  162. KubeCloudShared: &cmoptions.KubeCloudSharedOptions{
  163. KubeCloudSharedConfiguration: &kubectrlmgrconfig.KubeCloudSharedConfiguration{
  164. UseServiceAccountCredentials: true,
  165. RouteReconciliationPeriod: metav1.Duration{Duration: 30 * time.Second},
  166. NodeMonitorPeriod: metav1.Duration{Duration: 10 * time.Second},
  167. ClusterName: "k8s",
  168. ClusterCIDR: "1.2.3.4/24",
  169. AllocateNodeCIDRs: true,
  170. CIDRAllocatorType: "CloudAllocator",
  171. ConfigureCloudRoutes: false,
  172. },
  173. CloudProvider: &cmoptions.CloudProviderOptions{
  174. CloudProviderConfiguration: &kubectrlmgrconfig.CloudProviderConfiguration{
  175. Name: "gce",
  176. CloudConfigFile: "/cloud-config",
  177. },
  178. },
  179. },
  180. ServiceController: &cmoptions.ServiceControllerOptions{
  181. ServiceControllerConfiguration: &serviceconfig.ServiceControllerConfiguration{
  182. ConcurrentServiceSyncs: 2,
  183. },
  184. },
  185. AttachDetachController: &AttachDetachControllerOptions{
  186. &attachdetachconfig.AttachDetachControllerConfiguration{
  187. ReconcilerSyncLoopPeriod: metav1.Duration{Duration: 30 * time.Second},
  188. DisableAttachDetachReconcilerSync: true,
  189. },
  190. },
  191. CSRSigningController: &CSRSigningControllerOptions{
  192. &csrsigningconfig.CSRSigningControllerConfiguration{
  193. ClusterSigningCertFile: "/cluster-signing-cert",
  194. ClusterSigningKeyFile: "/cluster-signing-key",
  195. ClusterSigningDuration: metav1.Duration{Duration: 10 * time.Hour},
  196. },
  197. },
  198. DaemonSetController: &DaemonSetControllerOptions{
  199. &daemonconfig.DaemonSetControllerConfiguration{
  200. ConcurrentDaemonSetSyncs: 2,
  201. },
  202. },
  203. DeploymentController: &DeploymentControllerOptions{
  204. &deploymentconfig.DeploymentControllerConfiguration{
  205. ConcurrentDeploymentSyncs: 10,
  206. DeploymentControllerSyncPeriod: metav1.Duration{Duration: 45 * time.Second},
  207. },
  208. },
  209. DeprecatedFlags: &DeprecatedControllerOptions{
  210. &kubectrlmgrconfig.DeprecatedControllerConfiguration{
  211. DeletingPodsQPS: 0.1,
  212. RegisterRetryCount: 10,
  213. },
  214. },
  215. EndpointController: &EndpointControllerOptions{
  216. &endpointconfig.EndpointControllerConfiguration{
  217. ConcurrentEndpointSyncs: 10,
  218. },
  219. },
  220. GarbageCollectorController: &GarbageCollectorControllerOptions{
  221. &garbagecollectorconfig.GarbageCollectorControllerConfiguration{
  222. ConcurrentGCSyncs: 30,
  223. GCIgnoredResources: []garbagecollectorconfig.GroupResource{
  224. {Group: "", Resource: "events"},
  225. },
  226. EnableGarbageCollector: false,
  227. },
  228. },
  229. HPAController: &HPAControllerOptions{
  230. &poautosclerconfig.HPAControllerConfiguration{
  231. HorizontalPodAutoscalerSyncPeriod: metav1.Duration{Duration: 45 * time.Second},
  232. HorizontalPodAutoscalerUpscaleForbiddenWindow: metav1.Duration{Duration: 1 * time.Minute},
  233. HorizontalPodAutoscalerDownscaleForbiddenWindow: metav1.Duration{Duration: 2 * time.Minute},
  234. HorizontalPodAutoscalerDownscaleStabilizationWindow: metav1.Duration{Duration: 3 * time.Minute},
  235. HorizontalPodAutoscalerCPUInitializationPeriod: metav1.Duration{Duration: 90 * time.Second},
  236. HorizontalPodAutoscalerInitialReadinessDelay: metav1.Duration{Duration: 50 * time.Second},
  237. HorizontalPodAutoscalerTolerance: 0.1,
  238. HorizontalPodAutoscalerUseRESTClients: true,
  239. },
  240. },
  241. JobController: &JobControllerOptions{
  242. &jobconfig.JobControllerConfiguration{
  243. ConcurrentJobSyncs: 5,
  244. },
  245. },
  246. NamespaceController: &NamespaceControllerOptions{
  247. &namespaceconfig.NamespaceControllerConfiguration{
  248. NamespaceSyncPeriod: metav1.Duration{Duration: 10 * time.Minute},
  249. ConcurrentNamespaceSyncs: 20,
  250. },
  251. },
  252. NodeIPAMController: &NodeIPAMControllerOptions{
  253. &nodeipamconfig.NodeIPAMControllerConfiguration{
  254. NodeCIDRMaskSize: 48,
  255. },
  256. },
  257. NodeLifecycleController: &NodeLifecycleControllerOptions{
  258. &nodelifecycleconfig.NodeLifecycleControllerConfiguration{
  259. EnableTaintManager: false,
  260. NodeEvictionRate: 0.2,
  261. SecondaryNodeEvictionRate: 0.05,
  262. NodeMonitorGracePeriod: metav1.Duration{Duration: 30 * time.Second},
  263. NodeStartupGracePeriod: metav1.Duration{Duration: 30 * time.Second},
  264. PodEvictionTimeout: metav1.Duration{Duration: 2 * time.Minute},
  265. LargeClusterSizeThreshold: 100,
  266. UnhealthyZoneThreshold: 0.6,
  267. },
  268. },
  269. PersistentVolumeBinderController: &PersistentVolumeBinderControllerOptions{
  270. &persistentvolumeconfig.PersistentVolumeBinderControllerConfiguration{
  271. PVClaimBinderSyncPeriod: metav1.Duration{Duration: 30 * time.Second},
  272. VolumeConfiguration: persistentvolumeconfig.VolumeConfiguration{
  273. EnableDynamicProvisioning: false,
  274. EnableHostPathProvisioning: true,
  275. FlexVolumePluginDir: "/flex-volume-plugin",
  276. PersistentVolumeRecyclerConfiguration: persistentvolumeconfig.PersistentVolumeRecyclerConfiguration{
  277. MaximumRetry: 3,
  278. MinimumTimeoutNFS: 200,
  279. IncrementTimeoutNFS: 45,
  280. MinimumTimeoutHostPath: 45,
  281. IncrementTimeoutHostPath: 45,
  282. },
  283. },
  284. },
  285. },
  286. PodGCController: &PodGCControllerOptions{
  287. &podgcconfig.PodGCControllerConfiguration{
  288. TerminatedPodGCThreshold: 12000,
  289. },
  290. },
  291. ReplicaSetController: &ReplicaSetControllerOptions{
  292. &replicasetconfig.ReplicaSetControllerConfiguration{
  293. ConcurrentRSSyncs: 10,
  294. },
  295. },
  296. ReplicationController: &ReplicationControllerOptions{
  297. &replicationconfig.ReplicationControllerConfiguration{
  298. ConcurrentRCSyncs: 10,
  299. },
  300. },
  301. ResourceQuotaController: &ResourceQuotaControllerOptions{
  302. &resourcequotaconfig.ResourceQuotaControllerConfiguration{
  303. ResourceQuotaSyncPeriod: metav1.Duration{Duration: 10 * time.Minute},
  304. ConcurrentResourceQuotaSyncs: 10,
  305. },
  306. },
  307. SAController: &SAControllerOptions{
  308. &serviceaccountconfig.SAControllerConfiguration{
  309. ServiceAccountKeyFile: "/service-account-private-key",
  310. ConcurrentSATokenSyncs: 10,
  311. },
  312. },
  313. TTLAfterFinishedController: &TTLAfterFinishedControllerOptions{
  314. &ttlafterfinishedconfig.TTLAfterFinishedControllerConfiguration{
  315. ConcurrentTTLSyncs: 8,
  316. },
  317. },
  318. SecureServing: (&apiserveroptions.SecureServingOptions{
  319. BindPort: 10001,
  320. BindAddress: net.ParseIP("192.168.4.21"),
  321. ServerCert: apiserveroptions.GeneratableKeyCert{
  322. CertDirectory: "/a/b/c",
  323. PairName: "kube-controller-manager",
  324. },
  325. HTTP2MaxStreamsPerConnection: 47,
  326. }).WithLoopback(),
  327. InsecureServing: (&apiserveroptions.DeprecatedInsecureServingOptions{
  328. BindAddress: net.ParseIP("192.168.4.10"),
  329. BindPort: int(10000),
  330. BindNetwork: "tcp",
  331. }).WithLoopback(),
  332. Authentication: &apiserveroptions.DelegatingAuthenticationOptions{
  333. CacheTTL: 10 * time.Second,
  334. ClientCert: apiserveroptions.ClientCertAuthenticationOptions{},
  335. RequestHeader: apiserveroptions.RequestHeaderAuthenticationOptions{
  336. UsernameHeaders: []string{"x-remote-user"},
  337. GroupHeaders: []string{"x-remote-group"},
  338. ExtraHeaderPrefixes: []string{"x-remote-extra-"},
  339. },
  340. RemoteKubeConfigFileOptional: true,
  341. },
  342. Authorization: &apiserveroptions.DelegatingAuthorizationOptions{
  343. AllowCacheTTL: 10 * time.Second,
  344. DenyCacheTTL: 10 * time.Second,
  345. RemoteKubeConfigFileOptional: true,
  346. AlwaysAllowPaths: []string{"/healthz"}, // note: this does not match /healthz/ or /healthz/*
  347. },
  348. Kubeconfig: "/kubeconfig",
  349. Master: "192.168.4.20",
  350. }
  351. // Sort GCIgnoredResources because it's built from a map, which means the
  352. // insertion order is random.
  353. sort.Sort(sortedGCIgnoredResources(expected.GarbageCollectorController.GCIgnoredResources))
  354. if !reflect.DeepEqual(expected, s) {
  355. t.Errorf("Got different run options than expected.\nDifference detected on:\n%s", diff.ObjectReflectDiff(expected, s))
  356. }
  357. }
  358. type sortedGCIgnoredResources []garbagecollectorconfig.GroupResource
  359. func (r sortedGCIgnoredResources) Len() int {
  360. return len(r)
  361. }
  362. func (r sortedGCIgnoredResources) Less(i, j int) bool {
  363. if r[i].Group < r[j].Group {
  364. return true
  365. } else if r[i].Group > r[j].Group {
  366. return false
  367. }
  368. return r[i].Resource < r[j].Resource
  369. }
  370. func (r sortedGCIgnoredResources) Swap(i, j int) {
  371. r[i], r[j] = r[j], r[i]
  372. }