| 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 |
"fmt" |
| 21 |
"net" |
| 22 |
"net/url" |
| 23 |
"os" |
| 24 |
"strconv" |
| 25 |
"time" |
| 26 |
|
| 27 |
"github.com/google/uuid" |
| 28 |
"github.com/spf13/pflag" |
| 29 |
"google.golang.org/grpc" |
| 30 |
"k8s.io/apimachinery/pkg/runtime" |
| 31 |
"k8s.io/klog/v2" |
| 32 |
|
| 33 |
"sigs.k8s.io/apiserver-network-proxy/pkg/agent" |
| 34 |
"sigs.k8s.io/apiserver-network-proxy/pkg/util" |
| 35 |
"sigs.k8s.io/apiserver-network-proxy/proto/header" |
| 36 |
) |
| 37 |
|
| 38 |
type GrpcProxyAgentOptions struct { |
| 39 |
// Configuration for authenticating with the proxy-server |
| 40 |
AgentCert string |
| 41 |
AgentKey string |
| 42 |
CaCert string |
| 43 |
|
| 44 |
// Configuration for connecting to the proxy-server |
| 45 |
ProxyServerHost string |
| 46 |
ProxyServerPort int |
| 47 |
AlpnProtos []string |
| 48 |
|
| 49 |
// Bind address for the health connections. |
| 50 |
HealthServerHost string |
| 51 |
// Port we listen for health connections on. |
| 52 |
HealthServerPort int |
| 53 |
// Bind address for the admin connections. |
| 54 |
AdminBindAddress string |
| 55 |
// Port we listen for admin connections on. |
| 56 |
AdminServerPort int |
| 57 |
// Enables pprof at host:adminPort/debug/pprof. |
| 58 |
EnableProfiling bool |
| 59 |
// If EnableProfiling is true, this enables the lock contention |
| 60 |
// profiling at host:adminPort/debug/pprof/block. |
| 61 |
EnableContentionProfiling bool |
| 62 |
|
| 63 |
AgentID string |
| 64 |
AgentIdentifiers string |
| 65 |
SyncInterval time.Duration |
| 66 |
ProbeInterval time.Duration |
| 67 |
SyncIntervalCap time.Duration |
| 68 |
// After a duration of this time if the agent doesn't see any activity it |
| 69 |
// pings the server to see if the transport is still alive. |
| 70 |
KeepaliveTime time.Duration |
| 71 |
|
| 72 |
// file contains service account authorization token for enabling proxy-server token based authorization |
| 73 |
ServiceAccountTokenPath string |
| 74 |
|
| 75 |
// This warns if we attempt to push onto a "full" transfer channel. |
| 76 |
// However checking that the transfer channel is full is not safe. |
| 77 |
// It violates our race condition checking. Adding locks around a potentially |
| 78 |
// blocking call has its own problems, so it cannot easily be made race condition safe. |
| 79 |
// The check is an "unlocked" read but is still use at your own peril. |
| 80 |
WarnOnChannelLimit bool |
| 81 |
|
| 82 |
SyncForever bool |
| 83 |
XfrChannelSize int |
| 84 |
|
| 85 |
// Enables updating the server count by counting the number of valid leases |
| 86 |
// matching the selector. |
| 87 |
CountServerLeases bool |
| 88 |
// Namespace where lease objects are managed. |
| 89 |
LeaseNamespace string |
| 90 |
// Labels on which lease objects are managed. |
| 91 |
LeaseLabel string |
| 92 |
// ServerCountSource describes how server counts should be combined. |
| 93 |
ServerCountSource string |
| 94 |
// Path to kubeconfig (used by kubernetes client for lease listing) |
| 95 |
KubeconfigPath string |
| 96 |
// Content type of requests sent to apiserver. |
| 97 |
APIContentType string |
| 98 |
} |
| 99 |
|
| 100 |
func (o *GrpcProxyAgentOptions) ClientSetConfig(dialOptions ...grpc.DialOption) *agent.ClientSetConfig { |
| 101 |
return &agent.ClientSetConfig{ |
| 102 |
Address: net.JoinHostPort(o.ProxyServerHost, strconv.Itoa(o.ProxyServerPort)), |
| 103 |
AgentID: o.AgentID, |
| 104 |
AgentIdentifiers: o.AgentIdentifiers, |
| 105 |
SyncInterval: o.SyncInterval, |
| 106 |
ProbeInterval: o.ProbeInterval, |
| 107 |
SyncIntervalCap: o.SyncIntervalCap, |
| 108 |
DialOptions: dialOptions, |
| 109 |
ServiceAccountTokenPath: o.ServiceAccountTokenPath, |
| 110 |
WarnOnChannelLimit: o.WarnOnChannelLimit, |
| 111 |
SyncForever: o.SyncForever, |
| 112 |
XfrChannelSize: o.XfrChannelSize, |
| 113 |
ServerCountSource: o.ServerCountSource, |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
func (o *GrpcProxyAgentOptions) Flags() *pflag.FlagSet { |
| 118 |
flags := pflag.NewFlagSet("proxy-agent", pflag.ContinueOnError) |
| 119 |
flags.StringVar(&o.AgentCert, "agent-cert", o.AgentCert, "If non-empty secure communication with this cert.") |
| 120 |
flags.StringVar(&o.AgentKey, "agent-key", o.AgentKey, "If non-empty secure communication with this key.") |
| 121 |
flags.StringVar(&o.CaCert, "ca-cert", o.CaCert, "If non-empty the CAs we use to validate clients.") |
| 122 |
flags.StringVar(&o.ProxyServerHost, "proxy-server-host", o.ProxyServerHost, "The hostname to use to connect to the proxy-server.") |
| 123 |
flags.IntVar(&o.ProxyServerPort, "proxy-server-port", o.ProxyServerPort, "The port the proxy server is listening on.") |
| 124 |
flags.StringSliceVar(&o.AlpnProtos, "alpn-proto", o.AlpnProtos, "Additional ALPN protocols to be presented when connecting to the server. Useful to distinguish between network proxy and apiserver connections that share the same destination address.") |
| 125 |
flags.StringVar(&o.HealthServerHost, "health-server-host", o.HealthServerHost, "The host address to listen on, without port.") |
| 126 |
flags.IntVar(&o.HealthServerPort, "health-server-port", o.HealthServerPort, "The port the health server is listening on.") |
| 127 |
flags.IntVar(&o.AdminServerPort, "admin-server-port", o.AdminServerPort, "The port the admin server is listening on.") |
| 128 |
flags.StringVar(&o.AdminBindAddress, "admin-bind-address", o.AdminBindAddress, "Bind address for admin connections. If empty, we will bind to all interfaces.") |
| 129 |
flags.BoolVar(&o.EnableProfiling, "enable-profiling", o.EnableProfiling, "enable pprof at host:admin-port/debug/pprof") |
| 130 |
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.") |
| 131 |
flags.StringVar(&o.AgentID, "agent-id", o.AgentID, "The unique ID of this agent. Can also be set by the 'PROXY_AGENT_ID' environment variable. Default to a generated uuid if not set.") |
| 132 |
flags.DurationVar(&o.SyncInterval, "sync-interval", o.SyncInterval, "The initial interval by which the agent periodically checks if it has connections to all instances of the proxy server.") |
| 133 |
flags.DurationVar(&o.ProbeInterval, "probe-interval", o.ProbeInterval, "The interval by which the agent periodically checks if its connections to the proxy server are ready.") |
| 134 |
flags.DurationVar(&o.SyncIntervalCap, "sync-interval-cap", o.SyncIntervalCap, "The maximum interval for the SyncInterval to back off to when unable to connect to the proxy server") |
| 135 |
flags.DurationVar(&o.KeepaliveTime, "keepalive-time", o.KeepaliveTime, "Time for gRPC agent server keepalive.") |
| 136 |
flags.StringVar(&o.ServiceAccountTokenPath, "service-account-token-path", o.ServiceAccountTokenPath, "If non-empty proxy agent uses this token to prove its identity to the proxy server.") |
| 137 |
flags.StringVar(&o.AgentIdentifiers, "agent-identifiers", o.AgentIdentifiers, "Identifiers of the agent that will be used by the server when choosing agent. N.B. the list of identifiers must be in URL encoded format. e.g.,host=localhost&host=node1.mydomain.com&cidr=127.0.0.1/16&ipv4=1.2.3.4&ipv4=5.6.7.8&ipv6=:::::&default-route=true") |
| 138 |
flags.BoolVar(&o.WarnOnChannelLimit, "warn-on-channel-limit", o.WarnOnChannelLimit, "Turns on a warning if the system is going to push to a full channel. The check involves an unsafe read.") |
| 139 |
flags.BoolVar(&o.SyncForever, "sync-forever", o.SyncForever, "If true, the agent continues syncing, in order to support server count changes.") |
| 140 |
flags.IntVar(&o.XfrChannelSize, "xfr-channel-size", 150, "Set the size of the channel for transferring data between the agent and the proxy server.") |
| 141 |
flags.BoolVar(&o.CountServerLeases, "count-server-leases", o.CountServerLeases, "Enables lease counting system to determine the number of proxy servers to connect to.") |
| 142 |
flags.StringVar(&o.LeaseNamespace, "lease-namespace", o.LeaseNamespace, "Namespace where lease objects are managed.") |
| 143 |
flags.StringVar(&o.LeaseLabel, "lease-label", o.LeaseLabel, "The labels on which the lease objects are managed.") |
| 144 |
flags.StringVar(&o.ServerCountSource, "server-count-source", o.ServerCountSource, "Defines how the server counts from lease and from server responses are combined. Possible values: 'default' to use only one source (server or leases depending on other flags), 'max' to take the larger value.") |
| 145 |
flags.StringVar(&o.KubeconfigPath, "kubeconfig", o.KubeconfigPath, "Path to the kubeconfig file") |
| 146 |
flags.StringVar(&o.APIContentType, "kube-api-content-type", o.APIContentType, "Content type of requests sent to apiserver.") |
| 147 |
return flags |
| 148 |
} |
| 149 |
|
| 150 |
func (o *GrpcProxyAgentOptions) Print() { |
| 151 |
klog.V(1).Infof("AgentCert set to %q.\n", o.AgentCert) |
| 152 |
klog.V(1).Infof("AgentKey set to %q.\n", o.AgentKey) |
| 153 |
klog.V(1).Infof("CACert set to %q.\n", o.CaCert) |
| 154 |
klog.V(1).Infof("ProxyServerHost set to %q.\n", o.ProxyServerHost) |
| 155 |
klog.V(1).Infof("ProxyServerPort set to %d.\n", o.ProxyServerPort) |
| 156 |
klog.V(1).Infof("ALPNProtos set to %+s.\n", o.AlpnProtos) |
| 157 |
klog.V(1).Infof("HealthServerHost set to %s\n", o.HealthServerHost) |
| 158 |
klog.V(1).Infof("HealthServerPort set to %d.\n", o.HealthServerPort) |
| 159 |
klog.V(1).Infof("Admin bind address set to %q.\n", o.AdminBindAddress) |
| 160 |
klog.V(1).Infof("AdminServerPort set to %d.\n", o.AdminServerPort) |
| 161 |
klog.V(1).Infof("EnableProfiling set to %v.\n", o.EnableProfiling) |
| 162 |
klog.V(1).Infof("EnableContentionProfiling set to %v.\n", o.EnableContentionProfiling) |
| 163 |
klog.V(1).Infof("AgentID set to %s.\n", o.AgentID) |
| 164 |
klog.V(1).Infof("SyncInterval set to %v.\n", o.SyncInterval) |
| 165 |
klog.V(1).Infof("ProbeInterval set to %v.\n", o.ProbeInterval) |
| 166 |
klog.V(1).Infof("SyncIntervalCap set to %v.\n", o.SyncIntervalCap) |
| 167 |
klog.V(1).Infof("Keepalive time set to %v.\n", o.KeepaliveTime) |
| 168 |
klog.V(1).Infof("ServiceAccountTokenPath set to %q.\n", o.ServiceAccountTokenPath) |
| 169 |
klog.V(1).Infof("AgentIdentifiers set to %s.\n", util.PrettyPrintURL(o.AgentIdentifiers)) |
| 170 |
klog.V(1).Infof("WarnOnChannelLimit set to %t.\n", o.WarnOnChannelLimit) |
| 171 |
klog.V(1).Infof("SyncForever set to %v.\n", o.SyncForever) |
| 172 |
klog.V(1).Infof("CountServerLeases set to %v.\n", o.CountServerLeases) |
| 173 |
klog.V(1).Infof("LeaseNamespace set to %s.\n", o.LeaseNamespace) |
| 174 |
klog.V(1).Infof("LeaseLabel set to %s.\n", o.LeaseLabel) |
| 175 |
klog.V(1).Infof("ServerCountSource set to %s.\n", o.ServerCountSource) |
| 176 |
klog.V(1).Infof("ChannelSize set to %d.\n", o.XfrChannelSize) |
| 177 |
klog.V(1).Infof("APIContentType set to %v.\n", o.APIContentType) |
| 178 |
} |
| 179 |
|
| 180 |
func (o *GrpcProxyAgentOptions) Validate() error { |
| 181 |
if o.AgentKey != "" { |
| 182 |
if _, err := os.Stat(o.AgentKey); os.IsNotExist(err) { |
| 183 |
return fmt.Errorf("error checking agent key %s, got %v", o.AgentKey, err) |
| 184 |
} |
| 185 |
if o.AgentCert == "" { |
| 186 |
return fmt.Errorf("cannot have agent cert empty when agent key is set to \"%s\"", o.AgentKey) |
| 187 |
} |
| 188 |
} |
| 189 |
if o.AgentCert != "" { |
| 190 |
if _, err := os.Stat(o.AgentCert); os.IsNotExist(err) { |
| 191 |
return fmt.Errorf("error checking agent cert %s, got %v", o.AgentCert, err) |
| 192 |
} |
| 193 |
if o.AgentKey == "" { |
| 194 |
return fmt.Errorf("cannot have agent key empty when agent cert is set to \"%s\"", o.AgentCert) |
| 195 |
} |
| 196 |
} |
| 197 |
if o.CaCert != "" { |
| 198 |
if _, err := os.Stat(o.CaCert); os.IsNotExist(err) { |
| 199 |
return fmt.Errorf("error checking agent CA cert %s, got %v", o.CaCert, err) |
| 200 |
} |
| 201 |
} |
| 202 |
if o.ProxyServerPort <= 0 { |
| 203 |
return fmt.Errorf("proxy server port %d must be greater than 0", o.ProxyServerPort) |
| 204 |
} |
| 205 |
if o.HealthServerPort <= 0 { |
| 206 |
return fmt.Errorf("health server port %d must be greater than 0", o.HealthServerPort) |
| 207 |
} |
| 208 |
if o.AdminServerPort <= 0 { |
| 209 |
return fmt.Errorf("admin server port %d must be greater than 0", o.AdminServerPort) |
| 210 |
} |
| 211 |
if o.XfrChannelSize <= 0 { |
| 212 |
return fmt.Errorf("channel size %d must be greater than 0", o.XfrChannelSize) |
| 213 |
} |
| 214 |
if o.EnableContentionProfiling && !o.EnableProfiling { |
| 215 |
return fmt.Errorf("if --enable-contention-profiling is set, --enable-profiling must also be set") |
| 216 |
} |
| 217 |
if o.SyncInterval > o.SyncIntervalCap { |
| 218 |
return fmt.Errorf("sync interval %v must be less than sync interval cap %v", o.SyncInterval, o.SyncIntervalCap) |
| 219 |
} |
| 220 |
if o.ServiceAccountTokenPath != "" { |
| 221 |
if _, err := os.Stat(o.ServiceAccountTokenPath); os.IsNotExist(err) { |
| 222 |
return fmt.Errorf("error checking service account token path %s, got %v", o.ServiceAccountTokenPath, err) |
| 223 |
} |
| 224 |
} |
| 225 |
if err := validateAgentIdentifiers(o.AgentIdentifiers); err != nil { |
| 226 |
return fmt.Errorf("agent address is invalid: %v", err) |
| 227 |
} |
| 228 |
if o.KubeconfigPath != "" { |
| 229 |
if _, err := os.Stat(o.KubeconfigPath); os.IsNotExist(err) { |
| 230 |
return fmt.Errorf("error checking KubeconfigPath %q, got %v", o.KubeconfigPath, err) |
| 231 |
} |
| 232 |
} |
| 233 |
// Validate labels provided. |
| 234 |
if o.CountServerLeases { |
| 235 |
_, err := util.ParseLabels(o.LeaseLabel) |
| 236 |
if err != nil { |
| 237 |
return err |
| 238 |
} |
| 239 |
} |
| 240 |
if o.ServerCountSource != "" { |
| 241 |
if o.ServerCountSource != "default" && o.ServerCountSource != "max" { |
| 242 |
return fmt.Errorf("--server-count-source must be one of '', 'default', 'max', got %v", o.ServerCountSource) |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
return nil |
| 247 |
} |
| 248 |
|
| 249 |
func validateAgentIdentifiers(agentIdentifiers string) error { |
| 250 |
decoded, err := url.ParseQuery(agentIdentifiers) |
| 251 |
if err != nil { |
| 252 |
return err |
| 253 |
} |
| 254 |
for idType := range decoded { |
| 255 |
switch header.IdentifierType(idType) { |
| 256 |
case header.IPv4: |
| 257 |
case header.IPv6: |
| 258 |
case header.CIDR: |
| 259 |
case header.Host: |
| 260 |
case header.DefaultRoute: |
| 261 |
default: |
| 262 |
return fmt.Errorf("unknown address type: %s", idType) |
| 263 |
} |
| 264 |
} |
| 265 |
return nil |
| 266 |
} |
| 267 |
|
| 268 |
func NewGrpcProxyAgentOptions() *GrpcProxyAgentOptions { |
| 269 |
o := GrpcProxyAgentOptions{ |
| 270 |
AgentCert: "", |
| 271 |
AgentKey: "", |
| 272 |
CaCert: "", |
| 273 |
ProxyServerHost: "127.0.0.1", |
| 274 |
ProxyServerPort: 8091, |
| 275 |
HealthServerHost: "", |
| 276 |
HealthServerPort: 8093, |
| 277 |
AdminBindAddress: "127.0.0.1", |
| 278 |
AdminServerPort: 8094, |
| 279 |
EnableProfiling: false, |
| 280 |
EnableContentionProfiling: false, |
| 281 |
AgentID: defaultAgentID(), |
| 282 |
AgentIdentifiers: "", |
| 283 |
SyncInterval: 1 * time.Second, |
| 284 |
ProbeInterval: 1 * time.Second, |
| 285 |
SyncIntervalCap: 10 * time.Second, |
| 286 |
KeepaliveTime: 1 * time.Hour, |
| 287 |
ServiceAccountTokenPath: "", |
| 288 |
WarnOnChannelLimit: false, |
| 289 |
SyncForever: false, |
| 290 |
XfrChannelSize: 150, |
| 291 |
CountServerLeases: false, |
| 292 |
LeaseNamespace: "kube-system", |
| 293 |
LeaseLabel: "k8s-app=konnectivity-server", |
| 294 |
ServerCountSource: "default", |
| 295 |
KubeconfigPath: "", |
| 296 |
APIContentType: runtime.ContentTypeProtobuf, |
| 297 |
} |
| 298 |
return &o |
| 299 |
} |
| 300 |
|
| 301 |
func defaultAgentID() string { |
| 302 |
// Default to the value set by the PROXY_AGENT_ID environment variable. If both the flag & |
| 303 |
// environment variable are set, the flag always wins. |
| 304 |
if id := os.Getenv("PROXY_AGENT_ID"); id != "" { |
| 305 |
return id |
| 306 |
} |
| 307 |
return uuid.New().String() |
| 308 |
} |