options_test.go 11 KB

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