Skip to content

Commit

Permalink
fix: do not allow removing/changing the reserved labels (#945)
Browse files Browse the repository at this point in the history
also, show only custom labels in `kubectl directpv list drives
--show-labels` and `kubectl directpv list volumes --show-labels`
  • Loading branch information
Praveenrajmani committed Sep 11, 2024
1 parent 75ecef4 commit 7eb41df
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 @@ -62,6 +62,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 7eb41df

Please sign in to comment.