Skip to content

Commit

Permalink
[controller] Fix a bug with labels containing spaces. (#89)
Browse files Browse the repository at this point in the history
Signed-off-by: Viktor Kramarenko <[email protected]>
  • Loading branch information
ViktorKram authored Aug 29, 2024
1 parent 6b7a11b commit 6fa875a
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 38 deletions.
2 changes: 2 additions & 0 deletions images/agent/src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ require (
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20240525223248-4bfdf5a9a2af // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gosimple/slug v1.14.0 // indirect
github.com/gosimple/unidecode v1.0.1 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down
4 changes: 4 additions & 0 deletions images/agent/src/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ github.com/google/pprof v0.0.0-20240525223248-4bfdf5a9a2af h1:kmjWCqn2qkEml422C2
github.com/google/pprof v0.0.0-20240525223248-4bfdf5a9a2af/go.mod h1:K1liHPHnj73Fdn/EKuT8nrFqBihUSKXoLYU0BuatOYo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gosimple/slug v1.14.0 h1:RtTL/71mJNDfpUbCOmnf/XFkzKRtD6wL6Uy+3akm4Es=
github.com/gosimple/slug v1.14.0/go.mod h1:UiRaFH+GEilHstLUmcBgWcI42viBN7mAb818JrYOeFQ=
github.com/gosimple/unidecode v1.0.1 h1:hZzFTMMqSswvf0LBJZCZgThIZrpDHFXux9KeGmn6T/o=
github.com/gosimple/unidecode v1.0.1/go.mod h1:CP0Cr1Y1kogOtx0bJblKzsVWrqYaqfNOnHzpgWw4Awc=
github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
Expand Down
20 changes: 20 additions & 0 deletions images/agent/src/internal/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ const (
ReasonTerminating = "Terminating"
ReasonScanFailed = "ScanFailed"
ReasonUpdated = "Updated"

BlockDeviceLabelPrefix = "status.blockdevice.storage.deckhouse.io"

MetadataNameLabelKey = "kubernetes.io/metadata.name"
HostNameLabelKey = "kubernetes.io/hostname"

BlockDeviceTypeLabelKey = BlockDeviceLabelPrefix + "/type"
BlockDeviceFSTypeLabelKey = BlockDeviceLabelPrefix + "/fstype"
BlockDevicePVUUIDLabelKey = BlockDeviceLabelPrefix + "/pvuuid"
BlockDeviceVGUUIDLabelKey = BlockDeviceLabelPrefix + "/vguuid"
BlockDevicePartUUIDLabelKey = BlockDeviceLabelPrefix + "/partuuid"
BlockDeviceLVMVolumeGroupNameLabelKey = BlockDeviceLabelPrefix + "/lvmvolumegroupname"
BlockDeviceActualVGNameLabelKey = BlockDeviceLabelPrefix + "/actualvgnameonthenode"
BlockDeviceWWNLabelKey = BlockDeviceLabelPrefix + "/wwn"
BlockDeviceSerialLabelKey = BlockDeviceLabelPrefix + "/serial"
BlockDeviceSizeLabelKey = BlockDeviceLabelPrefix + "/size"
BlockDeviceModelLabelKey = BlockDeviceLabelPrefix + "/model"
BlockDeviceRotaLabelKey = BlockDeviceLabelPrefix + "/rota"
BlockDeviceHotPlugLabelKey = BlockDeviceLabelPrefix + "/hotplug"
BlockDeviceMachineIDLabelKey = BlockDeviceLabelPrefix + "/machineid"
)

var (
Expand Down
37 changes: 19 additions & 18 deletions images/agent/src/pkg/controller/block_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"time"

"github.com/deckhouse/sds-node-configurator/api/v1alpha1"
"github.com/gosimple/slug"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kclient "sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -44,8 +45,7 @@ import (
)

const (
BlockDeviceCtrlName = "block-device-controller"
BlockDeviceLabelPrefix = "status.blockdevice.storage.deckhouse.io"
BlockDeviceCtrlName = "block-device-controller"
)

func RunBlockDeviceController(
Expand Down Expand Up @@ -549,22 +549,23 @@ func ConfigureBlockDeviceLabels(blockDevice v1alpha1.BlockDevice) map[string]str
labels[key] = value
}

labels["kubernetes.io/metadata.name"] = blockDevice.ObjectMeta.Name
labels["kubernetes.io/hostname"] = blockDevice.Status.NodeName
labels[BlockDeviceLabelPrefix+"/type"] = blockDevice.Status.Type
labels[BlockDeviceLabelPrefix+"/fstype"] = blockDevice.Status.FsType
labels[BlockDeviceLabelPrefix+"/pvuuid"] = blockDevice.Status.PVUuid
labels[BlockDeviceLabelPrefix+"/vguuid"] = blockDevice.Status.VGUuid
labels[BlockDeviceLabelPrefix+"/partuuid"] = blockDevice.Status.PartUUID
labels[BlockDeviceLabelPrefix+"/lvmvolumegroupname"] = blockDevice.Status.LvmVolumeGroupName
labels[BlockDeviceLabelPrefix+"/actualvgnameonthenode"] = blockDevice.Status.ActualVGNameOnTheNode
labels[BlockDeviceLabelPrefix+"/wwn"] = blockDevice.Status.Wwn
labels[BlockDeviceLabelPrefix+"/serial"] = blockDevice.Status.Serial
labels[BlockDeviceLabelPrefix+"/size"] = blockDevice.Status.Size.String()
labels[BlockDeviceLabelPrefix+"/model"] = blockDevice.Status.Model
labels[BlockDeviceLabelPrefix+"/rota"] = strconv.FormatBool(blockDevice.Status.Rota)
labels[BlockDeviceLabelPrefix+"/hotplug"] = strconv.FormatBool(blockDevice.Status.HotPlug)
labels[BlockDeviceLabelPrefix+"/machineid"] = blockDevice.Status.MachineID
slug.Lowercase = false
labels[internal.MetadataNameLabelKey] = slug.Make(blockDevice.ObjectMeta.Name)
labels[internal.HostNameLabelKey] = slug.Make(blockDevice.Status.NodeName)
labels[internal.BlockDeviceTypeLabelKey] = slug.Make(blockDevice.Status.Type)
labels[internal.BlockDeviceFSTypeLabelKey] = slug.Make(blockDevice.Status.FsType)
labels[internal.BlockDevicePVUUIDLabelKey] = blockDevice.Status.PVUuid
labels[internal.BlockDeviceVGUUIDLabelKey] = blockDevice.Status.VGUuid
labels[internal.BlockDevicePartUUIDLabelKey] = blockDevice.Status.PartUUID
labels[internal.BlockDeviceLVMVolumeGroupNameLabelKey] = slug.Make(blockDevice.Status.LvmVolumeGroupName)
labels[internal.BlockDeviceActualVGNameLabelKey] = slug.Make(blockDevice.Status.ActualVGNameOnTheNode)
labels[internal.BlockDeviceWWNLabelKey] = slug.Make(blockDevice.Status.Wwn)
labels[internal.BlockDeviceSerialLabelKey] = slug.Make(blockDevice.Status.Serial)
labels[internal.BlockDeviceSizeLabelKey] = blockDevice.Status.Size.String()
labels[internal.BlockDeviceModelLabelKey] = slug.Make(blockDevice.Status.Model)
labels[internal.BlockDeviceRotaLabelKey] = strconv.FormatBool(blockDevice.Status.Rota)
labels[internal.BlockDeviceHotPlugLabelKey] = strconv.FormatBool(blockDevice.Status.HotPlug)
labels[internal.BlockDeviceMachineIDLabelKey] = slug.Make(blockDevice.Status.MachineID)

return labels
}
Expand Down
38 changes: 19 additions & 19 deletions images/agent/src/pkg/controller/block_device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func TestBlockDeviceCtrl(t *testing.T) {
Serial: "testSERIAL",
Path: "testPATH",
Size: resource.MustParse("0"),
Model: "testMODEL",
Model: "Very good model-1241",
Rota: false,
HotPlug: false,
MachineID: "testMACHINE",
Expand All @@ -346,24 +346,24 @@ func TestBlockDeviceCtrl(t *testing.T) {
}

expectedLabels := map[string]string{
"kubernetes.io/metadata.name": blockDevice.ObjectMeta.Name,
"kubernetes.io/hostname": blockDevice.Status.NodeName,
BlockDeviceLabelPrefix + "/type": blockDevice.Status.Type,
BlockDeviceLabelPrefix + "/fstype": blockDevice.Status.FsType,
BlockDeviceLabelPrefix + "/pvuuid": blockDevice.Status.PVUuid,
BlockDeviceLabelPrefix + "/vguuid": blockDevice.Status.VGUuid,
BlockDeviceLabelPrefix + "/partuuid": blockDevice.Status.PartUUID,
BlockDeviceLabelPrefix + "/lvmvolumegroupname": blockDevice.Status.LvmVolumeGroupName,
BlockDeviceLabelPrefix + "/actualvgnameonthenode": blockDevice.Status.ActualVGNameOnTheNode,
BlockDeviceLabelPrefix + "/wwn": blockDevice.Status.Wwn,
BlockDeviceLabelPrefix + "/serial": blockDevice.Status.Serial,
BlockDeviceLabelPrefix + "/size": blockDevice.Status.Size.String(),
BlockDeviceLabelPrefix + "/model": blockDevice.Status.Model,
BlockDeviceLabelPrefix + "/rota": strconv.FormatBool(blockDevice.Status.Rota),
BlockDeviceLabelPrefix + "/hotplug": strconv.FormatBool(blockDevice.Status.HotPlug),
BlockDeviceLabelPrefix + "/machineid": blockDevice.Status.MachineID,
"some-custom-label1": "v",
"some-custom-label2": "v",
internal.MetadataNameLabelKey: blockDevice.ObjectMeta.Name,
internal.HostNameLabelKey: blockDevice.Status.NodeName,
internal.BlockDeviceTypeLabelKey: blockDevice.Status.Type,
internal.BlockDeviceFSTypeLabelKey: blockDevice.Status.FsType,
internal.BlockDevicePVUUIDLabelKey: blockDevice.Status.PVUuid,
internal.BlockDeviceVGUUIDLabelKey: blockDevice.Status.VGUuid,
internal.BlockDevicePartUUIDLabelKey: blockDevice.Status.PartUUID,
internal.BlockDeviceLVMVolumeGroupNameLabelKey: blockDevice.Status.LvmVolumeGroupName,
internal.BlockDeviceActualVGNameLabelKey: blockDevice.Status.ActualVGNameOnTheNode,
internal.BlockDeviceWWNLabelKey: blockDevice.Status.Wwn,
internal.BlockDeviceSerialLabelKey: blockDevice.Status.Serial,
internal.BlockDeviceSizeLabelKey: blockDevice.Status.Size.String(),
internal.BlockDeviceModelLabelKey: "Very-good-model-1241",
internal.BlockDeviceRotaLabelKey: strconv.FormatBool(blockDevice.Status.Rota),
internal.BlockDeviceHotPlugLabelKey: strconv.FormatBool(blockDevice.Status.HotPlug),
internal.BlockDeviceMachineIDLabelKey: blockDevice.Status.MachineID,
"some-custom-label1": "v",
"some-custom-label2": "v",
}

assert.Equal(t, expectedLabels, ConfigureBlockDeviceLabels(blockDevice))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var _ = Describe("Storage Controller", func() {
Path: deviceName,
Size: resource.Quantity{},
Rota: false,
Model: "",
Model: "very good-model",
Name: "/dev/sda",
HotPlug: false,
KName: "/dev/sda",
Expand Down

0 comments on commit 6fa875a

Please sign in to comment.