|
| 1 | +// Copyright 2025 NVIDIA CORPORATION & AFFILIATES |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +// SPDX-License-Identifier: Apache-2.0 |
| 16 | + |
| 17 | +package maintenance |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + . "github.com/onsi/ginkgo/v2" |
| 24 | + . "github.com/onsi/gomega" |
| 25 | + |
| 26 | + maintenanceoperator "github.com/Mellanox/maintenance-operator/api/v1alpha1" |
| 27 | + corev1 "k8s.io/api/core/v1" |
| 28 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 29 | + "k8s.io/apimachinery/pkg/runtime" |
| 30 | + "k8s.io/apimachinery/pkg/types" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 33 | + |
| 34 | + "github.com/Mellanox/nic-configuration-operator/pkg/consts" |
| 35 | + hostmocks "github.com/Mellanox/nic-configuration-operator/pkg/host/mocks" |
| 36 | +) |
| 37 | + |
| 38 | +var _ = Describe("maintenanceManager", func() { |
| 39 | + var ( |
| 40 | + ctx context.Context |
| 41 | + scheme *runtime.Scheme |
| 42 | + namespace string |
| 43 | + nodeName string |
| 44 | + ) |
| 45 | + |
| 46 | + BeforeEach(func() { |
| 47 | + ctx = context.Background() |
| 48 | + scheme = runtime.NewScheme() |
| 49 | + Expect(corev1.AddToScheme(scheme)).To(Succeed()) |
| 50 | + Expect(maintenanceoperator.AddToScheme(scheme)).To(Succeed()) |
| 51 | + namespace = "test-ns" |
| 52 | + nodeName = "test-node" |
| 53 | + }) |
| 54 | + |
| 55 | + It("SetNodeLabel adds, updates and deletes a label via strategic merge patch", func() { |
| 56 | + node := &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: nodeName}} |
| 57 | + cl := fake.NewClientBuilder().WithScheme(scheme).WithObjects(node).Build() |
| 58 | + m := maintenanceManager{client: cl, nodeName: nodeName} |
| 59 | + |
| 60 | + // add |
| 61 | + Expect(m.SetNodeWaitLabel(ctx, "value1")).To(Succeed()) |
| 62 | + updated := &corev1.Node{} |
| 63 | + Expect(m.client.Get(ctx, types.NamespacedName{Name: nodeName}, updated)).To(Succeed()) |
| 64 | + Expect(updated.Labels).To(HaveKeyWithValue(consts.NodeNicConfigurationWaitLabel, "value1")) |
| 65 | + |
| 66 | + // same value (no-op server-side) |
| 67 | + Expect(m.SetNodeWaitLabel(ctx, "value1")).To(Succeed()) |
| 68 | + |
| 69 | + // update |
| 70 | + Expect(m.SetNodeWaitLabel(ctx, "value2")).To(Succeed()) |
| 71 | + Expect(m.client.Get(ctx, types.NamespacedName{Name: nodeName}, updated)).To(Succeed()) |
| 72 | + Expect(updated.Labels).To(HaveKeyWithValue(consts.NodeNicConfigurationWaitLabel, "value2")) |
| 73 | + |
| 74 | + // delete |
| 75 | + Expect(m.SetNodeWaitLabel(ctx, "")).To(Succeed()) |
| 76 | + Expect(m.client.Get(ctx, types.NamespacedName{Name: nodeName}, updated)).To(Succeed()) |
| 77 | + Expect(updated.Labels).ToNot(HaveKey(consts.NodeNicConfigurationWaitLabel)) |
| 78 | + }) |
| 79 | + |
| 80 | + It("schedules maintenance and sets the wait label; second call is idempotent", func() { |
| 81 | + node := &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: nodeName}} |
| 82 | + cl := fake.NewClientBuilder().WithScheme(scheme).WithObjects(node).Build() |
| 83 | + m := maintenanceManager{client: cl, nodeName: nodeName, namespace: namespace} |
| 84 | + |
| 85 | + // first schedule creates one object and sets wait label true |
| 86 | + Expect(m.ScheduleMaintenance(ctx)).To(Succeed()) |
| 87 | + |
| 88 | + nmList := &maintenanceoperator.NodeMaintenanceList{} |
| 89 | + Expect(cl.List(ctx, nmList, clientInNamespace(namespace))).To(Succeed()) |
| 90 | + Expect(nmList.Items).To(HaveLen(1)) |
| 91 | + Expect(nmList.Items[0].Spec.NodeName).To(Equal(nodeName)) |
| 92 | + Expect(nmList.Items[0].Spec.RequestorID).To(Equal(consts.MaintenanceRequestor)) |
| 93 | + |
| 94 | + updated := &corev1.Node{} |
| 95 | + Expect(cl.Get(ctx, types.NamespacedName{Name: nodeName}, updated)).To(Succeed()) |
| 96 | + Expect(updated.Labels).To(HaveKeyWithValue(consts.NodeNicConfigurationWaitLabel, consts.LabelValueTrue)) |
| 97 | + |
| 98 | + // second schedule is a no-op and label remains true |
| 99 | + Expect(m.ScheduleMaintenance(ctx)).To(Succeed()) |
| 100 | + nmList = &maintenanceoperator.NodeMaintenanceList{} |
| 101 | + Expect(cl.List(ctx, nmList, clientInNamespace(namespace))).To(Succeed()) |
| 102 | + Expect(nmList.Items).To(HaveLen(1)) |
| 103 | + Expect(cl.Get(ctx, types.NamespacedName{Name: nodeName}, updated)).To(Succeed()) |
| 104 | + Expect(updated.Labels).To(HaveKeyWithValue(consts.NodeNicConfigurationWaitLabel, consts.LabelValueTrue)) |
| 105 | + }) |
| 106 | + |
| 107 | + It("reports maintenance allowed only when Ready condition is true", func() { |
| 108 | + node := &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: nodeName}} |
| 109 | + cl := fake.NewClientBuilder().WithScheme(scheme).WithObjects(node).Build() |
| 110 | + m := maintenanceManager{client: cl, nodeName: nodeName, namespace: namespace} |
| 111 | + |
| 112 | + // no object |
| 113 | + allowed, err := m.MaintenanceAllowed(ctx) |
| 114 | + Expect(err).To(BeNil()) |
| 115 | + Expect(allowed).To(BeFalse()) |
| 116 | + |
| 117 | + // object without Ready condition |
| 118 | + nm := &maintenanceoperator.NodeMaintenance{ |
| 119 | + ObjectMeta: metav1.ObjectMeta{Name: consts.MaintenanceRequestName + "-" + nodeName, Namespace: namespace}, |
| 120 | + Spec: maintenanceoperator.NodeMaintenanceSpec{RequestorID: consts.MaintenanceRequestor, NodeName: nodeName}, |
| 121 | + } |
| 122 | + cl = fake.NewClientBuilder().WithScheme(scheme).WithObjects(node, nm).Build() |
| 123 | + m.client = cl |
| 124 | + |
| 125 | + allowed, err = m.MaintenanceAllowed(ctx) |
| 126 | + Expect(err).To(BeNil()) |
| 127 | + Expect(allowed).To(BeFalse()) |
| 128 | + |
| 129 | + // object with Ready=false |
| 130 | + nm.Status.Conditions = []metav1.Condition{{Type: maintenanceoperator.ConditionTypeReady, Status: metav1.ConditionFalse}} |
| 131 | + cl = fake.NewClientBuilder().WithScheme(scheme).WithObjects(node, nm).Build() |
| 132 | + m.client = cl |
| 133 | + allowed, err = m.MaintenanceAllowed(ctx) |
| 134 | + Expect(err).To(BeNil()) |
| 135 | + Expect(allowed).To(BeFalse()) |
| 136 | + |
| 137 | + // object with Ready=true |
| 138 | + nm.Status.Conditions = []metav1.Condition{{Type: maintenanceoperator.ConditionTypeReady, Status: metav1.ConditionTrue}} |
| 139 | + cl = fake.NewClientBuilder().WithScheme(scheme).WithObjects(node, nm).Build() |
| 140 | + m.client = cl |
| 141 | + allowed, err = m.MaintenanceAllowed(ctx) |
| 142 | + Expect(err).To(BeNil()) |
| 143 | + Expect(allowed).To(BeTrue()) |
| 144 | + }) |
| 145 | + |
| 146 | + It("releases maintenance and clears the wait label when present", func() { |
| 147 | + node := &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: nodeName}} |
| 148 | + nm := &maintenanceoperator.NodeMaintenance{ |
| 149 | + ObjectMeta: metav1.ObjectMeta{Name: consts.MaintenanceRequestName + "-" + nodeName, Namespace: namespace}, |
| 150 | + Spec: maintenanceoperator.NodeMaintenanceSpec{RequestorID: consts.MaintenanceRequestor, NodeName: nodeName}, |
| 151 | + } |
| 152 | + cl := fake.NewClientBuilder().WithScheme(scheme).WithObjects(node, nm).Build() |
| 153 | + m := maintenanceManager{client: cl, nodeName: nodeName, namespace: namespace} |
| 154 | + |
| 155 | + // ensure label is set true first (simulate schedule) |
| 156 | + Expect(m.SetNodeWaitLabel(ctx, consts.LabelValueTrue)).To(Succeed()) |
| 157 | + |
| 158 | + // release maintenance should delete object and set label false |
| 159 | + Expect(m.ReleaseMaintenance(ctx)).To(Succeed()) |
| 160 | + nmList := &maintenanceoperator.NodeMaintenanceList{} |
| 161 | + Expect(cl.List(ctx, nmList, clientInNamespace(namespace))).To(Succeed()) |
| 162 | + Expect(nmList.Items).To(HaveLen(0)) |
| 163 | + |
| 164 | + updated := &corev1.Node{} |
| 165 | + Expect(cl.Get(ctx, types.NamespacedName{Name: nodeName}, updated)).To(Succeed()) |
| 166 | + Expect(updated.Labels).To(HaveKeyWithValue(consts.NodeNicConfigurationWaitLabel, consts.LabelValueFalse)) |
| 167 | + }) |
| 168 | + |
| 169 | + It("calls host utils to reboot and propagates errors", func() { |
| 170 | + mockHU := &hostmocks.HostUtils{} |
| 171 | + mockHU.On("ScheduleReboot").Return(nil).Once() |
| 172 | + m := maintenanceManager{hostUtils: mockHU} |
| 173 | + Expect(m.Reboot()).To(Succeed()) |
| 174 | + mockHU.AssertExpectations(GinkgoT()) |
| 175 | + |
| 176 | + mockHU2 := &hostmocks.HostUtils{} |
| 177 | + rebootErr := fmt.Errorf("reboot failed") |
| 178 | + mockHU2.On("ScheduleReboot").Return(rebootErr).Once() |
| 179 | + m = maintenanceManager{hostUtils: mockHU2} |
| 180 | + Expect(m.Reboot()).To(MatchError(rebootErr)) |
| 181 | + mockHU2.AssertExpectations(GinkgoT()) |
| 182 | + }) |
| 183 | +}) |
| 184 | + |
| 185 | +// helpers |
| 186 | +func clientInNamespace(ns string) clientListOptionInNamespace { |
| 187 | + return clientListOptionInNamespace{Namespace: ns} |
| 188 | +} |
| 189 | + |
| 190 | +type clientListOptionInNamespace struct{ Namespace string } |
| 191 | + |
| 192 | +func (o clientListOptionInNamespace) ApplyToList(opts *client.ListOptions) { |
| 193 | + opts.Namespace = o.Namespace |
| 194 | +} |
0 commit comments