Skip to content

Commit

Permalink
Export DirectPV client and admin API package (#889)
Browse files Browse the repository at this point in the history
  • Loading branch information
Praveenrajmani committed Jul 10, 2024
1 parent 6cf6998 commit 8d75e9f
Show file tree
Hide file tree
Showing 98 changed files with 3,938 additions and 2,106 deletions.
2 changes: 1 addition & 1 deletion cmd/directpv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import (
"syscall"
"time"

"github.com/minio/directpv/pkg/admin/installer"
directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types"
"github.com/minio/directpv/pkg/client"
"github.com/minio/directpv/pkg/consts"
"github.com/minio/directpv/pkg/installer"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"k8s.io/klog/v2"
Expand Down
90 changes: 19 additions & 71 deletions cmd/kubectl-directpv/clean.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// This file is part of MinIO DirectPV
// Copyright (c) 2021, 2022 MinIO, Inc.
// Copyright (c) 2024 MinIO, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
Expand All @@ -19,20 +19,12 @@ package main
import (
"context"
"errors"
"fmt"
"os"
"strings"

"github.com/minio/directpv/pkg/client"
"github.com/minio/directpv/pkg/admin"
"github.com/minio/directpv/pkg/consts"
"github.com/minio/directpv/pkg/k8s"
"github.com/minio/directpv/pkg/types"
"github.com/minio/directpv/pkg/utils"
"github.com/minio/directpv/pkg/volume"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var cleanCmd = &cobra.Command{
Expand Down Expand Up @@ -68,7 +60,7 @@ var cleanCmd = &cobra.Command{
volumeNameArgs = args

if err := validateCleanCmd(); err != nil {
utils.Eprintf(quietFlag, true, "%v\n", err)
eprintf(true, "%v\n", err)
os.Exit(-1)
}

Expand Down Expand Up @@ -138,65 +130,21 @@ func validateCleanCmd() error {
}

func cleanMain(ctx context.Context) {
ctx, cancelFunc := context.WithCancel(ctx)
defer cancelFunc()

resultCh := volume.NewLister().
NodeSelector(toLabelValues(nodesArgs)).
DriveNameSelector(toLabelValues(drivesArgs)).
DriveIDSelector(toLabelValues(driveIDArgs)).
PodNameSelector(toLabelValues(podNameArgs)).
PodNSSelector(toLabelValues(podNSArgs)).
StatusSelector(volumeStatusSelectors).
VolumeNameSelector(volumeNameArgs).
List(ctx)

matchFunc := func(volume *types.Volume) bool {
pv, err := k8s.KubeClient().CoreV1().PersistentVolumes().Get(ctx, volume.Name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
return true
}
utils.Eprintf(quietFlag, true, "unable to get PV for volume %v; %v\n", volume.Name, err)
return false
}
switch pv.Status.Phase {
case corev1.VolumeReleased, corev1.VolumeFailed:
return true
default:
return false
}
}

for result := range resultCh {
if result.Err != nil {
utils.Eprintf(quietFlag, true, "%v\n", result.Err)
os.Exit(1)
}

if !matchFunc(&result.Volume) {
continue
}

result.Volume.RemovePVProtection()

if dryRunFlag {
continue
}

if _, err := client.VolumeClient().Update(ctx, &result.Volume, metav1.UpdateOptions{
TypeMeta: types.NewVolumeTypeMeta(),
}); err != nil {
utils.Eprintf(quietFlag, true, "%v\n", err)
os.Exit(1)
}
if err := client.VolumeClient().Delete(ctx, result.Volume.Name, metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
utils.Eprintf(quietFlag, true, "%v\n", err)
os.Exit(1)
}

if !quietFlag {
fmt.Println("Removing volume", result.Volume.Name)
}
_, err := adminClient.Clean(
ctx,
admin.CleanArgs{
Nodes: nodesArgs,
Drives: drivesArgs,
DriveIDs: driveIDArgs,
PodNames: podNameArgs,
PodNamespaces: podNSArgs,
VolumeStatus: volumeStatusSelectors,
VolumeNames: volumeNameArgs,
},
logFunc,
)
if err != nil {
eprintf(true, "%v\n", err)
os.Exit(1)
}
}
81 changes: 15 additions & 66 deletions cmd/kubectl-directpv/cordon.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,12 @@ package main
import (
"context"
"errors"
"fmt"
"os"
"strings"

directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types"
"github.com/minio/directpv/pkg/client"
"github.com/minio/directpv/pkg/admin"
"github.com/minio/directpv/pkg/consts"
"github.com/minio/directpv/pkg/drive"
"github.com/minio/directpv/pkg/utils"
"github.com/minio/directpv/pkg/volume"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var cordonCmd = &cobra.Command{
Expand Down Expand Up @@ -60,7 +54,7 @@ var cordonCmd = &cobra.Command{
driveIDArgs = args

if err := validateCordonCmd(); err != nil {
utils.Eprintf(quietFlag, true, "%v\n", err)
eprintf(true, "%v\n", err)
os.Exit(-1)
}

Expand Down Expand Up @@ -116,64 +110,19 @@ func validateCordonCmd() error {
}

func cordonMain(ctx context.Context) {
var processed bool

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

resultCh := drive.NewLister().
NodeSelector(toLabelValues(nodesArgs)).
DriveNameSelector(toLabelValues(drivesArgs)).
StatusSelector(driveStatusSelectors).
DriveIDSelector(driveIDSelectors).
List(ctx)
for result := range resultCh {
if result.Err != nil {
utils.Eprintf(quietFlag, true, "%v\n", result.Err)
os.Exit(1)
}

processed = true

if result.Drive.IsUnschedulable() {
continue
}

volumes := result.Drive.GetVolumes()
if len(volumes) != 0 {
for vresult := range volume.NewLister().VolumeNameSelector(volumes).IgnoreNotFound(true).List(ctx) {
if vresult.Err != nil {
utils.Eprintf(quietFlag, true, "%v\n", vresult.Err)
os.Exit(1)
}

if vresult.Volume.Status.Status == directpvtypes.VolumeStatusPending {
utils.Eprintf(quietFlag, true, "unable to cordon drive %v; pending volumes found\n", result.Drive.GetDriveID())
os.Exit(1)
}
}
}

result.Drive.Unschedulable()
if !dryRunFlag {
if _, err := client.DriveClient().Update(ctx, &result.Drive, metav1.UpdateOptions{}); err != nil {
utils.Eprintf(quietFlag, true, "unable to cordon drive %v; %v\n", result.Drive.GetDriveID(), err)
os.Exit(1)
}
}

if !quietFlag {
fmt.Printf("Drive %v/%v cordoned\n", result.Drive.GetNodeID(), result.Drive.GetDriveName())
}
}

if !processed {
if allFlag {
utils.Eprintf(quietFlag, false, "No resources found\n")
} else {
utils.Eprintf(quietFlag, false, "No matching resources found\n")
}

_, err := adminClient.Cordon(
ctx,
admin.CordonArgs{
Nodes: nodesArgs,
Drives: drivesArgs,
Status: driveStatusSelectors,
DriveIDs: driveIDSelectors,
DryRun: dryRunFlag,
},
logFunc,
)
if err != nil {
eprintf(!errors.Is(err, admin.ErrNoMatchingResourcesFound), "%v\n", err)
os.Exit(1)
}
}
Loading

0 comments on commit 8d75e9f

Please sign in to comment.