github.com/cilium/cilium@v1.16.2/pkg/k8s/client/config.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package client
     5  
     6  import (
     7  	"os"
     8  	"time"
     9  
    10  	"github.com/spf13/pflag"
    11  
    12  	"github.com/cilium/cilium/pkg/defaults"
    13  	"github.com/cilium/cilium/pkg/option"
    14  )
    15  
    16  const OptUserAgent = "user-agent"
    17  
    18  type Config struct {
    19  	// EnableK8s is a flag that, when set to false, forcibly disables the clientset, to let cilium
    20  	// operates with CNI-compatible orchestrators other than Kubernetes. Default to true.
    21  	EnableK8s bool
    22  
    23  	// K8sAPIServer is the kubernetes api address server (for https use --k8s-kubeconfig-path instead)
    24  	K8sAPIServer string
    25  
    26  	// K8sKubeConfigPath is the absolute path of the kubernetes kubeconfig file
    27  	K8sKubeConfigPath string
    28  
    29  	// K8sClientQPS is the queries per second limit for the K8s client. Defaults to k8s client defaults.
    30  	K8sClientQPS float32
    31  
    32  	// K8sClientBurst is the burst value allowed for the K8s client. Defaults to k8s client defaults.
    33  	K8sClientBurst int
    34  
    35  	// K8sClientConnectionTimeout configures the timeout for K8s client connections.
    36  	K8sClientConnectionTimeout time.Duration
    37  
    38  	// K8sClientConnectionKeepAlive configures the keep alive duration for K8s client connections.
    39  	K8sClientConnectionKeepAlive time.Duration
    40  
    41  	// K8sHeartbeatTimeout configures the timeout for apiserver heartbeat
    42  	K8sHeartbeatTimeout time.Duration
    43  
    44  	// K8sEnableAPIDiscovery enables Kubernetes API discovery
    45  	EnableK8sAPIDiscovery bool
    46  }
    47  
    48  var defaultConfig = Config{
    49  	EnableK8s:                    true,
    50  	K8sAPIServer:                 "",
    51  	K8sKubeConfigPath:            "",
    52  	K8sClientQPS:                 defaults.K8sClientQPSLimit,
    53  	K8sClientBurst:               defaults.K8sClientBurst,
    54  	K8sClientConnectionTimeout:   30 * time.Second,
    55  	K8sClientConnectionKeepAlive: 30 * time.Second,
    56  	K8sHeartbeatTimeout:          30 * time.Second,
    57  	EnableK8sAPIDiscovery:        defaults.K8sEnableAPIDiscovery,
    58  }
    59  
    60  func (def Config) Flags(flags *pflag.FlagSet) {
    61  	flags.Bool(option.EnableK8s, def.EnableK8s, "Enable the k8s clientset")
    62  	flags.String(option.K8sAPIServer, def.K8sAPIServer, "Kubernetes API server URL")
    63  	flags.String(option.K8sKubeConfigPath, def.K8sKubeConfigPath, "Absolute path of the kubernetes kubeconfig file")
    64  	flags.Float32(option.K8sClientQPSLimit, def.K8sClientQPS, "Queries per second limit for the K8s client")
    65  	flags.Int(option.K8sClientBurst, def.K8sClientBurst, "Burst value allowed for the K8s client")
    66  	flags.Duration(option.K8sClientConnectionTimeout, def.K8sClientConnectionTimeout, "Configures the timeout of K8s client connections. K8s client is disabled if the value is set to 0")
    67  	flags.Duration(option.K8sClientConnectionKeepAlive, def.K8sClientConnectionKeepAlive, "Configures the keep alive duration of K8s client connections. K8 client is disabled if the value is set to 0")
    68  	flags.Duration(option.K8sHeartbeatTimeout, def.K8sHeartbeatTimeout, "Configures the timeout for api-server heartbeat, set to 0 to disable")
    69  	flags.Bool(option.K8sEnableAPIDiscovery, def.EnableK8sAPIDiscovery, "Enable discovery of Kubernetes API groups and resources with the discovery API")
    70  }
    71  
    72  func (cfg Config) isEnabled() bool {
    73  	if !cfg.EnableK8s {
    74  		return false
    75  	}
    76  	return cfg.K8sAPIServer != "" ||
    77  		cfg.K8sKubeConfigPath != "" ||
    78  		(os.Getenv("KUBERNETES_SERVICE_HOST") != "" &&
    79  			os.Getenv("KUBERNETES_SERVICE_PORT") != "") ||
    80  		os.Getenv("K8S_NODE_NAME") != ""
    81  }