Skip to content

Commit

Permalink
add label with metadata.name to LVG
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksandr Zimin <[email protected]>
  • Loading branch information
AleksZimin committed Aug 5, 2024
1 parent 71eaf34 commit 74564d6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
16 changes: 15 additions & 1 deletion images/agent/src/pkg/controller/lvm_volume_group_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"context"
"errors"
"fmt"

"github.com/deckhouse/sds-node-configurator/api/v1alpha1"
errors2 "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -43,7 +44,7 @@ import (

const (
LVMVolumeGroupWatcherCtrlName = "lvm-volume-group-watcher-controller"
LVGHostnameLabelKey = "kubernetes.io/metadata.name"
LVGMetadateNameLabelKey = "kubernetes.io/metadata.name"
)

func RunLVMVolumeGroupWatcherController(
Expand Down Expand Up @@ -124,6 +125,19 @@ func RunLVMVolumeGroupWatcherController(
}
log.Debug(fmt.Sprintf("[RunLVMVolumeGroupWatcherController] the LVMVolumeGroup %s belongs to the node %s. Starts to reconcile", lvg.Name, cfg.NodeName))

log.Debug(fmt.Sprintf("[RunLVMVolumeGroupWatcherController] tries to add label %s to the LVMVolumeGroup %s", LVGMetadateNameLabelKey, cfg.NodeName))
added, err = addLVGLabelIfNeeded(ctx, cl, log, lvg, LVGMetadateNameLabelKey, lvg.Name)
if err != nil {
log.Error(err, fmt.Sprintf("[RunLVMVolumeGroupWatcherController] unable to add label %s to the LVMVolumeGroup %s", LVGMetadateNameLabelKey, lvg.Name))
return reconcile.Result{}, err
}

if added {
log.Debug(fmt.Sprintf("[RunLVMVolumeGroupWatcherController] successfully added label %s to the LVMVolumeGroup %s", LVGMetadateNameLabelKey, lvg.Name))
} else {
log.Debug(fmt.Sprintf("[RunLVMVolumeGroupWatcherController] no need to add label %s to the LVMVolumeGroup %s", LVGMetadateNameLabelKey, lvg.Name))
}

// this case handles the situation when a user decides to remove LVMVolumeGroup resource without created VG
vgs, _ := sdsCache.GetVGs()
if !checkIfVGExist(lvg.Spec.ActualVGNameOnTheNode, vgs) && lvg.DeletionTimestamp != nil {
Expand Down
43 changes: 31 additions & 12 deletions images/agent/src/pkg/controller/lvm_volume_group_watcher_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@ import (
"context"
"errors"
"fmt"
"github.com/deckhouse/sds-node-configurator/api/v1alpha1"
"k8s.io/apimachinery/pkg/api/resource"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/strings/slices"
"reflect"
"strconv"
"strings"
"time"

"github.com/deckhouse/sds-node-configurator/api/v1alpha1"
"k8s.io/apimachinery/pkg/api/resource"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/strings/slices"

"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -73,22 +74,22 @@ func checkIfVGExist(vgName string, vgs []internal.VGData) bool {
return false
}

func checkLabels(log logger.Logger, newLVG *v1alpha1.LvmVolumeGroup) bool {
func checkLVGLabels(log logger.Logger, lvg *v1alpha1.LvmVolumeGroup, labelKey, labelValue string) bool {

if newLVG.Labels == nil {
log.Debug(fmt.Sprintf("[shouldLVGWatcherReconcileUpdateEvent] update event should be reconciled as the LVMVolumeGroup %s has no labels", newLVG.Name))
if lvg.Labels == nil {
log.Debug(fmt.Sprintf("[checkLabels] the LVMVolumeGroup %s has no labels.", lvg.Name))
return false
}

labelValue, exist := newLVG.Labels[LVGHostnameLabelKey]
val, exist := lvg.Labels[labelKey]

if !exist {
log.Debug(fmt.Sprintf("[shouldLVGWatcherReconcileUpdateEvent] update event should be reconciled as the LVMVolumeGroup %s has no label %s", newLVG.Name, LVGHostnameLabelKey))
log.Debug(fmt.Sprintf("[checkLabels] the LVMVolumeGroup %s has no label %s.", lvg.Name, labelKey))
return false
}

if labelValue != newLVG.Name {
log.Debug(fmt.Sprintf("[shouldLVGWatcherReconcileUpdateEvent] update event should be reconciled as the LVMVolumeGroup %s has label %s but the value is incorrect - %q, but should be %q", newLVG.Name, LVGHostnameLabelKey, labelValue, newLVG.Name))
if val != labelValue {
log.Debug(fmt.Sprintf("[checkLabels] the LVMVolumeGroup %s has label %s but the value is incorrect - %q (should be %q)", lvg.Name, labelKey, val, labelValue))
return false
}

Expand All @@ -101,7 +102,7 @@ func shouldLVGWatcherReconcileUpdateEvent(log logger.Logger, oldLVG, newLVG *v1a
return true
}

if !checkLabels(log, newLVG) {
if !checkLVGLabels(log, newLVG, LVGMetadateNameLabelKey, newLVG.Name) {
log.Debug(fmt.Sprintf("[shouldLVGWatcherReconcileUpdateEvent] update event should be reconciled as the LVMVolumeGroup's %s labels have been changed", newLVG.Name))
return true
}
Expand Down Expand Up @@ -985,3 +986,21 @@ func ExtendThinPool(log logger.Logger, metrics monitoring.Metrics, lvg *v1alpha1

return nil
}

func addLVGLabelIfNeeded(ctx context.Context, cl client.Client, log logger.Logger, lvg *v1alpha1.LvmVolumeGroup, labelKey, labelValue string) (bool, error) {
if checkLVGLabels(log, lvg, labelKey, labelValue) {
return false, nil
}

if lvg.Labels == nil {
lvg.Labels = make(map[string]string)
}

lvg.Labels[labelKey] = labelValue
err := cl.Update(ctx, lvg)
if err != nil {
return false, err
}

return true, nil
}

0 comments on commit 74564d6

Please sign in to comment.