github.com/looshlee/beatles@v0.0.0-20220727174639-742810ab631c/pkg/option/config.go (about)

     1  // Copyright 2016-2019 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package option
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"io/ioutil"
    21  	"net"
    22  	"os"
    23  	"path/filepath"
    24  	"runtime"
    25  	"sort"
    26  	"strconv"
    27  	"strings"
    28  	"time"
    29  
    30  	"github.com/cilium/cilium/api/v1/models"
    31  	"github.com/cilium/cilium/common"
    32  	"github.com/cilium/cilium/pkg/cidr"
    33  	"github.com/cilium/cilium/pkg/defaults"
    34  	"github.com/cilium/cilium/pkg/ip"
    35  	"github.com/cilium/cilium/pkg/lock"
    36  	"github.com/cilium/cilium/pkg/logging"
    37  	"github.com/cilium/cilium/pkg/logging/logfields"
    38  	"github.com/cilium/cilium/pkg/metrics"
    39  
    40  	"github.com/prometheus/client_golang/prometheus"
    41  	"github.com/sirupsen/logrus"
    42  	"github.com/spf13/viper"
    43  )
    44  
    45  var (
    46  	log = logging.DefaultLogger.WithField(logfields.LogSubsys, "config")
    47  )
    48  
    49  const (
    50  	// AccessLog is the path to access log of supported L7 requests observed
    51  	AccessLog = "access-log"
    52  
    53  	// AgentLabels are additional labels to identify this agent
    54  	AgentLabels = "agent-labels"
    55  
    56  	// AllowLocalhost is the policy when to allow local stack to reach local endpoints { auto | always | policy }
    57  	AllowLocalhost = "allow-localhost"
    58  
    59  	// AllowLocalhostAuto defaults to policy except when running in
    60  	// Kubernetes where it then defaults to "always"
    61  	AllowLocalhostAuto = "auto"
    62  
    63  	// AllowLocalhostAlways always allows the local stack to reach local
    64  	// endpoints
    65  	AllowLocalhostAlways = "always"
    66  
    67  	// AllowLocalhostPolicy requires a policy rule to allow the local stack
    68  	// to reach particular endpoints or policy enforcement must be
    69  	// disabled.
    70  	AllowLocalhostPolicy = "policy"
    71  
    72  	// AnnotateK8sNode enables annotating a kubernetes node while bootstrapping
    73  	// the daemon, which can also be disbled using this option.
    74  	AnnotateK8sNode = "annotate-k8s-node"
    75  
    76  	// AwsInstanceLimitMapping allows overwirting AWS instance limits defined in
    77  	// pkg/aws/eni/limits.go
    78  	// e.g. {"a1.medium": "2,4,4", "a2.custom2": "4,5,6"}
    79  	AwsInstanceLimitMapping = "aws-instance-limit-mapping"
    80  
    81  	// AwsReleaseExcessIps allows releasing excess free IP addresses from ENI.
    82  	// Enabling this option reduces waste of IP addresses but may increase
    83  	// the number of API calls to AWS EC2 service.
    84  	AwsReleaseExcessIps = "aws-release-excess-ips"
    85  
    86  	// BPFRoot is the Path to BPF filesystem
    87  	BPFRoot = "bpf-root"
    88  
    89  	// CGroupRoot is the path to Cgroup2 filesystem
    90  	CGroupRoot = "cgroup-root"
    91  
    92  	// ConfigFile is the Configuration file (default "$HOME/ciliumd.yaml")
    93  	ConfigFile = "config"
    94  
    95  	// ConfigDir is the directory that contains a file for each option where
    96  	// the filename represents the option name and the content of that file
    97  	// represents the value of that option.
    98  	ConfigDir = "config-dir"
    99  
   100  	// ConntrackGarbageCollectorIntervalDeprecated is the deprecated option
   101  	// name to set the conntrack gc interval
   102  	ConntrackGarbageCollectorIntervalDeprecated = "conntrack-garbage-collector-interval"
   103  
   104  	// ConntrackGCInterval is the name of the ConntrackGCInterval option
   105  	ConntrackGCInterval = "conntrack-gc-interval"
   106  
   107  	// ContainerRuntime sets the container runtime(s) used by Cilium
   108  	// { containerd | crio | docker | none | auto } ( "auto" uses the container
   109  	// runtime found in the order: "docker", "containerd", "crio" )
   110  	ContainerRuntime = "container-runtime"
   111  
   112  	// ContainerRuntimeEndpoint set the container runtime(s) endpoint(s)
   113  	ContainerRuntimeEndpoint = "container-runtime-endpoint"
   114  
   115  	// DebugArg is the argument enables debugging mode
   116  	DebugArg = "debug"
   117  
   118  	// DebugVerbose is the argument enables verbose log message for particular subsystems
   119  	DebugVerbose = "debug-verbose"
   120  
   121  	// Device facing cluster/external network for direct L3 (non-overlay mode)
   122  	Device = "device"
   123  
   124  	// DisableConntrack disables connection tracking
   125  	DisableConntrack = "disable-conntrack"
   126  
   127  	// DisableEnvoyVersionCheck do not perform Envoy binary version check on startup
   128  	DisableEnvoyVersionCheck = "disable-envoy-version-check"
   129  
   130  	// Docker is the path to docker runtime socket (DEPRECATED: use container-runtime-endpoint instead)
   131  	Docker = "docker"
   132  
   133  	// EnablePolicy enables policy enforcement in the agent.
   134  	EnablePolicy = "enable-policy"
   135  
   136  	// EnableL7Proxy is the name of the option to enable L7 proxy
   137  	EnableL7Proxy = "enable-l7-proxy"
   138  
   139  	// EnableTracing enables tracing mode in the agent.
   140  	EnableTracing = "enable-tracing"
   141  
   142  	// EncryptInterface enables encryption on specified interface
   143  	EncryptInterface = "encrypt-interface"
   144  
   145  	// EncryptNode enables node IP encryption
   146  	EncryptNode = "encrypt-node"
   147  
   148  	// EnvoyLog sets the path to a separate Envoy log file, if any
   149  	EnvoyLog = "envoy-log"
   150  
   151  	// FixedIdentityMapping is the key-value for the fixed identity mapping
   152  	// which allows to use reserved label for fixed identities
   153  	FixedIdentityMapping = "fixed-identity-mapping"
   154  
   155  	// IPv4ClusterCIDRMaskSize is the mask size for the cluster wide CIDR
   156  	IPv4ClusterCIDRMaskSize = "ipv4-cluster-cidr-mask-size"
   157  
   158  	// IPv4Range is the per-node IPv4 endpoint prefix, e.g. 10.16.0.0/16
   159  	IPv4Range = "ipv4-range"
   160  
   161  	// IPv6Range is the per-node IPv6 endpoint prefix, must be /96, e.g. fd02:1:1::/96
   162  	IPv6Range = "ipv6-range"
   163  
   164  	// IPv4ServiceRange is the Kubernetes IPv4 services CIDR if not inside cluster prefix
   165  	IPv4ServiceRange = "ipv4-service-range"
   166  
   167  	// IPv6ServiceRange is the Kubernetes IPv6 services CIDR if not inside cluster prefix
   168  	IPv6ServiceRange = "ipv6-service-range"
   169  
   170  	// ModePreFilterNative for loading progs with xdpdrv
   171  	ModePreFilterNative = "native"
   172  
   173  	// ModePreFilterGeneric for loading progs with xdpgeneric
   174  	ModePreFilterGeneric = "generic"
   175  
   176  	// IPv6ClusterAllocCIDRName is the name of the IPv6ClusterAllocCIDR option
   177  	IPv6ClusterAllocCIDRName = "ipv6-cluster-alloc-cidr"
   178  
   179  	// K8sRequireIPv4PodCIDRName is the name of the K8sRequireIPv4PodCIDR option
   180  	K8sRequireIPv4PodCIDRName = "k8s-require-ipv4-pod-cidr"
   181  
   182  	// K8sRequireIPv6PodCIDRName is the name of the K8sRequireIPv6PodCIDR option
   183  	K8sRequireIPv6PodCIDRName = "k8s-require-ipv6-pod-cidr"
   184  
   185  	// K8sForceJSONPatch when set, uses JSON Patch to update CNP and CEP
   186  	// status in kube-apiserver.
   187  	K8sForceJSONPatch = "k8s-force-json-patch"
   188  
   189  	// K8sWatcherEndpointSelector specifies the k8s endpoints that Cilium
   190  	// should watch for.
   191  	K8sWatcherEndpointSelector = "k8s-watcher-endpoint-selector"
   192  
   193  	// K8sAPIServer is the kubernetes api address server (for https use --k8s-kubeconfig-path instead)
   194  	K8sAPIServer = "k8s-api-server"
   195  
   196  	// K8sKubeConfigPath is the absolute path of the kubernetes kubeconfig file
   197  	K8sKubeConfigPath = "k8s-kubeconfig-path"
   198  
   199  	// K8sServiceCacheSize is service cache size for cilium k8s package.
   200  	K8sServiceCacheSize = "k8s-service-cache-size"
   201  
   202  	// K8sWatcherQueueSize is the queue size used to serialize each k8s event type
   203  	K8sWatcherQueueSize = "k8s-watcher-queue-size"
   204  
   205  	// KeepConfig when restoring state, keeps containers' configuration in place
   206  	KeepConfig = "keep-config"
   207  
   208  	// KeepBPFTemplates do not restore BPF template files from binary
   209  	KeepBPFTemplates = "keep-bpf-templates"
   210  
   211  	// KVStore key-value store type
   212  	KVStore = "kvstore"
   213  
   214  	// KVStoreOpt key-value store options
   215  	KVStoreOpt = "kvstore-opt"
   216  
   217  	// Labels is the list of label prefixes used to determine identity of an endpoint
   218  	Labels = "labels"
   219  
   220  	// LabelPrefixFile is the valid label prefixes file path
   221  	LabelPrefixFile = "label-prefix-file"
   222  
   223  	// LB enables load balancer mode where load balancer bpf program is attached to the given interface
   224  	LB = "lb"
   225  
   226  	// EnableNodePort enables NodePort services implemented by Cilium in BPF
   227  	EnableNodePort = "enable-node-port"
   228  
   229  	// NodePortRange defines a custom range where to look up NodePort services
   230  	NodePortRange = "node-port-range"
   231  
   232  	// LibDir enables the directory path to store runtime build environment
   233  	LibDir = "lib-dir"
   234  
   235  	// LogDriver sets logging endpoints to use for example syslog, fluentd
   236  	LogDriver = "log-driver"
   237  
   238  	// LogOpt sets log driver options for cilium
   239  	LogOpt = "log-opt"
   240  
   241  	// Logstash enables logstash integration
   242  	Logstash = "logstash"
   243  
   244  	// NAT46Range is the IPv6 prefix to map IPv4 addresses to
   245  	NAT46Range = "nat46-range"
   246  
   247  	// Masquerade are the packets from endpoints leaving the host
   248  	Masquerade = "masquerade"
   249  
   250  	// InstallIptRules sets whether Cilium should install any iptables in general
   251  	InstallIptRules = "install-iptables-rules"
   252  
   253  	IPTablesLockTimeout = "iptables-lock-timeout"
   254  
   255  	IPTablesLockWaitInterval = "iptables-lock-wait-interval"
   256  
   257  	// IPv6NodeAddr is the IPv6 address of node
   258  	IPv6NodeAddr = "ipv6-node"
   259  
   260  	// IPv4NodeAddr is the IPv4 address of node
   261  	IPv4NodeAddr = "ipv4-node"
   262  
   263  	// Restore restores state, if possible, from previous daemon
   264  	Restore = "restore"
   265  
   266  	// SidecarHTTPProxy disable host HTTP proxy, assuming proxies in sidecar containers
   267  	SidecarHTTPProxy = "sidecar-http-proxy"
   268  
   269  	// SidecarIstioProxyImage regular expression matching compatible Istio sidecar istio-proxy container image names
   270  	SidecarIstioProxyImage = "sidecar-istio-proxy-image"
   271  
   272  	// SocketPath sets daemon's socket path to listen for connections
   273  	SocketPath = "socket-path"
   274  
   275  	// StateDir is the directory path to store runtime state
   276  	StateDir = "state-dir"
   277  
   278  	// TracePayloadlen length of payload to capture when tracing
   279  	TracePayloadlen = "trace-payloadlen"
   280  
   281  	// Version prints the version information
   282  	Version = "version"
   283  
   284  	// FlannelMasterDevice installs a BPF program to allow for policy
   285  	// enforcement in the given network interface. Allows to run Cilium on top
   286  	// of other CNI plugins that provide networking, e.g. flannel, where for
   287  	// flannel, this value should be set with 'cni0'. [EXPERIMENTAL]")
   288  	FlannelMasterDevice = "flannel-master-device"
   289  
   290  	// FlannelUninstallOnExit should be used along the flannel-master-device flag,
   291  	// it cleans up all BPF programs installed when Cilium agent is terminated.
   292  	FlannelUninstallOnExit = "flannel-uninstall-on-exit"
   293  
   294  	// FlannelManageExistingContainers sets if Cilium should install the BPF
   295  	// programs on already running interfaces created by flannel. Require
   296  	// Cilium to be running in the hostPID.
   297  	FlannelManageExistingContainers = "flannel-manage-existing-containers"
   298  
   299  	// PProf enables serving the pprof debugging API
   300  	PProf = "pprof"
   301  
   302  	// PrefilterDevice is the device facing external network for XDP prefiltering
   303  	PrefilterDevice = "prefilter-device"
   304  
   305  	// PrefilterMode { "+ModePreFilterNative+" | "+ModePreFilterGeneric+" } (default: "+option.ModePreFilterNative+")
   306  	PrefilterMode = "prefilter-mode"
   307  
   308  	// PrometheusServeAddr IP:Port on which to serve prometheus metrics (pass ":Port" to bind on all interfaces, "" is off)
   309  	PrometheusServeAddr = "prometheus-serve-addr"
   310  
   311  	// PrometheusServeAddrDeprecated IP:Port on which to serve prometheus metrics (pass ":Port" to bind on all interfaces, "" is off)
   312  	PrometheusServeAddrDeprecated = "prometheus-serve-addr-deprecated"
   313  
   314  	// CMDRef is the path to cmdref output directory
   315  	CMDRef = "cmdref"
   316  
   317  	// ToFQDNsMinTTL is the minimum time, in seconds, to use DNS data for toFQDNs policies.
   318  	ToFQDNsMinTTL = "tofqdns-min-ttl"
   319  
   320  	// ToFQDNsProxyPort is the global port on which the in-agent DNS proxy should listen. Default 0 is a OS-assigned port.
   321  	ToFQDNsProxyPort = "tofqdns-proxy-port"
   322  
   323  	// ToFQDNsEnablePoller enables proactive polling of DNS names in toFQDNs.matchName rules.
   324  	ToFQDNsEnablePoller = "tofqdns-enable-poller"
   325  
   326  	// ToFQDNsEmitPollerEvents controls if poller lookups are sent as monitor events
   327  	ToFQDNsEnablePollerEvents = "tofqdns-enable-poller-events"
   328  
   329  	// ToFQDNsMaxIPsPerHost defines the maximum number of IPs to maintain
   330  	// for each FQDN name in an endpoint's FQDN cache
   331  	ToFQDNsMaxIPsPerHost = "tofqdns-endpoint-max-ip-per-hostname"
   332  
   333  	// ToFQDNsPreCache is a path to a file with DNS cache data to insert into the
   334  	// global cache on startup.
   335  	// The file is not re-read after agent start.
   336  	ToFQDNsPreCache = "tofqdns-pre-cache"
   337  
   338  	// MTUName is the name of the MTU option
   339  	MTUName = "mtu"
   340  
   341  	// DatapathMode is the name of the DatapathMode option
   342  	DatapathMode = "datapath-mode"
   343  
   344  	// IpvlanMasterDevice is the name of the IpvlanMasterDevice option
   345  	IpvlanMasterDevice = "ipvlan-master-device"
   346  
   347  	// EnableHostReachableServices is the name of the EnableHostReachableServices option
   348  	EnableHostReachableServices = "enable-host-reachable-services"
   349  
   350  	// HostReachableServicesProtos is the name of the HostReachableServicesProtos option
   351  	HostReachableServicesProtos = "host-reachable-services-protos"
   352  
   353  	// HostServicesTCP is the name of EnableHostServicesTCP config
   354  	HostServicesTCP = "tcp"
   355  
   356  	// HostServicesUDP is the name of EnableHostServicesUDP config
   357  	HostServicesUDP = "udp"
   358  
   359  	// TunnelName is the name of the Tunnel option
   360  	TunnelName = "tunnel"
   361  
   362  	// SingleClusterRouteName is the name of the SingleClusterRoute option
   363  	//
   364  	// SingleClusterRoute enables use of a single route covering the entire
   365  	// cluster CIDR to point to the cilium_host interface instead of using
   366  	// a separate route for each cluster node CIDR. This option is not
   367  	// compatible with Tunnel=TunnelDisabled
   368  	SingleClusterRouteName = "single-cluster-route"
   369  
   370  	// MonitorAggregationName specifies the MonitorAggregationLevel on the
   371  	// comandline.
   372  	MonitorAggregationName = "monitor-aggregation"
   373  
   374  	// ciliumEnvPrefix is the prefix used for environment variables
   375  	ciliumEnvPrefix = "CILIUM_"
   376  
   377  	// ClusterName is the name of the ClusterName option
   378  	ClusterName = "cluster-name"
   379  
   380  	// ClusterIDName is the name of the ClusterID option
   381  	ClusterIDName = "cluster-id"
   382  
   383  	// ClusterIDMin is the minimum value of the cluster ID
   384  	ClusterIDMin = 0
   385  
   386  	// ClusterIDMax is the maximum value of the cluster ID
   387  	ClusterIDMax = 255
   388  
   389  	// ClusterMeshConfigName is the name of the ClusterMeshConfig option
   390  	ClusterMeshConfigName = "clustermesh-config"
   391  
   392  	// BPFCompileDebugName is the name of the option to enable BPF compiliation debugging
   393  	BPFCompileDebugName = "bpf-compile-debug"
   394  
   395  	// CTMapEntriesGlobalTCP retains the Cilium 1.2 (or earlier) size to
   396  	// minimize disruption during upgrade.
   397  	CTMapEntriesGlobalTCPDefault = 1000000
   398  	CTMapEntriesGlobalAnyDefault = 2 << 17 // 256Ki
   399  	CTMapEntriesGlobalTCPName    = "bpf-ct-global-tcp-max"
   400  	CTMapEntriesGlobalAnyName    = "bpf-ct-global-any-max"
   401  
   402  	// CTMapEntriesTimeout* name option and default value mappings
   403  	CTMapEntriesTimeoutSYNName    = "bpf-ct-timeout-regular-tcp-syn"
   404  	CTMapEntriesTimeoutFINName    = "bpf-ct-timeout-regular-tcp-fin"
   405  	CTMapEntriesTimeoutTCPName    = "bpf-ct-timeout-regular-tcp"
   406  	CTMapEntriesTimeoutAnyName    = "bpf-ct-timeout-regular-any"
   407  	CTMapEntriesTimeoutSVCTCPName = "bpf-ct-timeout-service-tcp"
   408  	CTMapEntriesTimeoutSVCAnyName = "bpf-ct-timeout-service-any"
   409  
   410  	// NATMapEntriesGlobalDefault holds the default size of the NAT map
   411  	// and is 2/3 of the full CT size as a heuristic
   412  	NATMapEntriesGlobalDefault = int((CTMapEntriesGlobalTCPDefault + CTMapEntriesGlobalAnyDefault) * 2 / 3)
   413  
   414  	// LimitTableMin defines the minimum CT or NAT table limit
   415  	LimitTableMin = 1 << 10 // 1Ki entries
   416  
   417  	// LimitTableMax defines the maximum CT or NAT table limit
   418  	LimitTableMax = 1 << 24 // 16Mi entries (~1GiB of entries per map)
   419  
   420  	// NATMapEntriesGlobalName configures max entries for BPF NAT table
   421  	NATMapEntriesGlobalName = "bpf-nat-global-max"
   422  
   423  	// PolicyMapEntriesName configures max entries for BPF policymap.
   424  	PolicyMapEntriesName = "bpf-policy-map-max"
   425  
   426  	// LogSystemLoadConfigName is the name of the option to enable system
   427  	// load loggging
   428  	LogSystemLoadConfigName = "log-system-load"
   429  
   430  	// PrependIptablesChainsName is the name of the option to enable
   431  	// prepending iptables chains instead of appending
   432  	PrependIptablesChainsName = "prepend-iptables-chains"
   433  
   434  	// DisableCiliumEndpointCRDName is the name of the option to disable
   435  	// use of the CEP CRD
   436  	DisableCiliumEndpointCRDName = "disable-endpoint-crd"
   437  
   438  	// DisableK8sServices disables east-west K8s load balancing by cilium
   439  	DisableK8sServices = "disable-k8s-services"
   440  
   441  	// MaxCtrlIntervalName and MaxCtrlIntervalNameEnv allow configuration
   442  	// of MaxControllerInterval.
   443  	MaxCtrlIntervalName = "max-controller-interval"
   444  
   445  	// SockopsEnableName is the name of the option to enable sockops
   446  	SockopsEnableName = "sockops-enable"
   447  
   448  	// K8sNamespaceName is the name of the K8sNamespace option
   449  	K8sNamespaceName = "k8s-namespace"
   450  
   451  	// EnableIPv4Name is the name of the option to enable IPv4 support
   452  	EnableIPv4Name = "enable-ipv4"
   453  
   454  	// LegacyDisableIPv4Name is the name of the legacy option to disable
   455  	// IPv4 support
   456  	LegacyDisableIPv4Name = "disable-ipv4"
   457  
   458  	// EnableIPv6Name is the name of the option to enable IPv6 support
   459  	EnableIPv6Name = "enable-ipv6"
   460  
   461  	// MonitorQueueSizeName is the name of the option MonitorQueueSize
   462  	MonitorQueueSizeName = "monitor-queue-size"
   463  
   464  	//FQDNRejectResponseCode is the name for the option for dns-proxy reject response code
   465  	FQDNRejectResponseCode = "tofqdns-dns-reject-response-code"
   466  
   467  	// FQDNProxyDenyWithNameError is useful when stub resolvers, like the one
   468  	// in Alpine Linux's libc (musl), treat a REFUSED as a resolution error.
   469  	// This happens when trying a DNS search list, as in kubernetes, and breaks
   470  	// even whitelisted DNS names.
   471  	FQDNProxyDenyWithNameError = "nameError"
   472  
   473  	// FQDNProxyDenyWithRefused is the response code for Domain refused. It is
   474  	// the default for denied DNS requests.
   475  	FQDNProxyDenyWithRefused = "refused"
   476  
   477  	// FQDNProxyResponseMaxDelay is the maximum time the proxy holds back a response
   478  	FQDNProxyResponseMaxDelay = "tofqdns-proxy-response-max-delay"
   479  
   480  	// PreAllocateMapsName is the name of the option PreAllocateMaps
   481  	PreAllocateMapsName = "preallocate-bpf-maps"
   482  
   483  	// EnableXTSocketFallbackName is the name of the EnableXTSocketFallback option
   484  	EnableXTSocketFallbackName = "enable-xt-socket-fallback"
   485  
   486  	// EnableAutoDirectRoutingName is the name for the EnableAutoDirectRouting option
   487  	EnableAutoDirectRoutingName = "auto-direct-node-routes"
   488  
   489  	// EnableIPSecName is the name of the option to enable IPSec
   490  	EnableIPSecName = "enable-ipsec"
   491  
   492  	// IPSecKeyFileName is the name of the option for ipsec key file
   493  	IPSecKeyFileName = "ipsec-key-file"
   494  
   495  	// KVstoreLeaseTTL is the time-to-live for lease in kvstore.
   496  	KVstoreLeaseTTL = "kvstore-lease-ttl"
   497  
   498  	// KVstorePeriodicSync is the time interval in which periodic
   499  	// synchronization with the kvstore occurs
   500  	KVstorePeriodicSync = "kvstore-periodic-sync"
   501  
   502  	// KVstoreConnectivityTimeout is the timeout when performing kvstore operations
   503  	KVstoreConnectivityTimeout = "kvstore-connectivity-timeout"
   504  
   505  	// IPAllocationTimeout is the timeout when allocating CIDRs
   506  	IPAllocationTimeout = "ip-allocation-timeout"
   507  
   508  	// IdentityChangeGracePeriod is the name of the
   509  	// IdentityChangeGracePeriod option
   510  	IdentityChangeGracePeriod = "identity-change-grace-period"
   511  
   512  	// EnableHealthChecking is the name of the EnableHealthChecking option
   513  	EnableHealthChecking = "enable-health-checking"
   514  
   515  	// EnableEndpointHealthChecking is the name of the EnableEndpointHealthChecking option
   516  	EnableEndpointHealthChecking = "enable-endpoint-health-checking"
   517  
   518  	// PolicyQueueSize is the size of the queues utilized by the policy
   519  	// repository.
   520  	PolicyQueueSize = "policy-queue-size"
   521  
   522  	// EndpointQueueSize is the size of the EventQueue per-endpoint.
   523  	EndpointQueueSize = "endpoint-queue-size"
   524  
   525  	// SelectiveRegeneration specifies whether only the endpoints which policy
   526  	// changes select should be regenerated upon policy changes.
   527  	SelectiveRegeneration = "enable-selective-regeneration"
   528  
   529  	// K8sEventHandover is the name of the K8sEventHandover option
   530  	K8sEventHandover = "enable-k8s-event-handover"
   531  
   532  	// Metrics represents the metrics subsystem that Cilium should expose
   533  	// to prometheus.
   534  	Metrics = "metrics"
   535  
   536  	// LoopbackIPv4 is the address to use for service loopback SNAT
   537  	LoopbackIPv4 = "ipv4-service-loopback-address"
   538  
   539  	// EndpointInterfaceNamePrefix is the prefix name of the interface
   540  	// names shared by all endpoints
   541  	EndpointInterfaceNamePrefix = "endpoint-interface-name-prefix"
   542  
   543  	// BlacklistConflictingRoutes removes all IPs from the IPAM block if a
   544  	// local route not owned by Cilium conflicts with it
   545  	BlacklistConflictingRoutes = "blacklist-conflicting-routes"
   546  
   547  	// ForceLocalPolicyEvalAtSource forces a policy decision at the source
   548  	// endpoint for all local communication
   549  	ForceLocalPolicyEvalAtSource = "force-local-policy-eval-at-source"
   550  
   551  	// SkipCRDCreation specifies whether the CustomResourceDefinition will be
   552  	// created by the daemon
   553  	SkipCRDCreation = "skip-crd-creation"
   554  
   555  	// EnableEndpointRoutes enables use of per endpoint routes
   556  	EnableEndpointRoutes = "enable-endpoint-routes"
   557  
   558  	// ExcludeLocalAddress excludes certain addresses to be recognized as a
   559  	// local address
   560  	ExcludeLocalAddress = "exclude-local-address"
   561  
   562  	// IPv4PodSubnets A list of IPv4 subnets that pods may be
   563  	// assigned from. Used with CNI chaining where IPs are not directly managed
   564  	// by Cilium.
   565  	IPv4PodSubnets = "ipv4-pod-subnets"
   566  
   567  	// IPv6PodSubnets A list of IPv6 subnets that pods may be
   568  	// assigned from. Used with CNI chaining where IPs are not directly managed
   569  	// by Cilium.
   570  	IPv6PodSubnets = "ipv6-pod-subnets"
   571  
   572  	// IPAM is the IPAM method to use
   573  	IPAM = "ipam"
   574  
   575  	// IPAMCRD is the value to select the CRD-backed IPAM plugin for
   576  	// option.IPAM
   577  	IPAMCRD = "crd"
   578  
   579  	// IPAMENI is the value to select the AWS ENI IPAM plugin for option.IPAM
   580  	IPAMENI = "eni"
   581  
   582  	// AWSClientQPSLimit is the queries per second limit for the AWS client used by AWS ENI IPAM
   583  	AWSClientQPSLimit = "aws-client-qps"
   584  
   585  	// AWSClientBurst is the burst value allowed for the AWS client used by the AWS ENI IPAM
   586  	AWSClientBurst = "aws-client-burst"
   587  
   588  	// K8sClientQPSLimit is the queries per second limit for the K8s client. Defaults to k8s client defaults.
   589  	K8sClientQPSLimit = "k8s-client-qps"
   590  
   591  	// K8sClientBurst is the burst value allowed for the K8s client. Defaults to k8s client defaults.
   592  	K8sClientBurst = "k8s-client-burst"
   593  
   594  	// AutoCreateCiliumNodeResource enables automatic creation of a
   595  	// CiliumNode resource for the local node
   596  	AutoCreateCiliumNodeResource = "auto-create-cilium-node-resource"
   597  
   598  	// IPv4NativeRoutingCIDR describes a CIDR in which pod IPs are routable
   599  	IPv4NativeRoutingCIDR = "native-routing-cidr"
   600  
   601  	// EgressMasqueradeInterfaces is the selector used to select interfaces
   602  	// subject to egress masquerading
   603  	EgressMasqueradeInterfaces = "egress-masquerade-interfaces"
   604  
   605  	// DeprecatedEnableLegacyServices enables the legacy services
   606  	DeprecatedEnableLegacyServices = "enable-legacy-services"
   607  
   608  	// PolicyTriggerInterval is the amount of time between triggers of policy
   609  	// updates are invoked.
   610  	PolicyTriggerInterval = "policy-trigger-interval"
   611  
   612  	// IdentityAllocationMode specifies what mode to use for identity
   613  	// allocation
   614  	IdentityAllocationMode = "identity-allocation-mode"
   615  
   616  	// IdentityAllocationModeKVstore enables use of a key-value store such
   617  	// as etcd or consul for identity allocation
   618  	IdentityAllocationModeKVstore = "kvstore"
   619  
   620  	// IdentityAllocationModeCRD enables use of Kubernetes CRDs for
   621  	// identity allocation
   622  	IdentityAllocationModeCRD = "crd"
   623  
   624  	// DisableCNPStatusUpdates disables updating of CNP NodeStatus in the CNP
   625  	// CRD.
   626  	DisableCNPStatusUpdates = "disable-cnp-status-updates"
   627  )
   628  
   629  // Default string arguments
   630  var (
   631  	FQDNRejectOptions = []string{FQDNProxyDenyWithNameError, FQDNProxyDenyWithRefused}
   632  
   633  	// ContainerRuntimeAuto is the configuration for autodetecting the
   634  	// container runtime backends that Cilium should use.
   635  	ContainerRuntimeAuto = []string{"auto"}
   636  )
   637  
   638  // Available option for DaemonConfig.DatapathMode
   639  const (
   640  	// DatapathModeVeth specifies veth datapath mode (i.e. containers are
   641  	// attached to a network via veth pairs)
   642  	DatapathModeVeth = "veth"
   643  
   644  	// DatapathModeIpvlan specifies ipvlan datapath mode
   645  	DatapathModeIpvlan = "ipvlan"
   646  )
   647  
   648  // Available option for DaemonConfig.Tunnel
   649  const (
   650  	// TunnelVXLAN specifies VXLAN encapsulation
   651  	TunnelVXLAN = "vxlan"
   652  
   653  	// TunnelGeneve specifies Geneve encapsulation
   654  	TunnelGeneve = "geneve"
   655  
   656  	// TunnelDisabled specifies to disable encapsulation
   657  	TunnelDisabled = "disabled"
   658  )
   659  
   660  // Available option for DaemonConfig.Ipvlan.OperationMode
   661  const (
   662  	// OperationModeL3S will respect iptables rules e.g. set up for masquerading
   663  	OperationModeL3S = "L3S"
   664  
   665  	// OperationModeL3 will bypass iptables rules on the host
   666  	OperationModeL3 = "L3"
   667  )
   668  
   669  // Envoy option names
   670  const (
   671  	// HTTP403Message specifies the response body for 403 responses, defaults to "Access denied"
   672  	HTTP403Message = "http-403-msg"
   673  
   674  	// HTTPRequestTimeout specifies the time in seconds after which forwarded requests time out
   675  	HTTPRequestTimeout = "http-request-timeout"
   676  
   677  	// HTTPIdleTimeout spcifies the time in seconds if http stream being idle after which the
   678  	// request times out
   679  	HTTPIdleTimeout = "http-idle-timeout"
   680  
   681  	// HTTPMaxGRPCTimeout specifies the maximum time in seconds that limits the values of
   682  	// "grpc-timeout" headers being honored.
   683  	HTTPMaxGRPCTimeout = "http-max-grpc-timeout"
   684  
   685  	// HTTPRetryCount specifies the number of retries performed after a forwarded request fails
   686  	HTTPRetryCount = "http-retry-count"
   687  
   688  	// HTTPRetryTimeout is the time in seconds before an uncompleted request is retried.
   689  	HTTPRetryTimeout = "http-retry-timeout"
   690  
   691  	// ProxyConnectTimeout specifies the time in seconds after which a TCP connection attempt
   692  	// is considered timed out
   693  	ProxyConnectTimeout = "proxy-connect-timeout"
   694  
   695  	// ReadCNIConfiguration reads the CNI configuration file and extracts
   696  	// Cilium relevant information. This can be used to pass per node
   697  	// configuration to Cilium.
   698  	ReadCNIConfiguration = "read-cni-conf"
   699  
   700  	// WriteCNIConfigurationWhenReady writes the CNI configuration to the
   701  	// specified location once the agent is ready to serve requests. This
   702  	// allows to keep a Kubernetes node NotReady until Cilium is up and
   703  	// running and able to schedule endpoints.
   704  	WriteCNIConfigurationWhenReady = "write-cni-conf-when-ready"
   705  )
   706  
   707  const (
   708  	// NodePortMinDefault is the minimal port to listen for NodePort requests
   709  	NodePortMinDefault = 30000
   710  
   711  	// NodePortMaxDefault is the maximum port to listen for NodePort requests
   712  	NodePortMaxDefault = 32767
   713  )
   714  
   715  // GetTunnelModes returns the list of all tunnel modes
   716  func GetTunnelModes() string {
   717  	return fmt.Sprintf("%s, %s, %s", TunnelVXLAN, TunnelGeneve, TunnelDisabled)
   718  }
   719  
   720  // getEnvName returns the environment variable to be used for the given option name.
   721  func getEnvName(option string) string {
   722  	under := strings.Replace(option, "-", "_", -1)
   723  	upper := strings.ToUpper(under)
   724  	return ciliumEnvPrefix + upper
   725  }
   726  
   727  // RegisteredOptions maps all options that are bind to viper.
   728  var RegisteredOptions = map[string]struct{}{}
   729  
   730  // BindEnv binds the option name with an deterministic generated environment
   731  // variable which s based on the given optName. If the same optName is bind
   732  // more than 1 time, this function panics.
   733  func BindEnv(optName string) {
   734  	registerOpt(optName)
   735  	viper.BindEnv(optName, getEnvName(optName))
   736  }
   737  
   738  // BindEnvWithLegacyEnvFallback binds the given option name with either the same
   739  // environment variable as BindEnv, if it's set, or with the given legacyEnvName.
   740  //
   741  // The function is used to work around the viper.BindEnv limitation that only
   742  // one environment variable can be bound for an option, and we need multiple
   743  // environment variables due to backward compatibility reasons.
   744  func BindEnvWithLegacyEnvFallback(optName, legacyEnvName string) {
   745  	registerOpt(optName)
   746  
   747  	envName := getEnvName(optName)
   748  	if os.Getenv(envName) == "" {
   749  		envName = legacyEnvName
   750  	}
   751  
   752  	viper.BindEnv(optName, envName)
   753  }
   754  
   755  func registerOpt(optName string) {
   756  	_, ok := RegisteredOptions[optName]
   757  	if ok || optName == "" {
   758  		panic(fmt.Errorf("option already registered: %s", optName))
   759  	}
   760  	RegisteredOptions[optName] = struct{}{}
   761  }
   762  
   763  // LogRegisteredOptions logs all options that where bind to viper.
   764  func LogRegisteredOptions(entry *logrus.Entry) {
   765  	keys := make([]string, 0, len(RegisteredOptions))
   766  	for k := range RegisteredOptions {
   767  		keys = append(keys, k)
   768  	}
   769  	sort.Strings(keys)
   770  	for _, k := range keys {
   771  		entry.Infof("  --%s='%s'", k, viper.GetString(k))
   772  	}
   773  }
   774  
   775  // IpvlanConfig is the configuration used by Daemon when in ipvlan mode.
   776  type IpvlanConfig struct {
   777  	MasterDeviceIndex int
   778  	OperationMode     string
   779  }
   780  
   781  // DaemonConfig is the configuration used by Daemon.
   782  type DaemonConfig struct {
   783  	BpfDir           string     // BPF template files directory
   784  	LibDir           string     // Cilium library files directory
   785  	RunDir           string     // Cilium runtime directory
   786  	NAT46Prefix      *net.IPNet // NAT46 IPv6 Prefix
   787  	Device           string     // Receive device
   788  	DevicePreFilter  string     // XDP device
   789  	ModePreFilter    string     // XDP mode, values: { native | generic }
   790  	HostV4Addr       net.IP     // Host v4 address of the snooping device
   791  	HostV6Addr       net.IP     // Host v6 address of the snooping device
   792  	LBInterface      string     // Set with name of the interface to loadbalance packets from
   793  	EncryptInterface string     // Set with name of network facing interface to encrypt
   794  	EncryptNode      bool       // Set to true for encrypting node IP traffic
   795  	Workloads        []string   // List of Workloads set by the user to used by cilium.
   796  
   797  	Ipvlan IpvlanConfig // Ipvlan related configuration
   798  
   799  	DatapathMode string // Datapath mode
   800  	Tunnel       string // Tunnel mode
   801  
   802  	DryMode bool // Do not create BPF maps, devices, ..
   803  
   804  	// RestoreState enables restoring the state from previous running daemons.
   805  	RestoreState bool
   806  
   807  	// EnableHostIPRestore enables restoring the host IPs based on state
   808  	// left behind by previous Cilium runs.
   809  	EnableHostIPRestore bool
   810  
   811  	KeepConfig    bool // Keep configuration of existing endpoints when starting up.
   812  	KeepTemplates bool // Do not overwrite the template files
   813  
   814  	// AllowLocalhost defines when to allows the local stack to local endpoints
   815  	// values: { auto | always | policy }
   816  	AllowLocalhost string
   817  
   818  	// StateDir is the directory where runtime state of endpoints is stored
   819  	StateDir string
   820  
   821  	// Options changeable at runtime
   822  	Opts *IntOptions
   823  
   824  	// Mutex for serializing configuration updates to the daemon.
   825  	ConfigPatchMutex lock.RWMutex
   826  
   827  	// Monitor contains the configuration for the node monitor.
   828  	Monitor *models.MonitorStatus
   829  
   830  	// AccessLog is the path to the access log of supported L7 requests observed.
   831  	AccessLog string
   832  
   833  	// AgentLabels contains additional labels to identify this agent in monitor events.
   834  	AgentLabels []string
   835  
   836  	// IPv6ClusterAllocCIDR is the base CIDR used to allocate IPv6 node
   837  	// CIDRs if allocation is not performed by an orchestration system
   838  	IPv6ClusterAllocCIDR string
   839  
   840  	// IPv6ClusterAllocCIDRBase is derived from IPv6ClusterAllocCIDR and
   841  	// contains the CIDR without the mask, e.g. "fdfd::1/64" -> "fdfd::"
   842  	//
   843  	// This variable should never be written to, it is initialized via
   844  	// DaemonConfig.Validate()
   845  	IPv6ClusterAllocCIDRBase string
   846  
   847  	// K8sRequireIPv4PodCIDR requires the k8s node resource to specify the
   848  	// IPv4 PodCIDR. Cilium will block bootstrapping until the information
   849  	// is available.
   850  	K8sRequireIPv4PodCIDR bool
   851  
   852  	// K8sRequireIPv6PodCIDR requires the k8s node resource to specify the
   853  	// IPv6 PodCIDR. Cilium will block bootstrapping until the information
   854  	// is available.
   855  	K8sRequireIPv6PodCIDR bool
   856  
   857  	// K8sServiceCacheSize is the service cache size for cilium k8s package.
   858  	K8sServiceCacheSize uint
   859  
   860  	// K8sForceJSONPatch when set, uses JSON Patch to update CNP and CEP
   861  	// status in kube-apiserver.
   862  	K8sForceJSONPatch bool
   863  
   864  	// K8sWatcherQueueSize is the queue size used to serialize each k8s event
   865  	// type.
   866  	K8sWatcherQueueSize uint
   867  
   868  	// MTU is the maximum transmission unit of the underlying network
   869  	MTU int
   870  
   871  	// ClusterName is the name of the cluster
   872  	ClusterName string
   873  
   874  	// ClusterID is the unique identifier of the cluster
   875  	ClusterID int
   876  
   877  	// ClusterMeshConfig is the path to the clustermesh configuration directory
   878  	ClusterMeshConfig string
   879  
   880  	// CTMapEntriesGlobalTCP is the maximum number of conntrack entries
   881  	// allowed in each TCP CT table for IPv4/IPv6.
   882  	CTMapEntriesGlobalTCP int
   883  
   884  	// CTMapEntriesGlobalAny is the maximum number of conntrack entries
   885  	// allowed in each non-TCP CT table for IPv4/IPv6.
   886  	CTMapEntriesGlobalAny int
   887  
   888  	// CTMapEntriesTimeout* values configured by the user.
   889  	CTMapEntriesTimeoutTCP    time.Duration
   890  	CTMapEntriesTimeoutAny    time.Duration
   891  	CTMapEntriesTimeoutSVCTCP time.Duration
   892  	CTMapEntriesTimeoutSVCAny time.Duration
   893  	CTMapEntriesTimeoutSYN    time.Duration
   894  	CTMapEntriesTimeoutFIN    time.Duration
   895  
   896  	// NATMapEntriesGlobal is the maximum number of NAT mappings allowed
   897  	// in the BPF NAT table
   898  	NATMapEntriesGlobal int
   899  
   900  	// PolicyMapMaxEntries is the maximum number of peer identities that an
   901  	// endpoint may allow traffic to exchange traffic with.
   902  	PolicyMapMaxEntries int
   903  
   904  	// DisableCiliumEndpointCRD disables the use of CiliumEndpoint CRD
   905  	DisableCiliumEndpointCRD bool
   906  
   907  	// MaxControllerInterval is the maximum value for a controller's
   908  	// RunInterval. Zero means unlimited.
   909  	MaxControllerInterval int
   910  
   911  	// UseSingleClusterRoute specifies whether to use a single cluster route
   912  	// instead of per-node routes.
   913  	UseSingleClusterRoute bool
   914  
   915  	// HTTP403Message is the error message to return when a HTTP 403 is returned
   916  	// by the proxy, if L7 policy is configured.
   917  	HTTP403Message string
   918  
   919  	// HTTPRequestTimeout is the time in seconds after which Envoy responds with an
   920  	// error code on a request that has not yet completed. This needs to be longer
   921  	// than the HTTPIdleTimeout
   922  	HTTPRequestTimeout int
   923  
   924  	// HTTPIdleTimeout is the time in seconds of a HTTP stream having no traffic after
   925  	// which Envoy responds with an error code. This needs to be shorter than the
   926  	// HTTPRequestTimeout
   927  	HTTPIdleTimeout int
   928  
   929  	// HTTPMaxGRPCTimeout is the upper limit to which "grpc-timeout" headers in GRPC
   930  	// requests are honored by Envoy. If 0 there is no limit. GRPC requests are not
   931  	// bound by the HTTPRequestTimeout, but ARE affected by the idle timeout!
   932  	HTTPMaxGRPCTimeout int
   933  
   934  	// HTTPRetryCount is the upper limit on how many times Envoy retries failed requests.
   935  	HTTPRetryCount int
   936  
   937  	// HTTPRetryTimeout is the time in seconds before an uncompleted request is retried.
   938  	HTTPRetryTimeout int
   939  
   940  	// ProxyConnectTimeout is the time in seconds after which Envoy considers a TCP
   941  	// connection attempt to have timed out.
   942  	ProxyConnectTimeout int
   943  
   944  	// BPFCompilationDebug specifies whether to compile BPF programs compilation
   945  	// debugging enabled.
   946  	BPFCompilationDebug bool
   947  
   948  	// EnvoyLogPath specifies where to store the Envoy proxy logs when Envoy
   949  	// runs in the same container as Cilium.
   950  	EnvoyLogPath string
   951  
   952  	// EnableSockOps specifies whether to enable sockops (socket lookup).
   953  	SockopsEnable bool
   954  
   955  	// PrependIptablesChains is the name of the option to enable prepending
   956  	// iptables chains instead of appending
   957  	PrependIptablesChains bool
   958  
   959  	// IPTablesLockTimeout defines the "-w" iptables option when the
   960  	// iptables CLI is directly invoked from the Cilium agent.
   961  	IPTablesLockTimeout time.Duration
   962  
   963  	IPTablesLockWaitInterval string
   964  
   965  	// K8sNamespace is the name of the namespace in which Cilium is
   966  	// deployed in when running in Kubernetes mode
   967  	K8sNamespace string
   968  
   969  	// EnableIPv4 is true when IPv4 is enabled
   970  	EnableIPv4 bool
   971  
   972  	// EnableIPv6 is true when IPv6 is enabled
   973  	EnableIPv6 bool
   974  
   975  	// EnableL7Proxy is the option to enable L7 proxy
   976  	EnableL7Proxy bool
   977  
   978  	// EnableIPSec is true when IPSec is enabled
   979  	EnableIPSec bool
   980  
   981  	// IPSec key file for stored keys
   982  	IPSecKeyFile string
   983  
   984  	// MonitorQueueSize is the size of the monitor event queue
   985  	MonitorQueueSize int
   986  
   987  	// CLI options
   988  
   989  	BPFRoot                       string
   990  	CGroupRoot                    string
   991  	BPFCompileDebug               string
   992  	ConfigFile                    string
   993  	ConfigDir                     string
   994  	ContainerRuntimeEndpoint      map[string]string
   995  	Debug                         bool
   996  	DebugVerbose                  []string
   997  	DisableConntrack              bool
   998  	DisableK8sServices            bool
   999  	EnableHostReachableServices   bool
  1000  	EnableHostServicesTCP         bool
  1001  	EnableHostServicesUDP         bool
  1002  	DockerEndpoint                string
  1003  	EnablePolicy                  string
  1004  	EnableTracing                 bool
  1005  	EnvoyLog                      string
  1006  	DisableEnvoyVersionCheck      bool
  1007  	FixedIdentityMapping          map[string]string
  1008  	FixedIdentityMappingValidator func(val string) (string, error)
  1009  	IPv4ClusterCIDRMaskSize       int
  1010  	IPv4Range                     string
  1011  	IPv6Range                     string
  1012  	IPv4ServiceRange              string
  1013  	IPv6ServiceRange              string
  1014  	K8sAPIServer                  string
  1015  	K8sKubeConfigPath             string
  1016  	K8sWatcherEndpointSelector    string
  1017  	KVStore                       string
  1018  	KVStoreOpt                    map[string]string
  1019  	LabelPrefixFile               string
  1020  	Labels                        []string
  1021  	LogDriver                     []string
  1022  	LogOpt                        map[string]string
  1023  	Logstash                      bool
  1024  	LogSystemLoadConfig           bool
  1025  	NAT46Range                    string
  1026  
  1027  	// Masquerade specifies whether or not to masquerade packets from endpoints
  1028  	// leaving the host.
  1029  	Masquerade             bool
  1030  	InstallIptRules        bool
  1031  	MonitorAggregation     string
  1032  	PreAllocateMaps        bool
  1033  	IPv6NodeAddr           string
  1034  	IPv4NodeAddr           string
  1035  	SidecarHTTPProxy       bool
  1036  	SidecarIstioProxyImage string
  1037  	SocketPath             string
  1038  	TracePayloadlen        int
  1039  	Version                string
  1040  	PProf                  bool
  1041  	PrometheusServeAddr    string
  1042  	CMDRefDir              string
  1043  	ToFQDNsMinTTL          int
  1044  
  1045  	// ToFQDNsProxyPort is the user-configured global, shared, DNS listen port used
  1046  	// by the DNS Proxy. Both UDP and TCP are handled on the same port. When it
  1047  	// is 0 a random port will be assigned, and can be obtained from
  1048  	// DefaultDNSProxy below.
  1049  	ToFQDNsProxyPort int
  1050  
  1051  	// ToFQDNsEnablePoller enables the DNS poller that polls toFQDNs.matchName
  1052  	ToFQDNsEnablePoller bool
  1053  
  1054  	// ToFQDNsEnablePollerEvents controls sending a monitor event for each DNS
  1055  	// response the DNS poller sees
  1056  	ToFQDNsEnablePollerEvents bool
  1057  
  1058  	// ToFQDNsMaxIPsPerHost defines the maximum number of IPs to maintain
  1059  	// for each FQDN name in an endpoint's FQDN cache
  1060  	ToFQDNsMaxIPsPerHost int
  1061  
  1062  	// FQDNRejectResponse is the dns-proxy response for invalid dns-proxy request
  1063  	FQDNRejectResponse string
  1064  
  1065  	// FQDNProxyResponseMaxDelay The maximum time the DNS proxy holds an allowed
  1066  	// DNS response before sending it along. Responses are sent as soon as the
  1067  	// datapath is updated with the new IP information.
  1068  	FQDNProxyResponseMaxDelay time.Duration
  1069  
  1070  	// Path to a file with DNS cache data to preload on startup
  1071  	ToFQDNsPreCache string
  1072  
  1073  	// HostDevice will be device used by Cilium to connect to the outside world.
  1074  	HostDevice string
  1075  
  1076  	// FlannelMasterDevice installs a BPF program in the given interface
  1077  	// to allow for policy enforcement mode on top of flannel.
  1078  	FlannelMasterDevice string
  1079  
  1080  	// FlannelUninstallOnExit removes the BPF programs that were installed by
  1081  	// Cilium on all interfaces created by the flannel.
  1082  	FlannelUninstallOnExit bool
  1083  
  1084  	// FlannelManageExistingContainers sets if Cilium should install the BPF
  1085  	// programs on already running interfaces created by flannel. Require
  1086  	// Cilium to be running in the hostPID.
  1087  	FlannelManageExistingContainers bool
  1088  
  1089  	// EnableXTSocketFallback allows disabling of kernel's ip_early_demux
  1090  	// sysctl option if `xt_socket` kernel module is not available.
  1091  	EnableXTSocketFallback bool
  1092  
  1093  	// EnableAutoDirectRouting enables installation of direct routes to
  1094  	// other nodes when available
  1095  	EnableAutoDirectRouting bool
  1096  
  1097  	// EnableHealthChecking enables health checking between nodes and
  1098  	// health endpoints
  1099  	EnableHealthChecking bool
  1100  
  1101  	// EnableEndpointHealthChecking enables health checking between virtual
  1102  	// health endpoints
  1103  	EnableEndpointHealthChecking bool
  1104  
  1105  	// KVstoreKeepAliveInterval is the interval in which the lease is being
  1106  	// renewed. This must be set to a value lesser than the LeaseTTL ideally
  1107  	// by a factor of 3.
  1108  	KVstoreKeepAliveInterval time.Duration
  1109  
  1110  	// KVstoreLeaseTTL is the time-to-live for kvstore lease.
  1111  	KVstoreLeaseTTL time.Duration
  1112  
  1113  	// KVstorePeriodicSync is the time interval in which periodic
  1114  	// synchronization with the kvstore occurs
  1115  	KVstorePeriodicSync time.Duration
  1116  
  1117  	// KVstoreConnectivityTimeout is the timeout when performing kvstore operations
  1118  	KVstoreConnectivityTimeout time.Duration
  1119  
  1120  	// IPAllocationTimeout is the timeout when allocating CIDRs
  1121  	IPAllocationTimeout time.Duration
  1122  
  1123  	// IdentityChangeGracePeriod is the grace period that needs to pass
  1124  	// before an endpoint that has changed its identity will start using
  1125  	// that new identity. During the grace period, the new identity has
  1126  	// already been allocated and other nodes in the cluster have a chance
  1127  	// to whitelist the new upcoming identity of the endpoint.
  1128  	IdentityChangeGracePeriod time.Duration
  1129  
  1130  	// PolicyQueueSize is the size of the queues for the policy repository.
  1131  	// A larger queue means that more events related to policy can be buffered.
  1132  	PolicyQueueSize int
  1133  
  1134  	// EndpointQueueSize is the size of the EventQueue per-endpoint. A larger
  1135  	// queue means that more events can be buffered per-endpoint. This is useful
  1136  	// in the case where a cluster might be under high load for endpoint-related
  1137  	// events, specifically those which cause many regenerations.
  1138  	EndpointQueueSize int
  1139  
  1140  	// SelectiveRegeneration, when true, enables the functionality to only
  1141  	// regenerate endpoints which are selected by the policy rules that have
  1142  	// been changed (added, deleted, or updated). If false, then all endpoints
  1143  	// are regenerated upon every policy change regardless of the scope of the
  1144  	// policy change.
  1145  	SelectiveRegeneration bool
  1146  
  1147  	// ConntrackGCInterval is the connection tracking garbage collection
  1148  	// interval
  1149  	ConntrackGCInterval time.Duration
  1150  
  1151  	// K8sEventHandover enables use of the kvstore to optimize Kubernetes
  1152  	// event handling by listening for k8s events in the operator and
  1153  	// mirroring it into the kvstore for reduced overhead in large
  1154  	// clusters.
  1155  	K8sEventHandover bool
  1156  
  1157  	// MetricsConfig is the configuration set in metrics
  1158  	MetricsConfig metrics.Configuration
  1159  
  1160  	// LoopbackIPv4 is the address to use for service loopback SNAT
  1161  	LoopbackIPv4 string
  1162  
  1163  	// EndpointInterfaceNamePrefix is the prefix name of the interface
  1164  	// names shared by all endpoints
  1165  	EndpointInterfaceNamePrefix string
  1166  
  1167  	// BlacklistConflictingRoutes removes all IPs from the IPAM block if a
  1168  	// local route not owned by Cilium conflicts with it
  1169  	BlacklistConflictingRoutes bool
  1170  
  1171  	// ForceLocalPolicyEvalAtSource forces a policy decision at the source
  1172  	// endpoint for all local communication
  1173  	ForceLocalPolicyEvalAtSource bool
  1174  
  1175  	// SkipCRDCreation disables creation of the CustomResourceDefinition
  1176  	// on daemon startup
  1177  	SkipCRDCreation bool
  1178  
  1179  	// EnableEndpointRoutes enables use of per endpoint routes
  1180  	EnableEndpointRoutes bool
  1181  
  1182  	// Specifies wheather to annotate the kubernetes nodes or not
  1183  	AnnotateK8sNode bool
  1184  
  1185  	// RunMonitorAgent indicates whether to run the monitor agent
  1186  	RunMonitorAgent bool
  1187  
  1188  	// ReadCNIConfiguration reads the CNI configuration file and extracts
  1189  	// Cilium relevant information. This can be used to pass per node
  1190  	// configuration to Cilium.
  1191  	ReadCNIConfiguration string
  1192  
  1193  	// WriteCNIConfigurationWhenReady writes the CNI configuration to the
  1194  	// specified location once the agent is ready to serve requests. This
  1195  	// allows to keep a Kubernetes node NotReady until Cilium is up and
  1196  	// running and able to schedule endpoints.
  1197  	WriteCNIConfigurationWhenReady string
  1198  
  1199  	// EnableNodePort enables k8s NodePort service implementation in BPF
  1200  	EnableNodePort bool
  1201  
  1202  	// NodePortMin is the minimum port address for the NodePort range
  1203  	NodePortMin int
  1204  
  1205  	// NodePortMax is the maximum port address for the NodePort range
  1206  	NodePortMax int
  1207  
  1208  	// excludeLocalAddresses excludes certain addresses to be recognized as
  1209  	// a local address
  1210  	excludeLocalAddresses []*net.IPNet
  1211  
  1212  	// IPv4PodSubnets available subnets to be assign IPv4 addresses to pods from
  1213  	IPv4PodSubnets []*net.IPNet
  1214  
  1215  	// IPv6PodSubnets available subnets to be assign IPv6 addresses to pods from
  1216  	IPv6PodSubnets []*net.IPNet
  1217  
  1218  	// IPAM is the IPAM method to use
  1219  	IPAM string
  1220  
  1221  	// AutoCreateCiliumNodeResource enables automatic creation of a
  1222  	// CiliumNode resource for the local node
  1223  	AutoCreateCiliumNodeResource bool
  1224  
  1225  	// ipv4NativeRoutingCIDR describes a CIDR in which pod IPs are routable
  1226  	ipv4NativeRoutingCIDR *cidr.CIDR
  1227  
  1228  	// EgressMasqueradeInterfaces is the selector used to select interfaces
  1229  	// subject to egress masquerading
  1230  	EgressMasqueradeInterfaces string
  1231  
  1232  	// PolicyTriggerInterval is the amount of time between when policy updates
  1233  	// are triggered.
  1234  	PolicyTriggerInterval time.Duration
  1235  
  1236  	// IdentityAllocationMode specifies what mode to use for identity
  1237  	// allocation
  1238  	IdentityAllocationMode string
  1239  
  1240  	// DisableCNPStatusUpdates disables updating of CNP NodeStatus in the CNP
  1241  	// CRD.
  1242  	DisableCNPStatusUpdates bool
  1243  
  1244  	// AwsInstanceLimitMapping allows overwirting AWS instance limits defined in
  1245  	// pkg/aws/eni/limits.go
  1246  	// e.g. {"a1.medium": "2,4,4", "a2.custom2": "4,5,6"}
  1247  	AwsInstanceLimitMapping map[string]string
  1248  
  1249  	// AwsReleaseExcessIps allows releasing excess free IP addresses from ENI.
  1250  	// Enabling this option reduces waste of IP addresses but may increase
  1251  	// the number of API calls to AWS EC2 service.
  1252  	AwsReleaseExcessIps bool
  1253  }
  1254  
  1255  var (
  1256  	// Config represents the daemon configuration
  1257  	Config = &DaemonConfig{
  1258  		Opts:                         NewIntOptions(&DaemonOptionLibrary),
  1259  		Monitor:                      &models.MonitorStatus{Cpus: int64(runtime.NumCPU()), Npages: 64, Pagesize: int64(os.Getpagesize()), Lost: 0, Unknown: 0},
  1260  		IPv6ClusterAllocCIDR:         defaults.IPv6ClusterAllocCIDR,
  1261  		IPv6ClusterAllocCIDRBase:     defaults.IPv6ClusterAllocCIDRBase,
  1262  		EnableHostIPRestore:          defaults.EnableHostIPRestore,
  1263  		EnableHealthChecking:         defaults.EnableHealthChecking,
  1264  		EnableEndpointHealthChecking: defaults.EnableEndpointHealthChecking,
  1265  		EnableIPv4:                   defaults.EnableIPv4,
  1266  		EnableIPv6:                   defaults.EnableIPv6,
  1267  		EnableL7Proxy:                defaults.EnableL7Proxy,
  1268  		ToFQDNsMaxIPsPerHost:         defaults.ToFQDNsMaxIPsPerHost,
  1269  		KVstorePeriodicSync:          defaults.KVstorePeriodicSync,
  1270  		KVstoreConnectivityTimeout:   defaults.KVstoreConnectivityTimeout,
  1271  		IPAllocationTimeout:          defaults.IPAllocationTimeout,
  1272  		IdentityChangeGracePeriod:    defaults.IdentityChangeGracePeriod,
  1273  		ContainerRuntimeEndpoint:     make(map[string]string),
  1274  		FixedIdentityMapping:         make(map[string]string),
  1275  		KVStoreOpt:                   make(map[string]string),
  1276  		LogOpt:                       make(map[string]string),
  1277  		SelectiveRegeneration:        defaults.SelectiveRegeneration,
  1278  		LoopbackIPv4:                 defaults.LoopbackIPv4,
  1279  		EndpointInterfaceNamePrefix:  defaults.EndpointInterfaceNamePrefix,
  1280  		BlacklistConflictingRoutes:   defaults.BlacklistConflictingRoutes,
  1281  		ForceLocalPolicyEvalAtSource: defaults.ForceLocalPolicyEvalAtSource,
  1282  		EnableEndpointRoutes:         defaults.EnableEndpointRoutes,
  1283  		AnnotateK8sNode:              defaults.AnnotateK8sNode,
  1284  		K8sServiceCacheSize:          defaults.K8sServiceCacheSize,
  1285  		AutoCreateCiliumNodeResource: defaults.AutoCreateCiliumNodeResource,
  1286  		IdentityAllocationMode:       IdentityAllocationModeKVstore,
  1287  	}
  1288  )
  1289  
  1290  // IPv4NativeRoutingCIDR returns the native routing CIDR if configured
  1291  func (c *DaemonConfig) IPv4NativeRoutingCIDR() (cidr *cidr.CIDR) {
  1292  	c.ConfigPatchMutex.RLock()
  1293  	cidr = c.ipv4NativeRoutingCIDR
  1294  	c.ConfigPatchMutex.RUnlock()
  1295  	return
  1296  }
  1297  
  1298  // SetIPv4NativeRoutingCIDR sets the native routing CIDR
  1299  func (c *DaemonConfig) SetIPv4NativeRoutingCIDR(cidr *cidr.CIDR) {
  1300  	c.ConfigPatchMutex.Lock()
  1301  	c.ipv4NativeRoutingCIDR = cidr
  1302  	c.ConfigPatchMutex.Unlock()
  1303  }
  1304  
  1305  // IsExcludedLocalAddress returns true if the specified IP matches one of the
  1306  // excluded local IP ranges
  1307  func (c *DaemonConfig) IsExcludedLocalAddress(ip net.IP) bool {
  1308  	for _, ipnet := range c.excludeLocalAddresses {
  1309  		if ipnet.Contains(ip) {
  1310  			return true
  1311  		}
  1312  	}
  1313  
  1314  	return false
  1315  }
  1316  
  1317  // IsPodSubnetsDefined returns true if encryption subnets should be configured at init time.
  1318  func (c *DaemonConfig) IsPodSubnetsDefined() bool {
  1319  	return len(c.IPv4PodSubnets) > 0 || len(c.IPv6PodSubnets) > 0
  1320  }
  1321  
  1322  // IsLBEnabled returns true if LB should be enabled
  1323  func (c *DaemonConfig) IsLBEnabled() bool {
  1324  	return c.LBInterface != ""
  1325  }
  1326  
  1327  // GetNodeConfigPath returns the full path of the NodeConfigFile.
  1328  func (c *DaemonConfig) GetNodeConfigPath() string {
  1329  	return filepath.Join(c.GetGlobalsDir(), common.NodeConfigFile)
  1330  }
  1331  
  1332  // GetGlobalsDir returns the path for the globals directory.
  1333  func (c *DaemonConfig) GetGlobalsDir() string {
  1334  	return filepath.Join(c.StateDir, "globals")
  1335  }
  1336  
  1337  // WorkloadsEnabled returns true if any workload runtimes are enabled
  1338  func (c *DaemonConfig) WorkloadsEnabled() bool {
  1339  	for _, w := range c.Workloads {
  1340  		if w == "none" {
  1341  			return false
  1342  		}
  1343  	}
  1344  
  1345  	return len(c.Workloads) > 0
  1346  }
  1347  
  1348  // AlwaysAllowLocalhost returns true if the daemon has the option set that
  1349  // localhost can always reach local endpoints
  1350  func (c *DaemonConfig) AlwaysAllowLocalhost() bool {
  1351  	switch c.AllowLocalhost {
  1352  	case AllowLocalhostAlways:
  1353  		return true
  1354  	case AllowLocalhostAuto, AllowLocalhostPolicy:
  1355  		return false
  1356  	default:
  1357  		return false
  1358  	}
  1359  }
  1360  
  1361  // TracingEnabled returns if tracing policy (outlining which rules apply to a
  1362  // specific set of labels) is enabled.
  1363  func (c *DaemonConfig) TracingEnabled() bool {
  1364  	return c.Opts.IsEnabled(PolicyTracing)
  1365  }
  1366  
  1367  // IsFlannelMasterDeviceSet returns if the flannel master device is set.
  1368  func (c *DaemonConfig) IsFlannelMasterDeviceSet() bool {
  1369  	return len(c.FlannelMasterDevice) != 0
  1370  }
  1371  
  1372  func (c *DaemonConfig) validateIPv6ClusterAllocCIDR() error {
  1373  	ip, cidr, err := net.ParseCIDR(c.IPv6ClusterAllocCIDR)
  1374  	if err != nil {
  1375  		return err
  1376  	}
  1377  
  1378  	if cidr == nil {
  1379  		return fmt.Errorf("ParseCIDR returned nil")
  1380  	}
  1381  
  1382  	if ones, _ := cidr.Mask.Size(); ones != 64 {
  1383  		return fmt.Errorf("CIDR length must be /64")
  1384  	}
  1385  
  1386  	c.IPv6ClusterAllocCIDRBase = ip.Mask(cidr.Mask).String()
  1387  
  1388  	return nil
  1389  }
  1390  
  1391  // Validate validates the daemon configuration
  1392  func (c *DaemonConfig) Validate() error {
  1393  	if err := c.validateIPv6ClusterAllocCIDR(); err != nil {
  1394  		return fmt.Errorf("unable to parse CIDR value '%s' of option --%s: %s",
  1395  			c.IPv6ClusterAllocCIDR, IPv6ClusterAllocCIDRName, err)
  1396  	}
  1397  
  1398  	if c.MTU < 0 {
  1399  		return fmt.Errorf("MTU '%d' cannot be negative", c.MTU)
  1400  	}
  1401  
  1402  	switch c.Tunnel {
  1403  	case TunnelVXLAN, TunnelGeneve, "":
  1404  	case TunnelDisabled:
  1405  		if c.UseSingleClusterRoute {
  1406  			return fmt.Errorf("option --%s cannot be used in combination with --%s=%s",
  1407  				SingleClusterRouteName, TunnelName, TunnelDisabled)
  1408  		}
  1409  	default:
  1410  		return fmt.Errorf("invalid tunnel mode '%s', valid modes = {%s}", c.Tunnel, GetTunnelModes())
  1411  	}
  1412  
  1413  	if c.ClusterID < ClusterIDMin || c.ClusterID > ClusterIDMax {
  1414  		return fmt.Errorf("invalid cluster id %d: must be in range %d..%d",
  1415  			c.ClusterID, ClusterIDMin, ClusterIDMax)
  1416  	}
  1417  
  1418  	if c.ClusterID != 0 {
  1419  		if c.ClusterName == defaults.ClusterName {
  1420  			return fmt.Errorf("cannot use default cluster name (%s) with option %s",
  1421  				defaults.ClusterName, ClusterIDName)
  1422  		}
  1423  	}
  1424  
  1425  	if c.CTMapEntriesGlobalTCP < LimitTableMin || c.CTMapEntriesGlobalAny < LimitTableMin {
  1426  		return fmt.Errorf("specified CT tables values %d/%d must exceed minimum %d",
  1427  			c.CTMapEntriesGlobalTCP, c.CTMapEntriesGlobalAny, LimitTableMin)
  1428  	}
  1429  	if c.CTMapEntriesGlobalTCP > LimitTableMax || c.CTMapEntriesGlobalAny > LimitTableMax {
  1430  		return fmt.Errorf("specified CT tables values %d/%d must not exceed maximum %d",
  1431  			c.CTMapEntriesGlobalTCP, c.CTMapEntriesGlobalAny, LimitTableMax)
  1432  	}
  1433  	if c.NATMapEntriesGlobal < LimitTableMin {
  1434  		return fmt.Errorf("specified NAT table size %d must exceed minimum %d",
  1435  			c.NATMapEntriesGlobal, LimitTableMin)
  1436  	}
  1437  	if c.NATMapEntriesGlobal > LimitTableMax {
  1438  		return fmt.Errorf("specified NAT tables size %d must not exceed maximum %d",
  1439  			c.NATMapEntriesGlobal, LimitTableMax)
  1440  	}
  1441  	if c.NATMapEntriesGlobal > c.CTMapEntriesGlobalTCP+c.CTMapEntriesGlobalAny {
  1442  		if c.NATMapEntriesGlobal == NATMapEntriesGlobalDefault {
  1443  			// Auto-size for the case where CT table size was adapted but NAT still on default
  1444  			c.NATMapEntriesGlobal = int((c.CTMapEntriesGlobalTCP + c.CTMapEntriesGlobalAny) * 2 / 3)
  1445  		} else {
  1446  			return fmt.Errorf("specified NAT tables size %d must not exceed maximum CT table size %d",
  1447  				c.NATMapEntriesGlobal, c.CTMapEntriesGlobalTCP+c.CTMapEntriesGlobalAny)
  1448  		}
  1449  	}
  1450  
  1451  	policyMapMin := (1 << 8)
  1452  	policyMapMax := (1 << 16)
  1453  	if c.PolicyMapMaxEntries < policyMapMin {
  1454  		return fmt.Errorf("specified PolicyMap max entries %d must exceed minimum %d",
  1455  			c.PolicyMapMaxEntries, policyMapMin)
  1456  	}
  1457  	if c.PolicyMapMaxEntries > policyMapMax {
  1458  		return fmt.Errorf("specified PolicyMap max entries %d must not exceed maximum %d",
  1459  			c.PolicyMapMaxEntries, policyMapMax)
  1460  	}
  1461  	// Validate that the KVStore Lease TTL value lies between a particular range.
  1462  	if c.KVstoreLeaseTTL > defaults.KVstoreLeaseMaxTTL || c.KVstoreLeaseTTL < defaults.LockLeaseTTL {
  1463  		return fmt.Errorf("KVstoreLeaseTTL does not lie in required range(%ds, %ds)",
  1464  			int64(defaults.LockLeaseTTL.Seconds()),
  1465  			int64(defaults.KVstoreLeaseMaxTTL.Seconds()))
  1466  	}
  1467  
  1468  	if c.WriteCNIConfigurationWhenReady != "" && c.ReadCNIConfiguration == "" {
  1469  		return fmt.Errorf("%s must be set when using %s", ReadCNIConfiguration, WriteCNIConfigurationWhenReady)
  1470  	}
  1471  
  1472  	if c.EnableHostReachableServices && !c.EnableHostServicesUDP && !c.EnableHostServicesTCP {
  1473  		return fmt.Errorf("%s must be at minimum one of [%s,%s]",
  1474  			HostReachableServicesProtos, HostServicesTCP, HostServicesUDP)
  1475  	}
  1476  
  1477  	return nil
  1478  }
  1479  
  1480  // ReadDirConfig reads the given directory and returns a map that maps the
  1481  // filename to the contents of that file.
  1482  func ReadDirConfig(dirName string) (map[string]interface{}, error) {
  1483  	m := map[string]interface{}{}
  1484  	fi, err := ioutil.ReadDir(dirName)
  1485  	if err != nil && !os.IsNotExist(err) {
  1486  		return nil, fmt.Errorf("unable to read configuration directory: %s", err)
  1487  	}
  1488  	for _, f := range fi {
  1489  		if f.Mode().IsDir() {
  1490  			continue
  1491  		}
  1492  		fName := filepath.Join(dirName, f.Name())
  1493  
  1494  		// the file can still be a symlink to a directory
  1495  		if f.Mode()&os.ModeSymlink == 0 {
  1496  			absFileName, err := filepath.EvalSymlinks(fName)
  1497  			if err != nil {
  1498  				log.Warnf("Unable to read configuration file %q: %s", absFileName, err)
  1499  				continue
  1500  			}
  1501  			fName = absFileName
  1502  		}
  1503  
  1504  		f, err = os.Stat(fName)
  1505  		if err != nil {
  1506  			log.Warnf("Unable to read configuration file %q: %s", fName, err)
  1507  			continue
  1508  		}
  1509  		if f.Mode().IsDir() {
  1510  			continue
  1511  		}
  1512  
  1513  		b, err := ioutil.ReadFile(fName)
  1514  		if err != nil {
  1515  			log.Warnf("Unable to read configuration file %q: %s", fName, err)
  1516  			continue
  1517  		}
  1518  		m[f.Name()] = string(bytes.TrimSpace(b))
  1519  	}
  1520  	return m, nil
  1521  }
  1522  
  1523  // MergeConfig merges the given configuration map with viper's configuration.
  1524  func MergeConfig(m map[string]interface{}) error {
  1525  	err := viper.MergeConfigMap(m)
  1526  	if err != nil {
  1527  		return fmt.Errorf("unable to read merge directory configuration: %s", err)
  1528  	}
  1529  	return nil
  1530  }
  1531  
  1532  // ReplaceDeprecatedFields replaces the deprecated options set with the new set
  1533  // of options that overwrite the deprecated ones.
  1534  // This function replaces the deprecated fields used by environment variables
  1535  // with a different name than the option they are setting. This also replaces
  1536  // the deprecated names used in the Kubernetes ConfigMap.
  1537  // Once we remove them from this function we also need to remove them from
  1538  // daemon_main.go and warn users about the old environment variable nor the
  1539  // option in the configuration map have any effect.
  1540  func ReplaceDeprecatedFields(m map[string]interface{}) {
  1541  	deprecatedFields := map[string]string{
  1542  		"monitor-aggregation-level":   MonitorAggregationName,
  1543  		"ct-global-max-entries-tcp":   CTMapEntriesGlobalTCPName,
  1544  		"ct-global-max-entries-other": CTMapEntriesGlobalAnyName,
  1545  	}
  1546  	for deprecatedOption, newOption := range deprecatedFields {
  1547  		if deprecatedValue, ok := m[deprecatedOption]; ok {
  1548  			if _, ok := m[newOption]; !ok {
  1549  				m[newOption] = deprecatedValue
  1550  			}
  1551  		}
  1552  	}
  1553  }
  1554  
  1555  func (c *DaemonConfig) parseExcludedLocalAddresses(s []string) error {
  1556  	for _, ipString := range s {
  1557  		_, ipnet, err := net.ParseCIDR(ipString)
  1558  		if err != nil {
  1559  			return fmt.Errorf("unable to parse excluded local address %s: %s", ipString, err)
  1560  		}
  1561  
  1562  		c.excludeLocalAddresses = append(c.excludeLocalAddresses, ipnet)
  1563  	}
  1564  
  1565  	return nil
  1566  }
  1567  
  1568  // Populate sets all options with the values from viper
  1569  func (c *DaemonConfig) Populate() {
  1570  	var err error
  1571  
  1572  	c.AccessLog = viper.GetString(AccessLog)
  1573  	c.AgentLabels = viper.GetStringSlice(AgentLabels)
  1574  	c.AllowLocalhost = viper.GetString(AllowLocalhost)
  1575  	c.AnnotateK8sNode = viper.GetBool(AnnotateK8sNode)
  1576  	c.AutoCreateCiliumNodeResource = viper.GetBool(AutoCreateCiliumNodeResource)
  1577  	c.BPFCompilationDebug = viper.GetBool(BPFCompileDebugName)
  1578  	c.CTMapEntriesGlobalTCP = viper.GetInt(CTMapEntriesGlobalTCPName)
  1579  	c.CTMapEntriesGlobalAny = viper.GetInt(CTMapEntriesGlobalAnyName)
  1580  	c.NATMapEntriesGlobal = viper.GetInt(NATMapEntriesGlobalName)
  1581  	c.BPFRoot = viper.GetString(BPFRoot)
  1582  	c.CGroupRoot = viper.GetString(CGroupRoot)
  1583  	c.ClusterID = viper.GetInt(ClusterIDName)
  1584  	c.ClusterName = viper.GetString(ClusterName)
  1585  	c.ClusterMeshConfig = viper.GetString(ClusterMeshConfigName)
  1586  	c.DatapathMode = viper.GetString(DatapathMode)
  1587  	c.Debug = viper.GetBool(DebugArg)
  1588  	c.DebugVerbose = viper.GetStringSlice(DebugVerbose)
  1589  	c.Device = viper.GetString(Device)
  1590  	c.DisableConntrack = viper.GetBool(DisableConntrack)
  1591  	c.EnableIPv4 = getIPv4Enabled()
  1592  	c.EnableIPv6 = viper.GetBool(EnableIPv6Name)
  1593  	c.EnableIPSec = viper.GetBool(EnableIPSecName)
  1594  	c.EndpointInterfaceNamePrefix = viper.GetString(EndpointInterfaceNamePrefix)
  1595  	c.DevicePreFilter = viper.GetString(PrefilterDevice)
  1596  	c.DisableCiliumEndpointCRD = viper.GetBool(DisableCiliumEndpointCRDName)
  1597  	c.DisableK8sServices = viper.GetBool(DisableK8sServices)
  1598  	c.EgressMasqueradeInterfaces = viper.GetString(EgressMasqueradeInterfaces)
  1599  	c.EnableHostReachableServices = viper.GetBool(EnableHostReachableServices)
  1600  	c.DockerEndpoint = viper.GetString(Docker)
  1601  	c.EnableXTSocketFallback = viper.GetBool(EnableXTSocketFallbackName)
  1602  	c.EnableAutoDirectRouting = viper.GetBool(EnableAutoDirectRoutingName)
  1603  	c.EnableEndpointRoutes = viper.GetBool(EnableEndpointRoutes)
  1604  	c.EnableHealthChecking = viper.GetBool(EnableHealthChecking)
  1605  	c.EnableEndpointHealthChecking = viper.GetBool(EnableEndpointHealthChecking)
  1606  	c.EnablePolicy = strings.ToLower(viper.GetString(EnablePolicy))
  1607  	c.EnableL7Proxy = viper.GetBool(EnableL7Proxy)
  1608  	c.EnableTracing = viper.GetBool(EnableTracing)
  1609  	c.EnableNodePort = viper.GetBool(EnableNodePort)
  1610  	c.EncryptInterface = viper.GetString(EncryptInterface)
  1611  	c.EncryptNode = viper.GetBool(EncryptNode)
  1612  	c.EnvoyLogPath = viper.GetString(EnvoyLog)
  1613  	c.ForceLocalPolicyEvalAtSource = viper.GetBool(ForceLocalPolicyEvalAtSource)
  1614  	c.HostDevice = getHostDevice()
  1615  	c.HTTPIdleTimeout = viper.GetInt(HTTPIdleTimeout)
  1616  	c.HTTPMaxGRPCTimeout = viper.GetInt(HTTPMaxGRPCTimeout)
  1617  	c.HTTPRequestTimeout = viper.GetInt(HTTPRequestTimeout)
  1618  	c.HTTPRetryCount = viper.GetInt(HTTPRetryCount)
  1619  	c.HTTPRetryTimeout = viper.GetInt(HTTPRetryTimeout)
  1620  	c.IPv4ClusterCIDRMaskSize = viper.GetInt(IPv4ClusterCIDRMaskSize)
  1621  	c.IdentityChangeGracePeriod = viper.GetDuration(IdentityChangeGracePeriod)
  1622  	c.IPAM = viper.GetString(IPAM)
  1623  	c.IPv4Range = viper.GetString(IPv4Range)
  1624  	c.IPv4NodeAddr = viper.GetString(IPv4NodeAddr)
  1625  	c.IPv4ServiceRange = viper.GetString(IPv4ServiceRange)
  1626  	c.IPv6ClusterAllocCIDR = viper.GetString(IPv6ClusterAllocCIDRName)
  1627  	c.IPv6NodeAddr = viper.GetString(IPv6NodeAddr)
  1628  	c.IPv6Range = viper.GetString(IPv6Range)
  1629  	c.IPv6ServiceRange = viper.GetString(IPv6ServiceRange)
  1630  	c.K8sAPIServer = viper.GetString(K8sAPIServer)
  1631  	c.K8sKubeConfigPath = viper.GetString(K8sKubeConfigPath)
  1632  	c.K8sRequireIPv4PodCIDR = viper.GetBool(K8sRequireIPv4PodCIDRName)
  1633  	c.K8sRequireIPv6PodCIDR = viper.GetBool(K8sRequireIPv6PodCIDRName)
  1634  	c.K8sServiceCacheSize = uint(viper.GetInt(K8sServiceCacheSize))
  1635  	c.K8sForceJSONPatch = viper.GetBool(K8sForceJSONPatch)
  1636  	c.K8sEventHandover = viper.GetBool(K8sEventHandover)
  1637  	c.K8sWatcherQueueSize = uint(viper.GetInt(K8sWatcherQueueSize))
  1638  	c.K8sWatcherEndpointSelector = viper.GetString(K8sWatcherEndpointSelector)
  1639  	c.KeepTemplates = viper.GetBool(KeepBPFTemplates)
  1640  	c.KeepConfig = viper.GetBool(KeepConfig)
  1641  	c.KVStore = viper.GetString(KVStore)
  1642  	c.KVstoreLeaseTTL = viper.GetDuration(KVstoreLeaseTTL)
  1643  	c.KVstoreKeepAliveInterval = c.KVstoreLeaseTTL / defaults.KVstoreKeepAliveIntervalFactor
  1644  	c.KVstorePeriodicSync = viper.GetDuration(KVstorePeriodicSync)
  1645  	c.KVstoreConnectivityTimeout = viper.GetDuration(KVstoreConnectivityTimeout)
  1646  	c.IPAllocationTimeout = viper.GetDuration(IPAllocationTimeout)
  1647  	c.LabelPrefixFile = viper.GetString(LabelPrefixFile)
  1648  	c.Labels = viper.GetStringSlice(Labels)
  1649  	c.LBInterface = viper.GetString(LB)
  1650  	c.LibDir = viper.GetString(LibDir)
  1651  	c.LogDriver = viper.GetStringSlice(LogDriver)
  1652  	c.LogSystemLoadConfig = viper.GetBool(LogSystemLoadConfigName)
  1653  	c.Logstash = viper.GetBool(Logstash)
  1654  	c.LoopbackIPv4 = viper.GetString(LoopbackIPv4)
  1655  	c.Masquerade = viper.GetBool(Masquerade)
  1656  	c.InstallIptRules = viper.GetBool(InstallIptRules)
  1657  	c.IPTablesLockTimeout = viper.GetDuration(IPTablesLockTimeout)
  1658  	c.IPTablesLockWaitInterval = viper.GetString(IPTablesLockWaitInterval)
  1659  	c.IPSecKeyFile = viper.GetString(IPSecKeyFileName)
  1660  	c.ModePreFilter = viper.GetString(PrefilterMode)
  1661  	c.MonitorAggregation = viper.GetString(MonitorAggregationName)
  1662  	c.MonitorQueueSize = viper.GetInt(MonitorQueueSizeName)
  1663  	c.MTU = viper.GetInt(MTUName)
  1664  	c.NAT46Range = viper.GetString(NAT46Range)
  1665  	c.FlannelMasterDevice = viper.GetString(FlannelMasterDevice)
  1666  	c.FlannelUninstallOnExit = viper.GetBool(FlannelUninstallOnExit)
  1667  	c.FlannelManageExistingContainers = viper.GetBool(FlannelManageExistingContainers)
  1668  	c.PolicyMapMaxEntries = viper.GetInt(PolicyMapEntriesName)
  1669  	c.PProf = viper.GetBool(PProf)
  1670  	c.PreAllocateMaps = viper.GetBool(PreAllocateMapsName)
  1671  	c.PrependIptablesChains = viper.GetBool(PrependIptablesChainsName)
  1672  	c.PrometheusServeAddr = getPrometheusServerAddr()
  1673  	c.ProxyConnectTimeout = viper.GetInt(ProxyConnectTimeout)
  1674  	c.BlacklistConflictingRoutes = viper.GetBool(BlacklistConflictingRoutes)
  1675  	c.ReadCNIConfiguration = viper.GetString(ReadCNIConfiguration)
  1676  	c.RestoreState = viper.GetBool(Restore)
  1677  	c.RunDir = viper.GetString(StateDir)
  1678  	c.SidecarIstioProxyImage = viper.GetString(SidecarIstioProxyImage)
  1679  	c.UseSingleClusterRoute = viper.GetBool(SingleClusterRouteName)
  1680  	c.SocketPath = viper.GetString(SocketPath)
  1681  	c.SockopsEnable = viper.GetBool(SockopsEnableName)
  1682  	c.TracePayloadlen = viper.GetInt(TracePayloadlen)
  1683  	c.Tunnel = viper.GetString(TunnelName)
  1684  	c.Version = viper.GetString(Version)
  1685  	c.Workloads = viper.GetStringSlice(ContainerRuntime)
  1686  	c.WriteCNIConfigurationWhenReady = viper.GetString(WriteCNIConfigurationWhenReady)
  1687  	c.PolicyTriggerInterval = viper.GetDuration(PolicyTriggerInterval)
  1688  	c.CTMapEntriesTimeoutTCP = viper.GetDuration(CTMapEntriesTimeoutTCPName)
  1689  	c.CTMapEntriesTimeoutAny = viper.GetDuration(CTMapEntriesTimeoutAnyName)
  1690  	c.CTMapEntriesTimeoutSVCTCP = viper.GetDuration(CTMapEntriesTimeoutSVCTCPName)
  1691  	c.CTMapEntriesTimeoutSVCAny = viper.GetDuration(CTMapEntriesTimeoutSVCAnyName)
  1692  	c.CTMapEntriesTimeoutSYN = viper.GetDuration(CTMapEntriesTimeoutSYNName)
  1693  	c.CTMapEntriesTimeoutFIN = viper.GetDuration(CTMapEntriesTimeoutFINName)
  1694  
  1695  	if nativeCIDR := viper.GetString(IPv4NativeRoutingCIDR); nativeCIDR != "" {
  1696  		c.ipv4NativeRoutingCIDR = cidr.MustParseCIDR(nativeCIDR)
  1697  	}
  1698  
  1699  	// toFQDNs options
  1700  	// When the poller is enabled, the default MinTTL is lowered. This is to
  1701  	// avoid caching large sets of identities generated by a poller (it runs
  1702  	// every 5s). Without the poller, a longer default is better because it
  1703  	// avoids confusion about dropped connections.
  1704  	c.ToFQDNsEnablePoller = viper.GetBool(ToFQDNsEnablePoller)
  1705  	c.ToFQDNsEnablePollerEvents = viper.GetBool(ToFQDNsEnablePollerEvents)
  1706  	c.ToFQDNsMaxIPsPerHost = viper.GetInt(ToFQDNsMaxIPsPerHost)
  1707  	userSetMinTTL := viper.GetInt(ToFQDNsMinTTL)
  1708  	switch {
  1709  	case userSetMinTTL != 0: // set by user
  1710  		c.ToFQDNsMinTTL = userSetMinTTL
  1711  	case c.ToFQDNsEnablePoller:
  1712  		c.ToFQDNsMinTTL = defaults.ToFQDNsMinTTLPoller
  1713  	default:
  1714  		c.ToFQDNsMinTTL = defaults.ToFQDNsMinTTL
  1715  	}
  1716  	c.ToFQDNsProxyPort = viper.GetInt(ToFQDNsProxyPort)
  1717  	c.ToFQDNsPreCache = viper.GetString(ToFQDNsPreCache)
  1718  
  1719  	// Convert IP strings into net.IPNet types
  1720  	subnets, invalid := ip.ParseCIDRs(viper.GetStringSlice(IPv4PodSubnets))
  1721  	if len(invalid) > 0 {
  1722  		log.WithFields(
  1723  			logrus.Fields{
  1724  				"Subnets": invalid,
  1725  			}).Warning("IPv4PodSubnets parameter can not be parsed.")
  1726  	}
  1727  	c.IPv4PodSubnets = subnets
  1728  
  1729  	subnets, invalid = ip.ParseCIDRs(viper.GetStringSlice(IPv6PodSubnets))
  1730  	if len(invalid) > 0 {
  1731  		log.WithFields(
  1732  			logrus.Fields{
  1733  				"Subnets": invalid,
  1734  			}).Warning("IPv6PodSubnets parameter can not be parsed.")
  1735  	}
  1736  	c.IPv6PodSubnets = subnets
  1737  
  1738  	nodePortRange := viper.GetStringSlice(NodePortRange)
  1739  	if len(nodePortRange) > 0 {
  1740  		if len(nodePortRange) != 2 {
  1741  			log.Fatal("Unable to parse min/max port for NodePort range!")
  1742  		}
  1743  		c.NodePortMin, err = strconv.Atoi(nodePortRange[0])
  1744  		if err != nil {
  1745  			log.WithError(err).Fatal("Unable to parse min port value for NodePort range!")
  1746  		}
  1747  		c.NodePortMax, err = strconv.Atoi(nodePortRange[1])
  1748  		if err != nil {
  1749  			log.WithError(err).Fatal("Unable to parse max port value for NodePort range!")
  1750  		}
  1751  		if c.NodePortMax <= c.NodePortMin {
  1752  			log.Fatal("NodePort range min port must be smaller than max port!")
  1753  		}
  1754  	}
  1755  
  1756  	hostServicesProtos := viper.GetStringSlice(HostReachableServicesProtos)
  1757  	if len(hostServicesProtos) > 2 {
  1758  		log.Fatal("Unable to parse protocols for host reachable services!")
  1759  	}
  1760  	for i := 0; i < len(hostServicesProtos); i++ {
  1761  		switch strings.ToLower(hostServicesProtos[i]) {
  1762  		case HostServicesTCP:
  1763  			c.EnableHostServicesTCP = true
  1764  		case HostServicesUDP:
  1765  			c.EnableHostServicesUDP = true
  1766  		default:
  1767  			log.Fatalf("Unable to parse protocol %s for host reachable services!",
  1768  				hostServicesProtos[i])
  1769  		}
  1770  	}
  1771  
  1772  	// Map options
  1773  	if m := viper.GetStringMapString(ContainerRuntimeEndpoint); len(m) != 0 {
  1774  		c.ContainerRuntimeEndpoint = m
  1775  	}
  1776  
  1777  	if m := viper.GetStringMapString(FixedIdentityMapping); len(m) != 0 {
  1778  		c.FixedIdentityMapping = m
  1779  	}
  1780  
  1781  	if m := viper.GetStringMapString(KVStoreOpt); len(m) != 0 {
  1782  		c.KVStoreOpt = m
  1783  	}
  1784  
  1785  	if m := viper.GetStringMapString(LogOpt); len(m) != 0 {
  1786  		c.LogOpt = m
  1787  	}
  1788  
  1789  	if val := viper.GetInt(ConntrackGarbageCollectorIntervalDeprecated); val != 0 {
  1790  		c.ConntrackGCInterval = time.Duration(val) * time.Second
  1791  	} else {
  1792  		c.ConntrackGCInterval = viper.GetDuration(ConntrackGCInterval)
  1793  	}
  1794  
  1795  	if c.MonitorQueueSize == 0 {
  1796  		c.MonitorQueueSize = runtime.NumCPU() * defaults.MonitorQueueSizePerCPU
  1797  		if c.MonitorQueueSize > defaults.MonitorQueueSizePerCPUMaximum {
  1798  			c.MonitorQueueSize = defaults.MonitorQueueSizePerCPUMaximum
  1799  		}
  1800  	}
  1801  
  1802  	// Metrics Setup
  1803  	defaultMetrics := metrics.DefaultMetrics()
  1804  	for _, metric := range viper.GetStringSlice(Metrics) {
  1805  		switch metric[0] {
  1806  		case '+':
  1807  			defaultMetrics[metric[1:]] = struct{}{}
  1808  		case '-':
  1809  			delete(defaultMetrics, metric[1:])
  1810  		}
  1811  	}
  1812  	var collectors []prometheus.Collector
  1813  	metricsSlice := common.MapStringStructToSlice(defaultMetrics)
  1814  	c.MetricsConfig, collectors = metrics.CreateConfiguration(metricsSlice)
  1815  	metrics.MustRegister(collectors...)
  1816  
  1817  	if err := c.parseExcludedLocalAddresses(viper.GetStringSlice(ExcludeLocalAddress)); err != nil {
  1818  		log.WithError(err).Fatalf("Unable to parse excluded local addresses")
  1819  	}
  1820  
  1821  	c.IdentityAllocationMode = viper.GetString(IdentityAllocationMode)
  1822  	switch c.IdentityAllocationMode {
  1823  	// This is here for tests. Some call Populate without the normal init
  1824  	case "":
  1825  		c.IdentityAllocationMode = IdentityAllocationModeKVstore
  1826  
  1827  	case IdentityAllocationModeKVstore, IdentityAllocationModeCRD:
  1828  		// c.IdentityAllocationMode is set above
  1829  
  1830  	default:
  1831  		log.Fatalf("Invalid identity allocation mode %q. It must be one of %s or %s", c.IdentityAllocationMode, IdentityAllocationModeKVstore, IdentityAllocationModeCRD)
  1832  	}
  1833  	if c.KVStore == "" {
  1834  		if c.IdentityAllocationMode != IdentityAllocationModeCRD {
  1835  			log.Warningf("Running Cilium with %q=%q requires identity allocation via CRDs. Changing %s to %q", KVStore, c.KVStore, IdentityAllocationMode, IdentityAllocationModeCRD)
  1836  			c.IdentityAllocationMode = IdentityAllocationModeCRD
  1837  		}
  1838  		if c.DisableCiliumEndpointCRD {
  1839  			log.Warningf("Running Cilium with %q=%q requires endpoint CRDs. Changing %s to %t", KVStore, c.KVStore, DisableCiliumEndpointCRDName, false)
  1840  			c.DisableCiliumEndpointCRD = false
  1841  		}
  1842  	}
  1843  
  1844  	// Hidden options
  1845  	c.ConfigFile = viper.GetString(ConfigFile)
  1846  	c.HTTP403Message = viper.GetString(HTTP403Message)
  1847  	c.DisableEnvoyVersionCheck = viper.GetBool(DisableEnvoyVersionCheck)
  1848  	c.K8sNamespace = viper.GetString(K8sNamespaceName)
  1849  	c.MaxControllerInterval = viper.GetInt(MaxCtrlIntervalName)
  1850  	c.SidecarHTTPProxy = viper.GetBool(SidecarHTTPProxy)
  1851  	c.CMDRefDir = viper.GetString(CMDRef)
  1852  	c.PolicyQueueSize = sanitizeIntParam(PolicyQueueSize, defaults.PolicyQueueSize)
  1853  	c.EndpointQueueSize = sanitizeIntParam(EndpointQueueSize, defaults.EndpointQueueSize)
  1854  	c.SelectiveRegeneration = viper.GetBool(SelectiveRegeneration)
  1855  	c.SkipCRDCreation = viper.GetBool(SkipCRDCreation)
  1856  	c.DisableCNPStatusUpdates = viper.GetBool(DisableCNPStatusUpdates)
  1857  	c.AwsReleaseExcessIps = viper.GetBool(AwsReleaseExcessIps)
  1858  }
  1859  
  1860  func sanitizeIntParam(paramName string, paramDefault int) int {
  1861  	intParam := viper.GetInt(paramName)
  1862  	if intParam <= 0 {
  1863  		log.WithFields(
  1864  			logrus.Fields{
  1865  				"parameter":    paramName,
  1866  				"defaultValue": paramDefault,
  1867  			}).Warning("user-provided parameter had value <= 0 , which is invalid ; setting to default")
  1868  		return paramDefault
  1869  	}
  1870  	return intParam
  1871  }
  1872  
  1873  func getIPv4Enabled() bool {
  1874  	if viper.GetBool(LegacyDisableIPv4Name) {
  1875  		return false
  1876  	}
  1877  
  1878  	return viper.GetBool(EnableIPv4Name)
  1879  }
  1880  
  1881  func getPrometheusServerAddr() string {
  1882  	promAddr := viper.GetString(PrometheusServeAddr)
  1883  	if promAddr == "" {
  1884  		return viper.GetString("prometheus-serve-addr-deprecated")
  1885  	}
  1886  	return promAddr
  1887  }
  1888  
  1889  func getHostDevice() string {
  1890  	hostDevice := viper.GetString(FlannelMasterDevice)
  1891  	if hostDevice == "" {
  1892  		return defaults.HostDevice
  1893  	}
  1894  	return hostDevice
  1895  }