github.com/cilium/cilium@v1.16.2/test/controlplane/suite/agent.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package suite
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"log/slog"
    10  	"os"
    11  	"testing"
    12  
    13  	"github.com/cilium/hive/cell"
    14  	"github.com/cilium/statedb"
    15  
    16  	"github.com/cilium/cilium/daemon/cmd"
    17  	cnicell "github.com/cilium/cilium/daemon/cmd/cni"
    18  	fakecni "github.com/cilium/cilium/daemon/cmd/cni/fake"
    19  	fakeDatapath "github.com/cilium/cilium/pkg/datapath/fake"
    20  	fakeTypes "github.com/cilium/cilium/pkg/datapath/fake/types"
    21  	"github.com/cilium/cilium/pkg/datapath/prefilter"
    22  	datapathTables "github.com/cilium/cilium/pkg/datapath/tables"
    23  	fqdnproxy "github.com/cilium/cilium/pkg/fqdn/proxy"
    24  	"github.com/cilium/cilium/pkg/hive"
    25  	ipamOption "github.com/cilium/cilium/pkg/ipam/option"
    26  	k8sClient "github.com/cilium/cilium/pkg/k8s/client"
    27  	k8sSynced "github.com/cilium/cilium/pkg/k8s/synced"
    28  	"github.com/cilium/cilium/pkg/kvstore/store"
    29  	"github.com/cilium/cilium/pkg/maps/ctmap/gc"
    30  	"github.com/cilium/cilium/pkg/metrics"
    31  	monitorAgent "github.com/cilium/cilium/pkg/monitor/agent"
    32  	"github.com/cilium/cilium/pkg/option"
    33  	"github.com/cilium/cilium/pkg/promise"
    34  	"github.com/cilium/cilium/pkg/proxy"
    35  )
    36  
    37  type agentHandle struct {
    38  	t         *testing.T
    39  	db        *statedb.DB
    40  	nodeAddrs statedb.Table[datapathTables.NodeAddress]
    41  	d         *cmd.Daemon
    42  	p         promise.Promise[*cmd.Daemon]
    43  	dp        *fakeTypes.FakeDatapath
    44  
    45  	hive *hive.Hive
    46  	log  *slog.Logger
    47  }
    48  
    49  func (h *agentHandle) tearDown() {
    50  	if h == nil {
    51  		return
    52  	}
    53  
    54  	// If hive is nil, we have not yet started.
    55  	if h.hive != nil {
    56  		if err := h.hive.Stop(h.log, context.TODO()); err != nil {
    57  			h.t.Fatalf("Failed to stop the agent: %s", err)
    58  		}
    59  	}
    60  
    61  	if h.d != nil {
    62  		h.d.Close()
    63  	}
    64  }
    65  
    66  func (h *agentHandle) setupCiliumAgentHive(clientset k8sClient.Clientset, extraCell cell.Cell) {
    67  	h.hive = hive.New(
    68  		// Extra cell from the test case. Here as the first cell so it can
    69  		// insert lifecycle hooks before anything else.
    70  		extraCell,
    71  
    72  		// Provide the mocked infrastructure and datapath components
    73  		cell.Provide(
    74  			func() k8sClient.Clientset { return clientset },
    75  			func() *option.DaemonConfig { return option.Config },
    76  			func() cnicell.CNIConfigManager { return &fakecni.FakeCNIConfigManager{} },
    77  			func() gc.Enabler { return gc.NewFake() },
    78  			k8sSynced.RejectedCRDSyncPromise,
    79  		),
    80  		fakeDatapath.Cell,
    81  		prefilter.Cell,
    82  		monitorAgent.Cell,
    83  		metrics.Cell,
    84  		store.Cell,
    85  		cmd.ControlPlane,
    86  		cell.Invoke(func(p promise.Promise[*cmd.Daemon], dp *fakeTypes.FakeDatapath) {
    87  			h.p = p
    88  			h.dp = dp
    89  		}),
    90  
    91  		cell.Invoke(func(db *statedb.DB, nodeAddrs statedb.Table[datapathTables.NodeAddress]) {
    92  			h.db = db
    93  			h.nodeAddrs = nodeAddrs
    94  		}),
    95  	)
    96  }
    97  
    98  func (h *agentHandle) populateCiliumAgentOptions(testDir string, modConfig func(*option.DaemonConfig)) {
    99  	option.Config.Populate(h.hive.Viper())
   100  
   101  	option.Config.RunDir = testDir
   102  	option.Config.StateDir = testDir
   103  
   104  	// Apply the controlplane tests default configuration
   105  	option.Config.IdentityAllocationMode = option.IdentityAllocationModeCRD
   106  	option.Config.DryMode = true
   107  	option.Config.IPAM = ipamOption.IPAMKubernetes
   108  	option.Config.Opts = option.NewIntOptions(&option.DaemonMutableOptionLibrary)
   109  	option.Config.Opts.SetBool(option.DropNotify, true)
   110  	option.Config.Opts.SetBool(option.TraceNotify, true)
   111  	option.Config.Opts.SetBool(option.PolicyVerdictNotify, true)
   112  	option.Config.Opts.SetBool(option.Debug, true)
   113  	option.Config.EnableIPSec = false
   114  	option.Config.EnableIPv6 = false
   115  	option.Config.KubeProxyReplacement = option.KubeProxyReplacementTrue
   116  	option.Config.K8sRequireIPv6PodCIDR = false
   117  	option.Config.EnableL7Proxy = false
   118  	option.Config.EnableHealthCheckNodePort = false
   119  	option.Config.Debug = true
   120  
   121  	// Apply the test-specific agent configuration modifier
   122  	modConfig(option.Config)
   123  
   124  	// Unlike global configuration options, cell-specific configuration options
   125  	// (i.e. the ones defined through cell.Config(...)) must be set to the *viper.Viper
   126  	// object bound to the test hive.
   127  	h.hive.Viper().Set(option.EndpointGCInterval, 0)
   128  
   129  	if option.Config.EnableL7Proxy {
   130  		proxy.DefaultDNSProxy = fqdnproxy.MockFQDNProxy{}
   131  	}
   132  }
   133  
   134  func (h *agentHandle) startCiliumAgent() (*cmd.Daemon, error) {
   135  	if err := h.hive.Start(h.log, context.TODO()); err != nil {
   136  		return nil, err
   137  	}
   138  
   139  	return h.p.Await(context.TODO())
   140  }
   141  
   142  func setupTestDirectories() string {
   143  	tempDir, err := os.MkdirTemp("", "cilium-test-")
   144  	if err != nil {
   145  		panic(fmt.Sprintf("TempDir() failed: %s", err))
   146  	}
   147  	return tempDir
   148  }