Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clean: use cobra built-in feature for pre-validation #213

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions cmd/commands/ceph.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ var CephCmd = &cobra.Command{
Short: "call a 'ceph' CLI command with arbitrary args",
DisableFlagParsing: true,
Args: cobra.MinimumNArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
verifyOperatorPodIsRunning(cmd.Context(), clientSets)
},
Run: func(cmd *cobra.Command, args []string) {
clientsets := GetClientsets(cmd.Context())
VerifyOperatorPodIsRunning(cmd.Context(), clientsets, OperatorNamespace, CephClusterNamespace)
logging.Info("running 'ceph' command with args: %v", args)
exec.RunCommandInOperatorPod(cmd.Context(), clientsets, cmd.Use, args, OperatorNamespace, CephClusterNamespace, false, true)
exec.RunCommandInOperatorPod(cmd.Context(), clientSets, cmd.Use, args, operatorNamespace, cephClusterNamespace, false, true)
},
}
11 changes: 5 additions & 6 deletions cmd/commands/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@ var DebugCmd = &cobra.Command{
Short: "Debug a deployment by scaling it down and creating a debug copy. This is supported for mons and OSDs only",
DisableFlagParsing: true,
Args: cobra.ExactArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
verifyOperatorPodIsRunning(cmd.Context(), clientSets)
},
}

var startDebugCmd = &cobra.Command{
Use: "start",
Short: "Start debugging a deployment with an optional alternative ceph container image",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
clientsets := GetClientsets(cmd.Context())
VerifyOperatorPodIsRunning(cmd.Context(), clientsets, OperatorNamespace, CephClusterNamespace)
alternateImage := cmd.Flag("alternate-image").Value.String()
debug.StartDebug(cmd.Context(), clientsets.Kube, CephClusterNamespace, args[0], alternateImage)
debug.StartDebug(cmd.Context(), clientSets.Kube, cephClusterNamespace, args[0], alternateImage)
},
}

Expand All @@ -46,9 +47,7 @@ var stopDebugCmd = &cobra.Command{
Short: "Stop debugging a deployment",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
clientsets := GetClientsets(cmd.Context())
VerifyOperatorPodIsRunning(cmd.Context(), clientsets, OperatorNamespace, CephClusterNamespace)
debug.StopDebug(cmd.Context(), clientsets.Kube, CephClusterNamespace, args[0])
debug.StopDebug(cmd.Context(), clientSets.Kube, cephClusterNamespace, args[0])
},
}

Expand Down
7 changes: 4 additions & 3 deletions cmd/commands/dr_health.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ var DrCmd = &cobra.Command{
Short: "Calls subcommand health",
DisableFlagParsing: true,
Args: cobra.ExactArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
verifyOperatorPodIsRunning(cmd.Context(), clientSets)
},
}

var healthCmd = &cobra.Command{
Expand All @@ -18,9 +21,7 @@ var healthCmd = &cobra.Command{
DisableFlagParsing: true,
Args: cobra.MaximumNArgs(2),
Run: func(cmd *cobra.Command, args []string) {
clientsets := GetClientsets(cmd.Context())
VerifyOperatorPodIsRunning(cmd.Context(), clientsets, OperatorNamespace, CephClusterNamespace)
dr.Health(cmd.Context(), clientsets, OperatorNamespace, CephClusterNamespace, args)
dr.Health(cmd.Context(), clientSets, operatorNamespace, cephClusterNamespace, args)
},
}

Expand Down
7 changes: 4 additions & 3 deletions cmd/commands/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ var Health = &cobra.Command{
Short: "check health of the cluster and common configuration issues",
DisableFlagParsing: true,
Args: cobra.NoArgs,
PreRun: func(cmd *cobra.Command, args []string) {
verifyOperatorPodIsRunning(cmd.Context(), clientSets)
},
Run: func(cmd *cobra.Command, _ []string) {
clientsets := GetClientsets(cmd.Context())
VerifyOperatorPodIsRunning(cmd.Context(), clientsets, OperatorNamespace, CephClusterNamespace)
health.Health(cmd.Context(), clientsets, OperatorNamespace, CephClusterNamespace)
health.Health(cmd.Context(), clientSets, operatorNamespace, cephClusterNamespace)
},
}
6 changes: 2 additions & 4 deletions cmd/commands/mons.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ var MonCmd = &cobra.Command{
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
clientsets := GetClientsets(cmd.Context())
fmt.Println(mons.GetMonEndpoint(cmd.Context(), clientsets.Kube, CephClusterNamespace))
fmt.Println(mons.GetMonEndpoint(cmd.Context(), clientSets.Kube, cephClusterNamespace))
}
},
}
Expand All @@ -44,8 +43,7 @@ var RestoreQuorum = &cobra.Command{
DisableFlagParsing: true,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
clientsets := GetClientsets(cmd.Context())
mons.RestoreQuorum(cmd.Context(), clientsets, OperatorNamespace, CephClusterNamespace, args[0])
mons.RestoreQuorum(cmd.Context(), clientSets, operatorNamespace, cephClusterNamespace, args[0])
},
}

