diff --git a/controllers/elfcluster_controller.go b/controllers/elfcluster_controller.go index e1134236..9e50fa10 100644 --- a/controllers/elfcluster_controller.go +++ b/controllers/elfcluster_controller.go @@ -293,7 +293,7 @@ func (r *ElfClusterReconciler) cleanOrphanLabels(ctx *context.ClusterContext) { ctx.Logger.V(1).Info(fmt.Sprintf("Cleaning orphan labels in Tower %s created by CAPE", ctx.ElfCluster.Spec.Tower.Server)) keys := []string{towerresources.GetVMLabelClusterName(), towerresources.GetVMLabelVIP(), towerresources.GetVMLabelNamespace()} - labelIDs, err := ctx.VMService.CleanLabels(keys) + labelIDs, err := ctx.VMService.CleanUnusedLabels(keys) if err != nil { ctx.Logger.Error(err, fmt.Sprintf("Warning: failed to clean orphan labels in Tower %s", ctx.ElfCluster.Spec.Tower.Server)) diff --git a/controllers/elfcluster_controller_test.go b/controllers/elfcluster_controller_test.go index ff040f19..df7fbe2a 100644 --- a/controllers/elfcluster_controller_test.go +++ b/controllers/elfcluster_controller_test.go @@ -136,7 +136,7 @@ var _ = Describe("ElfClusterReconciler", func() { fake.InitClusterOwnerReferences(ctrlContext, elfCluster, cluster) keys := []string{towerresources.GetVMLabelClusterName(), towerresources.GetVMLabelVIP(), towerresources.GetVMLabelNamespace()} - mockVMService.EXPECT().CleanLabels(keys).Return(nil, nil) + mockVMService.EXPECT().CleanUnusedLabels(keys).Return(nil, nil) elfClusterKey := capiutil.ObjectKey(elfCluster) reconciler := &ElfClusterReconciler{ControllerContext: ctrlContext, NewVMService: mockNewVMService} @@ -286,7 +286,7 @@ var _ = Describe("ElfClusterReconciler", func() { }) }) - Context("CleanLabels", func() { + Context("CleanUnusedLabels", func() { BeforeEach(func() { resetMemoryCache() }) @@ -308,13 +308,13 @@ var _ = Describe("ElfClusterReconciler", func() { logBuffer.Reset() unexpectedError := errors.New("unexpected error") keys := []string{towerresources.GetVMLabelClusterName(), towerresources.GetVMLabelVIP(), towerresources.GetVMLabelNamespace()} - mockVMService.EXPECT().CleanLabels(keys).Return(nil, unexpectedError) + mockVMService.EXPECT().CleanUnusedLabels(keys).Return(nil, unexpectedError) reconciler := &ElfClusterReconciler{ControllerContext: ctrlContext, NewVMService: mockNewVMService} reconciler.cleanOrphanLabels(clusterContext) Expect(logBuffer.String()).To(ContainSubstring(fmt.Sprintf("Warning: failed to clean orphan labels in Tower %s", elfCluster.Spec.Tower.Server))) logBuffer.Reset() - mockVMService.EXPECT().CleanLabels(keys).Return(nil, nil) + mockVMService.EXPECT().CleanUnusedLabels(keys).Return(nil, nil) reconciler.cleanOrphanLabels(clusterContext) Expect(logBuffer.String()).To(ContainSubstring(fmt.Sprintf("Labels of Tower %s are cleaned successfully", elfCluster.Spec.Tower.Server))) diff --git a/pkg/service/mock_services/vm_mock.go b/pkg/service/mock_services/vm_mock.go index 1411d174..057af947 100644 --- a/pkg/service/mock_services/vm_mock.go +++ b/pkg/service/mock_services/vm_mock.go @@ -83,19 +83,19 @@ func (mr *MockVMServiceMockRecorder) AddVMsToPlacementGroup(placementGroup, vmID return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddVMsToPlacementGroup", reflect.TypeOf((*MockVMService)(nil).AddVMsToPlacementGroup), placementGroup, vmIDs) } -// CleanLabels mocks base method. -func (m *MockVMService) CleanLabels(keys []string) ([]string, error) { +// CleanUnusedLabels mocks base method. +func (m *MockVMService) CleanUnusedLabels(keys []string) ([]string, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "CleanLabels", keys) + ret := m.ctrl.Call(m, "CleanUnusedLabels", keys) ret0, _ := ret[0].([]string) ret1, _ := ret[1].(error) return ret0, ret1 } -// CleanLabels indicates an expected call of CleanLabels. -func (mr *MockVMServiceMockRecorder) CleanLabels(keys interface{}) *gomock.Call { +// CleanUnusedLabels indicates an expected call of CleanUnusedLabels. +func (mr *MockVMServiceMockRecorder) CleanUnusedLabels(keys interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CleanLabels", reflect.TypeOf((*MockVMService)(nil).CleanLabels), keys) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CleanUnusedLabels", reflect.TypeOf((*MockVMService)(nil).CleanUnusedLabels), keys) } // Clone mocks base method. diff --git a/pkg/service/vm.go b/pkg/service/vm.go index 9fde9b94..fd3cf82c 100644 --- a/pkg/service/vm.go +++ b/pkg/service/vm.go @@ -67,7 +67,7 @@ type VMService interface { GetVlan(id string) (*models.Vlan, error) UpsertLabel(key, value string) (*models.Label, error) DeleteLabel(key, value string, strict bool) (string, error) - CleanLabels(keys []string) ([]string, error) + CleanUnusedLabels(keys []string) ([]string, error) AddLabelsToVM(vmID string, labels []string) (*models.Task, error) CreateVMPlacementGroup(name, clusterID string, vmPolicy models.VMVMPolicy) (*models.WithTaskVMPlacementGroup, error) GetVMPlacementGroup(name string) (*models.VMPlacementGroup, error) @@ -786,14 +786,15 @@ func (svr *TowerVMService) DeleteLabel(key, value string, strict bool) (string, return *deleteLabelResp.Payload[0].Data.ID, nil } -// CleanLabels deletes specified unused labels. -// CleanLabels is used to clean unused labels regularly and should not be called frequently. -func (svr *TowerVMService) CleanLabels(keys []string) ([]string, error) { +// CleanUnusedLabels deletes specified unused labels. +// CleanUnusedLabels is used to clean unused labels regularly and should not be called frequently. +func (svr *TowerVMService) CleanUnusedLabels(keys []string) ([]string, error) { deleteLabelParams := clientlabel.NewDeleteLabelParams() deleteLabelParams.RequestBody = &models.LabelDeletionParams{ Where: &models.LabelWhereInput{ KeyIn: keys, CreatedAtLte: TowerString(time.Now().Add(-24 * time.Hour).UTC().Format(time.RFC3339)), + TotalNum: TowerInt32(0), }, }