Skip to content

Commit

Permalink
few changes to the logic
Browse files Browse the repository at this point in the history
Signed-off-by: Feny Mehta <[email protected]>
  • Loading branch information
fbm3307 committed Sep 17, 2024
1 parent f2c29ee commit ba8866e
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 181 deletions.
78 changes: 39 additions & 39 deletions pkg/cmd/adm/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,46 +28,62 @@ import (
func NewRestartCmd() *cobra.Command {
var targetCluster string
command := &cobra.Command{
Use: "restart -t <cluster-name> <host|member-1|member-2>",
Use: "restart -t <cluster-name>",
Short: "Restarts a deployment",
Long: `Restarts the deployment with the given name in the operator namespace.
If no deployment name is provided, then it lists all existing deployments in the namespace.`,
Args: cobra.RangeArgs(0, 1),
RunE: func(cmd *cobra.Command, args []string) error {
term := ioutils.NewTerminal(cmd.InOrStdin, cmd.OutOrStdout)
ctx := clicontext.NewCommandContext(term, client.DefaultNewClient)
return restart(ctx, targetCluster, args...)
return restart(ctx, targetCluster)
},
}
command.Flags().StringVarP(&targetCluster, "target-cluster", "t", "", "The target cluster")
flags.MustMarkRequired(command, "target-cluster")
return command
}

func restart(ctx *clicontext.CommandContext, clusterName string, operatorType ...string) error {
func restart(ctx *clicontext.CommandContext, clusterName string) error {
kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
factory := cmdutil.NewFactory(cmdutil.NewMatchVersionFlags(kubeConfigFlags))
ioStreams := genericclioptions.IOStreams{
In: os.Stdin,
Out: os.Stdout,
ErrOut: os.Stderr,
}
kubeConfigFlags.ClusterName = nil // `cluster` flag is redefined for our own purpose
kubeConfigFlags.AuthInfoName = nil // unused here, so we can hide it
kubeConfigFlags.Context = nil // unused here, so we can hide it

cfg, err := configuration.LoadClusterConfig(ctx, clusterName)
if err != nil {
return err
}
cl, err := ctx.NewClient(cfg.Token, cfg.ServerAPI)

kubeConfigFlags.Namespace = &cfg.OperatorNamespace
kubeConfigFlags.APIServer = &cfg.ServerAPI
kubeConfigFlags.BearerToken = &cfg.Token
kubeconfig, err := client.EnsureKsctlConfigFile()
if err != nil {
return err
}
kubeConfigFlags.KubeConfig = &kubeconfig

if len(operatorType) == 0 {
return fmt.Errorf("please mention one of the following operator names to restart: host | member-1 | member-2")
}
cl, err := ctx.NewClient(cfg.Token, cfg.ServerAPI)

if !ctx.AskForConfirmation(
ioutils.WithMessagef("restart the '%s' operator in namespace '%s'", operatorType[0], cfg.OperatorNamespace)) {
return nil
if err != nil {
return err
}

return restartDeployment(ctx, cl, cfg.OperatorNamespace)
// if !ctx.AskForConfirmation(
// ioutils.WithMessagef("restart the '%s' operator in namespace '%s'", clusterName, cfg.OperatorNamespace)) {
// return nil
// }

return restartDeployment(ctx, cl, cfg.OperatorNamespace, factory, ioStreams)
}

func restartDeployment(ctx *clicontext.CommandContext, cl runtimeclient.Client, ns string) error {
func restartDeployment(ctx *clicontext.CommandContext, cl runtimeclient.Client, ns string, f cmdutil.Factory, ioStreams genericclioptions.IOStreams) error {
olmDeploymentList, nonOlmDeploymentlist, err := getExistingDeployments(cl, ns)
if err != nil {
return err
Expand All @@ -77,26 +93,26 @@ func restartDeployment(ctx *clicontext.CommandContext, cl runtimeclient.Client,
return fmt.Errorf("OLM based deploymont not found in %s", ns)
}
for _, olmDeployment := range olmDeploymentList.Items {
if err := deletePods(ctx, cl, olmDeployment, ns); err != nil {
if err := deletePods(ctx, cl, olmDeployment, f, ioStreams); err != nil {
return err
}
}
if nonOlmDeploymentlist == nil {
return fmt.Errorf("non-OLM based deploymont not found in %s", ns)
}
for _, nonOlmDeployment := range nonOlmDeploymentlist.Items {
if err := restartNonOlmDeployments(ns, nonOlmDeployment); err != nil {
if err := restartNonOlmDeployments(nonOlmDeployment, f, ioStreams); err != nil {
return err
}
//check the rollout status
if err := checkRolloutStatus(ns); err != nil {
if err := checkRolloutStatus(f, ioStreams); err != nil {
return err
}
}
return nil
}

func deletePods(ctx *clicontext.CommandContext, cl runtimeclient.Client, deployment appsv1.Deployment, ns string) error {
func deletePods(ctx *clicontext.CommandContext, cl runtimeclient.Client, deployment appsv1.Deployment, f cmdutil.Factory, ioStreams genericclioptions.IOStreams) error {
//get pods by label selector from the deployment
pods := corev1.PodList{}
selector, _ := metav1.LabelSelectorAsSelector(deployment.Spec.Selector)
Expand All @@ -113,28 +129,21 @@ func deletePods(ctx *clicontext.CommandContext, cl runtimeclient.Client, deploym
}

//check the rollout status
if err := checkRolloutStatus(ns); err != nil {
if err := checkRolloutStatus(f, ioStreams); err != nil {
return err
}
return nil

}

func restartNonOlmDeployments(ns string, deployment appsv1.Deployment) error {
kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
hFactory := cmdutil.NewFactory(cmdutil.NewMatchVersionFlags(kubeConfigFlags))
ioStreams := genericclioptions.IOStreams{
In: nil, // Not to forward the Standard Input
Out: os.Stdout,
ErrOut: os.Stderr,
}
func restartNonOlmDeployments(deployment appsv1.Deployment, f cmdutil.Factory, ioStreams genericclioptions.IOStreams) error {

o := kubectlrollout.NewRolloutRestartOptions(ioStreams)

if err := o.Complete(hFactory, nil, []string{"deployments"}); err != nil {
if err := o.Complete(f, nil, []string{"deployments"}); err != nil {
panic(err)
}
o.Namespace = ns

o.Resources = []string{"deployment/" + deployment.Name}

if err := o.Validate(); err != nil {
Expand All @@ -143,22 +152,13 @@ func restartNonOlmDeployments(ns string, deployment appsv1.Deployment) error {
return o.RunRestart()
}

func checkRolloutStatus(ns string) error {
kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
Factory := cmdutil.NewFactory(cmdutil.NewMatchVersionFlags(kubeConfigFlags))
ioStreams := genericclioptions.IOStreams{
In: nil, // Not to forward the Standard Input
Out: os.Stdout,
ErrOut: os.Stderr,
}

func checkRolloutStatus(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) error {
cmd := kubectlrollout.NewRolloutStatusOptions(ioStreams)

if err := cmd.Complete(Factory, []string{"deployment"}); err != nil {
if err := cmd.Complete(f, []string{"deployment"}); err != nil {
panic(err)
}
cmd.LabelSelector = "provider=codeready-toolchain"
cmd.Namespace = ns
if err := cmd.Validate(); err != nil {
panic(err)
}
Expand Down
Loading

0 comments on commit ba8866e

Please sign in to comment.