Expand Down
10 changes: 5 additions & 5 deletions cmd/commands/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ var restartCmd = &cobra.Command{
Use: "restart",
Short: "Restart rook-ceph-operator pod",
Args: cobra.NoArgs,
PreRun: func(cmd *cobra.Command, args []string) {
verifyOperatorPodIsRunning(cmd.Context(), clientSets)
},
Run: func(cmd *cobra.Command, _ []string) {
clientsets := GetClientsets(cmd.Context())
VerifyOperatorPodIsRunning(cmd.Context(), clientsets, OperatorNamespace, CephClusterNamespace)
k8sutil.RestartDeployment(cmd.Context(), clientsets.Kube, OperatorNamespace, "rook-ceph-operator")
k8sutil.RestartDeployment(cmd.Context(), clientSets.Kube, operatorNamespace, "rook-ceph-operator")
},
}

Expand All @@ -45,8 +46,7 @@ var setCmd = &cobra.Command{
Short: "Set the property in the rook-ceph-operator-config configmap.",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
clientsets := GetClientsets(cmd.Context())
k8sutil.UpdateConfigMap(cmd.Context(), clientsets.Kube, OperatorNamespace, "rook-ceph-operator-config", args[0], args[1])
k8sutil.UpdateConfigMap(cmd.Context(), clientSets.Kube, operatorNamespace, "rook-ceph-operator-config", args[0], args[1])
},
}

Expand Down
7 changes: 4 additions & 3 deletions cmd/commands/rbd.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ var RbdCmd = &cobra.Command{
Short: "call a 'rbd' CLI command with arbitrary args",
DisableFlagParsing: true,
Args: cobra.MinimumNArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
verifyOperatorPodIsRunning(cmd.Context(), clientSets)
},
Run: func(cmd *cobra.Command, args []string) {
clientsets := GetClientsets(cmd.Context())
VerifyOperatorPodIsRunning(cmd.Context(), clientsets, OperatorNamespace, CephClusterNamespace)
exec.RunCommandInOperatorPod(cmd.Context(), clientsets, cmd.Use, args, OperatorNamespace, CephClusterNamespace, false, true)
exec.RunCommandInOperatorPod(cmd.Context(), clientSets, cmd.Use, args, operatorNamespace, cephClusterNamespace, false, true)
},
}
7 changes: 4 additions & 3 deletions cmd/commands/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ var RestoreCmd = &cobra.Command{
Use: "restore-deleted",
Short: "Restores a CR that was accidentally deleted and is still in terminating state. Ex: restore cephcluster <my-cluster>",
Args: cobra.MinimumNArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
verifyOperatorPodIsRunning(cmd.Context(), clientSets)
},
Run: func(cmd *cobra.Command, args []string) {
clientsets := GetClientsets(cmd.Context())
VerifyOperatorPodIsRunning(cmd.Context(), clientsets, OperatorNamespace, CephClusterNamespace)
restore.RestoreCrd(cmd.Context(), clientsets, OperatorNamespace, CephClusterNamespace, args)
restore.RestoreCrd(cmd.Context(), clientSets, operatorNamespace, cephClusterNamespace, args)
},
}
29 changes: 20 additions & 9 deletions cmd/commands/rook.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,22 @@ var versionCmd = &cobra.Command{
Short: "Prints rook version",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, _ []string) {
clientsets := GetClientsets(cmd.Context())
exec.RunCommandInOperatorPod(cmd.Context(), clientsets, "rook", []string{cmd.Use}, OperatorNamespace, CephClusterNamespace, false, true)
exec.RunCommandInOperatorPod(cmd.Context(), clientSets, "rook", []string{cmd.Use}, operatorNamespace, cephClusterNamespace, false, true)
},
}

