Skip to content

Commit

Permalink
Add commands for suspending and resuming drives and volumes
Browse files Browse the repository at this point in the history
Fixes #836
  • Loading branch information
Praveenrajmani committed Sep 15, 2023
1 parent 8fe0838 commit 7ed713b
Show file tree
Hide file tree
Showing 20 changed files with 1,048 additions and 68 deletions.
1 change: 0 additions & 1 deletion .gitignore
Expand Up @@ -7,5 +7,4 @@ kubectl-directpv
!kubectl-directpv/
vdb.xml
drives.yaml
kubectl-*
dist/
3 changes: 3 additions & 0 deletions cmd/kubectl-directpv/list_drives.go
Expand Up @@ -198,6 +198,9 @@ func listDrivesMain(ctx context.Context) {
if drive.IsUnschedulable() {
status += ",SchedulingDisabled"
}
if drive.IsSuspended() {
status += ",Suspended"
}
driveMake := drive.Status.Make
if driveMake == "" {
driveMake = "-"
Expand Down
2 changes: 2 additions & 0 deletions cmd/kubectl-directpv/main.go
Expand Up @@ -156,6 +156,8 @@ Use "{{.CommandPath}} [command] --help" for more information about this command.
mainCmd.AddCommand(migrateCmd)
mainCmd.AddCommand(moveCmd)
mainCmd.AddCommand(cleanCmd)
mainCmd.AddCommand(suspendCmd)
mainCmd.AddCommand(resumeCmd)
mainCmd.AddCommand(removeCmd)
mainCmd.AddCommand(uninstallCmd)
mainCmd.SetHelpCommand(&cobra.Command{
Expand Down
46 changes: 46 additions & 0 deletions cmd/kubectl-directpv/resume.go
@@ -0,0 +1,46 @@
// This file is part of MinIO DirectPV
// Copyright (c) 2022, 2023 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
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package main

import (
"github.com/spf13/cobra"
)

var resumeCmd = &cobra.Command{
Use: "resume",
Short: "Resume suspended drives and volumes",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if parent := cmd.Parent(); parent != nil {
parent.PersistentPreRunE(parent, args)
}
return nil
},
}

func init() {
resumeCmd.Flags().SortFlags = false
resumeCmd.InheritedFlags().SortFlags = false
resumeCmd.LocalFlags().SortFlags = false
resumeCmd.LocalNonPersistentFlags().SortFlags = false
resumeCmd.NonInheritedFlags().SortFlags = false
resumeCmd.PersistentFlags().SortFlags = false

addDryRunFlag(resumeCmd, "Run in dry run mode")

resumeCmd.AddCommand(resumeDrivesCmd)
resumeCmd.AddCommand(resumeVolumesCmd)
}
144 changes: 144 additions & 0 deletions cmd/kubectl-directpv/resume_drives.go
@@ -0,0 +1,144 @@
// This file is part of MinIO DirectPV
// Copyright (c) 2021, 2022, 2023 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
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package main

import (
"context"
"errors"
"fmt"
"os"
"strings"

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

var resumeDrivesCmd = &cobra.Command{
Use: "drives [DRIVE ...]",
Short: "Resume suspended drives",
SilenceUsage: true,
SilenceErrors: true,
Example: strings.ReplaceAll(
`1. Resume all suspended drives from a node
$ kubectl {PLUGIN_NAME} resume drives --nodes=node1
2. Resume specific drive from specific node
$ kubectl {PLUGIN_NAME} resume drives --nodes=node1 --drives=sda
3. Resume a suspended drive by its DRIVE-ID 'af3b8b4c-73b4-4a74-84b7-1ec30492a6f0'
$ kubectl {PLUGIN_NAME} resume drives af3b8b4c-73b4-4a74-84b7-1ec30492a6f0`,
`{PLUGIN_NAME}`,
consts.AppName,
),
Run: func(c *cobra.Command, args []string) {
driveIDArgs = args

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

resumeDrivesMain(c.Context())
},
}

func init() {
resumeDrivesCmd.Flags().SortFlags = false
resumeDrivesCmd.InheritedFlags().SortFlags = false
resumeDrivesCmd.LocalFlags().SortFlags = false
resumeDrivesCmd.LocalNonPersistentFlags().SortFlags = false
resumeDrivesCmd.NonInheritedFlags().SortFlags = false
resumeDrivesCmd.PersistentFlags().SortFlags = false

addNodesFlag(resumeDrivesCmd, "If present, resume drives from given nodes")
addDrivesFlag(resumeDrivesCmd, "If present, resume drives by given names")
addDryRunFlag(resumeDrivesCmd, "Run in dry run mode")
}

func validateResumeDrivesCmd() error {
if err := validateNodeArgs(); err != nil {
return err
}
if err := validateDriveNameArgs(); err != nil {
return err
}
if err := validateDriveIDArgs(); err != nil {
return err
}

switch {
case len(nodesArgs) != 0:
case len(drivesArgs) != 0:
case len(driveIDArgs) != 0:
default:
return errors.New("no drive selected to resume")
}

return nil
}

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

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

resultCh := drive.NewLister().
NodeSelector(toLabelValues(nodesArgs)).
DriveNameSelector(toLabelValues(drivesArgs)).
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.IsSuspended() {
// only suspended drives can be resumed.
continue
}

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

if !quietFlag {
fmt.Printf("Drive %v/%v resumed\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")
}

os.Exit(1)
}
}
150 changes: 150 additions & 0 deletions cmd/kubectl-directpv/resume_volumes.go
@@ -0,0 +1,150 @@
// This file is part of MinIO DirectPV
// Copyright (c) 2021, 2022, 2023 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
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package main

import (
"context"
"errors"
"fmt"
"os"
"strings"

"github.com/minio/directpv/pkg/client"
"github.com/minio/directpv/pkg/consts"
"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 resumeVolumesCmd = &cobra.Command{
Use: "volumes [VOLUME ...]",
Short: "Resume suspended volumes",
SilenceUsage: true,
SilenceErrors: true,
Example: strings.ReplaceAll(
`1. Resume all volumes from a node
$ kubectl {PLUGIN_NAME} resume volumes --nodes=node1
2. Resume specific volume from specific node
$ kubectl {PLUGIN_NAME} resume volumes --nodes=node1 --volumes=sda
3. Resume a volume by its name 'pvc-0700b8c7-85b2-4894-b83a-274484f220d0'
$ kubectl {PLUGIN_NAME} resume volumes pvc-0700b8c7-85b2-4894-b83a-274484f220d0`,
`{PLUGIN_NAME}`,
consts.AppName,
),
Run: func(c *cobra.Command, args []string) {
volumeNameArgs = args

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

resumeVolumesMain(c.Context())
},
}

func init() {
resumeVolumesCmd.Flags().SortFlags = false
resumeVolumesCmd.InheritedFlags().SortFlags = false
resumeVolumesCmd.LocalFlags().SortFlags = false
resumeVolumesCmd.LocalNonPersistentFlags().SortFlags = false
resumeVolumesCmd.NonInheritedFlags().SortFlags = false
resumeVolumesCmd.PersistentFlags().SortFlags = false

addNodesFlag(resumeVolumesCmd, "If present, resume volumes from given nodes")
addDrivesFlag(resumeVolumesCmd, "If present, resume volumes by given drive names")
addPodNameFlag(resumeVolumesCmd, "If present, resume volumes by given pod names")
addPodNSFlag(resumeVolumesCmd, "If present, resume volumes by given pod namespaces")
}

func validateResumeVolumesCmd() error {
if err := validateVolumeNameArgs(); err != nil {
return err
}
if err := validateNodeArgs(); err != nil {
return err
}
if err := validateDriveNameArgs(); err != nil {
return err
}
if err := validatePodNameArgs(); err != nil {
return err
}
if err := validatePodNSArgs(); err != nil {
return err
}

switch {
case len(volumeNameArgs) != 0:
case len(nodesArgs) != 0:
case len(drivesArgs) != 0:
case len(podNameArgs) != 0:
case len(podNSArgs) != 0:
default:
return errors.New("no volume selected to resume")
}

return nil
}

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

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

resultCh := volume.NewLister().
NodeSelector(toLabelValues(nodesArgs)).
DriveNameSelector(toLabelValues(drivesArgs)).
PodNameSelector(toLabelValues(podNameArgs)).
PodNSSelector(toLabelValues(podNSArgs)).
VolumeNameSelector(volumeNameArgs).
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.Volume.IsSuspended() {
// only suspended drives can be resumed.
continue
}

result.Volume.Resume()
if !dryRunFlag {
if _, err := client.VolumeClient().Update(ctx, &result.Volume, metav1.UpdateOptions{}); err != nil {
utils.Eprintf(quietFlag, true, "unable to resume volume %v; %v\n", result.Volume.Name, err)
os.Exit(1)
}
}

if !quietFlag {
fmt.Printf("Volume %v/%v resumed\n", result.Volume.GetNodeID(), result.Volume.Name)
}
}

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

0 comments on commit 7ed713b

Please sign in to comment.