Skip to content

Commit

Permalink
move to admin pkg
Browse files Browse the repository at this point in the history
Signed-off-by: Bala.FA <[email protected]>
  • Loading branch information
balamurugana committed Jul 10, 2024
1 parent eb5dac8 commit d982696
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 143 deletions.
4 changes: 2 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
linters-settings:
gofumpt:
lang-version: "1.22"
run:
go: "1.22"

misspell:
locale: US
Expand Down
125 changes: 13 additions & 112 deletions cmd/kubectl-directpv/repair.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,9 @@ import (
"os"
"strings"

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

var (
Expand All @@ -52,7 +46,7 @@ var repairCmd = &cobra.Command{
Run: func(c *cobra.Command, args []string) {
driveIDArgs = args
if err := validateRepairCmd(); err != nil {
utils.Eprintf(quietFlag, true, "%v\n", err)
eprintf(true, "%v\n", err)
os.Exit(-1)
}

Expand Down Expand Up @@ -81,111 +75,18 @@ func validateRepairCmd() error {
}

func repairMain(ctx context.Context) {
var failed bool

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

containerImage, imagePullSecrets, tolerations, err := getContainerParams(ctx)
_, err := adminClient.Repair(
ctx,
admin.RepairArgs{
DriveIDs: driveIDSelectors,
DryRun: dryRunFlag,
ForceFlag: forceFlag,
DisablePrefetchFlag: disablePrefetchFlag,
},
logFunc,
)
if err != nil {
utils.Eprintf(quietFlag, true, "unable to container parameters from daemonset; %v\n", err)
os.Exit(1)
}

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

jobName := "repair-" + result.Drive.Name
if _, err := k8s.KubeClient().BatchV1().Jobs(consts.AppName).Get(ctx, jobName, metav1.GetOptions{}); err != nil {
if !apierrors.IsNotFound(err) {
utils.Eprintf(quietFlag, true, "unable to get repair job %v; %v\n", jobName, err)
failed = true
continue
}
} else {
utils.Eprintf(quietFlag, true, "job %v already exists\n", jobName)
continue
}

nodeID := string(result.Drive.GetNodeID())

containerArgs := []string{"/directpv", "repair", result.Drive.Name, "--kube-node-name=" + nodeID}
if forceFlag {
containerArgs = append(containerArgs, "--force")
}
if disablePrefetchFlag {
containerArgs = append(containerArgs, "--disable-prefetch")
}
if dryRunFlag {
containerArgs = append(containerArgs, "--dry-run")
}

backOffLimit := int32(1)

volumes := []corev1.Volume{
k8s.NewHostPathVolume(consts.AppRootDirVolumeName, consts.AppRootDirVolumePath),
k8s.NewHostPathVolume(consts.LegacyAppRootDirVolumeName, consts.LegacyAppRootDirVolumePath),
k8s.NewHostPathVolume(consts.SysDirVolumeName, consts.SysDirVolumePath),
k8s.NewHostPathVolume(consts.DevDirVolumeName, consts.DevDirVolumePath),
k8s.NewHostPathVolume(consts.RunUdevDataVolumeName, consts.RunUdevDataVolumePath),
}

volumeMounts := []corev1.VolumeMount{
k8s.NewVolumeMount(consts.AppRootDirVolumeName, consts.AppRootDirVolumePath, corev1.MountPropagationBidirectional, false),
k8s.NewVolumeMount(consts.LegacyAppRootDirVolumeName, consts.LegacyAppRootDirVolumePath, corev1.MountPropagationBidirectional, false),
k8s.NewVolumeMount(consts.SysDirVolumeName, consts.SysDirVolumePath, corev1.MountPropagationBidirectional, false),
k8s.NewVolumeMount(consts.DevDirVolumeName, consts.DevDirVolumePath, corev1.MountPropagationHostToContainer, true),
k8s.NewVolumeMount(consts.RunUdevDataVolumeName, consts.RunUdevDataVolumePath, corev1.MountPropagationBidirectional, true),
}

privileged := true

job := batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: jobName,
Namespace: consts.AppName,
},
Spec: batchv1.JobSpec{
BackoffLimit: &backOffLimit,
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
NodeSelector: map[string]string{string(directpvtypes.NodeLabelKey): nodeID},
ServiceAccountName: consts.Identity,
Tolerations: tolerations,
ImagePullSecrets: imagePullSecrets,
Volumes: volumes,
Containers: []corev1.Container{
{
Name: jobName,
Image: containerImage,
Command: containerArgs,
SecurityContext: &corev1.SecurityContext{Privileged: &privileged},
VolumeMounts: volumeMounts,
TerminationMessagePolicy: corev1.TerminationMessageFallbackToLogsOnError,
TerminationMessagePath: "/var/log/repair-termination-log",
},
},
RestartPolicy: corev1.RestartPolicyNever,
},
},
},
}

if _, err := k8s.KubeClient().BatchV1().Jobs(consts.AppName).Create(ctx, &job, metav1.CreateOptions{}); err != nil {
utils.Eprintf(quietFlag, true, "unable to create repair job %v; %v\n", jobName, err)
} else {
utils.Eprintf(quietFlag, false, "repair job %v for drive %v is created\n", jobName, result.Drive.Name)
}
}

if failed {
eprintf(!errors.Is(err, admin.ErrNoMatchingResourcesFound), "%v\n", err)
os.Exit(1)
}
}
29 changes: 0 additions & 29 deletions cmd/kubectl-directpv/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package main

import (
"context"
"errors"
"fmt"
"os"
Expand All @@ -27,12 +26,8 @@ import (
"github.com/jedib0t/go-pretty/v6/table"
"github.com/minio/directpv/pkg/admin"
"github.com/minio/directpv/pkg/consts"
"github.com/minio/directpv/pkg/k8s"
"github.com/minio/directpv/pkg/utils"
"github.com/mitchellh/go-homedir"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
)

Expand Down Expand Up @@ -133,27 +128,3 @@ func eprintf(isErr bool, format string, args ...any) {
func logFunc(log admin.LogMessage) {
eprintf(log.Type == admin.ErrorLogType, log.FormattedMessage)
}

func getContainerParams(ctx context.Context) (string, []corev1.LocalObjectReference, []corev1.Toleration, error) {
daemonSet, err := k8s.KubeClient().AppsV1().DaemonSets(consts.AppName).Get(
ctx, consts.NodeServerName, metav1.GetOptions{},
)

if err != nil && !apierrors.IsNotFound(err) {
return "", nil, nil, err
}

if daemonSet == nil || daemonSet.UID == "" {
return "", nil, nil, fmt.Errorf("invalid daemonset found")
}

var containerImage string
for _, container := range daemonSet.Spec.Template.Spec.Containers {
if container.Name == consts.NodeServerName {
containerImage = container.Image
break
}
}

return containerImage, daemonSet.Spec.Template.Spec.ImagePullSecrets, daemonSet.Spec.Template.Spec.Tolerations, nil
}
Loading

0 comments on commit d982696

Please sign in to comment.