options_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. Copyright 2014 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. "k8s.io/apimachinery/pkg/util/diff"
  21. apiserveroptions "k8s.io/apiserver/pkg/server/options"
  22. "k8s.io/apiserver/pkg/storage/storagebackend"
  23. auditbuffered "k8s.io/apiserver/plugin/pkg/audit/buffered"
  24. auditdynamic "k8s.io/apiserver/plugin/pkg/audit/dynamic"
  25. audittruncate "k8s.io/apiserver/plugin/pkg/audit/truncate"
  26. restclient "k8s.io/client-go/rest"
  27. cliflag "k8s.io/component-base/cli/flag"
  28. kapi "k8s.io/kubernetes/pkg/apis/core"
  29. kubeoptions "k8s.io/kubernetes/pkg/kubeapiserver/options"
  30. kubeletclient "k8s.io/kubernetes/pkg/kubelet/client"
  31. "k8s.io/kubernetes/pkg/master/reconcilers"
  32. )
  33. func TestAddFlags(t *testing.T) {
  34. fs := pflag.NewFlagSet("addflagstest", pflag.ContinueOnError)
  35. s := NewServerRunOptions()
  36. for _, f := range s.Flags().FlagSets {
  37. fs.AddFlagSet(f)
  38. }
  39. args := []string{
  40. "--enable-admission-plugins=AlwaysDeny",
  41. "--admission-control-config-file=/admission-control-config",
  42. "--advertise-address=192.168.10.10",
  43. "--allow-privileged=false",
  44. "--anonymous-auth=false",
  45. "--apiserver-count=5",
  46. "--audit-log-maxage=11",
  47. "--audit-log-maxbackup=12",
  48. "--audit-log-maxsize=13",
  49. "--audit-log-path=/var/log",
  50. "--audit-log-mode=blocking",
  51. "--audit-log-batch-buffer-size=46",
  52. "--audit-log-batch-max-size=47",
  53. "--audit-log-batch-max-wait=48s",
  54. "--audit-log-batch-throttle-enable=true",
  55. "--audit-log-batch-throttle-qps=49.5",
  56. "--audit-log-batch-throttle-burst=50",
  57. "--audit-log-truncate-enabled=true",
  58. "--audit-log-truncate-max-batch-size=45",
  59. "--audit-log-truncate-max-event-size=44",
  60. "--audit-log-version=audit.k8s.io/v1alpha1",
  61. "--audit-policy-file=/policy",
  62. "--audit-webhook-config-file=/webhook-config",
  63. "--audit-webhook-mode=blocking",
  64. "--audit-webhook-batch-buffer-size=42",
  65. "--audit-webhook-batch-max-size=43",
  66. "--audit-webhook-batch-max-wait=1s",
  67. "--audit-webhook-batch-throttle-enable=false",
  68. "--audit-webhook-batch-throttle-qps=43.5",
  69. "--audit-webhook-batch-throttle-burst=44",
  70. "--audit-webhook-truncate-enabled=true",
  71. "--audit-webhook-truncate-max-batch-size=43",
  72. "--audit-webhook-truncate-max-event-size=42",
  73. "--audit-webhook-initial-backoff=2s",
  74. "--audit-webhook-version=audit.k8s.io/v1alpha1",
  75. "--authentication-token-webhook-cache-ttl=3m",
  76. "--authentication-token-webhook-config-file=/token-webhook-config",
  77. "--authorization-mode=AlwaysDeny,RBAC",
  78. "--authorization-policy-file=/policy",
  79. "--authorization-webhook-cache-authorized-ttl=3m",
  80. "--authorization-webhook-cache-unauthorized-ttl=1m",
  81. "--authorization-webhook-config-file=/webhook-config",
  82. "--bind-address=192.168.10.20",
  83. "--client-ca-file=/client-ca",
  84. "--cloud-config=/cloud-config",
  85. "--cloud-provider=azure",
  86. "--cors-allowed-origins=10.10.10.100,10.10.10.200",
  87. "--contention-profiling=true",
  88. "--enable-aggregator-routing=true",
  89. "--enable-logs-handler=false",
  90. "--endpoint-reconciler-type=" + string(reconcilers.LeaseEndpointReconcilerType),
  91. "--etcd-keyfile=/var/run/kubernetes/etcd.key",
  92. "--etcd-certfile=/var/run/kubernetes/etcdce.crt",
  93. "--etcd-cafile=/var/run/kubernetes/etcdca.crt",
  94. "--http2-max-streams-per-connection=42",
  95. "--kubelet-https=true",
  96. "--kubelet-read-only-port=10255",
  97. "--kubelet-timeout=5s",
  98. "--kubelet-client-certificate=/var/run/kubernetes/ceserver.crt",
  99. "--kubelet-client-key=/var/run/kubernetes/server.key",
  100. "--kubelet-certificate-authority=/var/run/kubernetes/caserver.crt",
  101. "--proxy-client-cert-file=/var/run/kubernetes/proxy.crt",
  102. "--proxy-client-key-file=/var/run/kubernetes/proxy.key",
  103. "--request-timeout=2m",
  104. "--storage-backend=etcd3",
  105. }
  106. fs.Parse(args)
  107. // This is a snapshot of expected options parsed by args.
  108. expected := &ServerRunOptions{
  109. ServiceNodePortRange: kubeoptions.DefaultServiceNodePortRange,
  110. ServiceClusterIPRange: kubeoptions.DefaultServiceIPCIDR,
  111. MasterCount: 5,
  112. EndpointReconcilerType: string(reconcilers.LeaseEndpointReconcilerType),
  113. AllowPrivileged: false,
  114. GenericServerRunOptions: &apiserveroptions.ServerRunOptions{
  115. AdvertiseAddress: net.ParseIP("192.168.10.10"),
  116. CorsAllowedOriginList: []string{"10.10.10.100", "10.10.10.200"},
  117. MaxRequestsInFlight: 400,
  118. MaxMutatingRequestsInFlight: 200,
  119. RequestTimeout: time.Duration(2) * time.Minute,
  120. MinRequestTimeout: 1800,
  121. JSONPatchMaxCopyBytes: int64(100 * 1024 * 1024),
  122. MaxRequestBodyBytes: int64(100 * 1024 * 1024),
  123. },
  124. Admission: &kubeoptions.AdmissionOptions{
  125. GenericAdmission: &apiserveroptions.AdmissionOptions{
  126. RecommendedPluginOrder: s.Admission.GenericAdmission.RecommendedPluginOrder,
  127. DefaultOffPlugins: s.Admission.GenericAdmission.DefaultOffPlugins,
  128. EnablePlugins: []string{"AlwaysDeny"},
  129. ConfigFile: "/admission-control-config",
  130. Plugins: s.Admission.GenericAdmission.Plugins,
  131. Decorators: s.Admission.GenericAdmission.Decorators,
  132. },
  133. },
  134. Etcd: &apiserveroptions.EtcdOptions{
  135. StorageConfig: storagebackend.Config{
  136. Type: "etcd3",
  137. Transport: storagebackend.TransportConfig{
  138. ServerList: nil,
  139. KeyFile: "/var/run/kubernetes/etcd.key",
  140. CAFile: "/var/run/kubernetes/etcdca.crt",
  141. CertFile: "/var/run/kubernetes/etcdce.crt",
  142. },
  143. Paging: true,
  144. Prefix: "/registry",
  145. CompactionInterval: storagebackend.DefaultCompactInterval,
  146. CountMetricPollPeriod: time.Minute,
  147. },
  148. DefaultStorageMediaType: "application/vnd.kubernetes.protobuf",
  149. DeleteCollectionWorkers: 1,
  150. EnableGarbageCollection: true,
  151. EnableWatchCache: true,
  152. DefaultWatchCacheSize: 100,
  153. },
  154. SecureServing: (&apiserveroptions.SecureServingOptions{
  155. BindAddress: net.ParseIP("192.168.10.20"),
  156. BindPort: 6443,
  157. ServerCert: apiserveroptions.GeneratableKeyCert{
  158. CertDirectory: "/var/run/kubernetes",
  159. PairName: "apiserver",
  160. },
  161. HTTP2MaxStreamsPerConnection: 42,
  162. Required: true,
  163. }).WithLoopback(),
  164. InsecureServing: (&apiserveroptions.DeprecatedInsecureServingOptions{
  165. BindAddress: net.ParseIP("127.0.0.1"),
  166. BindPort: 8080,
  167. }).WithLoopback(),
  168. EventTTL: 1 * time.Hour,
  169. KubeletConfig: kubeletclient.KubeletClientConfig{
  170. Port: 10250,
  171. ReadOnlyPort: 10255,
  172. PreferredAddressTypes: []string{
  173. string(kapi.NodeHostName),
  174. string(kapi.NodeInternalDNS),
  175. string(kapi.NodeInternalIP),
  176. string(kapi.NodeExternalDNS),
  177. string(kapi.NodeExternalIP),
  178. },
  179. EnableHttps: true,
  180. HTTPTimeout: time.Duration(5) * time.Second,
  181. TLSClientConfig: restclient.TLSClientConfig{
  182. CertFile: "/var/run/kubernetes/ceserver.crt",
  183. KeyFile: "/var/run/kubernetes/server.key",
  184. CAFile: "/var/run/kubernetes/caserver.crt",
  185. },
  186. },
  187. Audit: &apiserveroptions.AuditOptions{
  188. LogOptions: apiserveroptions.AuditLogOptions{
  189. Path: "/var/log",
  190. MaxAge: 11,
  191. MaxBackups: 12,
  192. MaxSize: 13,
  193. Format: "json",
  194. BatchOptions: apiserveroptions.AuditBatchOptions{
  195. Mode: "blocking",
  196. BatchConfig: auditbuffered.BatchConfig{
  197. BufferSize: 46,
  198. MaxBatchSize: 47,
  199. MaxBatchWait: 48 * time.Second,
  200. ThrottleEnable: true,
  201. ThrottleQPS: 49.5,
  202. ThrottleBurst: 50,
  203. },
  204. },
  205. TruncateOptions: apiserveroptions.AuditTruncateOptions{
  206. Enabled: true,
  207. TruncateConfig: audittruncate.Config{
  208. MaxBatchSize: 45,
  209. MaxEventSize: 44,
  210. },
  211. },
  212. GroupVersionString: "audit.k8s.io/v1alpha1",
  213. },
  214. WebhookOptions: apiserveroptions.AuditWebhookOptions{
  215. ConfigFile: "/webhook-config",
  216. BatchOptions: apiserveroptions.AuditBatchOptions{
  217. Mode: "blocking",
  218. BatchConfig: auditbuffered.BatchConfig{
  219. BufferSize: 42,
  220. MaxBatchSize: 43,
  221. MaxBatchWait: 1 * time.Second,
  222. ThrottleEnable: false,
  223. ThrottleQPS: 43.5,
  224. ThrottleBurst: 44,
  225. AsyncDelegate: true,
  226. },
  227. },
  228. TruncateOptions: apiserveroptions.AuditTruncateOptions{
  229. Enabled: true,
  230. TruncateConfig: audittruncate.Config{
  231. MaxBatchSize: 43,
  232. MaxEventSize: 42,
  233. },
  234. },
  235. InitialBackoff: 2 * time.Second,
  236. GroupVersionString: "audit.k8s.io/v1alpha1",
  237. },
  238. DynamicOptions: apiserveroptions.AuditDynamicOptions{
  239. BatchConfig: auditdynamic.NewDefaultWebhookBatchConfig(),
  240. },
  241. PolicyFile: "/policy",
  242. },
  243. Features: &apiserveroptions.FeatureOptions{
  244. EnableProfiling: true,
  245. EnableContentionProfiling: true,
  246. },
  247. Authentication: &kubeoptions.BuiltInAuthenticationOptions{
  248. Anonymous: &kubeoptions.AnonymousAuthenticationOptions{
  249. Allow: false,
  250. },
  251. ClientCert: &apiserveroptions.ClientCertAuthenticationOptions{
  252. ClientCA: "/client-ca",
  253. },
  254. WebHook: &kubeoptions.WebHookAuthenticationOptions{
  255. CacheTTL: 180000000000,
  256. ConfigFile: "/token-webhook-config",
  257. },
  258. BootstrapToken: &kubeoptions.BootstrapTokenAuthenticationOptions{},
  259. OIDC: &kubeoptions.OIDCAuthenticationOptions{
  260. UsernameClaim: "sub",
  261. SigningAlgs: []string{"RS256"},
  262. },
  263. PasswordFile: &kubeoptions.PasswordFileAuthenticationOptions{},
  264. RequestHeader: &apiserveroptions.RequestHeaderAuthenticationOptions{},
  265. ServiceAccounts: &kubeoptions.ServiceAccountAuthenticationOptions{
  266. Lookup: true,
  267. },
  268. TokenFile: &kubeoptions.TokenFileAuthenticationOptions{},
  269. TokenSuccessCacheTTL: 10 * time.Second,
  270. TokenFailureCacheTTL: 0,
  271. },
  272. Authorization: &kubeoptions.BuiltInAuthorizationOptions{
  273. Modes: []string{"AlwaysDeny", "RBAC"},
  274. PolicyFile: "/policy",
  275. WebhookConfigFile: "/webhook-config",
  276. WebhookCacheAuthorizedTTL: 180000000000,
  277. WebhookCacheUnauthorizedTTL: 60000000000,
  278. },
  279. CloudProvider: &kubeoptions.CloudProviderOptions{
  280. CloudConfigFile: "/cloud-config",
  281. CloudProvider: "azure",
  282. },
  283. APIEnablement: &apiserveroptions.APIEnablementOptions{
  284. RuntimeConfig: cliflag.ConfigurationMap{},
  285. },
  286. EnableLogsHandler: false,
  287. EnableAggregatorRouting: true,
  288. ProxyClientKeyFile: "/var/run/kubernetes/proxy.key",
  289. ProxyClientCertFile: "/var/run/kubernetes/proxy.crt",
  290. }
  291. if !reflect.DeepEqual(expected, s) {
  292. t.Errorf("Got different run options than expected.\nDifference detected on:\n%s", diff.ObjectReflectDiff(expected, s))
  293. }
  294. }