blob: fccb24e03ad5adbdd557e5a7dd1f202fabc15304 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2016 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package options
18
19import (
20 "fmt"
21 "net"
22 "time"
23
24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25 "k8s.io/apimachinery/pkg/runtime/serializer"
26 "k8s.io/apiserver/pkg/server"
27 utilfeature "k8s.io/apiserver/pkg/util/feature"
28
29 // add the generic feature gates
30 _ "k8s.io/apiserver/pkg/features"
31
32 "github.com/spf13/pflag"
33)
34
35// ServerRunOptions contains the options while running a generic api server.
36type ServerRunOptions struct {
37 AdvertiseAddress net.IP
38
39 CorsAllowedOriginList []string
40 ExternalHost string
41 MaxRequestsInFlight int
42 MaxMutatingRequestsInFlight int
43 RequestTimeout time.Duration
44 MinRequestTimeout int
45 TargetRAMMB int
46}
47
48func NewServerRunOptions() *ServerRunOptions {
49 defaults := server.NewConfig(serializer.CodecFactory{})
50 return &ServerRunOptions{
51 MaxRequestsInFlight: defaults.MaxRequestsInFlight,
52 MaxMutatingRequestsInFlight: defaults.MaxMutatingRequestsInFlight,
53 RequestTimeout: defaults.RequestTimeout,
54 MinRequestTimeout: defaults.MinRequestTimeout,
55 }
56}
57
58// ApplyOptions applies the run options to the method receiver and returns self
59func (s *ServerRunOptions) ApplyTo(c *server.Config) error {
60 c.CorsAllowedOriginList = s.CorsAllowedOriginList
61 c.ExternalAddress = s.ExternalHost
62 c.MaxRequestsInFlight = s.MaxRequestsInFlight
63 c.MaxMutatingRequestsInFlight = s.MaxMutatingRequestsInFlight
64 c.RequestTimeout = s.RequestTimeout
65 c.MinRequestTimeout = s.MinRequestTimeout
66 c.PublicAddress = s.AdvertiseAddress
67
68 return nil
69}
70
71// DefaultAdvertiseAddress sets the field AdvertiseAddress if unset. The field will be set based on the SecureServingOptions.
72func (s *ServerRunOptions) DefaultAdvertiseAddress(secure *SecureServingOptions) error {
73 if secure == nil {
74 return nil
75 }
76
77 if s.AdvertiseAddress == nil || s.AdvertiseAddress.IsUnspecified() {
78 hostIP, err := secure.DefaultExternalAddress()
79 if err != nil {
80 return fmt.Errorf("Unable to find suitable network address.error='%v'. "+
81 "Try to set the AdvertiseAddress directly or provide a valid BindAddress to fix this.", err)
82 }
83 s.AdvertiseAddress = hostIP
84 }
85
86 return nil
87}
88
89// Validate checks validation of ServerRunOptions
90func (s *ServerRunOptions) Validate() []error {
91 errors := []error{}
92 if s.TargetRAMMB < 0 {
93 errors = append(errors, fmt.Errorf("--target-ram-mb can not be negative value"))
94 }
95 if s.MaxRequestsInFlight < 0 {
96 errors = append(errors, fmt.Errorf("--max-requests-inflight can not be negative value"))
97 }
98 if s.MaxMutatingRequestsInFlight < 0 {
99 errors = append(errors, fmt.Errorf("--max-mutating-requests-inflight can not be negative value"))
100 }
101
102 if s.RequestTimeout.Nanoseconds() < 0 {
103 errors = append(errors, fmt.Errorf("--request-timeout can not be negative value"))
104 }
105
106 if s.MinRequestTimeout < 0 {
107 errors = append(errors, fmt.Errorf("--min-request-timeout can not be negative value"))
108 }
109
110 return errors
111}
112
113// AddFlags adds flags for a specific APIServer to the specified FlagSet
114func (s *ServerRunOptions) AddUniversalFlags(fs *pflag.FlagSet) {
115 // Note: the weird ""+ in below lines seems to be the only way to get gofmt to
116 // arrange these text blocks sensibly. Grrr.
117
118 fs.IPVar(&s.AdvertiseAddress, "advertise-address", s.AdvertiseAddress, ""+
119 "The IP address on which to advertise the apiserver to members of the cluster. This "+
120 "address must be reachable by the rest of the cluster. If blank, the --bind-address "+
121 "will be used. If --bind-address is unspecified, the host's default interface will "+
122 "be used.")
123
124 fs.StringSliceVar(&s.CorsAllowedOriginList, "cors-allowed-origins", s.CorsAllowedOriginList, ""+
125 "List of allowed origins for CORS, comma separated. An allowed origin can be a regular "+
126 "expression to support subdomain matching. If this list is empty CORS will not be enabled.")
127
128 fs.IntVar(&s.TargetRAMMB, "target-ram-mb", s.TargetRAMMB,
129 "Memory limit for apiserver in MB (used to configure sizes of caches, etc.)")
130
131 fs.StringVar(&s.ExternalHost, "external-hostname", s.ExternalHost,
132 "The hostname to use when generating externalized URLs for this master (e.g. Swagger API Docs).")
133
134 deprecatedMasterServiceNamespace := metav1.NamespaceDefault
135 fs.StringVar(&deprecatedMasterServiceNamespace, "master-service-namespace", deprecatedMasterServiceNamespace, ""+
136 "DEPRECATED: the namespace from which the kubernetes master services should be injected into pods.")
137
138 fs.IntVar(&s.MaxRequestsInFlight, "max-requests-inflight", s.MaxRequestsInFlight, ""+
139 "The maximum number of non-mutating requests in flight at a given time. When the server exceeds this, "+
140 "it rejects requests. Zero for no limit.")
141
142 fs.IntVar(&s.MaxMutatingRequestsInFlight, "max-mutating-requests-inflight", s.MaxMutatingRequestsInFlight, ""+
143 "The maximum number of mutating requests in flight at a given time. When the server exceeds this, "+
144 "it rejects requests. Zero for no limit.")
145
146 fs.DurationVar(&s.RequestTimeout, "request-timeout", s.RequestTimeout, ""+
147 "An optional field indicating the duration a handler must keep a request open before timing "+
148 "it out. This is the default request timeout for requests but may be overridden by flags such as "+
149 "--min-request-timeout for specific types of requests.")
150
151 fs.IntVar(&s.MinRequestTimeout, "min-request-timeout", s.MinRequestTimeout, ""+
152 "An optional field indicating the minimum number of seconds a handler must keep "+
153 "a request open before timing it out. Currently only honored by the watch request "+
154 "handler, which picks a randomized value above this number as the connection timeout, "+
155 "to spread out load.")
156
157 utilfeature.DefaultFeatureGate.AddFlag(fs)
158}