Skip to content

Commit

Permalink
Merge pull request rook#112 from BlaineEXE/use-passed-context
Browse files Browse the repository at this point in the history
Use recommended context.Context passing
  • Loading branch information
BlaineEXE authored May 25, 2023
2 parents 8a4aea7 + dd039b6 commit 21a07b9
Show file tree
Hide file tree
Showing 20 changed files with 174 additions and 177 deletions.
4 changes: 2 additions & 2 deletions cmd/commands/ceph.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var CephCmd = &cobra.Command{
DisableFlagParsing: true,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
context := GetContext()
fmt.Println(exec.RunCommandInOperatorPod(context, cmd.Use, args, OperatorNamespace, CephClusterNamespace, true))
clientsets := GetClientsets()
fmt.Println(exec.RunCommandInOperatorPod(cmd.Context(), clientsets, cmd.Use, args, OperatorNamespace, CephClusterNamespace, true))
},
}
10 changes: 5 additions & 5 deletions cmd/commands/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ var startDebugCmd = &cobra.Command{
Short: "Start debugging a deployment with an optional alternative ceph container image",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
context := GetContext()
clientsets := GetClientsets()
alternateImage := cmd.Flag("alternate-image").Value.String()
debug.StartDebug(context, CephClusterNamespace, args[0], alternateImage)
debug.StartDebug(cmd.Context(), clientsets.Kube, CephClusterNamespace, args[0], alternateImage)
},
}

