options.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 contains flags and options for initializing an apiserver
  14. package options
  15. import (
  16. "net"
  17. "strings"
  18. "time"
  19. utilnet "k8s.io/apimachinery/pkg/util/net"
  20. genericoptions "k8s.io/apiserver/pkg/server/options"
  21. "k8s.io/apiserver/pkg/storage/storagebackend"
  22. cliflag "k8s.io/component-base/cli/flag"
  23. api "k8s.io/kubernetes/pkg/apis/core"
  24. _ "k8s.io/kubernetes/pkg/features" // add the kubernetes feature gates
  25. kubeoptions "k8s.io/kubernetes/pkg/kubeapiserver/options"
  26. kubeletclient "k8s.io/kubernetes/pkg/kubelet/client"
  27. "k8s.io/kubernetes/pkg/master/ports"
  28. "k8s.io/kubernetes/pkg/master/reconcilers"
  29. "k8s.io/kubernetes/pkg/serviceaccount"
  30. )
  31. // ServerRunOptions runs a kubernetes api server.
  32. type ServerRunOptions struct {
  33. GenericServerRunOptions *genericoptions.ServerRunOptions
  34. Etcd *genericoptions.EtcdOptions
  35. SecureServing *genericoptions.SecureServingOptionsWithLoopback
  36. InsecureServing *genericoptions.DeprecatedInsecureServingOptionsWithLoopback
  37. Audit *genericoptions.AuditOptions
  38. Features *genericoptions.FeatureOptions
  39. Admission *kubeoptions.AdmissionOptions
  40. Authentication *kubeoptions.BuiltInAuthenticationOptions
  41. Authorization *kubeoptions.BuiltInAuthorizationOptions
  42. CloudProvider *kubeoptions.CloudProviderOptions
  43. APIEnablement *genericoptions.APIEnablementOptions
  44. EgressSelector *genericoptions.EgressSelectorOptions
  45. AllowPrivileged bool
  46. EnableLogsHandler bool
  47. EventTTL time.Duration
  48. KubeletConfig kubeletclient.KubeletClientConfig
  49. KubernetesServiceNodePort int
  50. MaxConnectionBytesPerSec int64
  51. // ServiceClusterIPRange is mapped to input provided by user
  52. ServiceClusterIPRanges string
  53. //PrimaryServiceClusterIPRange and SecondaryServiceClusterIPRange are the results
  54. // of parsing ServiceClusterIPRange into actual values
  55. PrimaryServiceClusterIPRange net.IPNet
  56. SecondaryServiceClusterIPRange net.IPNet
  57. ServiceNodePortRange utilnet.PortRange
  58. SSHKeyfile string
  59. SSHUser string
  60. ProxyClientCertFile string
  61. ProxyClientKeyFile string
  62. EnableAggregatorRouting bool
  63. MasterCount int
  64. EndpointReconcilerType string
  65. ServiceAccountSigningKeyFile string
  66. ServiceAccountIssuer serviceaccount.TokenGenerator
  67. ServiceAccountTokenMaxExpiration time.Duration
  68. ShowHiddenMetricsForVersion string
  69. }
  70. // NewServerRunOptions creates a new ServerRunOptions object with default parameters
  71. func NewServerRunOptions() *ServerRunOptions {
  72. s := ServerRunOptions{
  73. GenericServerRunOptions: genericoptions.NewServerRunOptions(),
  74. Etcd: genericoptions.NewEtcdOptions(storagebackend.NewDefaultConfig(kubeoptions.DefaultEtcdPathPrefix, nil)),
  75. SecureServing: kubeoptions.NewSecureServingOptions(),
  76. InsecureServing: kubeoptions.NewInsecureServingOptions(),
  77. Audit: genericoptions.NewAuditOptions(),
  78. Features: genericoptions.NewFeatureOptions(),
  79. Admission: kubeoptions.NewAdmissionOptions(),
  80. Authentication: kubeoptions.NewBuiltInAuthenticationOptions().WithAll(),
  81. Authorization: kubeoptions.NewBuiltInAuthorizationOptions(),
  82. CloudProvider: kubeoptions.NewCloudProviderOptions(),
  83. APIEnablement: genericoptions.NewAPIEnablementOptions(),
  84. EgressSelector: genericoptions.NewEgressSelectorOptions(),
  85. EnableLogsHandler: true,
  86. EventTTL: 1 * time.Hour,
  87. MasterCount: 1,
  88. EndpointReconcilerType: string(reconcilers.LeaseEndpointReconcilerType),
  89. KubeletConfig: kubeletclient.KubeletClientConfig{
  90. Port: ports.KubeletPort,
  91. ReadOnlyPort: ports.KubeletReadOnlyPort,
  92. PreferredAddressTypes: []string{
  93. // --override-hostname
  94. string(api.NodeHostName),
  95. // internal, preferring DNS if reported
  96. string(api.NodeInternalDNS),
  97. string(api.NodeInternalIP),
  98. // external, preferring DNS if reported
  99. string(api.NodeExternalDNS),
  100. string(api.NodeExternalIP),
  101. },
  102. EnableHTTPS: true,
  103. HTTPTimeout: time.Duration(5) * time.Second,
  104. },
  105. ServiceNodePortRange: kubeoptions.DefaultServiceNodePortRange,
  106. }
  107. // Overwrite the default for storage data format.
  108. s.Etcd.DefaultStorageMediaType = "application/vnd.kubernetes.protobuf"
  109. return &s
  110. }
  111. // Flags returns flags for a specific APIServer by section name
  112. func (s *ServerRunOptions) Flags() (fss cliflag.NamedFlagSets) {
  113. // Add the generic flags.
  114. s.GenericServerRunOptions.AddUniversalFlags(fss.FlagSet("generic"))
  115. s.Etcd.AddFlags(fss.FlagSet("etcd"))
  116. s.SecureServing.AddFlags(fss.FlagSet("secure serving"))
  117. s.InsecureServing.AddFlags(fss.FlagSet("insecure serving"))
  118. s.InsecureServing.AddUnqualifiedFlags(fss.FlagSet("insecure serving")) // TODO: remove it until kops stops using `--address`
  119. s.Audit.AddFlags(fss.FlagSet("auditing"))
  120. s.Features.AddFlags(fss.FlagSet("features"))
  121. s.Authentication.AddFlags(fss.FlagSet("authentication"))
  122. s.Authorization.AddFlags(fss.FlagSet("authorization"))
  123. s.CloudProvider.AddFlags(fss.FlagSet("cloud provider"))
  124. s.APIEnablement.AddFlags(fss.FlagSet("API enablement"))
  125. s.EgressSelector.AddFlags(fss.FlagSet("egress selector"))
  126. s.Admission.AddFlags(fss.FlagSet("admission"))
  127. // TODO(RainbowMango): move it to genericoptions before next flag comes.
  128. mfs := fss.FlagSet("metrics")
  129. mfs.StringVar(&s.ShowHiddenMetricsForVersion, "show-hidden-metrics-for-version", s.ShowHiddenMetricsForVersion,
  130. "The previous version for which you want to show hidden metrics. "+
  131. "Only the previous minor version is meaningful, other values will not be allowed. "+
  132. "The format is <major>.<minor>, e.g.: '1.16'. "+
  133. "The purpose of this format is make sure you have the opportunity to notice if the next release hides additional metrics, "+
  134. "rather than being surprised when they are permanently removed in the release after that.")
  135. // Note: the weird ""+ in below lines seems to be the only way to get gofmt to
  136. // arrange these text blocks sensibly. Grrr.
  137. fs := fss.FlagSet("misc")
  138. fs.DurationVar(&s.EventTTL, "event-ttl", s.EventTTL,
  139. "Amount of time to retain events.")
  140. fs.BoolVar(&s.AllowPrivileged, "allow-privileged", s.AllowPrivileged,
  141. "If true, allow privileged containers. [default=false]")
  142. fs.BoolVar(&s.EnableLogsHandler, "enable-logs-handler", s.EnableLogsHandler,
  143. "If true, install a /logs handler for the apiserver logs.")
  144. fs.MarkDeprecated("enable-logs-handler", "This flag will be removed in v1.19")
  145. // Deprecated in release 1.9
  146. fs.StringVar(&s.SSHUser, "ssh-user", s.SSHUser,
  147. "If non-empty, use secure SSH proxy to the nodes, using this user name")
  148. fs.MarkDeprecated("ssh-user", "This flag will be removed in a future version.")
  149. // Deprecated in release 1.9
  150. fs.StringVar(&s.SSHKeyfile, "ssh-keyfile", s.SSHKeyfile,
  151. "If non-empty, use secure SSH proxy to the nodes, using this user keyfile")
  152. fs.MarkDeprecated("ssh-keyfile", "This flag will be removed in a future version.")
  153. fs.Int64Var(&s.MaxConnectionBytesPerSec, "max-connection-bytes-per-sec", s.MaxConnectionBytesPerSec, ""+
  154. "If non-zero, throttle each user connection to this number of bytes/sec. "+
  155. "Currently only applies to long-running requests.")
  156. fs.IntVar(&s.MasterCount, "apiserver-count", s.MasterCount,
  157. "The number of apiservers running in the cluster, must be a positive number. (In use when --endpoint-reconciler-type=master-count is enabled.)")
  158. fs.StringVar(&s.EndpointReconcilerType, "endpoint-reconciler-type", string(s.EndpointReconcilerType),
  159. "Use an endpoint reconciler ("+strings.Join(reconcilers.AllTypes.Names(), ", ")+")")
  160. // See #14282 for details on how to test/try this option out.
  161. // TODO: remove this comment once this option is tested in CI.
  162. fs.IntVar(&s.KubernetesServiceNodePort, "kubernetes-service-node-port", s.KubernetesServiceNodePort, ""+
  163. "If non-zero, the Kubernetes master service (which apiserver creates/maintains) will be "+
  164. "of type NodePort, using this as the value of the port. If zero, the Kubernetes master "+
  165. "service will be of type ClusterIP.")
  166. // TODO (khenidak) change documentation as we move IPv6DualStack feature from ALPHA to BETA
  167. fs.StringVar(&s.ServiceClusterIPRanges, "service-cluster-ip-range", s.ServiceClusterIPRanges, ""+
  168. "A CIDR notation IP range from which to assign service cluster IPs. This must not "+
  169. "overlap with any IP ranges assigned to nodes for pods.")
  170. fs.Var(&s.ServiceNodePortRange, "service-node-port-range", ""+
  171. "A port range to reserve for services with NodePort visibility. "+
  172. "Example: '30000-32767'. Inclusive at both ends of the range.")
  173. // Kubelet related flags:
  174. fs.BoolVar(&s.KubeletConfig.EnableHTTPS, "kubelet-https", s.KubeletConfig.EnableHTTPS,
  175. "Use https for kubelet connections.")
  176. fs.StringSliceVar(&s.KubeletConfig.PreferredAddressTypes, "kubelet-preferred-address-types", s.KubeletConfig.PreferredAddressTypes,
  177. "List of the preferred NodeAddressTypes to use for kubelet connections.")
  178. fs.UintVar(&s.KubeletConfig.Port, "kubelet-port", s.KubeletConfig.Port,
  179. "DEPRECATED: kubelet port.")
  180. fs.MarkDeprecated("kubelet-port", "kubelet-port is deprecated and will be removed.")
  181. fs.UintVar(&s.KubeletConfig.ReadOnlyPort, "kubelet-read-only-port", s.KubeletConfig.ReadOnlyPort,
  182. "DEPRECATED: kubelet read only port.")
  183. fs.MarkDeprecated("kubelet-read-only-port", "kubelet-read-only-port is deprecated and will be removed.")
  184. fs.DurationVar(&s.KubeletConfig.HTTPTimeout, "kubelet-timeout", s.KubeletConfig.HTTPTimeout,
  185. "Timeout for kubelet operations.")
  186. fs.StringVar(&s.KubeletConfig.CertFile, "kubelet-client-certificate", s.KubeletConfig.CertFile,
  187. "Path to a client cert file for TLS.")
  188. fs.StringVar(&s.KubeletConfig.KeyFile, "kubelet-client-key", s.KubeletConfig.KeyFile,
  189. "Path to a client key file for TLS.")
  190. fs.StringVar(&s.KubeletConfig.CAFile, "kubelet-certificate-authority", s.KubeletConfig.CAFile,
  191. "Path to a cert file for the certificate authority.")
  192. fs.StringVar(&s.ProxyClientCertFile, "proxy-client-cert-file", s.ProxyClientCertFile, ""+
  193. "Client certificate used to prove the identity of the aggregator or kube-apiserver "+
  194. "when it must call out during a request. This includes proxying requests to a user "+
  195. "api-server and calling out to webhook admission plugins. It is expected that this "+
  196. "cert includes a signature from the CA in the --requestheader-client-ca-file flag. "+
  197. "That CA is published in the 'extension-apiserver-authentication' configmap in "+
  198. "the kube-system namespace. Components receiving calls from kube-aggregator should "+
  199. "use that CA to perform their half of the mutual TLS verification.")
  200. fs.StringVar(&s.ProxyClientKeyFile, "proxy-client-key-file", s.ProxyClientKeyFile, ""+
  201. "Private key for the client certificate used to prove the identity of the aggregator or kube-apiserver "+
  202. "when it must call out during a request. This includes proxying requests to a user "+
  203. "api-server and calling out to webhook admission plugins.")
  204. fs.BoolVar(&s.EnableAggregatorRouting, "enable-aggregator-routing", s.EnableAggregatorRouting,
  205. "Turns on aggregator routing requests to endpoints IP rather than cluster IP.")
  206. fs.StringVar(&s.ServiceAccountSigningKeyFile, "service-account-signing-key-file", s.ServiceAccountSigningKeyFile, ""+
  207. "Path to the file that contains the current private key of the service account token issuer. The issuer will sign issued ID tokens with this private key. (Requires the 'TokenRequest' feature gate.)")
  208. return fss
  209. }