From 98c6df45d9be0b4c67569dab79777f498338c7c5 Mon Sep 17 00:00:00 2001 From: Gaurav Genani Date: Sun, 13 Jun 2021 22:41:43 +0530 Subject: [PATCH 1/4] api,cli: expose active cilium config through CLI Note: All the daemon configurations have to be public for reflections to work Signed-off-by: Gaurav Genani --- api/v1/models/daemon_configuration_status.go | 3 + api/v1/openapi.yaml | 4 + api/v1/server/embedded_spec.go | 12 +++ cilium/cmd/config.go | 3 + daemon/cmd/config.go | 18 +++- pkg/option/config.go | 101 ++++++++++--------- pkg/option/config_test.go | 2 +- 7 files changed, 88 insertions(+), 55 deletions(-) diff --git a/api/v1/models/daemon_configuration_status.go b/api/v1/models/daemon_configuration_status.go index 9f2825bd8f2ed..f8308ad37204a 100644 --- a/api/v1/models/daemon_configuration_status.go +++ b/api/v1/models/daemon_configuration_status.go @@ -25,6 +25,9 @@ type DaemonConfigurationStatus struct { // addressing Addressing *NodeAddressing `json:"addressing,omitempty"` + // Config map which contains all the active daemon configurations + DaemonConfigurationMap map[string]interface{} `json:"daemonConfigurationMap,omitempty"` + // datapath mode DatapathMode DatapathMode `json:"datapathMode,omitempty"` diff --git a/api/v1/openapi.yaml b/api/v1/openapi.yaml index 1c687cab84fb3..3ea2cdf536165 100644 --- a/api/v1/openapi.yaml +++ b/api/v1/openapi.yaml @@ -2198,6 +2198,10 @@ definitions: egress-multi-home-ip-rule-compat: description: Configured compatibility mode for --egress-multi-home-ip-rule-compat type: boolean + daemonConfigurationMap: + description: Config map which contains all the active daemon configurations + additionalProperties: + type: object DatapathMode: description: Datapath mode type: string diff --git a/api/v1/server/embedded_spec.go b/api/v1/server/embedded_spec.go index b2138fee4a858..6f3e91a7ca9fd 100644 --- a/api/v1/server/embedded_spec.go +++ b/api/v1/server/embedded_spec.go @@ -1908,6 +1908,12 @@ func init() { "addressing": { "$ref": "#/definitions/NodeAddressing" }, + "daemonConfigurationMap": { + "description": "Config map which contains all the active daemon configurations", + "additionalProperties": { + "type": "object" + } + }, "datapathMode": { "$ref": "#/definitions/DatapathMode" }, @@ -6289,6 +6295,12 @@ func init() { "addressing": { "$ref": "#/definitions/NodeAddressing" }, + "daemonConfigurationMap": { + "description": "Config map which contains all the active daemon configurations", + "additionalProperties": { + "type": "object" + } + }, "datapathMode": { "$ref": "#/definitions/DatapathMode" }, diff --git a/cilium/cmd/config.go b/cilium/cmd/config.go index feb4988f50259..edf65075a2ba0 100644 --- a/cilium/cmd/config.go +++ b/cilium/cmd/config.go @@ -64,6 +64,9 @@ func configDaemon(cmd *cobra.Command, opts []string) { } return } + for k, v := range cfgStatus.DaemonConfigurationMap { + fmt.Println(k, " ", v) + } dumpConfig(cfgStatus.Immutable) dumpConfig(cfgStatus.Realized.Options) fmt.Printf("%-24s %s\n", "k8s-configuration", cfgStatus.K8sConfiguration) diff --git a/daemon/cmd/config.go b/daemon/cmd/config.go index ad3c2c7e09a9d..096bb46de9686 100644 --- a/daemon/cmd/config.go +++ b/daemon/cmd/config.go @@ -5,6 +5,7 @@ package cmd import ( "fmt" + "reflect" "github.com/cilium/cilium/api/v1/models" . "github.com/cilium/cilium/api/v1/server/restapi/daemon" @@ -154,6 +155,14 @@ func (h *getConfig) Handle(params GetConfigParams) middleware.Responder { log.WithField(logfields.Params, logfields.Repr(params)).Debug("GET /config request") d := h.daemon + m := make(map[string]interface{}) + e := reflect.ValueOf(option.Config).Elem() + + for i := 0; i < e.NumField(); i++ { + if e.Field(i).Kind() != reflect.Func { + m[e.Type().Field(i).Name] = e.Field(i).Interface() + } + } spec := &models.DaemonConfigurationSpec{ Options: *option.Config.Opts.GetMutableModel(), @@ -169,10 +178,11 @@ func (h *getConfig) Handle(params GetConfigParams) middleware.Responder { Type: option.Config.KVStore, Options: option.Config.KVStoreOpt, }, - Realized: spec, - DeviceMTU: int64(d.mtuConfig.GetDeviceMTU()), - RouteMTU: int64(d.mtuConfig.GetRouteMTU()), - DatapathMode: models.DatapathMode(option.Config.DatapathMode), + Realized: spec, + DaemonConfigurationMap: m, + DeviceMTU: int64(d.mtuConfig.GetDeviceMTU()), + RouteMTU: int64(d.mtuConfig.GetRouteMTU()), + DatapathMode: models.DatapathMode(option.Config.DatapathMode), IpvlanConfiguration: &models.IpvlanConfiguration{ MasterDeviceIndex: int64(option.Config.Ipvlan.MasterDeviceIndex), OperationMode: option.Config.Ipvlan.OperationMode, diff --git a/pkg/option/config.go b/pkg/option/config.go index 9b3e104f1f30c..312f07a8a12bc 100644 --- a/pkg/option/config.go +++ b/pkg/option/config.go @@ -1445,11 +1445,12 @@ type DaemonConfig struct { // Masquerade specifies whether or not to masquerade packets from endpoints // leaving the host. - EnableIPv4Masquerade bool - EnableIPv6Masquerade bool - EnableBPFMasquerade bool - EnableBPFClockProbe bool - EnableIPMasqAgent bool + EnableIPv4Masquerade bool + EnableIPv6Masquerade bool + EnableBPFMasquerade bool + EnableBPFClockProbe bool + EnableIPMasqAgent bool + // expired DNS lookups with still-active connections EnableEgressGateway bool IPMasqAgentConfigPath string InstallIptRules bool @@ -1750,9 +1751,9 @@ type DaemonConfig struct { // KernelHz is the HZ rate the kernel is operating in KernelHz int - // excludeLocalAddresses excludes certain addresses to be recognized as + // ExcludeLocalAddresses excludes certain addresses to be recognized as // a local address - excludeLocalAddresses []*net.IPNet + ExcludeLocalAddresses []*net.IPNet // IPv4PodSubnets available subnets to be assign IPv4 addresses to pods from IPv4PodSubnets []*net.IPNet @@ -1767,8 +1768,8 @@ type DaemonConfig struct { // CiliumNode resource for the local node AutoCreateCiliumNodeResource bool - // ipv4NativeRoutingCIDR describes a CIDR in which pod IPs are routable - ipv4NativeRoutingCIDR *cidr.CIDR + // Ipv4NativeRoutingCIDR describes a CIDR in which pod IPs are routable + Ipv4NativeRoutingCIDR *cidr.CIDR // EgressMasqueradeInterfaces is the selector used to select interfaces // subject to egress masquerading @@ -1891,21 +1892,21 @@ type DaemonConfig struct { // ports for all fragments. FragmentsMapEntries int - // sizeofCTElement is the size of an element (key + value) in the CT map. - sizeofCTElement int + // SizeofCTElement is the size of an element (key + value) in the CT map. + SizeofCTElement int - // sizeofNATElement is the size of an element (key + value) in the NAT map. - sizeofNATElement int + // SizeofNATElement is the size of an element (key + value) in the NAT map. + SizeofNATElement int - // sizeofNeighElement is the size of an element (key + value) in the neigh + // SizeofNeighElement is the size of an element (key + value) in the neigh // map. - sizeofNeighElement int + SizeofNeighElement int - // sizeofSockRevElement is the size of an element (key + value) in the neigh + // SizeofSockRevElement is the size of an element (key + value) in the neigh // map. - sizeofSockRevElement int + SizeofSockRevElement int - k8sEnableAPIDiscovery bool + K8sEnableAPIDiscovery bool // k8sEnableLeasesFallbackDiscovery enables k8s to fallback to API probing to check // for the support of Leases in Kubernetes when there is an error in discovering @@ -1913,17 +1914,17 @@ type DaemonConfig struct { // We require to check for Leases capabilities in operator only, which uses Leases for leader // election purposes in HA mode. // This is only enabled for cilium-operator - k8sEnableLeasesFallbackDiscovery bool + K8sEnableLeasesFallbackDiscovery bool // LBMapEntries is the maximum number of entries allowed in BPF lbmap. LBMapEntries int - // k8sServiceProxyName is the value of service.kubernetes.io/service-proxy-name label, + // K8sServiceProxyNametest is the value of service.kubernetes.io/service-proxy-name label, // that identifies the service objects Cilium should handle. // If the provided value is an empty string, Cilium will manage service objects when // the label is not present. For more details - // https://github.com/kubernetes/enhancements/blob/master/keps/sig-network/0031-20181017-kube-proxy-services-optional.md - k8sServiceProxyName string + K8sServiceProxyNametest string // APIRateLimitName enables configuration of the API rate limits APIRateLimit map[string]string @@ -2007,10 +2008,10 @@ var ( AllowICMPFragNeeded: defaults.AllowICMPFragNeeded, EnableWellKnownIdentities: defaults.EnableWellKnownIdentities, K8sEnableK8sEndpointSlice: defaults.K8sEnableEndpointSlice, - k8sEnableAPIDiscovery: defaults.K8sEnableAPIDiscovery, + K8sEnableAPIDiscovery: defaults.K8sEnableAPIDiscovery, AllocatorListTimeout: defaults.AllocatorListTimeout, - k8sEnableLeasesFallbackDiscovery: defaults.K8sEnableLeasesFallbackDiscovery, + K8sEnableLeasesFallbackDiscovery: defaults.K8sEnableLeasesFallbackDiscovery, APIRateLimit: make(map[string]string), ExternalClusterIP: defaults.ExternalClusterIP, @@ -2020,7 +2021,7 @@ var ( // IPv4NativeRoutingCIDR returns the native routing CIDR if configured func (c *DaemonConfig) IPv4NativeRoutingCIDR() (cidr *cidr.CIDR) { c.ConfigPatchMutex.RLock() - cidr = c.ipv4NativeRoutingCIDR + cidr = c.Ipv4NativeRoutingCIDR c.ConfigPatchMutex.RUnlock() return } @@ -2028,14 +2029,14 @@ func (c *DaemonConfig) IPv4NativeRoutingCIDR() (cidr *cidr.CIDR) { // SetIPv4NativeRoutingCIDR sets the native routing CIDR func (c *DaemonConfig) SetIPv4NativeRoutingCIDR(cidr *cidr.CIDR) { c.ConfigPatchMutex.Lock() - c.ipv4NativeRoutingCIDR = cidr + c.Ipv4NativeRoutingCIDR = cidr c.ConfigPatchMutex.Unlock() } // IsExcludedLocalAddress returns true if the specified IP matches one of the // excluded local IP ranges func (c *DaemonConfig) IsExcludedLocalAddress(ip net.IP) bool { - for _, ipnet := range c.excludeLocalAddresses { + for _, ipnet := range c.ExcludeLocalAddresses { if ipnet.Contains(ip) { return true } @@ -2139,7 +2140,7 @@ func (c *DaemonConfig) LocalClusterName() string { // service.kubernetes.io/service-proxy-name label in order for services to be // handled. func (c *DaemonConfig) K8sServiceProxyName() string { - return c.k8sServiceProxyName + return c.K8sServiceProxyNametest } // CiliumNamespaceName returns the name of the namespace in which Cilium is @@ -2151,20 +2152,20 @@ func (c *DaemonConfig) CiliumNamespaceName() string { // K8sAPIDiscoveryEnabled returns true if API discovery of API groups and // resources is enabled func (c *DaemonConfig) K8sAPIDiscoveryEnabled() bool { - return c.k8sEnableAPIDiscovery + return c.K8sEnableAPIDiscovery } // K8sLeasesFallbackDiscoveryEnabled returns true if we should fallback to direct API // probing when checking for support of Leases in case Discovery API fails to discover // required groups. func (c *DaemonConfig) K8sLeasesFallbackDiscoveryEnabled() bool { - return c.k8sEnableAPIDiscovery + return c.K8sEnableAPIDiscovery } // EnableK8sLeasesFallbackDiscovery enables using direct API probing as a fallback to check // for the support of Leases when discovering API groups is not possible. func (c *DaemonConfig) EnableK8sLeasesFallbackDiscovery() { - c.k8sEnableAPIDiscovery = true + c.K8sEnableAPIDiscovery = true } func (c *DaemonConfig) validateIPv6ClusterAllocCIDR() error { @@ -2349,7 +2350,7 @@ func (c *DaemonConfig) parseExcludedLocalAddresses(s []string) error { return fmt.Errorf("unable to parse excluded local address %s: %s", ipString, err) } - c.excludeLocalAddresses = append(c.excludeLocalAddresses, ipnet) + c.ExcludeLocalAddresses = append(c.ExcludeLocalAddresses, ipnet) } return nil @@ -2445,7 +2446,7 @@ func (c *DaemonConfig) Populate() { c.K8sClientBurst = viper.GetInt(K8sClientBurst) c.K8sClientQPSLimit = viper.GetFloat64(K8sClientQPSLimit) c.K8sEnableK8sEndpointSlice = viper.GetBool(K8sEnableEndpointSlice) - c.k8sEnableAPIDiscovery = viper.GetBool(K8sEnableAPIDiscovery) + c.K8sEnableAPIDiscovery = viper.GetBool(K8sEnableAPIDiscovery) c.K8sKubeConfigPath = viper.GetString(K8sKubeConfigPath) c.K8sRequireIPv4PodCIDR = viper.GetBool(K8sRequireIPv4PodCIDRName) c.K8sRequireIPv6PodCIDR = viper.GetBool(K8sRequireIPv6PodCIDRName) @@ -2514,7 +2515,7 @@ func (c *DaemonConfig) Populate() { c.PolicyAuditMode = viper.GetBool(PolicyAuditModeArg) c.EnableIPv4FragmentsTracking = viper.GetBool(EnableIPv4FragmentsTrackingName) c.FragmentsMapEntries = viper.GetInt(FragmentsMapEntriesName) - c.k8sServiceProxyName = viper.GetString(K8sServiceProxyName) + c.K8sServiceProxyNametest = viper.GetString(K8sServiceProxyName) c.CRDWaitTimeout = viper.GetDuration(CRDWaitTimeout) c.LoadBalancerDSRDispatch = viper.GetString(LoadBalancerDSRDispatch) c.LoadBalancerDSRL4Xlate = viper.GetString(LoadBalancerDSRL4Xlate) @@ -2545,15 +2546,15 @@ func (c *DaemonConfig) Populate() { } if nativeRoutingCIDR != "" { - c.ipv4NativeRoutingCIDR = cidr.MustParseCIDR(nativeRoutingCIDR) + c.Ipv4NativeRoutingCIDR = cidr.MustParseCIDR(nativeRoutingCIDR) - if len(c.ipv4NativeRoutingCIDR.IP) != net.IPv4len { + if len(c.Ipv4NativeRoutingCIDR.IP) != net.IPv4len { log.Fatalf("%s must be an IPv4 CIDR", NativeRoutingCIDR) } } else if ipv4NativeRoutingCIDR != "" { - c.ipv4NativeRoutingCIDR = cidr.MustParseCIDR(ipv4NativeRoutingCIDR) + c.Ipv4NativeRoutingCIDR = cidr.MustParseCIDR(ipv4NativeRoutingCIDR) - if len(c.ipv4NativeRoutingCIDR.IP) != net.IPv4len { + if len(c.Ipv4NativeRoutingCIDR.IP) != net.IPv4len { log.Fatalf("%s must be an IPv4 CIDR", IPv4NativeRoutingCIDR) } } @@ -2950,10 +2951,10 @@ func (c *DaemonConfig) calculateBPFMapSizes() error { // Don't attempt dynamic sizing if any of the sizeof members was not // populated by the daemon (or any other caller). - if c.sizeofCTElement == 0 || - c.sizeofNATElement == 0 || - c.sizeofNeighElement == 0 || - c.sizeofSockRevElement == 0 { + if c.SizeofCTElement == 0 || + c.SizeofNATElement == 0 || + c.SizeofNeighElement == 0 || + c.SizeofSockRevElement == 0 { return nil } @@ -2984,10 +2985,10 @@ func (c *DaemonConfig) SetMapElementSizes( sizeofNeighElement, sizeofSockRevElement int) { - c.sizeofCTElement = sizeofCTElement - c.sizeofNATElement = sizeofNATElement - c.sizeofNeighElement = sizeofNeighElement - c.sizeofSockRevElement = sizeofSockRevElement + c.SizeofCTElement = sizeofCTElement + c.SizeofNATElement = sizeofNATElement + c.SizeofNeighElement = sizeofNeighElement + c.SizeofSockRevElement = sizeofSockRevElement } func (c *DaemonConfig) calculateDynamicBPFMapSizes(totalMemory uint64, dynamicSizeRatio float64) { @@ -3007,12 +3008,12 @@ func (c *DaemonConfig) calculateDynamicBPFMapSizes(totalMemory uint64, dynamicSi // 16GB 1060485 530242 1060485 memoryAvailableForMaps := int(float64(totalMemory) * dynamicSizeRatio) log.Infof("Memory available for map entries (%.3f%% of %dB): %dB", dynamicSizeRatio, totalMemory, memoryAvailableForMaps) - totalMapMemoryDefault := CTMapEntriesGlobalTCPDefault*c.sizeofCTElement + - CTMapEntriesGlobalAnyDefault*c.sizeofCTElement + - NATMapEntriesGlobalDefault*c.sizeofNATElement + + totalMapMemoryDefault := CTMapEntriesGlobalTCPDefault*c.SizeofCTElement + + CTMapEntriesGlobalAnyDefault*c.SizeofCTElement + + NATMapEntriesGlobalDefault*c.SizeofNATElement + // Neigh table has the same number of entries as NAT Map has. - NATMapEntriesGlobalDefault*c.sizeofNeighElement + - SockRevNATMapEntriesDefault*c.sizeofSockRevElement + NATMapEntriesGlobalDefault*c.SizeofNeighElement + + SockRevNATMapEntriesDefault*c.SizeofSockRevElement log.Debugf("Total memory for default map entries: %d", totalMapMemoryDefault) getEntries := func(entriesDefault, min, max int) int { diff --git a/pkg/option/config_test.go b/pkg/option/config_test.go index fe19294cf3024..d33acae2eae97 100644 --- a/pkg/option/config_test.go +++ b/pkg/option/config_test.go @@ -486,7 +486,7 @@ func TestCheckIPv4NativeRoutingCIDR(t *testing.T) { EnableIPv6Masquerade: true, Tunnel: TunnelDisabled, IPAM: ipamOption.IPAMAzure, - ipv4NativeRoutingCIDR: cidr.MustParseCIDR("10.127.64.0/18"), + Ipv4NativeRoutingCIDR: cidr.MustParseCIDR("10.127.64.0/18"), EnableIPv4: true, }, wantErr: false, From 08dfc724a5c776368ebce3bc63f1b731df714ee5 Mon Sep 17 00:00:00 2001 From: Gaurav Genani Date: Mon, 12 Jul 2021 23:43:37 +0530 Subject: [PATCH 2/4] cli: add subcommand `cilium config get` cilium config get gives configuration value from the cli. conifg_name should be in kebab case. Signed-off-by: Gaurav Genani --- Documentation/cmdref/cilium_config.md | 1 + Documentation/cmdref/cilium_config_get.md | 29 +++++ cilium/cmd/config.go | 35 +++++- cilium/cmd/config_get.go | 53 ++++++++ cilium/cmd/helpers.go | 31 ++++- go.mod | 1 + go.sum | 2 + .../github.com/iancoleman/strcase/.travis.yml | 10 ++ vendor/github.com/iancoleman/strcase/LICENSE | 22 ++++ .../github.com/iancoleman/strcase/README.md | 59 +++++++++ .../github.com/iancoleman/strcase/acronyms.go | 10 ++ vendor/github.com/iancoleman/strcase/camel.go | 80 ++++++++++++ vendor/github.com/iancoleman/strcase/doc.go | 12 ++ vendor/github.com/iancoleman/strcase/snake.go | 114 ++++++++++++++++++ vendor/modules.txt | 3 + 15 files changed, 453 insertions(+), 9 deletions(-) create mode 100644 Documentation/cmdref/cilium_config_get.md create mode 100644 cilium/cmd/config_get.go create mode 100644 vendor/github.com/iancoleman/strcase/.travis.yml create mode 100644 vendor/github.com/iancoleman/strcase/LICENSE create mode 100644 vendor/github.com/iancoleman/strcase/README.md create mode 100644 vendor/github.com/iancoleman/strcase/acronyms.go create mode 100644 vendor/github.com/iancoleman/strcase/camel.go create mode 100644 vendor/github.com/iancoleman/strcase/doc.go create mode 100644 vendor/github.com/iancoleman/strcase/snake.go diff --git a/Documentation/cmdref/cilium_config.md b/Documentation/cmdref/cilium_config.md index 227a55982ce10..34b3885eaa976 100644 --- a/Documentation/cmdref/cilium_config.md +++ b/Documentation/cmdref/cilium_config.md @@ -28,4 +28,5 @@ cilium config [