var purgeCmd = &cobra.Command{
Use: "purge-osd",
Short: "Permanently remove an OSD from the cluster. Multiple OSDs can be removed with a comma-separated list of IDs, for example, purge-osd 0,1",
Args: cobra.ExactArgs(1),
Use: "purge-osd",
Short: "Permanently remove an OSD from the cluster. Multiple OSDs can be removed with a comma-separated list of IDs, for example, purge-osd 0,1",
PreRunE: validateOsdID,
Args: cobra.ExactArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
verifyOperatorPodIsRunning(cmd.Context(), clientSets)
},
Run: func(cmd *cobra.Command, args []string) {
clientsets := GetClientsets(cmd.Context())
VerifyOperatorPodIsRunning(cmd.Context(), clientsets, OperatorNamespace, CephClusterNamespace)
forceflagValue := cmd.Flag("force").Value.String()
osdID := args[0]
rook.PurgeOsd(cmd.Context(), clientsets, OperatorNamespace, CephClusterNamespace, osdID, forceflagValue)
rook.PurgeOsd(cmd.Context(), clientSets, operatorNamespace, cephClusterNamespace, osdID, forceflagValue)
},
}

Expand All @@ -67,7 +68,7 @@ var statusCmd = &cobra.Command{
if err != nil {
logging.Fatal(fmt.Errorf("failed to parse json flag: %v", err))
}
rook.PrintCustomResourceStatus(CephClusterNamespace, args, jsonValue)
rook.PrintCustomResourceStatus(cephClusterNamespace, args, jsonValue)
},
}

Expand All @@ -78,3 +79,13 @@ func init() {
statusCmd.PersistentFlags().Bool("json", false, "print status in json format")
purgeCmd.PersistentFlags().Bool("force", false, "force deletion of an OSD if the OSD still contains data")
}

func validateOsdID(cmd *cobra.Command, args []string) error {
osdID := args[0]
_, err := strconv.Atoi(osdID)
if err != nil {
return fmt.Errorf("Invalid ID %s, the OSD ID must be an integer. %v", osdID, err)
}

return nil
}
39 changes: 18 additions & 21 deletions cmd/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ import (
)

var (
KubeConfig string
OperatorNamespace string
CephClusterNamespace string
KubeContext string
kubeConfig string
operatorNamespace string
cephClusterNamespace string
kubeContext string
clientSets *k8sutil.Clientsets
)

// rookCmd represents the rook command
Expand All @@ -47,11 +48,11 @@ var RootCmd = &cobra.Command{
Args: cobra.MinimumNArgs(1),
TraverseChildren: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if CephClusterNamespace != "" && OperatorNamespace == "" {
OperatorNamespace = CephClusterNamespace
if cephClusterNamespace != "" && operatorNamespace == "" {
operatorNamespace = cephClusterNamespace
}
// logging.Info("CephCluster namespace: %q", CephClusterNamespace)
// logging.Info("Rook operator namespace: %q", OperatorNamespace)
clientSets = getClientsets(cmd.Context())
preValidationCheck(cmd.Context(), clientSets)
},
}

Expand All @@ -62,23 +63,21 @@ func Execute() {
}

func init() {

// Define your flags and configuration settings.

RootCmd.PersistentFlags().StringVar(&KubeConfig, "kubeconfig", "", "kubernetes config path")
RootCmd.PersistentFlags().StringVar(&OperatorNamespace, "operator-namespace", "", "Kubernetes namespace where rook operator is running")
RootCmd.PersistentFlags().StringVarP(&CephClusterNamespace, "namespace", "n", "rook-ceph", "Kubernetes namespace where CephCluster is created")
RootCmd.PersistentFlags().StringVar(&KubeContext, "context", "", "Kubernetes context to use")
RootCmd.PersistentFlags().StringVar(&kubeConfig, "kubeconfig", "", "kubernetes config path")
RootCmd.PersistentFlags().StringVar(&operatorNamespace, "operator-namespace", "", "Kubernetes namespace where rook operator is running")
RootCmd.PersistentFlags().StringVarP(&cephClusterNamespace, "namespace", "n", "rook-ceph", "Kubernetes namespace where CephCluster is created")
RootCmd.PersistentFlags().StringVar(&kubeContext, "context", "", "Kubernetes context to use")
}

