Skip to content

Commit

Permalink
fix: do not allow removing/changing the reserved labels (#944)
Browse files Browse the repository at this point in the history
We should not allow the reserved labels to be removed or edited as this
will affect the directpv operations.

also, show only custom labels in `kubectl directpv list drives
--show-labels` and `kubectl directpv list volumes --show-labels`
  • Loading branch information
Praveenrajmani authored Sep 11, 2024
1 parent 2edc6b0 commit c43ec19
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
10 changes: 8 additions & 2 deletions cmd/kubectl-directpv/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"sort"
"strings"

"github.com/minio/directpv/pkg/apis/directpv.min.io/types"
"github.com/minio/directpv/pkg/consts"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -64,8 +65,13 @@ func validateListCmd() error {
func labelsToString(labels map[string]string) string {
var labelsArray []string
for k, v := range labels {
k = strings.TrimPrefix(k, consts.GroupName+"/")
labelsArray = append(labelsArray, fmt.Sprintf("%s=%v", k, v))
if !types.LabelKey(k).IsReserved() {
k = strings.TrimPrefix(k, consts.GroupName+"/")
labelsArray = append(labelsArray, fmt.Sprintf("%s=%v", k, v))
}
}
if len(labelsArray) == 0 {
return "-"
}
sort.Strings(labelsArray)
return strings.Join(labelsArray, ",")
Expand Down
11 changes: 11 additions & 0 deletions pkg/admin/label_drives.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ func (client *Client) LabelDrives(ctx context.Context, args LabelDriveArgs, labe
log = nullLogger
}

for _, label := range labels {
if label.Key.IsReserved() {
action := "use"
if label.Remove {
action = "remove"
}
err = fmt.Errorf("cannot %v reserved key %v", action, label.Key)
return
}
}

var processed bool
ctx, cancelFunc := context.WithCancel(ctx)
defer cancelFunc()
Expand Down
11 changes: 11 additions & 0 deletions pkg/admin/label_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ func (client *Client) LabelVolumes(ctx context.Context, args LabelVolumeArgs, la
log = nullLogger
}

for _, label := range labels {
if label.Key.IsReserved() {
action := "use"
if label.Remove {
action = "remove"
}
err = fmt.Errorf("cannot %v reserved key %v", action, label.Key)
return
}
}

ctx, cancelFunc := context.WithCancel(ctx)
defer cancelFunc()

Expand Down
29 changes: 29 additions & 0 deletions pkg/apis/directpv.min.io/types/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,35 @@ const (
PluginVersionLabelKey LabelKey = consts.GroupName + "/plugin-version"
)

var reservedLabelKeys = map[LabelKey]struct{}{
NodeLabelKey: {},
DriveNameLabelKey: {},
AccessTierLabelKey: {},
DriveLabelKey: {},
VersionLabelKey: {},
CreatedByLabelKey: {},
PodNameLabelKey: {},
PodNSLabelKey: {},
LatestVersionLabelKey: {},
TopologyDriverIdentity: {},
TopologyDriverRack: {},
TopologyDriverZone: {},
TopologyDriverRegion: {},
MigratedLabelKey: {},
RequestIDLabelKey: {},
SuspendLabelKey: {},
VolumeClaimIDLabelKey: {},
ClaimIDLabelKey: {},
ImageTagLabelKey: {},
PluginVersionLabelKey: {},
}

// IsReserved returns if the key is a reserved key
func (k LabelKey) IsReserved() bool {
_, found := reservedLabelKeys[k]
return found || strings.HasPrefix(string(k), VolumeClaimIDLabelKeyPrefix)
}

// LabelValue is a type definition for label value
type LabelValue string

Expand Down

0 comments on commit c43ec19

Please sign in to comment.