k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/cmd/kube-apiserver/app/options/options.go (about)

     1  /*
     2  Copyright 2014 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Package options contains flags and options for initializing an apiserver
    18  package options
    19  
    20  import (
    21  	"net"
    22  	"strings"
    23  	"time"
    24  
    25  	v1 "k8s.io/api/core/v1"
    26  	utilnet "k8s.io/apimachinery/pkg/util/net"
    27  	cliflag "k8s.io/component-base/cli/flag"
    28  
    29  	api "k8s.io/kubernetes/pkg/apis/core"
    30  	"k8s.io/kubernetes/pkg/cluster/ports"
    31  	controlplaneapiserver "k8s.io/kubernetes/pkg/controlplane/apiserver/options"
    32  	"k8s.io/kubernetes/pkg/controlplane/reconcilers"
    33  	_ "k8s.io/kubernetes/pkg/features" // add the kubernetes feature gates
    34  	kubeoptions "k8s.io/kubernetes/pkg/kubeapiserver/options"
    35  	kubeletclient "k8s.io/kubernetes/pkg/kubelet/client"
    36  )
    37  
    38  // ServerRunOptions runs a kubernetes api server.
    39  type ServerRunOptions struct {
    40  	*controlplaneapiserver.Options // embedded to avoid noise in existing consumers
    41  	CloudProvider                  *kubeoptions.CloudProviderOptions
    42  
    43  	Extra
    44  }
    45  
    46  type Extra struct {
    47  	AllowPrivileged           bool
    48  	KubeletConfig             kubeletclient.KubeletClientConfig
    49  	KubernetesServiceNodePort int
    50  	// ServiceClusterIPRange is mapped to input provided by user
    51  	ServiceClusterIPRanges string
    52  	// PrimaryServiceClusterIPRange and SecondaryServiceClusterIPRange are the results
    53  	// of parsing ServiceClusterIPRange into actual values
    54  	PrimaryServiceClusterIPRange   net.IPNet
    55  	SecondaryServiceClusterIPRange net.IPNet
    56  	// APIServerServiceIP is the first valid IP from PrimaryServiceClusterIPRange
    57  	APIServerServiceIP net.IP
    58  
    59  	ServiceNodePortRange utilnet.PortRange
    60  
    61  	EndpointReconcilerType string
    62  
    63  	MasterCount int
    64  }
    65  
    66  // NewServerRunOptions creates a new ServerRunOptions object with default parameters
    67  func NewServerRunOptions() *ServerRunOptions {
    68  	s := ServerRunOptions{
    69  		Options:       controlplaneapiserver.NewOptions(),
    70  		CloudProvider: kubeoptions.NewCloudProviderOptions(),
    71  
    72  		Extra: Extra{
    73  			EndpointReconcilerType: string(reconcilers.LeaseEndpointReconcilerType),
    74  			KubeletConfig: kubeletclient.KubeletClientConfig{
    75  				Port:         ports.KubeletPort,
    76  				ReadOnlyPort: ports.KubeletReadOnlyPort,
    77  				PreferredAddressTypes: []string{
    78  					// --override-hostname
    79  					string(api.NodeHostName),
    80  
    81  					// internal, preferring DNS if reported
    82  					string(api.NodeInternalDNS),
    83  					string(api.NodeInternalIP),
    84  
    85  					// external, preferring DNS if reported
    86  					string(api.NodeExternalDNS),
    87  					string(api.NodeExternalIP),
    88  				},
    89  				HTTPTimeout: time.Duration(5) * time.Second,
    90  			},
    91  			ServiceNodePortRange: kubeoptions.DefaultServiceNodePortRange,
    92  			MasterCount:          1,
    93  		},
    94  	}
    95  
    96  	s.Options.SystemNamespaces = append(s.Options.SystemNamespaces, v1.NamespaceNodeLease)
    97  
    98  	return &s
    99  }
   100  
   101  // Flags returns flags for a specific APIServer by section name
   102  func (s *ServerRunOptions) Flags() (fss cliflag.NamedFlagSets) {
   103  	s.Options.AddFlags(&fss)
   104  	s.CloudProvider.AddFlags(fss.FlagSet("cloud provider"))
   105  
   106  	// Note: the weird ""+ in below lines seems to be the only way to get gofmt to
   107  	// arrange these text blocks sensibly. Grrr.
   108  	fs := fss.FlagSet("misc")
   109  
   110  	fs.BoolVar(&s.AllowPrivileged, "allow-privileged", s.AllowPrivileged,
   111  		"If true, allow privileged containers. [default=false]")
   112  
   113  	fs.StringVar(&s.EndpointReconcilerType, "endpoint-reconciler-type", s.EndpointReconcilerType,
   114  		"Use an endpoint reconciler ("+strings.Join(reconcilers.AllTypes.Names(), ", ")+") master-count is deprecated, and will be removed in a future version.")
   115  
   116  	// See #14282 for details on how to test/try this option out.
   117  	// TODO: remove this comment once this option is tested in CI.
   118  	fs.IntVar(&s.KubernetesServiceNodePort, "kubernetes-service-node-port", s.KubernetesServiceNodePort, ""+
   119  		"If non-zero, the Kubernetes master service (which apiserver creates/maintains) will be "+
   120  		"of type NodePort, using this as the value of the port. If zero, the Kubernetes master "+
   121  		"service will be of type ClusterIP.")
   122  
   123  	fs.StringVar(&s.ServiceClusterIPRanges, "service-cluster-ip-range", s.ServiceClusterIPRanges, ""+
   124  		"A CIDR notation IP range from which to assign service cluster IPs. This must not "+
   125  		"overlap with any IP ranges assigned to nodes or pods. Max of two dual-stack CIDRs is allowed.")
   126  
   127  	fs.Var(&s.ServiceNodePortRange, "service-node-port-range", ""+
   128  		"A port range to reserve for services with NodePort visibility.  This must not overlap with the ephemeral port range on nodes.  "+
   129  		"Example: '30000-32767'. Inclusive at both ends of the range.")
   130  
   131  	// Kubelet related flags:
   132  	fs.StringSliceVar(&s.KubeletConfig.PreferredAddressTypes, "kubelet-preferred-address-types", s.KubeletConfig.PreferredAddressTypes,
   133  		"List of the preferred NodeAddressTypes to use for kubelet connections.")
   134  
   135  	fs.UintVar(&s.KubeletConfig.Port, "kubelet-port", s.KubeletConfig.Port,
   136  		"DEPRECATED: kubelet port.")
   137  	fs.MarkDeprecated("kubelet-port", "kubelet-port is deprecated and will be removed.")
   138  
   139  	fs.UintVar(&s.KubeletConfig.ReadOnlyPort, "kubelet-read-only-port", s.KubeletConfig.ReadOnlyPort,
   140  		"DEPRECATED: kubelet read only port.")
   141  	fs.MarkDeprecated("kubelet-read-only-port", "kubelet-read-only-port is deprecated and will be removed.")
   142  
   143  	fs.DurationVar(&s.KubeletConfig.HTTPTimeout, "kubelet-timeout", s.KubeletConfig.HTTPTimeout,
   144  		"Timeout for kubelet operations.")
   145  
   146  	fs.StringVar(&s.KubeletConfig.TLSClientConfig.CertFile, "kubelet-client-certificate", s.KubeletConfig.TLSClientConfig.CertFile,
   147  		"Path to a client cert file for TLS.")
   148  
   149  	fs.StringVar(&s.KubeletConfig.TLSClientConfig.KeyFile, "kubelet-client-key", s.KubeletConfig.TLSClientConfig.KeyFile,
   150  		"Path to a client key file for TLS.")
   151  
   152  	fs.StringVar(&s.KubeletConfig.TLSClientConfig.CAFile, "kubelet-certificate-authority", s.KubeletConfig.TLSClientConfig.CAFile,
   153  		"Path to a cert file for the certificate authority.")
   154  
   155  	fs.IntVar(&s.MasterCount, "apiserver-count", s.MasterCount,
   156  		"The number of apiservers running in the cluster, must be a positive number. (In use when --endpoint-reconciler-type=master-count is enabled.)")
   157  	fs.MarkDeprecated("apiserver-count", "apiserver-count is deprecated and will be removed in a future version.")
   158  
   159  	return fss
   160  }