Skip to content

Commit

Permalink
fix kubectl directpv plugin command line arguments (#643)
Browse files Browse the repository at this point in the history
Signed-off-by: Bala.FA <bala@minio.io>
  • Loading branch information
balamurugana committed Sep 21, 2022
1 parent 379090f commit e5a1963
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 195 deletions.
137 changes: 0 additions & 137 deletions cmd/kubectl-directpv/cmd.go

This file was deleted.

39 changes: 17 additions & 22 deletions cmd/kubectl-directpv/info.go
Expand Up @@ -41,17 +41,19 @@ import (
"k8s.io/klog/v2"
)

var errInstallationNotFound = errors.New("installation not found")

var infoCmd = &cobra.Command{
Use: "info",
Short: "Info about " + consts.AppPrettyName + " installation",
Short: "Show information about " + consts.AppPrettyName + " installation.",
SilenceUsage: true,
SilenceErrors: true,
RunE: func(c *cobra.Command, args []string) error {
return getInfo(c.Context(), args, false)
return getInfo(c.Context(), args)
},
}

func getInfo(ctx context.Context, args []string, quiet bool) error {
func getInfo(ctx context.Context, args []string) error {
crds, err := k8s.CRDClient().List(ctx, metav1.ListOptions{})
if err != nil {
if !quiet {
Expand All @@ -74,16 +76,16 @@ func getInfo(ctx context.Context, args []string, quiet bool) error {
return fmt.Errorf(consts.AppPrettyName + " installation not found")
}

cln, gvk, err := k8s.GetClientForNonCoreGroupVersionKind("storage.k8s.io", "CSINode", "v1", "v1beta1", "v1alpha1")
storageClient, gvk, err := k8s.GetClientForNonCoreGroupVersionKind("storage.k8s.io", "CSINode", "v1", "v1beta1", "v1alpha1")
if err != nil {
return err
}

nodeList := []string{}

if gvk.Version == "v1" {
switch gvk.Version {
case "v1":
result := &storagev1.CSINodeList{}
if err := cln.Get().
if err := storageClient.Get().
Resource("csinodes").
VersionedParams(&metav1.ListOptions{}, scheme.ParameterCodec).
Timeout(10 * time.Second).
Expand All @@ -96,16 +98,15 @@ func getInfo(ctx context.Context, args []string, quiet bool) error {
}
for _, csiNode := range result.Items {
for _, driver := range csiNode.Spec.Drivers {
if driver.Name == strings.ReplaceAll(identity, ".", "-") {
if driver.Name == consts.Identity {
nodeList = append(nodeList, csiNode.Name)
break
}
}
}
}
if gvk.Version == "v1beta1" {
case "v1beta1":
result := &storagev1beta1.CSINodeList{}
if err := cln.Get().
if err := storageClient.Get().
Resource(gvk.Kind).
VersionedParams(&metav1.ListOptions{}, scheme.ParameterCodec).
Timeout(10 * time.Second).
Expand All @@ -118,24 +119,18 @@ func getInfo(ctx context.Context, args []string, quiet bool) error {
}
for _, csiNode := range result.Items {
for _, driver := range csiNode.Spec.Drivers {
if driver.Name == strings.ReplaceAll(identity, ".", "-") {
if driver.Name == consts.Identity {
nodeList = append(nodeList, csiNode.Name)
break
}
}
}
}

if gvk.Version == "v1alpha1" {
return errors.New("this version of CSINode is not supported by " + consts.AppPrettyName)
case "v1apha1":
return errors.New("storage.k8s.io/v1alpha1 is not supported")
}

if len(nodeList) == 0 {
if !quiet {
fmt.Printf("%s %s installation not found\n\n", color.HiRedString("Error"), consts.AppPrettyName)
fmt.Printf("run '%s' to get started\n", color.HiWhiteString("kubectl "+consts.AppName+" install"))
}
return fmt.Errorf(consts.AppPrettyName + " installation not found")
return errInstallationNotFound
}

drives, err := drive.GetDriveList(ctx, nil, nil, nil)
Expand Down Expand Up @@ -185,7 +180,7 @@ func getInfo(ctx context.Context, args []string, quiet bool) error {

if driveCount == 0 {
t.AppendRow([]interface{}{
fmt.Sprintf("%s %s", color.RedString(dot), n),
fmt.Sprintf("%s %s", color.YellowString(dot), n),
"-",
"-",
"-",
Expand Down
41 changes: 18 additions & 23 deletions cmd/kubectl-directpv/install.go
Expand Up @@ -19,6 +19,7 @@ package main
import (
"context"
"fmt"
"os"

"github.com/fatih/color"
"github.com/minio/directpv/pkg/consts"
Expand All @@ -29,7 +30,7 @@ import (

var installCmd = &cobra.Command{
Use: "install",
Short: "Install " + consts.AppPrettyName + " in kubernetes cluster",
Short: "Install " + consts.AppPrettyName + " in Kubernetes.",
SilenceUsage: true,
SilenceErrors: true,
RunE: func(c *cobra.Command, args []string) error {
Expand All @@ -48,44 +49,37 @@ var (
apparmorProfile = ""
auditInstall = "install"
imagePullSecrets = []string{}
disableUDevListener = false
)

func init() {
if Version == "" {
image = consts.AppName + ":0.0.0-dev"
}
installCmd.PersistentFlags().StringVarP(&image, "image", "i", image, consts.AppPrettyName+" image")
installCmd.PersistentFlags().StringSliceVarP(&imagePullSecrets, "image-pull-secrets", "", imagePullSecrets, "image pull secrets to be set in pod specs")
installCmd.PersistentFlags().StringVarP(&registry, "registry", "r", registry, "registry where "+consts.AppPrettyName+" images are available")
installCmd.PersistentFlags().StringVarP(&org, "org", "g", org, "organization name where "+consts.AppPrettyName+" images are available")
installCmd.PersistentFlags().BoolVarP(&admissionControl, "admission-control", "", admissionControl, "turn on "+consts.AppPrettyName+" admission controller")
installCmd.PersistentFlags().StringSliceVarP(&nodeSelectorParameters, "node-selector", "n", nodeSelectorParameters, "node selector parameters")
installCmd.PersistentFlags().StringSliceVarP(&tolerationParameters, "tolerations", "t", tolerationParameters, "tolerations parameters")
installCmd.PersistentFlags().StringVarP(&seccompProfile, "seccomp-profile", "", seccompProfile, "set Seccomp profile")
installCmd.PersistentFlags().StringVarP(&apparmorProfile, "apparmor-profile", "", apparmorProfile, "set Apparmor profile")
installCmd.PersistentFlags().BoolVarP(&disableUDevListener, "disable-udev-listener", "", disableUDevListener, "disable uevent listener and rely on 30secs internal drive-sync mechanism")
installCmd.PersistentFlags().StringSliceVarP(&imagePullSecrets, "image-pull-secrets", "", imagePullSecrets, "Image pull secrets to be set in pod specs")
installCmd.PersistentFlags().StringVarP(&registry, "registry", "r", registry, "Registry where "+consts.AppPrettyName+" images are available")
installCmd.PersistentFlags().StringVarP(&org, "org", "g", org, "Organization name on the registry holds "+consts.AppPrettyName+" images")
installCmd.PersistentFlags().BoolVarP(&admissionControl, "admission-control", "", admissionControl, "Turn on "+consts.AppPrettyName+" admission controller")
installCmd.PersistentFlags().StringSliceVarP(&nodeSelectorParameters, "node-selector", "n", nodeSelectorParameters, "Node selector parameters")
installCmd.PersistentFlags().StringSliceVarP(&tolerationParameters, "tolerations", "t", tolerationParameters, "Tolerations parameters")
installCmd.PersistentFlags().StringVarP(&seccompProfile, "seccomp-profile", "", seccompProfile, "Set Seccomp profile")
installCmd.PersistentFlags().StringVarP(&apparmorProfile, "apparmor-profile", "", apparmorProfile, "Set Apparmor profile")
}

func install(ctx context.Context, args []string) (err error) {
// if err := validImage(image); err != nil {
// return fmt.Errorf("invalid argument. format of '--image' must be [image:tag] err=%v", err)
// }
// if err := validOrg(org); err != nil {
// return fmt.Errorf("invalid org. format of '--org' must be [a-zA-Z][a-zA-Z0-9-.]* err=%v", err)
// }
// if err := validRegistry(registry); err != nil {
// return fmt.Errorf("invalid registry. format of '--registry' must be [host:port?]")
// }
nodeSelector, err := parseNodeSelector(nodeSelectorParameters)
if err != nil {
return fmt.Errorf("invalid node selector. format of '--node-selector' must be [<key>=<value>]")
return fmt.Errorf("%w; format of '--node-selector' flag value must be [<key>=<value>]", err)
}
tolerations, err := parseTolerations(tolerationParameters)
if err != nil {
return fmt.Errorf("invalid tolerations. format of '--tolerations' must be <key>[=value]:<NoSchedule|PreferNoSchedule|NoExecute>")
return fmt.Errorf("%w; format of '--tolerations' flag value must be <key>[=value]:<NoSchedule|PreferNoSchedule|NoExecute>", err)
}

file, err := OpenAuditFile(auditInstall)
file, err := openAuditFile(auditInstall)
if err != nil {
klog.ErrorS(err, "unable to open audit file", "file", auditInstall)
fmt.Fprintln(os.Stderr, color.HiYellowString("Skipping audit logging"))
}

defer func() {
Expand Down Expand Up @@ -114,5 +108,6 @@ func install(ctx context.Context, args []string) (err error) {
if err = installer.Install(ctx, installConfig); err == nil && !dryRun {
fmt.Println(color.HiWhiteString(consts.AppPrettyName), "is installed successfully")
}

return err
}

0 comments on commit e5a1963

Please sign in to comment.