func GetClientsets(ctx context.Context) *k8sutil.Clientsets {
func getClientsets(ctx context.Context) *k8sutil.Clientsets {
var err error

clientsets := &k8sutil.Clientsets{}

congfigOverride := &clientcmd.ConfigOverrides{}
if KubeContext != "" {
congfigOverride = &clientcmd.ConfigOverrides{CurrentContext: KubeContext}
if kubeContext != "" {
congfigOverride = &clientcmd.ConfigOverrides{CurrentContext: kubeContext}
}

// 1. Create Kubernetes Client
Expand All @@ -102,12 +101,10 @@ func GetClientsets(ctx context.Context) *k8sutil.Clientsets {
logging.Fatal(err)
}

PreValidationCheck(ctx, clientsets, OperatorNamespace, CephClusterNamespace)

return clientsets
}

func PreValidationCheck(ctx context.Context, k8sclientset *k8sutil.Clientsets, operatorNamespace, cephClusterNamespace string) {
func preValidationCheck(ctx context.Context, k8sclientset *k8sutil.Clientsets) {
_, err := k8sclientset.Kube.CoreV1().Namespaces().Get(ctx, operatorNamespace, v1.GetOptions{})
if err != nil {
logging.Fatal(fmt.Errorf("Operator namespace '%s' does not exist. %v", operatorNamespace, err))
Expand All @@ -118,7 +115,7 @@ func PreValidationCheck(ctx context.Context, k8sclientset *k8sutil.Clientsets, o
}
}

func VerifyOperatorPodIsRunning(ctx context.Context, k8sclientset *k8sutil.Clientsets, operatorNamespace, cephClusterNamespace string) {
func verifyOperatorPodIsRunning(ctx context.Context, k8sclientset *k8sutil.Clientsets) {
rookVersionOutput := exec.RunCommandInOperatorPod(ctx, k8sclientset, "rook", []string{"version"}, operatorNamespace, cephClusterNamespace, true, false)
rookVersion := trimGoVersionFromRookVersion(rookVersionOutput)
if strings.Contains(rookVersion, "alpha") || strings.Contains(rookVersion, "beta") {
Expand Down
13 changes: 6 additions & 7 deletions cmd/commands/subvolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,19 @@ import (
var SubvolumeCmd = &cobra.Command{
Use: "subvolume",
Short: "manages stale subvolumes",
Args: cobra.ExactArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
verifyOperatorPodIsRunning(cmd.Context(), clientSets)
},
Args: cobra.ExactArgs(1),
}

var listCmd = &cobra.Command{
Use: "ls",
Short: "Print the list of subvolumes.",
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
clientsets := GetClientsets(ctx)
VerifyOperatorPodIsRunning(ctx, clientsets, OperatorNamespace, CephClusterNamespace)
staleSubvol, _ := cmd.Flags().GetBool("stale")
subvolume.List(ctx, clientsets, OperatorNamespace, CephClusterNamespace, staleSubvol)
subvolume.List(ctx, clientSets, operatorNamespace, cephClusterNamespace, staleSubvol)
},
}

Expand All @@ -45,12 +46,10 @@ var deleteCmd = &cobra.Command{
Args: cobra.ExactArgs(3),
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
clientsets := GetClientsets(ctx)
VerifyOperatorPodIsRunning(ctx, clientsets, OperatorNamespace, CephClusterNamespace)
subList := args[0]
fs := args[1]
svg := args[2]
subvolume.Delete(ctx, clientsets, OperatorNamespace, CephClusterNamespace, subList, fs, svg)
subvolume.Delete(ctx, clientSets, operatorNamespace, cephClusterNamespace, subList, fs, svg)
},
}

Expand Down
Loading