Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SKS-2344: Fix issue when cleaning unused CloudTower labels created by CAPE every 24h #168

Merged
merged 2 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
jessehu marked this conversation as resolved.
Show resolved Hide resolved
},
}

Expand Down