options_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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-inflight-quota-handler=false",
  93. "--enable-logs-handler=false",
  94. "--endpoint-reconciler-type=" + string(reconcilers.LeaseEndpointReconcilerType),
  95. "--etcd-keyfile=/var/run/kubernetes/etcd.key",
  96. "--etcd-certfile=/var/run/kubernetes/etcdce.crt",
  97. "--etcd-cafile=/var/run/kubernetes/etcdca.crt",
  98. "--http2-max-streams-per-connection=42",
  99. "--kubelet-https=true",
  100. "--kubelet-read-only-port=10255",
  101. "--kubelet-timeout=5s",
  102. "--kubelet-client-certificate=/var/run/kubernetes/ceserver.crt",
  103. "--kubelet-client-key=/var/run/kubernetes/server.key",
  104. "--kubelet-certificate-authority=/var/run/kubernetes/caserver.crt",
  105. "--proxy-client-cert-file=/var/run/kubernetes/proxy.crt",
  106. "--proxy-client-key-file=/var/run/kubernetes/proxy.key",
  107. "--request-timeout=2m",
  108. "--storage-backend=etcd3",
  109. "--service-cluster-ip-range=192.168.128.0/17",
  110. }
  111. fs.Parse(args)
  112. // This is a snapshot of expected options parsed by args.
  113. expected := &ServerRunOptions{
  114. ServiceNodePortRange: kubeoptions.DefaultServiceNodePortRange,
  115. ServiceClusterIPRanges: (&net.IPNet{IP: net.ParseIP("192.168.128.0"), Mask: net.CIDRMask(17, 32)}).String(),
  116. MasterCount: 5,
  117. EndpointReconcilerType: string(reconcilers.LeaseEndpointReconcilerType),
  118. AllowPrivileged: false,
  119. GenericServerRunOptions: &apiserveroptions.ServerRunOptions{
  120. AdvertiseAddress: net.ParseIP("192.168.10.10"),
  121. CorsAllowedOriginList: []string{"10.10.10.100", "10.10.10.200"},
  122. MaxRequestsInFlight: 400,
  123. MaxMutatingRequestsInFlight: 200,
  124. RequestTimeout: time.Duration(2) * time.Minute,
  125. MinRequestTimeout: 1800,
  126. JSONPatchMaxCopyBytes: int64(3 * 1024 * 1024),
  127. MaxRequestBodyBytes: int64(3 * 1024 * 1024),
  128. },
  129. Admission: &kubeoptions.AdmissionOptions{
  130. GenericAdmission: &apiserveroptions.AdmissionOptions{
  131. RecommendedPluginOrder: s.Admission.GenericAdmission.RecommendedPluginOrder,
  132. DefaultOffPlugins: s.Admission.GenericAdmission.DefaultOffPlugins,
  133. EnablePlugins: []string{"AlwaysDeny"},
  134. ConfigFile: "/admission-control-config",
  135. Plugins: s.Admission.GenericAdmission.Plugins,
  136. Decorators: s.Admission.GenericAdmission.Decorators,
  137. },
  138. },
  139. Etcd: &apiserveroptions.EtcdOptions{
  140. StorageConfig: storagebackend.Config{
  141. Type: "etcd3",
  142. Transport: storagebackend.TransportConfig{
  143. ServerList: nil,
  144. KeyFile: "/var/run/kubernetes/etcd.key",
  145. TrustedCAFile: "/var/run/kubernetes/etcdca.crt",
  146. CertFile: "/var/run/kubernetes/etcdce.crt",
  147. },
  148. Paging: true,
  149. Prefix: "/registry",
  150. CompactionInterval: storagebackend.DefaultCompactInterval,
  151. CountMetricPollPeriod: time.Minute,
  152. },
  153. DefaultStorageMediaType: "application/vnd.kubernetes.protobuf",
  154. DeleteCollectionWorkers: 1,
  155. EnableGarbageCollection: true,
  156. EnableWatchCache: true,
  157. DefaultWatchCacheSize: 100,
  158. },
  159. SecureServing: (&apiserveroptions.SecureServingOptions{
  160. BindAddress: net.ParseIP("192.168.10.20"),
  161. BindPort: 6443,
  162. ServerCert: apiserveroptions.GeneratableKeyCert{
  163. CertDirectory: "/var/run/kubernetes",
  164. PairName: "apiserver",
  165. },
  166. HTTP2MaxStreamsPerConnection: 42,
  167. Required: true,
  168. }).WithLoopback(),
  169. InsecureServing: (&apiserveroptions.DeprecatedInsecureServingOptions{
  170. BindAddress: net.ParseIP("127.0.0.1"),
  171. BindPort: 8080,
  172. }).WithLoopback(),
  173. EventTTL: 1 * time.Hour,
  174. KubeletConfig: kubeletclient.KubeletClientConfig{
  175. Port: 10250,
  176. ReadOnlyPort: 10255,
  177. PreferredAddressTypes: []string{
  178. string(kapi.NodeHostName),
  179. string(kapi.NodeInternalDNS),
  180. string(kapi.NodeInternalIP),
  181. string(kapi.NodeExternalDNS),
  182. string(kapi.NodeExternalIP),
  183. },
  184. EnableHTTPS: true,
  185. HTTPTimeout: time.Duration(5) * time.Second,
  186. TLSClientConfig: restclient.TLSClientConfig{
  187. CertFile: "/var/run/kubernetes/ceserver.crt",
  188. KeyFile: "/var/run/kubernetes/server.key",
  189. CAFile: "/var/run/kubernetes/caserver.crt",
  190. },
  191. },
  192. Audit: &apiserveroptions.AuditOptions{
  193. LogOptions: apiserveroptions.AuditLogOptions{
  194. Path: "/var/log",
  195. MaxAge: 11,
  196. MaxBackups: 12,
  197. MaxSize: 13,
  198. Format: "json",
  199. BatchOptions: apiserveroptions.AuditBatchOptions{
  200. Mode: "blocking",
  201. BatchConfig: auditbuffered.BatchConfig{
  202. BufferSize: 46,
  203. MaxBatchSize: 47,
  204. MaxBatchWait: 48 * time.Second,
  205. ThrottleEnable: true,
  206. ThrottleQPS: 49.5,
  207. ThrottleBurst: 50,
  208. },
  209. },
  210. TruncateOptions: apiserveroptions.AuditTruncateOptions{
  211. Enabled: true,
  212. TruncateConfig: audittruncate.Config{
  213. MaxBatchSize: 45,
  214. MaxEventSize: 44,
  215. },
  216. },
  217. GroupVersionString: "audit.k8s.io/v1alpha1",
  218. },
  219. WebhookOptions: apiserveroptions.AuditWebhookOptions{
  220. ConfigFile: "/webhook-config",
  221. BatchOptions: apiserveroptions.AuditBatchOptions{
  222. Mode: "blocking",
  223. BatchConfig: auditbuffered.BatchConfig{
  224. BufferSize: 42,
  225. MaxBatchSize: 43,
  226. MaxBatchWait: 1 * time.Second,
  227. ThrottleEnable: false,
  228. ThrottleQPS: 43.5,
  229. ThrottleBurst: 44,
  230. AsyncDelegate: true,
  231. },
  232. },
  233. TruncateOptions: apiserveroptions.AuditTruncateOptions{
  234. Enabled: true,
  235. TruncateConfig: audittruncate.Config{
  236. MaxBatchSize: 43,
  237. MaxEventSize: 42,
  238. },
  239. },
  240. InitialBackoff: 2 * time.Second,
  241. GroupVersionString: "audit.k8s.io/v1alpha1",
  242. },
  243. DynamicOptions: apiserveroptions.AuditDynamicOptions{
  244. BatchConfig: auditdynamic.NewDefaultWebhookBatchConfig(),
  245. },
  246. PolicyFile: "/policy",
  247. },
  248. Features: &apiserveroptions.FeatureOptions{
  249. EnableProfiling: true,
  250. EnableContentionProfiling: true,
  251. },
  252. Authentication: &kubeoptions.BuiltInAuthenticationOptions{
  253. Anonymous: &kubeoptions.AnonymousAuthenticationOptions{
  254. Allow: false,
  255. },
  256. ClientCert: &apiserveroptions.ClientCertAuthenticationOptions{
  257. ClientCA: "/client-ca",
  258. },
  259. WebHook: &kubeoptions.WebHookAuthenticationOptions{
  260. CacheTTL: 180000000000,
  261. ConfigFile: "/token-webhook-config",
  262. Version: "v1beta1",
  263. },
  264. BootstrapToken: &kubeoptions.BootstrapTokenAuthenticationOptions{},
  265. OIDC: &kubeoptions.OIDCAuthenticationOptions{
  266. UsernameClaim: "sub",
  267. SigningAlgs: []string{"RS256"},
  268. },
  269. PasswordFile: &kubeoptions.PasswordFileAuthenticationOptions{},
  270. RequestHeader: &apiserveroptions.RequestHeaderAuthenticationOptions{},
  271. ServiceAccounts: &kubeoptions.ServiceAccountAuthenticationOptions{
  272. Lookup: true,
  273. },
  274. TokenFile: &kubeoptions.TokenFileAuthenticationOptions{},
  275. TokenSuccessCacheTTL: 10 * time.Second,
  276. TokenFailureCacheTTL: 0,
  277. },
  278. Authorization: &kubeoptions.BuiltInAuthorizationOptions{
  279. Modes: []string{"AlwaysDeny", "RBAC"},
  280. PolicyFile: "/policy",
  281. WebhookConfigFile: "/webhook-config",
  282. WebhookCacheAuthorizedTTL: 180000000000,
  283. WebhookCacheUnauthorizedTTL: 60000000000,
  284. WebhookVersion: "v1beta1",
  285. },
  286. CloudProvider: &kubeoptions.CloudProviderOptions{
  287. CloudConfigFile: "/cloud-config",
  288. CloudProvider: "azure",
  289. },
  290. APIEnablement: &apiserveroptions.APIEnablementOptions{
  291. RuntimeConfig: cliflag.ConfigurationMap{},
  292. },
  293. EgressSelector: &apiserveroptions.EgressSelectorOptions{
  294. ConfigFile: "/var/run/kubernetes/egress-selector/connectivity.yaml",
  295. },
  296. EnableLogsHandler: false,
  297. EnableAggregatorRouting: true,
  298. ProxyClientKeyFile: "/var/run/kubernetes/proxy.key",
  299. ProxyClientCertFile: "/var/run/kubernetes/proxy.crt",
  300. }
  301. if !reflect.DeepEqual(expected, s) {
  302. t.Errorf("Got different run options than expected.\nDifference detected on:\n%s", cmp.Diff(expected, s, cmpopts.IgnoreUnexported(admission.Plugins{})))
  303. }
  304. }