var stopDebugCmd = &cobra.Command{
Use: "stop",
Short: "Stop debugging a deployment",
Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) {
context := GetContext()
debug.StopDebug(context, CephClusterNamespace, args[0])
Run: func(cmd *cobra.Command, args []string) {
clientsets := GetClientsets()
debug.StopDebug(cmd.Context(), clientsets.Kube, CephClusterNamespace, args[0])
},
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/commands/dr_health.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ var healthCmd = &cobra.Command{
Short: "Print the ceph status of a peer cluster in a mirroring-enabled cluster.",
DisableFlagParsing: true,
Args: cobra.MaximumNArgs(2),
Run: func(_ *cobra.Command, args []string) {
context := GetContext()
dr.Health(context, OperatorNamespace, CephClusterNamespace, args)
Run: func(cmd *cobra.Command, args []string) {
clientsets := GetClientsets()
dr.Health(cmd.Context(), clientsets, OperatorNamespace, CephClusterNamespace, args)
},
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/commands/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ var Health = &cobra.Command{
Short: "check health of the cluster and common configuration issues",
DisableFlagParsing: true,
Args: cobra.NoArgs,
Run: func(_ *cobra.Command, _ []string) {
context := GetContext()
health.Health(context, OperatorNamespace, CephClusterNamespace)
Run: func(cmd *cobra.Command, _ []string) {
clientsets := GetClientsets()
health.Health(cmd.Context(), clientsets, OperatorNamespace, CephClusterNamespace)
},
}
12 changes: 6 additions & 6 deletions cmd/commands/mons.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ var MonCmd = &cobra.Command{
Short: "Output mon endpoints",
DisableFlagParsing: true,
Args: cobra.MaximumNArgs(1),
Run: func(_ *cobra.Command, args []string) {
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
context := GetContext()
fmt.Println(mons.GetMonEndpoint(context, CephClusterNamespace))
clientsets := GetClientsets()
fmt.Println(mons.GetMonEndpoint(cmd.Context(), clientsets.Kube, CephClusterNamespace))
}
},
}
Expand All @@ -44,9 +44,9 @@ var RestoreQuorum = &cobra.Command{
Short: "When quorum is lost, restore quorum to the remaining healthy mon",
DisableFlagParsing: true,
Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) {
context := GetContext()
mons.RestoreQuorum(context, OperatorNamespace, CephClusterNamespace, args[0])
Run: func(cmd *cobra.Command, args []string) {
clientsets := GetClientsets()
mons.RestoreQuorum(cmd.Context(), clientsets, OperatorNamespace, CephClusterNamespace, args[0])
},
}

Expand Down
12 changes: 6 additions & 6 deletions cmd/commands/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ var restartCmd = &cobra.Command{
Use: "restart",
Short: "Restart rook-ceph-operator pod",
Args: cobra.NoArgs,
Run: func(_ *cobra.Command, _ []string) {
context := GetContext()
k8sutil.RestartDeployment(context, OperatorNamespace, "rook-ceph-operator")
Run: func(cmd *cobra.Command, args []string) {
clientsets := GetClientsets()
k8sutil.RestartDeployment(cmd.Context(), clientsets.Kube, OperatorNamespace, "rook-ceph-operator")
},
}

var setCmd = &cobra.Command{
Use: "set",
Short: "Set the property in the rook-ceph-operator-config configmap.",
Args: cobra.ExactArgs(2),
Run: func(_ *cobra.Command, args []string) {
context := GetContext()
k8sutil.UpdateConfigMap(context, OperatorNamespace, "rook-ceph-operator-config", args[0], args[1])
Run: func(cmd *cobra.Command, args []string) {
clientsets := GetClientsets()
k8sutil.UpdateConfigMap(cmd.Context(), clientsets.Kube, OperatorNamespace, "rook-ceph-operator-config", args[0], args[1])
},
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/commands/rbd.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var RbdCmd = &cobra.Command{
DisableFlagParsing: true,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
context := GetContext()
fmt.Println(exec.RunCommandInOperatorPod(context, cmd.Use, args, OperatorNamespace, CephClusterNamespace, true))
clientsets := GetClientsets()
fmt.Println(exec.RunCommandInOperatorPod(cmd.Context(), clientsets, cmd.Use, args, OperatorNamespace, CephClusterNamespace, true))
},
}
8 changes: 4 additions & 4 deletions cmd/commands/rook.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ var versionCmd = &cobra.Command{
Short: "Prints rook version",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, _ []string) {
context := GetContext()
fmt.Println(exec.RunCommandInOperatorPod(context, "rook", []string{cmd.Use}, OperatorNamespace, CephClusterNamespace, true))
clientsets := GetClientsets()
fmt.Println(exec.RunCommandInOperatorPod(cmd.Context(), clientsets, "rook", []string{cmd.Use}, OperatorNamespace, CephClusterNamespace, true))
},
}

Expand All @@ -46,10 +46,10 @@ var purgeCmd = &cobra.Command{
Short: "Permanently remove an OSD from the cluster. Multiple OSDs can be removed with a comma-separated list of IDs.",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
context := GetContext()
clientsets := GetClientsets()
forceflagValue := cmd.Flag("force").Value.String()
osdID := args[0]
rook.PurgeOsd(context, OperatorNamespace, CephClusterNamespace, osdID, forceflagValue)
rook.PurgeOsd(cmd.Context(), clientsets, OperatorNamespace, CephClusterNamespace, osdID, forceflagValue)
},
}

Expand Down
19 changes: 6 additions & 13 deletions cmd/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,38 +53,31 @@ func init() {
RootCmd.PersistentFlags().StringVar(&CephClusterNamespace, "namespace", "rook-ceph", "Kubernetes namespace where ceph cluster is created")
}

func GetContext() *k8sutil.Context {
func GetClientsets() *k8sutil.Clientsets {
var err error

context := &k8sutil.Context{}
clientsets := &k8sutil.Clientsets{}

// 1. Create Kubernetes Client
kubeconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
clientcmd.NewDefaultClientConfigLoadingRules(),
&clientcmd.ConfigOverrides{},
)

context.KubeConfig, err = kubeconfig.ClientConfig()
clientsets.KubeConfig, err = kubeconfig.ClientConfig()
if err != nil {
logging.Fatal(err)
}

context.RookClientset, err = rookclient.NewForConfig(context.KubeConfig)
clientsets.Rook, err = rookclient.NewForConfig(clientsets.KubeConfig)
if err != nil {
logging.Fatal(err)
}

context.RookClientset, err = rookclient.NewForConfig(context.KubeConfig)
clientsets.Kube, err = k8s.NewForConfig(clientsets.KubeConfig)
if err != nil {
logging.Fatal(err)
}

context.Clientset, err = k8s.NewForConfig(context.KubeConfig)
if err != nil {
logging.Fatal(err)
}

context.Context = RootCmd.Context()

return context
return clientsets
}
34 changes: 18 additions & 16 deletions pkg/debug/start_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package debug

import (
"context"
"fmt"
"time"

Expand All @@ -26,17 +27,18 @@ import (
autoscalingv1 "k8s.io/api/autoscaling/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

func StartDebug(context *k8sutil.Context, clusterNamespace, deploymentName, alternateImageValue string) {
err := startDebug(context, clusterNamespace, deploymentName, alternateImageValue)
func StartDebug(ctx context.Context, k8sclientset kubernetes.Interface, clusterNamespace, deploymentName, alternateImageValue string) {
err := startDebug(ctx, k8sclientset, clusterNamespace, deploymentName, alternateImageValue)
if err != nil {
logging.Fatal(err)
}
}

func startDebug(context *k8sutil.Context, clusterNamespace, deploymentName, alternateImageValue string) error {
originalDeployment, err := GetDeployment(context, clusterNamespace, deploymentName)
func startDebug(ctx context.Context, k8sclientset kubernetes.Interface, clusterNamespace, deploymentName, alternateImageValue string) error {
originalDeployment, err := GetDeployment(ctx, k8sclientset, clusterNamespace, deploymentName)
if err != nil {
return fmt.Errorf("Missing mon or osd deployment name %s. %v\n", deploymentName, err)
}
Expand All @@ -61,19 +63,19 @@ func startDebug(context *k8sutil.Context, clusterNamespace, deploymentName, alte
deployment.Spec.Template.Spec.Containers[0].Args = []string{}

labelSelector := fmt.Sprintf("ceph_daemon_type=%s,ceph_daemon_id=%s", deployment.Spec.Template.Labels["ceph_daemon_type"], deployment.Spec.Template.Labels["ceph_daemon_id"])
deploymentPodName, err := k8sutil.WaitForPodToRun(context, clusterNamespace, labelSelector)
deploymentPodName, err := k8sutil.WaitForPodToRun(ctx, k8sclientset, clusterNamespace, labelSelector)
if err != nil {
return err
}

if err := SetDeploymentScale(context, clusterNamespace, deployment.Name, 0); err != nil {
if err := SetDeploymentScale(ctx, k8sclientset, clusterNamespace, deployment.Name, 0); err != nil {
return err
}

logging.Info("deployment %s scaled down\n", deployment.Name)
logging.Info("waiting for the deployment pod %s to be deleted\n", deploymentPodName.Name)

err = waitForPodDeletion(context, clusterNamespace, deploymentName)
err = waitForPodDeletion(ctx, k8sclientset, clusterNamespace, deploymentName)
if err != nil {
return err
}
Expand All @@ -87,17 +89,17 @@ func startDebug(context *k8sutil.Context, clusterNamespace, deploymentName, alte
Spec: deployment.Spec,
}

debugDeployment, err := context.Clientset.AppsV1().Deployments(clusterNamespace).Create(context.Context, debugDeploymentSpec, v1.CreateOptions{})
debugDeployment, err := k8sclientset.AppsV1().Deployments(clusterNamespace).Create(ctx, debugDeploymentSpec, v1.CreateOptions{})
if err != nil {
return fmt.Errorf("Error creating deployment %s. %v\n", debugDeploymentSpec, err)
}
logging.Info("ensure the debug deployment %s is scaled up\n", deploymentName)

if err := SetDeploymentScale(context, clusterNamespace, debugDeployment.Name, 1); err != nil {
if err := SetDeploymentScale(ctx, k8sclientset, clusterNamespace, debugDeployment.Name, 1); err != nil {
return err
}

pod, err := k8sutil.WaitForPodToRun(context, clusterNamespace, labelSelector)
pod, err := k8sutil.WaitForPodToRun(ctx, k8sclientset, clusterNamespace, labelSelector)
if err != nil {
logging.Fatal(err)
}
Expand All @@ -106,7 +108,7 @@ func startDebug(context *k8sutil.Context, clusterNamespace, deploymentName, alte
return nil
}

func SetDeploymentScale(context *k8sutil.Context, clusterNamespace, deploymentName string, scaleCount int) error {
func SetDeploymentScale(ctx context.Context, k8sclientset kubernetes.Interface, clusterNamespace, deploymentName string, scaleCount int) error {
scale := &autoscalingv1.Scale{
ObjectMeta: v1.ObjectMeta{
Name: deploymentName,
Expand All @@ -116,16 +118,16 @@ func SetDeploymentScale(context *k8sutil.Context, clusterNamespace, deploymentNa
Replicas: int32(scaleCount),
},
}
_, err := context.Clientset.AppsV1().Deployments(clusterNamespace).UpdateScale(context.Context, deploymentName, scale, v1.UpdateOptions{})
_, err := k8sclientset.AppsV1().Deployments(clusterNamespace).UpdateScale(ctx, deploymentName, scale, v1.UpdateOptions{})
if err != nil {
return fmt.Errorf("failed to update scale of deployment %s. %v\n", deploymentName, err)
}
return nil
}

func GetDeployment(context *k8sutil.Context, clusterNamespace, deploymentName string) (*appsv1.Deployment, error) {
func GetDeployment(ctx context.Context, k8sclientset kubernetes.Interface, clusterNamespace, deploymentName string) (*appsv1.Deployment, error) {
logging.Info("fetching the deployment %s to be running\n", deploymentName)
deployment, err := context.Clientset.AppsV1().Deployments(clusterNamespace).Get(context.Context, deploymentName, v1.GetOptions{})
deployment, err := k8sclientset.AppsV1().Deployments(clusterNamespace).Get(ctx, deploymentName, v1.GetOptions{})
if err != nil {
return nil, err
}
Expand All @@ -134,9 +136,9 @@ func GetDeployment(context *k8sutil.Context, clusterNamespace, deploymentName st
return deployment, nil
}

func waitForPodDeletion(context *k8sutil.Context, clusterNamespace, podName string) error {
func waitForPodDeletion(ctx context.Context, k8sclientset kubernetes.Interface, clusterNamespace, podName string) error {
for i := 0; i < 60; i++ {
_, err := context.Clientset.CoreV1().Pods(clusterNamespace).Get(context.Context, podName, v1.GetOptions{})
_, err := k8sclientset.CoreV1().Pods(clusterNamespace).Get(ctx, podName, v1.GetOptions{})
if kerrors.IsNotFound(err) {
return nil
}
Expand Down
18 changes: 9 additions & 9 deletions pkg/debug/stop_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,42 @@ limitations under the License.
package debug

import (
"context"
"fmt"
"strings"

"github.com/rook/kubectl-rook-ceph/pkg/logging"
kerrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/rook/kubectl-rook-ceph/pkg/k8sutil"
"github.com/rook/kubectl-rook-ceph/pkg/logging"
"k8s.io/client-go/kubernetes"
)

func StopDebug(context *k8sutil.Context, clusterNamespace, deploymentName string) {
func StopDebug(ctx context.Context, k8sclientset kubernetes.Interface, clusterNamespace, deploymentName string) {

err := stopDebug(context, clusterNamespace, deploymentName)
err := stopDebug(ctx, k8sclientset, clusterNamespace, deploymentName)
if err != nil {
logging.Fatal(err)
}
}

func stopDebug(context *k8sutil.Context, clusterNamespace, deploymentName string) error {
func stopDebug(ctx context.Context, k8sclientset kubernetes.Interface, clusterNamespace, deploymentName string) error {
if !strings.HasSuffix(deploymentName, "-debug") {
deploymentName = deploymentName + "-debug"
}

debugDeployment, err := GetDeployment(context, clusterNamespace, deploymentName)
debugDeployment, err := GetDeployment(ctx, k8sclientset, clusterNamespace, deploymentName)
if err != nil {
return fmt.Errorf("Missing mon or osd debug deployment name %s. %v\n", deploymentName, err)
}

logging.Info("removing debug mode from deployment %s\n", debugDeployment.Name)
err = context.Clientset.AppsV1().Deployments(clusterNamespace).Delete(context.Context, debugDeployment.Name, v1.DeleteOptions{})
err = k8sclientset.AppsV1().Deployments(clusterNamespace).Delete(ctx, debugDeployment.Name, v1.DeleteOptions{})
if err != nil && !kerrors.IsNotFound(err) {
return fmt.Errorf("Error deleting deployment %s: %v", debugDeployment.Name, err)
}

original_deployment_name := strings.ReplaceAll(deploymentName, "-debug", "")
if err := SetDeploymentScale(context, clusterNamespace, original_deployment_name, 1); err != nil {
if err := SetDeploymentScale(ctx, k8sclientset, clusterNamespace, original_deployment_name, 1); err != nil {
return err
}
logging.Info("Successfully deleted debug deployment and restored deployment %q", original_deployment_name)
Expand Down
Loading

0 comments on commit 21a07b9

Please sign in to comment.