| 1 |
/* |
| 2 |
Copyright 2022 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 |
| 18 |
|
| 19 |
import ( |
| 20 |
"crypto/tls" |
| 21 |
"fmt" |
| 22 |
"os" |
| 23 |
"time" |
| 24 |
|
| 25 |
"github.com/google/uuid" |
| 26 |
"github.com/spf13/pflag" |
| 27 |
"k8s.io/apimachinery/pkg/runtime" |
| 28 |
"k8s.io/klog/v2" |
| 29 |
|
| 30 |
"sigs.k8s.io/apiserver-network-proxy/pkg/server" |
| 31 |
"sigs.k8s.io/apiserver-network-proxy/pkg/server/proxystrategies" |
| 32 |
"sigs.k8s.io/apiserver-network-proxy/pkg/util" |
| 33 |
) |
| 34 |
|
| 35 |
type ProxyRunOptions struct { |
| 36 |
// Certificate setup for securing communication to the "client" i.e. the Kube API Server. |
| 37 |
ServerCert string |
| 38 |
ServerKey string |
| 39 |
ServerCaCert string |
| 40 |
// Certificate setup for securing communication to the "agent" i.e. the managed cluster. |
| 41 |
ClusterCert string |
| 42 |
ClusterKey string |
| 43 |
ClusterCaCert string |
| 44 |
// Flag to switch between gRPC and HTTP Connect |
| 45 |
Mode string |
| 46 |
// Location for use by the "unix" network. Setting enables UDS for server connections. |
| 47 |
UdsName string |
| 48 |
// If file UdsName already exists, delete the file before listen on that UDS file. |
| 49 |
DeleteUDSFile bool |
| 50 |
// Port we listen for server connections on. |
| 51 |
ServerPort int |
| 52 |
// Bind address for the server. |
| 53 |
ServerBindAddress string |
| 54 |
// Port we listen for agent connections on. |
| 55 |
AgentPort int |
| 56 |
// Bind address for the agent. |
| 57 |
AgentBindAddress string |
| 58 |
// Port we listen for admin connections on. |
| 59 |
AdminPort int |
| 60 |
// Bind address for the admin connections. |
| 61 |
AdminBindAddress string |
| 62 |
// Port we listen for health connections on. |
| 63 |
HealthPort int |
| 64 |
// Bind address for the health connections. |
| 65 |
HealthBindAddress string |
| 66 |
// After a duration of this time if the server doesn't see any activity it |
| 67 |
// pings the client to see if the transport is still alive. |
| 68 |
KeepaliveTime time.Duration |
| 69 |
FrontendKeepaliveTime time.Duration |
| 70 |
// Enables pprof at host:AdminPort/debug/pprof. |
| 71 |
EnableProfiling bool |
| 72 |
// If EnableProfiling is true, this enables the lock contention |
| 73 |
// profiling at host:AdminPort/debug/pprof/block. |
| 74 |
EnableContentionProfiling bool |
| 75 |
|
| 76 |
// ID of this proxy server. |
| 77 |
ServerID string |
| 78 |
// Number of proxy server instances, should be 1 unless it is a HA proxy server. |
| 79 |
ServerCount int |
| 80 |
// Agent pod's namespace for token-based agent authentication |
| 81 |
AgentNamespace string |
| 82 |
// Agent pod's service account for token-based agent authentication |
| 83 |
AgentServiceAccount string |
| 84 |
// Token's audience for token-based agent authentication |
| 85 |
AuthenticationAudience string |
| 86 |
// Path to kubeconfig (used by kubernetes client) |
| 87 |
KubeconfigPath string |
| 88 |
// Client maximum QPS. |
| 89 |
KubeconfigQPS float32 |
| 90 |
// Client maximum burst for throttle. |
| 91 |
KubeconfigBurst int |
| 92 |
// Content type of requests sent to apiserver. |
| 93 |
APIContentType string |
| 94 |
|
| 95 |
// Proxy strategies used by the server. |
| 96 |
// NOTE the order of the strategies matters. e.g., for list |
| 97 |
// "destHost,destCIDR,default", the server will try to find a backend associating |
| 98 |
// to the destination host first, if not found, it will try to find a |
| 99 |
// backend within the destCIDR. if it still can't find any backend, |
| 100 |
// it will choose a random backend. |
| 101 |
ProxyStrategies string |
| 102 |
|
| 103 |
// Cipher suites used by the server. |
| 104 |
// If empty, the default suite will be used from tls.CipherSuites(), |
| 105 |
// also checks if given comma separated list contains cipher from tls.InsecureCipherSuites(). |
| 106 |
// NOTE that cipher suites are not configurable for TLS1.3, |
| 107 |
// see: https://pkg.go.dev/crypto/tls#Config, so in that case, this option won't have any effect. |
| 108 |
CipherSuites []string |
| 109 |
// Minimum TLS version for server connections. |
| 110 |
// Accepted values: VersionTLS10, VersionTLS11, VersionTLS12, VersionTLS13. |
| 111 |
// If empty, defaults to VersionTLS12. |
| 112 |
TLSMinVersion string |
| 113 |
XfrChannelSize int |
| 114 |
|
| 115 |
// Lease controller configuration |
| 116 |
EnableLeaseController bool |
| 117 |
// Lease Namespace |
| 118 |
LeaseNamespace string |
| 119 |
// Lease Labels |
| 120 |
LeaseLabel string |
| 121 |
// Needs kubernetes client |
| 122 |
NeedsKubernetesClient bool |
| 123 |
// Graceful shutdown timeout duration |
| 124 |
GracefulShutdownTimeout time.Duration |
| 125 |
// Backend dial timeout duration for requests from the server to backend agents. |
| 126 |
BackendDialTimeout time.Duration |
| 127 |
} |
| 128 |
|
| 129 |
func (o *ProxyRunOptions) Flags() *pflag.FlagSet { |
| 130 |
flags := pflag.NewFlagSet("proxy-server", pflag.ContinueOnError) |
| 131 |
flags.StringVar(&o.ServerCert, "server-cert", o.ServerCert, "If non-empty secure communication with this cert.") |
| 132 |
flags.StringVar(&o.ServerKey, "server-key", o.ServerKey, "If non-empty secure communication with this key.") |
| 133 |
flags.StringVar(&o.ServerCaCert, "server-ca-cert", o.ServerCaCert, "If non-empty the CA we use to validate KAS clients.") |
| 134 |
flags.StringVar(&o.ClusterCert, "cluster-cert", o.ClusterCert, "If non-empty secure communication with this cert.") |
| 135 |
flags.StringVar(&o.ClusterKey, "cluster-key", o.ClusterKey, "If non-empty secure communication with this key.") |
| 136 |
flags.StringVar(&o.ClusterCaCert, "cluster-ca-cert", o.ClusterCaCert, "If non-empty the CA we use to validate Agent clients.") |
| 137 |
flags.StringVar(&o.Mode, "mode", o.Mode, "mode can be either 'grpc' or 'http-connect'.") |
| 138 |
flags.StringVar(&o.UdsName, "uds-name", o.UdsName, "uds-name should be empty for TCP traffic. For UDS set to its name.") |
| 139 |
flags.BoolVar(&o.DeleteUDSFile, "delete-existing-uds-file", o.DeleteUDSFile, "If true and if file UdsName already exists, delete the file before listen on that UDS file. Default is true.") |
| 140 |
flags.IntVar(&o.ServerPort, "server-port", o.ServerPort, "Port we listen for server connections on. Set to 0 for UDS.") |
| 141 |
flags.StringVar(&o.ServerBindAddress, "server-bind-address", o.ServerBindAddress, "Bind address for server connections. If empty, we will bind to all interfaces.") |
| 142 |
flags.IntVar(&o.AgentPort, "agent-port", o.AgentPort, "Port we listen for agent connections on.") |
| 143 |
flags.StringVar(&o.AgentBindAddress, "agent-bind-address", o.AgentBindAddress, "Bind address for agent connections. If empty, we will bind to all interfaces.") |
| 144 |
flags.IntVar(&o.AdminPort, "admin-port", o.AdminPort, "Port we listen for admin connections on.") |
| 145 |
flags.StringVar(&o.AdminBindAddress, "admin-bind-address", o.AdminBindAddress, "Bind address for admin connections. If empty, we will bind to localhost.") |
| 146 |
flags.IntVar(&o.HealthPort, "health-port", o.HealthPort, "Port we listen for health connections on.") |
| 147 |
flags.StringVar(&o.HealthBindAddress, "health-bind-address", o.HealthBindAddress, "Bind address for health connections. If empty, we will bind to all interfaces.") |
| 148 |
flags.DurationVar(&o.KeepaliveTime, "keepalive-time", o.KeepaliveTime, "Time for gRPC agent server keepalive.") |
| 149 |
flags.DurationVar(&o.FrontendKeepaliveTime, "frontend-keepalive-time", o.FrontendKeepaliveTime, "Time for gRPC frontend server keepalive.") |
| 150 |
flags.BoolVar(&o.EnableProfiling, "enable-profiling", o.EnableProfiling, "enable pprof at host:admin-port/debug/pprof") |
| 151 |
flags.BoolVar(&o.EnableContentionProfiling, "enable-contention-profiling", o.EnableContentionProfiling, "enable contention profiling at host:admin-port/debug/pprof/block. \"--enable-profiling\" must also be set.") |
| 152 |
flags.StringVar(&o.ServerID, "server-id", o.ServerID, "The unique ID of this server. Can also be set by the 'PROXY_SERVER_ID' environment variable.") |
| 153 |
flags.IntVar(&o.ServerCount, "server-count", o.ServerCount, "The number of proxy server instances, should be 1 unless it is an HA server.") |
| 154 |
flags.StringVar(&o.AgentNamespace, "agent-namespace", o.AgentNamespace, "Expected agent's namespace during agent authentication (used with agent-service-account, authentication-audience, kubeconfig).") |
| 155 |
flags.StringVar(&o.AgentServiceAccount, "agent-service-account", o.AgentServiceAccount, "Expected agent's service account during agent authentication (used with agent-namespace, authentication-audience, kubeconfig).") |
| 156 |
flags.StringVar(&o.KubeconfigPath, "kubeconfig", o.KubeconfigPath, "absolute path to the kubeconfig file (used with agent-namespace, agent-service-account, authentication-audience).") |
| 157 |
flags.Float32Var(&o.KubeconfigQPS, "kubeconfig-qps", o.KubeconfigQPS, "Maximum client QPS (proxy server uses this client to authenticate agent tokens).") |
| 158 |
flags.IntVar(&o.KubeconfigBurst, "kubeconfig-burst", o.KubeconfigBurst, "Maximum client burst (proxy server uses this client to authenticate agent tokens).") |
| 159 |
flags.StringVar(&o.APIContentType, "kube-api-content-type", o.APIContentType, "Content type of requests sent to apiserver.") |
| 160 |
flags.StringVar(&o.AuthenticationAudience, "authentication-audience", o.AuthenticationAudience, "Expected agent's token authentication audience (used with agent-namespace, agent-service-account, kubeconfig).") |
| 161 |
flags.StringVar(&o.ProxyStrategies, "proxy-strategies", o.ProxyStrategies, "The list of proxy strategies used by the server to pick an agent/tunnel, available strategies are: default, destHost, defaultRoute.") |
| 162 |
flags.StringSliceVar(&o.CipherSuites, "cipher-suites", o.CipherSuites, "The comma separated list of allowed cipher suites. Has no effect on TLS1.3. Empty means allow default list.") |
| 163 |
flags.StringVar(&o.TLSMinVersion, "tls-min-version", o.TLSMinVersion, "Minimum TLS version for server connections. Accepted values: VersionTLS10, VersionTLS11, VersionTLS12, VersionTLS13. Empty defaults to VersionTLS12.") |
| 164 |
flags.IntVar(&o.XfrChannelSize, "xfr-channel-size", o.XfrChannelSize, "The size of the two KNP server channels used in server for transferring data. One channel is for data coming from the Kubernetes API Server, and the other one is for data coming from the KNP agent.") |
| 165 |
flags.BoolVar(&o.EnableLeaseController, "enable-lease-controller", o.EnableLeaseController, "Enable lease controller to publish and garbage collect proxy server leases.") |
| 166 |
flags.StringVar(&o.LeaseNamespace, "lease-namespace", o.LeaseNamespace, "The namespace where lease objects are managed by the controller.") |
| 167 |
flags.StringVar(&o.LeaseLabel, "lease-label", o.LeaseLabel, "The labels on which the lease objects are managed.") |
| 168 |
flags.DurationVar(&o.GracefulShutdownTimeout, "graceful-shutdown-timeout", o.GracefulShutdownTimeout, "Timeout duration for graceful shutdown of the server. The server will wait for active connections to close before forcefully terminating. Set to 0 to disable graceful shutdown (default: 0).") |
| 169 |
flags.DurationVar(&o.BackendDialTimeout, "backend-dial-timeout", o.BackendDialTimeout, "Timeout duration for sending DIAL_REQ packets to backend agent streams and waiting for DIAL_RSP. Set to 0 to disable timeout (default: 0).") |
| 170 |
flags.Bool("warn-on-channel-limit", true, "This behavior is now thread safe and always on. This flag will be removed in a future release.") |
| 171 |
flags.MarkDeprecated("warn-on-channel-limit", "This behavior is now thread safe and always on. This flag will be removed in a future release.") |
| 172 |
|
| 173 |
return flags |
| 174 |
} |
| 175 |
|
| 176 |
func (o *ProxyRunOptions) Print() { |
| 177 |
klog.V(1).Infof("ServerCert set to %q.\n", o.ServerCert) |
| 178 |
klog.V(1).Infof("ServerKey set to %q.\n", o.ServerKey) |
| 179 |
klog.V(1).Infof("ServerCACert set to %q.\n", o.ServerCaCert) |
| 180 |
klog.V(1).Infof("ClusterCert set to %q.\n", o.ClusterCert) |
| 181 |
klog.V(1).Infof("ClusterKey set to %q.\n", o.ClusterKey) |
| 182 |
klog.V(1).Infof("ClusterCACert set to %q.\n", o.ClusterCaCert) |
| 183 |
klog.V(1).Infof("Mode set to %q.\n", o.Mode) |
| 184 |
klog.V(1).Infof("UDSName set to %q.\n", o.UdsName) |
| 185 |
klog.V(1).Infof("DeleteUDSFile set to %v.\n", o.DeleteUDSFile) |
| 186 |
klog.V(1).Infof("Server port set to %d.\n", o.ServerPort) |
| 187 |
klog.V(1).Infof("Server bind address set to %q.\n", o.ServerBindAddress) |
| 188 |
klog.V(1).Infof("Agent port set to %d.\n", o.AgentPort) |
| 189 |
klog.V(1).Infof("Agent bind address set to %q.\n", o.AgentBindAddress) |
| 190 |
klog.V(1).Infof("Admin port set to %d.\n", o.AdminPort) |
| 191 |
klog.V(1).Infof("Admin bind address set to %q.\n", o.AdminBindAddress) |
| 192 |
klog.V(1).Infof("Health port set to %d.\n", o.HealthPort) |
| 193 |
klog.V(1).Infof("Health bind address set to %q.\n", o.HealthBindAddress) |
| 194 |
klog.V(1).Infof("Keepalive time set to %v.\n", o.KeepaliveTime) |
| 195 |
klog.V(1).Infof("Frontend keepalive time set to %v.\n", o.FrontendKeepaliveTime) |
| 196 |
klog.V(1).Infof("EnableProfiling set to %v.\n", o.EnableProfiling) |
| 197 |
klog.V(1).Infof("EnableContentionProfiling set to %v.\n", o.EnableContentionProfiling) |
| 198 |
klog.V(1).Infof("ServerID set to %s.\n", o.ServerID) |
| 199 |
klog.V(1).Infof("ServerCount set to %d.\n", o.ServerCount) |
| 200 |
klog.V(1).Infof("AgentNamespace set to %q.\n", o.AgentNamespace) |
| 201 |
klog.V(1).Infof("AgentServiceAccount set to %q.\n", o.AgentServiceAccount) |
| 202 |
klog.V(1).Infof("AuthenticationAudience set to %q.\n", o.AuthenticationAudience) |
| 203 |
klog.V(1).Infof("KubeconfigPath set to %q.\n", o.KubeconfigPath) |
| 204 |
klog.V(1).Infof("KubeconfigQPS set to %f.\n", o.KubeconfigQPS) |
| 205 |
klog.V(1).Infof("KubeconfigBurst set to %d.\n", o.KubeconfigBurst) |
| 206 |
klog.V(1).Infof("APIContentType set to %v.\n", o.APIContentType) |
| 207 |
klog.V(1).Infof("ProxyStrategies set to %q.\n", o.ProxyStrategies) |
| 208 |
klog.V(1).Infof("EnableLeaseController set to %v.\n", o.EnableLeaseController) |
| 209 |
klog.V(1).Infof("LeaseNamespace set to %s.\n", o.LeaseNamespace) |
| 210 |
klog.V(1).Infof("LeaseLabel set to %s.\n", o.LeaseLabel) |
| 211 |
klog.V(1).Infof("CipherSuites set to %q.\n", o.CipherSuites) |
| 212 |
klog.V(1).Infof("TLSMinVersion set to %q.\n", o.TLSMinVersion) |
| 213 |
klog.V(1).Infof("XfrChannelSize set to %d.\n", o.XfrChannelSize) |
| 214 |
klog.V(1).Infof("GracefulShutdownTimeout set to %v.\n", o.GracefulShutdownTimeout) |
| 215 |
klog.V(1).Infof("BackendDialTimeout set to %v.\n", o.BackendDialTimeout) |
| 216 |
} |
| 217 |
|
| 218 |
func (o *ProxyRunOptions) Validate() error { |
| 219 |
if o.ServerKey != "" { |
| 220 |
if _, err := os.Stat(o.ServerKey); os.IsNotExist(err) { |
| 221 |
return fmt.Errorf("error checking server key %s, got %v", o.ServerKey, err) |
| 222 |
} |
| 223 |
if o.ServerCert == "" { |
| 224 |
return fmt.Errorf("cannot have server cert empty when server key is set to %q", o.ServerKey) |
| 225 |
} |
| 226 |
} |
| 227 |
if o.ServerCert != "" { |
| 228 |
if _, err := os.Stat(o.ServerCert); os.IsNotExist(err) { |
| 229 |
return fmt.Errorf("error checking server cert %s, got %v", o.ServerCert, err) |
| 230 |
} |
| 231 |
if o.ServerKey == "" { |
| 232 |
return fmt.Errorf("cannot have server key empty when server cert is set to %q", o.ServerCert) |
| 233 |
} |
| 234 |
} |
| 235 |
if o.ServerCaCert != "" { |
| 236 |
if _, err := os.Stat(o.ServerCaCert); os.IsNotExist(err) { |
| 237 |
return fmt.Errorf("error checking server CA cert %s, got %v", o.ServerCaCert, err) |
| 238 |
} |
| 239 |
} |
| 240 |
if o.ClusterKey != "" { |
| 241 |
if _, err := os.Stat(o.ClusterKey); os.IsNotExist(err) { |
| 242 |
return fmt.Errorf("error checking cluster key %s, got %v", o.ClusterKey, err) |
| 243 |
} |
| 244 |
if o.ClusterCert == "" { |
| 245 |
return fmt.Errorf("cannot have cluster cert empty when cluster key is set to %q", o.ClusterKey) |
| 246 |
} |
| 247 |
} |
| 248 |
if o.ClusterCert != "" { |
| 249 |
if _, err := os.Stat(o.ClusterCert); os.IsNotExist(err) { |
| 250 |
return fmt.Errorf("error checking cluster cert %s, got %v", o.ClusterCert, err) |
| 251 |
} |
| 252 |
if o.ClusterKey == "" { |
| 253 |
return fmt.Errorf("cannot have cluster key empty when cluster cert is set to %q", o.ClusterCert) |
| 254 |
} |
| 255 |
} |
| 256 |
if o.ClusterCaCert != "" { |
| 257 |
if _, err := os.Stat(o.ClusterCaCert); os.IsNotExist(err) { |
| 258 |
return fmt.Errorf("error checking cluster CA cert %s, got %v", o.ClusterCaCert, err) |
| 259 |
} |
| 260 |
} |
| 261 |
if o.Mode != server.ModeGRPC && o.Mode != server.ModeHTTPConnect { |
| 262 |
return fmt.Errorf("mode must be set to either 'grpc' or 'http-connect' not %q", o.Mode) |
| 263 |
} |
| 264 |
if o.UdsName != "" { |
| 265 |
if o.ServerPort != 0 { |
| 266 |
return fmt.Errorf("server port should be set to 0 not %d for UDS", o.ServerPort) |
| 267 |
} |
| 268 |
if o.ServerKey != "" { |
| 269 |
return fmt.Errorf("server key should not be set for UDS") |
| 270 |
} |
| 271 |
if o.ServerCert != "" { |
| 272 |
return fmt.Errorf("server cert should not be set for UDS") |
| 273 |
} |
| 274 |
if o.ServerCaCert != "" { |
| 275 |
return fmt.Errorf("server ca cert should not be set for UDS") |
| 276 |
} |
| 277 |
} else { |
| 278 |
if o.ServerCaCert == "" { |
| 279 |
return fmt.Errorf("server ca cert must be set when in TCP mode (uds-name is empty)") |
| 280 |
} |
| 281 |
} |
| 282 |
if o.ServerPort > 49151 { |
| 283 |
return fmt.Errorf("please do not try to use ephemeral port %d for the server port", o.ServerPort) |
| 284 |
} |
| 285 |
if o.AgentPort > 49151 { |
| 286 |
return fmt.Errorf("please do not try to use ephemeral port %d for the agent port", o.AgentPort) |
| 287 |
} |
| 288 |
if o.AdminPort > 49151 { |
| 289 |
return fmt.Errorf("please do not try to use ephemeral port %d for the admin port", o.AdminPort) |
| 290 |
} |
| 291 |
if o.HealthPort > 49151 { |
| 292 |
return fmt.Errorf("please do not try to use ephemeral port %d for the health port", o.HealthPort) |
| 293 |
} |
| 294 |
|
| 295 |
if o.ServerPort < 1024 { |
| 296 |
if o.UdsName == "" { |
| 297 |
return fmt.Errorf("please do not try to use reserved port %d for the server port", o.ServerPort) |
| 298 |
} |
| 299 |
} |
| 300 |
if o.AgentPort < 1024 { |
| 301 |
return fmt.Errorf("please do not try to use reserved port %d for the agent port", o.AgentPort) |
| 302 |
} |
| 303 |
if o.AdminPort < 1024 { |
| 304 |
return fmt.Errorf("please do not try to use reserved port %d for the admin port", o.AdminPort) |
| 305 |
} |
| 306 |
if o.HealthPort < 1024 { |
| 307 |
return fmt.Errorf("please do not try to use reserved port %d for the health port", o.HealthPort) |
| 308 |
} |
| 309 |
if o.EnableContentionProfiling && !o.EnableProfiling { |
| 310 |
return fmt.Errorf("if --enable-contention-profiling is set, --enable-profiling must also be set") |
| 311 |
} |
| 312 |
usingServiceAccountAuth := o.AgentNamespace != "" || o.AgentServiceAccount != "" || o.AuthenticationAudience != "" |
| 313 |
if usingServiceAccountAuth { |
| 314 |
if o.ClusterCaCert != "" { |
| 315 |
return fmt.Errorf("--cluster-ca-cert can not be used when agent authentication is enabled") |
| 316 |
} |
| 317 |
if o.AgentNamespace == "" { |
| 318 |
return fmt.Errorf("--agent-namespace cannot be empty when agent authentication is enabled") |
| 319 |
} |
| 320 |
if o.AgentServiceAccount == "" { |
| 321 |
return fmt.Errorf("--agent-service-account cannot be empty when agent authentication is enabled") |
| 322 |
} |
| 323 |
if o.AuthenticationAudience == "" { |
| 324 |
return fmt.Errorf("--authentication-audience cannot be empty when agent authentication is enabled") |
| 325 |
} |
| 326 |
} |
| 327 |
// Validate kubeconfig path if provided |
| 328 |
if o.KubeconfigPath != "" { |
| 329 |
if _, err := os.Stat(o.KubeconfigPath); os.IsNotExist(err) { |
| 330 |
return fmt.Errorf("checking KubeconfigPath %q, got %v", o.KubeconfigPath, err) |
| 331 |
} |
| 332 |
} |
| 333 |
// validate the proxy strategies |
| 334 |
if len(o.ProxyStrategies) == 0 { |
| 335 |
return fmt.Errorf("ProxyStrategies cannot be empty") |
| 336 |
} |
| 337 |
if _, err := proxystrategies.ParseProxyStrategies(o.ProxyStrategies); err != nil { |
| 338 |
return fmt.Errorf("invalid proxy strategies: %v", err) |
| 339 |
} |
| 340 |
if o.XfrChannelSize <= 0 { |
| 341 |
return fmt.Errorf("channel size %d must be greater than 0", o.XfrChannelSize) |
| 342 |
} |
| 343 |
// validate the TLS min version |
| 344 |
if o.TLSMinVersion != "" { |
| 345 |
tlsVer, err := util.GetTLSVersion(o.TLSMinVersion) |
| 346 |
if err != nil { |
| 347 |
return err |
| 348 |
} |
| 349 |
if tlsVer < tls.VersionTLS12 { |
| 350 |
klog.Warningf("--tls-min-version=%s is below TLS 1.2 and is considered insecure (RFC 8996)", o.TLSMinVersion) |
| 351 |
} |
| 352 |
} |
| 353 |
// validate the cipher suites |
| 354 |
if len(o.CipherSuites) != 0 { |
| 355 |
acceptedCiphers := util.GetAcceptedCiphers() |
| 356 |
for _, cipher := range o.CipherSuites { |
| 357 |
_, ok := acceptedCiphers[cipher] |
| 358 |
if !ok { |
| 359 |
return fmt.Errorf("cipher suite %s not supported, doesn't exist or considered as insecure", cipher) |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
// Validate labels provided. |
| 364 |
if o.EnableLeaseController { |
| 365 |
_, err := util.ParseLabels(o.LeaseLabel) |
| 366 |
if err != nil { |
| 367 |
return err |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
// Validate graceful shutdown timeout |
| 372 |
if o.GracefulShutdownTimeout < 0 { |
| 373 |
return fmt.Errorf("graceful-shutdown-timeout must be >= 0, got %v", o.GracefulShutdownTimeout) |
| 374 |
} |
| 375 |
if o.BackendDialTimeout < 0 { |
| 376 |
return fmt.Errorf("backend-dial-timeout must be >= 0, got %v", o.BackendDialTimeout) |
| 377 |
} |
| 378 |
|
| 379 |
o.NeedsKubernetesClient = usingServiceAccountAuth || o.EnableLeaseController |
| 380 |
|
| 381 |
return nil |
| 382 |
} |
| 383 |
|
| 384 |
func NewProxyRunOptions() *ProxyRunOptions { |
| 385 |
o := ProxyRunOptions{ |
| 386 |
ServerCert: "", |
| 387 |
ServerKey: "", |
| 388 |
ServerCaCert: "", |
| 389 |
ClusterCert: "", |
| 390 |
ClusterKey: "", |
| 391 |
ClusterCaCert: "", |
| 392 |
Mode: "grpc", |
| 393 |
UdsName: "", |
| 394 |
DeleteUDSFile: true, |
| 395 |
ServerPort: 8090, |
| 396 |
ServerBindAddress: "", |
| 397 |
AgentPort: 8091, |
| 398 |
AgentBindAddress: "", |
| 399 |
HealthPort: 8092, |
| 400 |
HealthBindAddress: "", |
| 401 |
AdminPort: 8095, |
| 402 |
AdminBindAddress: "127.0.0.1", |
| 403 |
KeepaliveTime: 1 * time.Hour, |
| 404 |
FrontendKeepaliveTime: 1 * time.Hour, |
| 405 |
EnableProfiling: false, |
| 406 |
EnableContentionProfiling: false, |
| 407 |
ServerID: defaultServerID(), |
| 408 |
ServerCount: 1, |
| 409 |
AgentNamespace: "", |
| 410 |
AgentServiceAccount: "", |
| 411 |
KubeconfigPath: "", |
| 412 |
KubeconfigQPS: 0, |
| 413 |
KubeconfigBurst: 0, |
| 414 |
APIContentType: runtime.ContentTypeProtobuf, |
| 415 |
AuthenticationAudience: "", |
| 416 |
ProxyStrategies: "default", |
| 417 |
CipherSuites: make([]string, 0), |
| 418 |
TLSMinVersion: "", |
| 419 |
XfrChannelSize: 10, |
| 420 |
EnableLeaseController: false, |
| 421 |
LeaseNamespace: "kube-system", |
| 422 |
LeaseLabel: "k8s-app=konnectivity-server", |
| 423 |
GracefulShutdownTimeout: 0, |
| 424 |
BackendDialTimeout: 0, |
| 425 |
} |
| 426 |
return &o |
| 427 |
} |
| 428 |
|
| 429 |
func defaultServerID() string { |
| 430 |
// Default to the value set by the PROXY_SERVER_ID environment variable. If both the flag & |
| 431 |
// environment variable are set, the flag always wins. |
| 432 |
if id := os.Getenv("PROXY_SERVER_ID"); id != "" { |
| 433 |
return id |
| 434 |
} |
| 435 |
return uuid.New().String() |
| 436 |
} |