Skip to content

Commit

Permalink
SKS-2344: Fix issue when cleaning unused CloudTower labels created by…
Browse files Browse the repository at this point in the history
… CAPE every 24h (#168)
  • Loading branch information
haijianyang authored Jan 15, 2024
1 parent f6ab21f commit 66ba3c9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
2 changes: 1 addition & 1 deletion controllers/elfcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
8 changes: 4 additions & 4 deletions controllers/elfcluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -286,7 +286,7 @@ var _ = Describe("ElfClusterReconciler", func() {
})
})

Context("CleanLabels", func() {
Context("CleanUnusedLabels", func() {
BeforeEach(func() {
resetMemoryCache()
})
Expand All @@ -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)))

Expand Down
12 changes: 6 additions & 6 deletions pkg/service/mock_services/vm_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions pkg/service/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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),
},
}

Expand Down

0 comments on commit 66ba3c9

Please sign in to comment.