Skip to content

Commit

Permalink
ipam: split IPAM mode option values into own package
Browse files Browse the repository at this point in the history
These are used in several places where a full import of pkg/option (and
thus all its dependencies) can be avoided because only the
IPAM mode constants are used.

Together with the following commits, this helps to reduce the binary
size of cilium-cni and cilium-docker.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
  • Loading branch information
tklauser authored and aanm committed May 18, 2020
1 parent 757493c commit d335ede
Show file tree
Hide file tree
Showing 21 changed files with 101 additions and 68 deletions.
3 changes: 2 additions & 1 deletion cilium-health/launch/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
healthDefaults "github.com/cilium/cilium/pkg/health/defaults"
"github.com/cilium/cilium/pkg/health/probe"
"github.com/cilium/cilium/pkg/identity/cache"
ipamOption "github.com/cilium/cilium/pkg/ipam/option"
"github.com/cilium/cilium/pkg/labels"
"github.com/cilium/cilium/pkg/launcher"
"github.com/cilium/cilium/pkg/logging/logfields"
Expand Down Expand Up @@ -343,7 +344,7 @@ func LaunchAsEndpoint(baseCtx context.Context,
return nil, fmt.Errorf("Error while configuring routes: %s", err)
}

if option.Config.IPAM == option.IPAMENI {
if option.Config.IPAM == ipamOption.IPAMENI {
if err := routingConfig.Configure(healthIP,
mtuConfig.GetDeviceMTU(),
option.Config.Masquerade); err != nil {
Expand Down
5 changes: 3 additions & 2 deletions daemon/cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
"github.com/cilium/cilium/pkg/identity/identitymanager"
identitymodel "github.com/cilium/cilium/pkg/identity/model"
"github.com/cilium/cilium/pkg/ipam"
ipamOption "github.com/cilium/cilium/pkg/ipam/option"
"github.com/cilium/cilium/pkg/ipcache"
"github.com/cilium/cilium/pkg/k8s"
"github.com/cilium/cilium/pkg/k8s/watchers"
Expand Down Expand Up @@ -411,7 +412,7 @@ func NewDaemon(ctx context.Context, dp datapath.Datapath) (*Daemon, *endpointRes
log.WithError(err).Fatal("Unable to register CRDs")
}

if option.Config.IPAM == option.IPAMOperator {
if option.Config.IPAM == ipamOption.IPAMOperator {
// Create the CiliumNode custom resource. This call will block until
// the custom resource has been created
d.nodeDiscovery.UpdateCiliumNodeResource()
Expand Down Expand Up @@ -491,7 +492,7 @@ func NewDaemon(ctx context.Context, dp datapath.Datapath) (*Daemon, *endpointRes

// Trigger refresh and update custom resource in the apiserver with all restored endpoints.
// Trigger after nodeDiscovery.StartDiscovery to avoid custom resource update conflict.
if option.Config.IPAM == option.IPAMCRD || option.Config.IPAM == option.IPAMENI || option.Config.IPAM == option.IPAMAzure {
if option.Config.IPAM == ipamOption.IPAMCRD || option.Config.IPAM == ipamOption.IPAMENI || option.Config.IPAM == ipamOption.IPAMAzure {
if option.Config.EnableIPv6 {
d.ipam.IPv6Allocator.RestoreFinished()
}
Expand Down
3 changes: 2 additions & 1 deletion daemon/cmd/daemon_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"github.com/cilium/cilium/pkg/envoy"
"github.com/cilium/cilium/pkg/flowdebug"
"github.com/cilium/cilium/pkg/identity"
ipamOption "github.com/cilium/cilium/pkg/ipam/option"
"github.com/cilium/cilium/pkg/ipmasq"
"github.com/cilium/cilium/pkg/k8s"
"github.com/cilium/cilium/pkg/k8s/watchers"
Expand Down Expand Up @@ -401,7 +402,7 @@ func init() {
flags.String(option.IdentityAllocationMode, option.IdentityAllocationModeKVstore, "Method to use for identity allocation")
option.BindEnv(option.IdentityAllocationMode)

flags.String(option.IPAM, option.IPAMHostScopeLegacy, "Backend to use for IPAM")
flags.String(option.IPAM, ipamOption.IPAMHostScopeLegacy, "Backend to use for IPAM")
option.BindEnv(option.IPAM)

flags.Int(option.IPv4ClusterCIDRMaskSize, 8, "Mask size for the cluster wide CIDR")
Expand Down
3 changes: 2 additions & 1 deletion daemon/cmd/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
linuxrouting "github.com/cilium/cilium/pkg/datapath/linux/routing"
"github.com/cilium/cilium/pkg/defaults"
"github.com/cilium/cilium/pkg/ipam"
ipamOption "github.com/cilium/cilium/pkg/ipam/option"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/node"
nodeTypes "github.com/cilium/cilium/pkg/node/types"
Expand Down Expand Up @@ -225,7 +226,7 @@ func (d *Daemon) allocateHealthIPs() error {
// In ENI mode, we require the gateway, CIDRs, and the ENI MAC addr
// in order to set up rules and routes on the local node to direct
// endpoint traffic out of the ENIs.
if option.Config.IPAM == option.IPAMENI {
if option.Config.IPAM == ipamOption.IPAMENI {
if err := d.parseHealthEndpointInfo(result); err != nil {
log.WithError(err).Warn("Unable to allocate health information for ENI")
}
Expand Down
11 changes: 6 additions & 5 deletions operator/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
operatorMetrics "github.com/cilium/cilium/operator/metrics"
operatorOption "github.com/cilium/cilium/operator/option"
"github.com/cilium/cilium/pkg/defaults"
ipamOption "github.com/cilium/cilium/pkg/ipam/option"
"github.com/cilium/cilium/pkg/option"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -137,7 +138,7 @@ func init() {
flags.Bool(operatorOption.EnableMetrics, false, "Enable Prometheus metrics")
option.BindEnv(operatorOption.EnableMetrics)

flags.String(option.IPAM, option.IPAMHostScopeLegacy, "Backend to use for IPAM")
flags.String(option.IPAM, ipamOption.IPAMHostScopeLegacy, "Backend to use for IPAM")
option.BindEnv(option.IPAM)

flags.Duration(operatorOption.IdentityHeartbeatTimeout, 2*defaults.KVstoreLeaseTTL, "Timeout after which identity expires on lack of heartbeat")
Expand All @@ -148,13 +149,13 @@ func init() {

flags.String(operatorOption.IPAMOperatorV4CIDR, "",
fmt.Sprintf("IPv4 CIDR Range for Pods in cluster. Requires '%s=%s' and '%s=%s'",
option.IPAM, option.IPAMOperator,
option.IPAM, ipamOption.IPAMOperator,
option.EnableIPv4Name, "true"))
option.BindEnv(operatorOption.IPAMOperatorV4CIDR)

flags.Int(operatorOption.NodeCIDRMaskSizeIPv4, 24,
fmt.Sprintf("Mask size for each IPv4 podCIDR per node. Requires '%s=%s' and '%s=%s'",
option.IPAM, option.IPAMOperator,
option.IPAM, ipamOption.IPAMOperator,
option.EnableIPv4Name, "true"))
option.BindEnv(operatorOption.NodeCIDRMaskSizeIPv4)

Expand All @@ -163,13 +164,13 @@ func init() {

flags.String(operatorOption.IPAMOperatorV6CIDR, "",
fmt.Sprintf("IPv6 CIDR Range for Pods in cluster. Requires '%s=%s' and '%s=%s'",
option.IPAM, option.IPAMOperator,
option.IPAM, ipamOption.IPAMOperator,
option.EnableIPv6Name, "true"))
option.BindEnv(operatorOption.IPAMOperatorV6CIDR)

flags.Int(operatorOption.NodeCIDRMaskSizeIPv6, 112,
fmt.Sprintf("Mask size for each IPv6 podCIDR per node. Requires '%s=%s' and '%s=%s'",
option.IPAM, option.IPAMOperator,
option.IPAM, ipamOption.IPAMOperator,
option.EnableIPv6Name, "true"))
option.BindEnv(operatorOption.NodeCIDRMaskSizeIPv6)

Expand Down
3 changes: 2 additions & 1 deletion operator/k8s_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
operatorOption "github.com/cilium/cilium/operator/option"
"github.com/cilium/cilium/pkg/controller"
"github.com/cilium/cilium/pkg/ipam/allocator"
ipamOption "github.com/cilium/cilium/pkg/ipam/option"
"github.com/cilium/cilium/pkg/k8s"
cilium_v2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
v2 "github.com/cilium/cilium/pkg/k8s/client/clientset/versioned/typed/cilium.io/v2"
Expand Down Expand Up @@ -103,7 +104,7 @@ func runNodeWatcher(nodeManager *allocator.NodeEventHandler) error {
// present in the k8sNodeStore.

switch option.Config.IPAM {
case option.IPAMENI, option.IPAMAzure:
case ipamOption.IPAMENI, ipamOption.IPAMAzure:
nodes, err := ciliumK8sClient.CiliumV2().CiliumNodes().List(context.TODO(), meta_v1.ListOptions{})
if err != nil {
log.WithError(err).Warning("Unable to list CiliumNodes. Won't clean up stale CiliumNodes")
Expand Down
5 changes: 3 additions & 2 deletions operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
operatorOption "github.com/cilium/cilium/operator/option"
"github.com/cilium/cilium/pkg/components"
"github.com/cilium/cilium/pkg/ipam/allocator"
ipamOption "github.com/cilium/cilium/pkg/ipam/option"
"github.com/cilium/cilium/pkg/k8s"
clientset "github.com/cilium/cilium/pkg/k8s/client/clientset/versioned"
k8sversion "github.com/cilium/cilium/pkg/k8s/version"
Expand Down Expand Up @@ -164,7 +165,7 @@ func runOperator(cmd *cobra.Command) {
err error
)
switch ipamMode := option.Config.IPAM; ipamMode {
case option.IPAMAzure, option.IPAMENI, option.IPAMOperator:
case ipamOption.IPAMAzure, ipamOption.IPAMENI, ipamOption.IPAMOperator:
alloc, providerBuiltin := allocatorProviders[ipamMode]
if !providerBuiltin {
log.Fatalf("%s allocator is not supported by this version of cilium-operator", ipamMode)
Expand All @@ -183,7 +184,7 @@ func runOperator(cmd *cobra.Command) {
nodeManager = &nm

switch ipamMode {
case option.IPAMOperator:
case ipamOption.IPAMOperator:
// We will use CiliumNodes as the source of truth for the podCIDRs.
// Once the CiliumNodes are synchronized with the operator we will
// be able to watch for K8s Node events which they will be used
Expand Down
4 changes: 2 additions & 2 deletions operator/provider_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ package main
import (
// These dependencies should be included only when this file is included in the build.
allocatorAWS "github.com/cilium/cilium/pkg/ipam/allocator/aws" // AWS allocator.
"github.com/cilium/cilium/pkg/option"
ipamOption "github.com/cilium/cilium/pkg/ipam/option"
_ "github.com/cilium/cilium/pkg/policy/groups/aws" // Register AWS policy group provider.
)

func init() {
allocatorProviders[option.IPAMENI] = &allocatorAWS.AllocatorAWS{}
allocatorProviders[ipamOption.IPAMENI] = &allocatorAWS.AllocatorAWS{}
}
4 changes: 2 additions & 2 deletions operator/provider_azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ package main
import (
// These dependencies should be included only when this file is included in the build.
allocatorAzure "github.com/cilium/cilium/pkg/ipam/allocator/azure" // Azure allocator task.
"github.com/cilium/cilium/pkg/option"
ipamOption "github.com/cilium/cilium/pkg/ipam/option"
)

func init() {
allocatorProviders[option.IPAMAzure] = &allocatorAzure.AllocatorAzure{}
allocatorProviders[ipamOption.IPAMAzure] = &allocatorAzure.AllocatorAzure{}
}
4 changes: 2 additions & 2 deletions operator/provider_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ package main
import (
// These dependencies should be included only when this file is included in the build.
allocatorOperator "github.com/cilium/cilium/pkg/ipam/allocator/operator" // Operator allocator.
"github.com/cilium/cilium/pkg/option"
ipamOption "github.com/cilium/cilium/pkg/ipam/option"
)

func init() {
allocatorProviders[option.IPAMOperator] = &allocatorOperator.AllocatorOperator{}
allocatorProviders[ipamOption.IPAMOperator] = &allocatorOperator.AllocatorOperator{}
}
3 changes: 2 additions & 1 deletion pkg/datapath/iptables/iptables.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/cilium/cilium/pkg/command/exec"
"github.com/cilium/cilium/pkg/datapath/linux/linux_defaults"
"github.com/cilium/cilium/pkg/defaults"
ipamOption "github.com/cilium/cilium/pkg/ipam/option"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/modules"
"github.com/cilium/cilium/pkg/node"
Expand Down Expand Up @@ -689,7 +690,7 @@ func (m *IptablesManager) remoteSnatDstAddrExclusion() string {

func getDeliveryInterface(ifName string) string {
deliveryInterface := ifName
if option.Config.IPAM == option.IPAMENI || option.Config.EnableEndpointRoutes {
if option.Config.IPAM == ipamOption.IPAMENI || option.Config.EnableEndpointRoutes {
deliveryInterface = "lxc+"
}
return deliveryInterface
Expand Down
3 changes: 2 additions & 1 deletion pkg/endpoint/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/cilium/cilium/pkg/identity"
"github.com/cilium/cilium/pkg/identity/cache"
"github.com/cilium/cilium/pkg/identity/identitymanager"
ipamOption "github.com/cilium/cilium/pkg/ipam/option"
ciliumio "github.com/cilium/cilium/pkg/k8s/apis/cilium.io"
slim_corev1 "github.com/cilium/cilium/pkg/k8s/slim/k8s/apis/core/v1"
"github.com/cilium/cilium/pkg/labels"
Expand Down Expand Up @@ -2172,7 +2173,7 @@ func (e *Endpoint) Delete(monitor monitorOwner, ipam ipReleaser, manager endpoin
}
}

if option.Config.IPAM == option.IPAMENI || option.Config.IPAM == option.IPAMAzure {
if option.Config.IPAM == ipamOption.IPAMENI || option.Config.IPAM == ipamOption.IPAMAzure {
e.getLogger().WithFields(logrus.Fields{
"ep": e.GetID(),
"ipAddr": e.GetIPv4Address(),
Expand Down
7 changes: 4 additions & 3 deletions pkg/ipam/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
eniTypes "github.com/cilium/cilium/pkg/aws/eni/types"
"github.com/cilium/cilium/pkg/cidr"
"github.com/cilium/cilium/pkg/ip"
ipamOption "github.com/cilium/cilium/pkg/ipam/option"
ipamTypes "github.com/cilium/cilium/pkg/ipam/types"
"github.com/cilium/cilium/pkg/k8s"
ciliumv2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
Expand Down Expand Up @@ -246,7 +247,7 @@ func (n *nodeStore) hasMinimumIPsInPool() (minimumReached bool, required, numAva
minimumReached = true
}

if n.conf.IPAMMode() == option.IPAMENI {
if n.conf.IPAMMode() == ipamOption.IPAMENI {
if vpcCIDR := deriveVpcCIDR(n.ownNode); vpcCIDR != nil {
if nativeCIDR := n.conf.IPv4NativeRoutingCIDR(); nativeCIDR != nil {
logFields := logrus.Fields{
Expand Down Expand Up @@ -481,7 +482,7 @@ func (a *crdAllocator) buildAllocationResult(ip net.IP, ipInfo *ipamTypes.Alloca

// In ENI mode, the Resource points to the ENI so we can derive the
// master interface and all CIDRs of the VPC
case option.IPAMENI:
case ipamOption.IPAMENI:
for _, eni := range a.store.ownNode.Status.ENI.ENIs {
if eni.ID == ipInfo.Resource {
result.Master = eni.MAC
Expand All @@ -504,7 +505,7 @@ func (a *crdAllocator) buildAllocationResult(ip net.IP, ipInfo *ipamTypes.Alloca

// In Azure mode, the Resource points to the azure interface so we can
// derive the master interface
case option.IPAMAzure:
case ipamOption.IPAMAzure:
for _, iface := range a.store.ownNode.Status.Azure.Interfaces {
if iface.ID == ipInfo.Resource {
result.Master = iface.MAC
Expand Down
6 changes: 3 additions & 3 deletions pkg/ipam/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (

"github.com/cilium/cilium/pkg/cidr"
"github.com/cilium/cilium/pkg/datapath"
ipamOption "github.com/cilium/cilium/pkg/ipam/option"
"github.com/cilium/cilium/pkg/logging"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/option"

"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -108,7 +108,7 @@ func NewIPAM(nodeAddressing datapath.NodeAddressing, c Configuration, owner Owne
}

switch c.IPAMMode() {
case option.IPAMHostScopeLegacy, option.IPAMKubernetes, option.IPAMOperator:
case ipamOption.IPAMHostScopeLegacy, ipamOption.IPAMKubernetes, ipamOption.IPAMOperator:
log.WithFields(logrus.Fields{
logfields.V4Prefix: nodeAddressing.IPv4().AllocationCIDR(),
logfields.V6Prefix: nodeAddressing.IPv6().AllocationCIDR(),
Expand All @@ -121,7 +121,7 @@ func NewIPAM(nodeAddressing datapath.NodeAddressing, c Configuration, owner Owne
if c.IPv4Enabled() {
ipam.IPv4Allocator = newHostScopeAllocator(nodeAddressing.IPv4().AllocationCIDR().IPNet)
}
case option.IPAMCRD, option.IPAMENI, option.IPAMAzure:
case ipamOption.IPAMCRD, ipamOption.IPAMENI, ipamOption.IPAMAzure:
log.Info("Initializing CRD-based IPAM")
if c.IPv6Enabled() {
ipam.IPv6Allocator = newCRDAllocator(IPv6, c, owner, k8sEventReg)
Expand Down
4 changes: 2 additions & 2 deletions pkg/ipam/ipam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/cilium/cilium/pkg/cidr"
"github.com/cilium/cilium/pkg/datapath"
"github.com/cilium/cilium/pkg/datapath/fake"
"github.com/cilium/cilium/pkg/option"
ipamOption "github.com/cilium/cilium/pkg/ipam/option"

. "gopkg.in/check.v1"
)
Expand Down Expand Up @@ -53,7 +53,7 @@ type testConfiguration struct{}
func (t *testConfiguration) IPv4Enabled() bool { return true }
func (t *testConfiguration) IPv6Enabled() bool { return true }
func (t *testConfiguration) HealthCheckingEnabled() bool { return true }
func (t *testConfiguration) IPAMMode() string { return option.IPAMHostScopeLegacy }
func (t *testConfiguration) IPAMMode() string { return ipamOption.IPAMHostScopeLegacy }
func (t *testConfiguration) BlacklistConflictingRoutesEnabled() bool { return false }
func (t *testConfiguration) SetIPv4NativeRoutingCIDR(cidr *cidr.CIDR) {}
func (t *testConfiguration) IPv4NativeRoutingCIDR() *cidr.CIDR { return nil }
Expand Down
40 changes: 40 additions & 0 deletions pkg/ipam/option/option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2020 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package option

const (
// IPAMHostScopeLegacy is the value to select the legacy hostscope IPAM mode
// This option will disappear in Cilium v1.9
IPAMHostScopeLegacy = "hostscope-legacy"

// IPAMKubernetes is the value to select the Kubernetes PodCIDR based
// hostscope IPAM mode
IPAMKubernetes = "kubernetes"

// IPAMCRD is the value to select the CRD-backed IPAM plugin for
// option.IPAM
IPAMCRD = "crd"

// IPAMENI is the value to select the AWS ENI IPAM plugin for option.IPAM
IPAMENI = "eni"

// IPAMAzure is the value to select the Azure IPAM plugin for
// option.IPAM
IPAMAzure = "azure"

// IPAMOperator is the value to select the Operator IPAM mode for
// option.IPAM
IPAMOperator = "cluster-pool"
)
3 changes: 2 additions & 1 deletion pkg/k8s/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/cilium/cilium/pkg/backoff"
"github.com/cilium/cilium/pkg/controller"
ipamOption "github.com/cilium/cilium/pkg/ipam/option"
cilium_v2_client "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2/client"
k8sconfig "github.com/cilium/cilium/pkg/k8s/config"
slim_corev1 "github.com/cilium/cilium/pkg/k8s/slim/k8s/apis/core/v1"
Expand Down Expand Up @@ -67,7 +68,7 @@ func retrieveNodeInformation(nodeName string) (*nodeTypes.Node, error) {
requireIPv6CIDR := option.Config.K8sRequireIPv6PodCIDR
var n *nodeTypes.Node

if option.Config.IPAM == option.IPAMOperator {
if option.Config.IPAM == ipamOption.IPAMOperator {
ciliumNode, err := CiliumClient().CiliumV2().CiliumNodes().Get(context.TODO(), nodeName, v1.GetOptions{})
if err != nil {
// If no CIDR is required, retrieving the node information is
Expand Down
Loading

0 comments on commit d335ede

Please sign in to comment.