|
| 1 | +package daemon_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "sync" |
| 6 | + |
| 7 | + . "github.com/onsi/ginkgo/v2" |
| 8 | + . "github.com/onsi/gomega" |
| 9 | + |
| 10 | + corev1 "k8s.io/api/core/v1" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + "k8s.io/client-go/kubernetes/scheme" |
| 13 | + ctrl "sigs.k8s.io/controller-runtime" |
| 14 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 15 | + |
| 16 | + sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" |
| 17 | + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts" |
| 18 | + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/daemon" |
| 19 | + snolog "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/log" |
| 20 | + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/vars" |
| 21 | +) |
| 22 | + |
| 23 | +var _ = Describe("Daemon OperatorConfig Controller", Ordered, func() { |
| 24 | + var cancel context.CancelFunc |
| 25 | + var ctx context.Context |
| 26 | + |
| 27 | + BeforeAll(func() { |
| 28 | + By("Setup controller manager") |
| 29 | + k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{ |
| 30 | + Scheme: scheme.Scheme, |
| 31 | + }) |
| 32 | + Expect(err).ToNot(HaveOccurred()) |
| 33 | + |
| 34 | + configController := daemon.NewOperatorConfigReconcile(k8sClient) |
| 35 | + err = configController.SetupWithManager(k8sManager) |
| 36 | + Expect(err).ToNot(HaveOccurred()) |
| 37 | + |
| 38 | + ctx, cancel = context.WithCancel(context.Background()) |
| 39 | + |
| 40 | + wg := sync.WaitGroup{} |
| 41 | + wg.Add(1) |
| 42 | + go func() { |
| 43 | + defer wg.Done() |
| 44 | + defer GinkgoRecover() |
| 45 | + By("Start controller manager") |
| 46 | + err := k8sManager.Start(ctx) |
| 47 | + Expect(err).ToNot(HaveOccurred()) |
| 48 | + }() |
| 49 | + |
| 50 | + DeferCleanup(func() { |
| 51 | + By("Shutdown controller manager") |
| 52 | + cancel() |
| 53 | + wg.Wait() |
| 54 | + }) |
| 55 | + |
| 56 | + err = k8sClient.Create(ctx, &corev1.ServiceAccount{ObjectMeta: metav1.ObjectMeta{Name: "default", Namespace: "default"}}) |
| 57 | + Expect(err).ToNot(HaveOccurred()) |
| 58 | + }) |
| 59 | + |
| 60 | + BeforeEach(func() { |
| 61 | + Expect(k8sClient.DeleteAllOf(context.Background(), &sriovnetworkv1.SriovOperatorConfig{}, client.InNamespace(testNamespace))).ToNot(HaveOccurred()) |
| 62 | + }) |
| 63 | + |
| 64 | + Context("LogLevel", func() { |
| 65 | + It("should configure the log level base on sriovOperatorConfig", func() { |
| 66 | + soc := &sriovnetworkv1.SriovOperatorConfig{ObjectMeta: metav1.ObjectMeta{ |
| 67 | + Name: consts.DefaultConfigName, |
| 68 | + Namespace: testNamespace, |
| 69 | + }, |
| 70 | + Spec: sriovnetworkv1.SriovOperatorConfigSpec{ |
| 71 | + LogLevel: 1, |
| 72 | + }, |
| 73 | + } |
| 74 | + |
| 75 | + err := k8sClient.Create(ctx, soc) |
| 76 | + Expect(err).ToNot(HaveOccurred()) |
| 77 | + validateExpectedLogLevel(1) |
| 78 | + |
| 79 | + }) |
| 80 | + |
| 81 | + It("should update the log level in runtime", func() { |
| 82 | + soc := &sriovnetworkv1.SriovOperatorConfig{ObjectMeta: metav1.ObjectMeta{ |
| 83 | + Name: consts.DefaultConfigName, |
| 84 | + Namespace: testNamespace, |
| 85 | + }, |
| 86 | + Spec: sriovnetworkv1.SriovOperatorConfigSpec{ |
| 87 | + LogLevel: 1, |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + err := k8sClient.Create(ctx, soc) |
| 92 | + Expect(err).ToNot(HaveOccurred()) |
| 93 | + validateExpectedLogLevel(1) |
| 94 | + |
| 95 | + soc.Spec.LogLevel = 2 |
| 96 | + err = k8sClient.Update(ctx, soc) |
| 97 | + Expect(err).ToNot(HaveOccurred()) |
| 98 | + validateExpectedLogLevel(2) |
| 99 | + }) |
| 100 | + }) |
| 101 | + |
| 102 | + Context("Disable Drain", func() { |
| 103 | + It("should update the skip drain flag", func() { |
| 104 | + soc := &sriovnetworkv1.SriovOperatorConfig{ObjectMeta: metav1.ObjectMeta{ |
| 105 | + Name: consts.DefaultConfigName, |
| 106 | + Namespace: testNamespace, |
| 107 | + }, |
| 108 | + Spec: sriovnetworkv1.SriovOperatorConfigSpec{ |
| 109 | + DisableDrain: true, |
| 110 | + }, |
| 111 | + } |
| 112 | + |
| 113 | + err := k8sClient.Create(ctx, soc) |
| 114 | + Expect(err).ToNot(HaveOccurred()) |
| 115 | + validateExpectedDrain(true) |
| 116 | + |
| 117 | + soc.Spec.DisableDrain = false |
| 118 | + err = k8sClient.Update(ctx, soc) |
| 119 | + Expect(err).ToNot(HaveOccurred()) |
| 120 | + validateExpectedDrain(false) |
| 121 | + }) |
| 122 | + }) |
| 123 | + |
| 124 | + Context("Feature gates", func() { |
| 125 | + It("should update the feature gates struct", func() { |
| 126 | + soc := &sriovnetworkv1.SriovOperatorConfig{ObjectMeta: metav1.ObjectMeta{ |
| 127 | + Name: consts.DefaultConfigName, |
| 128 | + Namespace: testNamespace, |
| 129 | + }, |
| 130 | + Spec: sriovnetworkv1.SriovOperatorConfigSpec{ |
| 131 | + FeatureGates: map[string]bool{ |
| 132 | + "test": true, |
| 133 | + "bla": true, |
| 134 | + }, |
| 135 | + }, |
| 136 | + } |
| 137 | + |
| 138 | + err := k8sClient.Create(ctx, soc) |
| 139 | + Expect(err).ToNot(HaveOccurred()) |
| 140 | + EventuallyWithOffset(1, func(g Gomega) { |
| 141 | + g.Expect(vars.FeatureGate.IsEnabled("test")).To(BeTrue()) |
| 142 | + }, "15s", "3s").Should(Succeed()) |
| 143 | + EventuallyWithOffset(1, func(g Gomega) { |
| 144 | + g.Expect(vars.FeatureGate.IsEnabled("bla")).To(BeTrue()) |
| 145 | + }, "15s", "3s").Should(Succeed()) |
| 146 | + |
| 147 | + soc.Spec.FeatureGates["test"] = false |
| 148 | + err = k8sClient.Update(ctx, soc) |
| 149 | + Expect(err).ToNot(HaveOccurred()) |
| 150 | + EventuallyWithOffset(1, func(g Gomega) { |
| 151 | + g.Expect(vars.FeatureGate.IsEnabled("test")).To(BeFalse()) |
| 152 | + }, "15s", "3s").Should(Succeed()) |
| 153 | + EventuallyWithOffset(1, func(g Gomega) { |
| 154 | + g.Expect(vars.FeatureGate.IsEnabled("bla")).To(BeTrue()) |
| 155 | + }, "15s", "3s").Should(Succeed()) |
| 156 | + }) |
| 157 | + }) |
| 158 | +}) |
| 159 | + |
| 160 | +func validateExpectedLogLevel(level int) { |
| 161 | + EventuallyWithOffset(1, func(g Gomega) { |
| 162 | + g.Expect(snolog.GetLogLevel()).To(Equal(level)) |
| 163 | + }, "15s", "3s").Should(Succeed()) |
| 164 | +} |
| 165 | + |
| 166 | +func validateExpectedDrain(disableDrain bool) { |
| 167 | + EventuallyWithOffset(1, func(g Gomega) { |
| 168 | + g.Expect(vars.DisableDrain).To(Equal(disableDrain)) |
| 169 | + }, "15s", "3s").Should(Succeed()) |
| 170 | +} |
0 commit comments