github.com/cilium/cilium@v1.16.2/test/controlplane/suite/operator.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package suite 5 6 import ( 7 "context" 8 "log/slog" 9 "testing" 10 11 "github.com/cilium/hive/cell" 12 "github.com/spf13/viper" 13 14 "github.com/cilium/cilium/operator/cmd" 15 "github.com/cilium/cilium/operator/option" 16 "github.com/cilium/cilium/pkg/hive" 17 "github.com/cilium/cilium/pkg/k8s/apis" 18 k8sClient "github.com/cilium/cilium/pkg/k8s/client" 19 ) 20 21 type operatorHandle struct { 22 t *testing.T 23 24 hive *hive.Hive 25 log *slog.Logger 26 } 27 28 func (h *operatorHandle) tearDown() { 29 // If hive is nil, we have not yet started. 30 if h.hive != nil { 31 if err := h.hive.Stop(h.log, context.TODO()); err != nil { 32 h.t.Fatalf("Operator hive failed to stop: %s", err) 33 } 34 } 35 } 36 37 func setupCiliumOperatorHive(clients *k8sClient.FakeClientset) *hive.Hive { 38 return hive.New( 39 cell.Provide(func() k8sClient.Clientset { 40 return clients 41 }), 42 k8sClient.FakeClientBuilderCell, 43 cmd.ControlPlane, 44 ) 45 } 46 47 func populateCiliumOperatorOptions( 48 vp *viper.Viper, 49 modConfig func(*option.OperatorConfig), 50 modCellConfig func(vp *viper.Viper), 51 ) { 52 option.Config.Populate(vp) 53 54 // Apply the controlplane tests default configuration 55 vp.Set(apis.SkipCRDCreation, true) 56 57 // Apply the test-specific operator configuration modifier 58 modConfig(option.Config) 59 60 // Apply the test specific operator cells configuration modifier 61 // 62 // Unlike global configuration options, cell-specific configuration options 63 // (i.e. the ones defined through cell.Config(...)) will not be loaded from 64 // agentOption or operatorOption, but from the *viper.Viper object bound to 65 // the agent or operator hive, respectively. 66 // modCellConfig function exposes the operator hive viper struct to each 67 // controlplane test, so to allow changing those options as needed. 68 modCellConfig(vp) 69 70 } 71 72 func startCiliumOperator(h *hive.Hive, log *slog.Logger) error { 73 return h.Start(log, context.TODO()) 74 }