Skip to content

Commit

Permalink
Move pkg/utils/ToLabelValues into pkg/apis/directpv.min.io/types (#975)
Browse files Browse the repository at this point in the history
Signed-off-by: Bala.FA <bala@minio.io>
  • Loading branch information
balamurugana authored Nov 28, 2024
1 parent e0d9577 commit 5397330
Show file tree
Hide file tree
Showing 17 changed files with 52 additions and 65 deletions.
3 changes: 1 addition & 2 deletions cmd/kubectl-directpv/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types"
"github.com/minio/directpv/pkg/consts"
"github.com/minio/directpv/pkg/types"
"github.com/minio/directpv/pkg/utils"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/watch"
)
Expand Down Expand Up @@ -255,7 +254,7 @@ func discoverDevices(ctx context.Context, nodes, drives []string, teaProgram *te
defer cancel()

eventCh, stop, err := adminClient.NewNodeLister().
NodeSelector(utils.ToLabelValues(nodes)).
NodeSelector(directpvtypes.ToLabelValues(nodes)).
Watch(ctx)
if err != nil {
return nil, err
Expand Down
5 changes: 2 additions & 3 deletions cmd/kubectl-directpv/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types"
"github.com/minio/directpv/pkg/consts"
"github.com/minio/directpv/pkg/types"
"github.com/minio/directpv/pkg/utils"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
Expand Down Expand Up @@ -167,7 +166,7 @@ func initDevices(ctx context.Context, initRequests []types.InitRequest, requestI
defer cancel()

eventCh, stop, err := adminClient.NewInitRequestLister().
RequestIDSelector(utils.ToLabelValues([]string{requestID})).
RequestIDSelector(directpvtypes.ToLabelValues([]string{requestID})).
Watch(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -235,7 +234,7 @@ func initMain(ctx context.Context, inputFile string) {
}
defer func() {
labelMap := map[directpvtypes.LabelKey][]directpvtypes.LabelValue{
directpvtypes.RequestIDLabelKey: utils.ToLabelValues([]string{requestID}),
directpvtypes.RequestIDLabelKey: directpvtypes.ToLabelValues([]string{requestID}),
}
adminClient.InitRequest().DeleteCollection(ctx, metav1.DeleteOptions{}, metav1.ListOptions{
LabelSelector: directpvtypes.ToLabelSelector(labelMap),
Expand Down
5 changes: 2 additions & 3 deletions cmd/kubectl-directpv/list_drives.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types"
"github.com/minio/directpv/pkg/consts"
"github.com/minio/directpv/pkg/types"
"github.com/minio/directpv/pkg/utils"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -117,8 +116,8 @@ func validateListDrivesArgs() error {

func listDrivesMain(ctx context.Context) {
drives, err := adminClient.NewDriveLister().
NodeSelector(utils.ToLabelValues(nodesArgs)).
DriveNameSelector(utils.ToLabelValues(drivesArgs)).
NodeSelector(directpvtypes.ToLabelValues(nodesArgs)).
DriveNameSelector(directpvtypes.ToLabelValues(drivesArgs)).
StatusSelector(driveStatusSelectors).
DriveIDSelector(driveIDSelectors).
LabelSelector(labelSelectors).
Expand Down
11 changes: 5 additions & 6 deletions cmd/kubectl-directpv/list_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types"
"github.com/minio/directpv/pkg/consts"
"github.com/minio/directpv/pkg/types"
"github.com/minio/directpv/pkg/utils"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -154,11 +153,11 @@ func getPVCName(ctx context.Context, volume types.Volume) string {

func listVolumesMain(ctx context.Context) {
volumes, err := adminClient.NewVolumeLister().
NodeSelector(utils.ToLabelValues(nodesArgs)).
DriveNameSelector(utils.ToLabelValues(drivesArgs)).
DriveIDSelector(utils.ToLabelValues(driveIDArgs)).
PodNameSelector(utils.ToLabelValues(podNameArgs)).
PodNSSelector(utils.ToLabelValues(podNSArgs)).
NodeSelector(directpvtypes.ToLabelValues(nodesArgs)).
DriveNameSelector(directpvtypes.ToLabelValues(drivesArgs)).
DriveIDSelector(directpvtypes.ToLabelValues(driveIDArgs)).
PodNameSelector(directpvtypes.ToLabelValues(podNameArgs)).
PodNSSelector(directpvtypes.ToLabelValues(podNSArgs)).
StatusSelector(volumeStatusSelectors).
VolumeNameSelector(volumeNameArgs).
LabelSelector(labelSelectors).
Expand Down
11 changes: 5 additions & 6 deletions pkg/admin/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types"
"github.com/minio/directpv/pkg/types"
"github.com/minio/directpv/pkg/utils"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -50,11 +49,11 @@ func (client *Client) Clean(ctx context.Context, args CleanArgs, log LogFunc) (r
defer cancelFunc()

resultCh := client.NewVolumeLister().
NodeSelector(utils.ToLabelValues(args.Nodes)).
DriveNameSelector(utils.ToLabelValues(args.Drives)).
DriveIDSelector(utils.ToLabelValues(args.DriveIDs)).
PodNameSelector(utils.ToLabelValues(args.PodNames)).
PodNSSelector(utils.ToLabelValues(args.PodNamespaces)).
NodeSelector(directpvtypes.ToLabelValues(args.Nodes)).
DriveNameSelector(directpvtypes.ToLabelValues(args.Drives)).
DriveIDSelector(directpvtypes.ToLabelValues(args.DriveIDs)).
PodNameSelector(directpvtypes.ToLabelValues(args.PodNames)).
PodNSSelector(directpvtypes.ToLabelValues(args.PodNamespaces)).
StatusSelector(args.VolumeStatus).
VolumeNameSelector(args.VolumeNames).
List(ctx)
Expand Down
5 changes: 2 additions & 3 deletions pkg/admin/cordon.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"

directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types"
"github.com/minio/directpv/pkg/utils"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -53,8 +52,8 @@ func (client *Client) Cordon(ctx context.Context, args CordonArgs, log LogFunc)
defer cancelFunc()

resultCh := client.NewDriveLister().
NodeSelector(utils.ToLabelValues(args.Nodes)).
DriveNameSelector(utils.ToLabelValues(args.Drives)).
NodeSelector(directpvtypes.ToLabelValues(args.Nodes)).
DriveNameSelector(directpvtypes.ToLabelValues(args.Drives)).
StatusSelector(args.Status).
DriveIDSelector(args.DriveIDs).
List(ctx)
Expand Down
5 changes: 2 additions & 3 deletions pkg/admin/label_drives.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"

directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types"
"github.com/minio/directpv/pkg/utils"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/util/retry"
)
Expand Down Expand Up @@ -79,8 +78,8 @@ func (client *Client) LabelDrives(ctx context.Context, args LabelDriveArgs, labe
defer cancelFunc()

resultCh := client.NewDriveLister().
NodeSelector(utils.ToLabelValues(args.Nodes)).
DriveNameSelector(utils.ToLabelValues(args.Drives)).
NodeSelector(directpvtypes.ToLabelValues(args.Nodes)).
DriveNameSelector(directpvtypes.ToLabelValues(args.Drives)).
StatusSelector(args.DriveStatus).
DriveIDSelector(args.DriveIDs).
LabelSelector(args.LabelSelectors).
Expand Down
11 changes: 5 additions & 6 deletions pkg/admin/label_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"

directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types"
"github.com/minio/directpv/pkg/utils"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/util/retry"
)
Expand Down Expand Up @@ -67,11 +66,11 @@ func (client *Client) LabelVolumes(ctx context.Context, args LabelVolumeArgs, la

var processed bool
resultCh := client.NewVolumeLister().
NodeSelector(utils.ToLabelValues(args.Nodes)).
DriveNameSelector(utils.ToLabelValues(args.Drives)).
DriveIDSelector(utils.ToLabelValues(args.DriveIDs)).
PodNameSelector(utils.ToLabelValues(args.PodNames)).
PodNSSelector(utils.ToLabelValues(args.PodNamespaces)).
NodeSelector(directpvtypes.ToLabelValues(args.Nodes)).
DriveNameSelector(directpvtypes.ToLabelValues(args.Drives)).
DriveIDSelector(directpvtypes.ToLabelValues(args.DriveIDs)).
PodNameSelector(directpvtypes.ToLabelValues(args.PodNames)).
PodNSSelector(directpvtypes.ToLabelValues(args.PodNamespaces)).
StatusSelector(args.VolumeStatus).
VolumeNameSelector(args.VolumeNames).
LabelSelector(args.LabelSelectors).
Expand Down
3 changes: 1 addition & 2 deletions pkg/admin/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types"
"github.com/minio/directpv/pkg/types"
"github.com/minio/directpv/pkg/utils"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/util/retry"
)
Expand All @@ -35,7 +34,7 @@ func (client *Client) RefreshNodes(ctx context.Context, selectedNodes []string)
}

nodes, err := client.NewNodeLister().
NodeSelector(utils.ToLabelValues(selectedNodes)).
NodeSelector(directpvtypes.ToLabelValues(selectedNodes)).
Get(ctx)
if err != nil {
return nil, nil, err
Expand Down
5 changes: 2 additions & 3 deletions pkg/admin/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"fmt"

directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types"
"github.com/minio/directpv/pkg/utils"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -55,8 +54,8 @@ func (client *Client) Remove(ctx context.Context, args RemoveArgs, log LogFunc)
defer cancelFunc()

resultCh := client.NewDriveLister().
NodeSelector(utils.ToLabelValues(args.Nodes)).
DriveNameSelector(utils.ToLabelValues(args.Drives)).
NodeSelector(directpvtypes.ToLabelValues(args.Nodes)).
DriveNameSelector(directpvtypes.ToLabelValues(args.Drives)).
StatusSelector(args.DriveStatus).
DriveIDSelector(args.DriveIDs).
IgnoreNotFound(true).
Expand Down
6 changes: 3 additions & 3 deletions pkg/admin/resume_drives.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"context"
"fmt"

"github.com/minio/directpv/pkg/utils"
directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/util/retry"
)
Expand All @@ -43,8 +43,8 @@ func (client *Client) ResumeDrives(ctx context.Context, args ResumeDriveArgs, lo
defer cancelFunc()

resultCh := client.NewDriveLister().
NodeSelector(utils.ToLabelValues(args.Nodes)).
DriveNameSelector(utils.ToLabelValues(args.Drives)).
NodeSelector(directpvtypes.ToLabelValues(args.Nodes)).
DriveNameSelector(directpvtypes.ToLabelValues(args.Drives)).
DriveIDSelector(args.DriveIDSelectors).
List(ctx)
for result := range resultCh {
Expand Down
10 changes: 5 additions & 5 deletions pkg/admin/resume_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"context"
"fmt"

"github.com/minio/directpv/pkg/utils"
directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/util/retry"
)
Expand All @@ -43,10 +43,10 @@ func (client *Client) ResumeVolumes(ctx context.Context, args ResumeVolumeArgs,
defer cancelFunc()

resultCh := client.NewVolumeLister().
NodeSelector(utils.ToLabelValues(args.Nodes)).
DriveNameSelector(utils.ToLabelValues(args.Drives)).
PodNameSelector(utils.ToLabelValues(args.PodNames)).
PodNSSelector(utils.ToLabelValues(args.PodNamespaces)).
NodeSelector(directpvtypes.ToLabelValues(args.Nodes)).
DriveNameSelector(directpvtypes.ToLabelValues(args.Drives)).
PodNameSelector(directpvtypes.ToLabelValues(args.PodNames)).
PodNSSelector(directpvtypes.ToLabelValues(args.PodNamespaces)).
VolumeNameSelector(args.VolumeNames).
List(ctx)
for result := range resultCh {
Expand Down
5 changes: 2 additions & 3 deletions pkg/admin/suspend_drives.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"fmt"

directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types"
"github.com/minio/directpv/pkg/utils"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/util/retry"
)
Expand Down Expand Up @@ -58,8 +57,8 @@ func (client *Client) SuspendDrives(ctx context.Context, args SuspendDriveArgs,
defer cancelFunc()

resultCh := client.NewDriveLister().
NodeSelector(utils.ToLabelValues(args.Nodes)).
DriveNameSelector(utils.ToLabelValues(args.Drives)).
NodeSelector(directpvtypes.ToLabelValues(args.Nodes)).
DriveNameSelector(directpvtypes.ToLabelValues(args.Drives)).
DriveIDSelector(args.DriveIDSelectors).
List(ctx)
for result := range resultCh {
Expand Down
9 changes: 4 additions & 5 deletions pkg/admin/suspend_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"

directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types"
"github.com/minio/directpv/pkg/utils"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/util/retry"
)
Expand Down Expand Up @@ -58,10 +57,10 @@ func (client *Client) SuspendVolumes(ctx context.Context, args SuspendVolumeArgs
defer cancelFunc()

resultCh := client.NewVolumeLister().
NodeSelector(utils.ToLabelValues(args.Nodes)).
DriveNameSelector(utils.ToLabelValues(args.Drives)).
PodNameSelector(utils.ToLabelValues(args.PodNames)).
PodNSSelector(utils.ToLabelValues(args.PodNamespaces)).
NodeSelector(directpvtypes.ToLabelValues(args.Nodes)).
DriveNameSelector(directpvtypes.ToLabelValues(args.Drives)).
PodNameSelector(directpvtypes.ToLabelValues(args.PodNames)).
PodNSSelector(directpvtypes.ToLabelValues(args.PodNamespaces)).
VolumeNameSelector(args.VolumeNames).
List(ctx)
for result := range resultCh {
Expand Down
6 changes: 3 additions & 3 deletions pkg/admin/uncordon.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"context"
"fmt"

"github.com/minio/directpv/pkg/utils"
directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand All @@ -42,8 +42,8 @@ func (client *Client) Uncordon(ctx context.Context, args UncordonArgs, log LogFu
defer cancelFunc()

resultCh := client.NewDriveLister().
NodeSelector(utils.ToLabelValues(args.Nodes)).
DriveNameSelector(utils.ToLabelValues(args.Drives)).
NodeSelector(directpvtypes.ToLabelValues(args.Nodes)).
DriveNameSelector(directpvtypes.ToLabelValues(args.Drives)).
StatusSelector(args.Status).
DriveIDSelector(args.DriveIDs).
List(ctx)
Expand Down
8 changes: 8 additions & 0 deletions pkg/apis/directpv.min.io/types/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ func ToLabelValue(value string) LabelValue {
return result
}

// ToLabelValues converts a string list to label values
func ToLabelValues(slice []string) (values []LabelValue) {
for _, s := range slice {
values = append(values, ToLabelValue(s))
}
return
}

// ToLabelSelector converts a map of label key and label value to selector string
func ToLabelSelector(labels map[LabelKey][]LabelValue) string {
selectors := []string{}
Expand Down
9 changes: 0 additions & 9 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"strings"

"github.com/fatih/color"
directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types"
"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -158,14 +157,6 @@ func Eprintf(quiet, asErr bool, format string, a ...any) {
fmt.Fprintf(os.Stderr, format, a...)
}

// ToLabelValues converts a string list to label values
func ToLabelValues(slice []string) (values []directpvtypes.LabelValue) {
for _, s := range slice {
values = append(values, directpvtypes.ToLabelValue(s))
}
return
}

// IBytes produces a human readable representation of an IEC size rounding to two decimal places.
func IBytes(ui64 uint64) string {
value := ui64
Expand Down

0 comments on commit 5397330

Please sign in to comment.