Skip to content

Commit

Permalink
Add mgrpxy start, stop and restart commands
Browse files Browse the repository at this point in the history
  • Loading branch information
cbosdo committed Mar 26, 2024
1 parent 6537a35 commit e296d24
Show file tree
Hide file tree
Showing 14 changed files with 297 additions and 15 deletions.
6 changes: 6 additions & 0 deletions mgrpxy/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/uyuni-project/uyuni-tools/mgrpxy/cmd/install"
"github.com/uyuni-project/uyuni-tools/mgrpxy/cmd/restart"
"github.com/uyuni-project/uyuni-tools/mgrpxy/cmd/start"
"github.com/uyuni-project/uyuni-tools/mgrpxy/cmd/status"
"github.com/uyuni-project/uyuni-tools/mgrpxy/cmd/stop"
"github.com/uyuni-project/uyuni-tools/mgrpxy/cmd/uninstall"
"github.com/uyuni-project/uyuni-tools/shared/completion"
"github.com/uyuni-project/uyuni-tools/shared/types"
Expand Down Expand Up @@ -59,6 +62,9 @@ func NewUyuniproxyCommand() (*cobra.Command, error) {
rootCmd.AddCommand(uninstallCmd)
rootCmd.AddCommand(completion.NewCommand(globalFlags))
rootCmd.AddCommand(status.NewCommand(globalFlags))
rootCmd.AddCommand(start.NewCommand(globalFlags))
rootCmd.AddCommand(stop.NewCommand(globalFlags))
rootCmd.AddCommand(restart.NewCommand(globalFlags))

return rootCmd, nil
}
20 changes: 20 additions & 0 deletions mgrpxy/cmd/restart/kubernetes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-FileCopyrightText: 2024 SUSE LLC
//
// SPDX-License-Identifier: Apache-2.0

package restart

import (
"github.com/spf13/cobra"
"github.com/uyuni-project/uyuni-tools/shared/kubernetes"
"github.com/uyuni-project/uyuni-tools/shared/types"
)

func kubernetesRestart(
globalFlags *types.GlobalFlags,
flags *restartFlags,
cmd *cobra.Command,
args []string,
) error {
return kubernetes.Restart(kubernetes.ProxyFilter)
}
20 changes: 20 additions & 0 deletions mgrpxy/cmd/restart/podman.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-FileCopyrightText: 2024 SUSE LLC
//
// SPDX-License-Identifier: Apache-2.0

package restart

import (
"github.com/spf13/cobra"
"github.com/uyuni-project/uyuni-tools/shared/podman"
"github.com/uyuni-project/uyuni-tools/shared/types"
)

func podmanRestart(
globalFlags *types.GlobalFlags,
flags *restartFlags,
cmd *cobra.Command,
args []string,
) error {
return podman.RestartService(podman.ProxyService)
}
44 changes: 44 additions & 0 deletions mgrpxy/cmd/restart/restart.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// SPDX-FileCopyrightText: 2024 SUSE LLC
//
// SPDX-License-Identifier: Apache-2.0

package restart

import (
"github.com/spf13/cobra"
"github.com/uyuni-project/uyuni-tools/shared"
"github.com/uyuni-project/uyuni-tools/shared/types"
"github.com/uyuni-project/uyuni-tools/shared/utils"
)

type restartFlags struct {
Backend string
}

// NewCommand to restart server.
func NewCommand(globalFlags *types.GlobalFlags) *cobra.Command {
restartCmd := &cobra.Command{
Use: "restart",
Short: "restart the proxy",
Long: "Restart the proxy",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
var flags restartFlags
return utils.CommandHelper(globalFlags, cmd, args, &flags, restart)
},
}
restartCmd.SetUsageTemplate(restartCmd.UsageTemplate())

utils.AddBackendFlag(restartCmd)

return restartCmd
}

func restart(globalFlags *types.GlobalFlags, flags *restartFlags, cmd *cobra.Command, args []string) error {
fn, err := shared.ChooseProxyPodmanOrKubernetes(cmd.Flags(), podmanRestart, kubernetesRestart)
if err != nil {
return err
}

return fn(globalFlags, flags, cmd, args)
}
20 changes: 20 additions & 0 deletions mgrpxy/cmd/start/kubernetes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-FileCopyrightText: 2024 SUSE LLC
//
// SPDX-License-Identifier: Apache-2.0

package start

import (
"github.com/spf13/cobra"
"github.com/uyuni-project/uyuni-tools/shared/kubernetes"
"github.com/uyuni-project/uyuni-tools/shared/types"
)

func kubernetesStart(
globalFlags *types.GlobalFlags,
flags *startFlags,
cmd *cobra.Command,
args []string,
) error {
return kubernetes.Start(kubernetes.ProxyFilter)
}
20 changes: 20 additions & 0 deletions mgrpxy/cmd/start/podman.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-FileCopyrightText: 2024 SUSE LLC
//
// SPDX-License-Identifier: Apache-2.0

package start

import (
"github.com/spf13/cobra"
"github.com/uyuni-project/uyuni-tools/shared/podman"
"github.com/uyuni-project/uyuni-tools/shared/types"
)

func podmanStart(
globalFlags *types.GlobalFlags,
flags *startFlags,
cmd *cobra.Command,
args []string,
) error {
return podman.StartService(podman.ProxyService)
}
46 changes: 46 additions & 0 deletions mgrpxy/cmd/start/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-FileCopyrightText: 2024 SUSE LLC
//
// SPDX-License-Identifier: Apache-2.0

package start

import (
"github.com/spf13/cobra"
"github.com/uyuni-project/uyuni-tools/shared"
"github.com/uyuni-project/uyuni-tools/shared/types"
"github.com/uyuni-project/uyuni-tools/shared/utils"
)

type startFlags struct {
Backend string
}

// NewCommand starts the server.
func NewCommand(globalFlags *types.GlobalFlags) *cobra.Command {
startCmd := &cobra.Command{
Use: "start",
Short: "start the proxy",
Long: "Start the proxy",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
var flags startFlags
return utils.CommandHelper(globalFlags, cmd, args, &flags, start)
},
}
startCmd.SetUsageTemplate(startCmd.UsageTemplate())

if utils.KubernetesBuilt {
utils.AddBackendFlag(startCmd)
}

return startCmd
}

func start(globalFlags *types.GlobalFlags, flags *startFlags, cmd *cobra.Command, args []string) error {
fn, err := shared.ChooseProxyPodmanOrKubernetes(cmd.Flags(), podmanStart, kubernetesStart)
if err != nil {
return err
}

return fn(globalFlags, flags, cmd, args)
}
20 changes: 20 additions & 0 deletions mgrpxy/cmd/stop/kubernetes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-FileCopyrightText: 2024 SUSE LLC
//
// SPDX-License-Identifier: Apache-2.0

package stop

import (
"github.com/spf13/cobra"
"github.com/uyuni-project/uyuni-tools/shared/kubernetes"
"github.com/uyuni-project/uyuni-tools/shared/types"
)

func kubernetesStop(
globalFlags *types.GlobalFlags,
flags *stopFlags,
cmd *cobra.Command,
args []string,
) error {
return kubernetes.Stop(kubernetes.ProxyFilter)
}
20 changes: 20 additions & 0 deletions mgrpxy/cmd/stop/podman.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-FileCopyrightText: 2024 SUSE LLC
//
// SPDX-License-Identifier: Apache-2.0

package stop

import (
"github.com/spf13/cobra"
"github.com/uyuni-project/uyuni-tools/shared/podman"
"github.com/uyuni-project/uyuni-tools/shared/types"
)

func podmanStop(
globalFlags *types.GlobalFlags,
flags *stopFlags,
cmd *cobra.Command,
args []string,
) error {
return podman.StopService(podman.ProxyService)
}
45 changes: 45 additions & 0 deletions mgrpxy/cmd/stop/stop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-FileCopyrightText: 2024 SUSE LLC
//
// SPDX-License-Identifier: Apache-2.0

package stop

import (
"github.com/spf13/cobra"
"github.com/uyuni-project/uyuni-tools/shared"
"github.com/uyuni-project/uyuni-tools/shared/types"
"github.com/uyuni-project/uyuni-tools/shared/utils"
)

type stopFlags struct {
Backend string
}

// NewCommand to stop server.
func NewCommand(globalFlags *types.GlobalFlags) *cobra.Command {
stopCmd := &cobra.Command{
Use: "stop",
Short: "stop the proxy",
Long: "Stop the proxy",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
var flags stopFlags
return utils.CommandHelper(globalFlags, cmd, args, &flags, stop)
},
}

stopCmd.SetUsageTemplate(stopCmd.UsageTemplate())

utils.AddBackendFlag(stopCmd)

return stopCmd
}

func stop(globalFlags *types.GlobalFlags, flags *stopFlags, cmd *cobra.Command, args []string) error {
fn, err := shared.ChooseProxyPodmanOrKubernetes(cmd.Flags(), podmanStop, kubernetesStop)
if err != nil {
return err
}

return fn(globalFlags, flags, cmd, args)
}
20 changes: 20 additions & 0 deletions shared/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,26 @@ func ChoosePodmanOrKubernetes[F interface{}](
}

cnx := NewConnection(backend, podman.ServerContainerName, kubernetes.ServerFilter)
return chooseBackend(cnx, podmanFn, kubernetesFn)
}

// ChooseProxyPodmanOrKubernetes selects either the podman or the kubernetes function based on the backend for the proxy.
func ChooseProxyPodmanOrKubernetes[F interface{}](
flags *pflag.FlagSet,
podmanFn utils.CommandFunc[F],
kubernetesFn utils.CommandFunc[F],
) (utils.CommandFunc[F], error) {
backend, _ := flags.GetString("backend")

cnx := NewConnection(backend, podman.ProxyContainerNames[0], kubernetes.ProxyFilter)
return chooseBackend(cnx, podmanFn, kubernetesFn)
}

func chooseBackend[F interface{}](
cnx *Connection,
podmanFn utils.CommandFunc[F],
kubernetesFn utils.CommandFunc[F],
) (utils.CommandFunc[F], error) {
command, err := cnx.GetCommand()
if err != nil {
return nil, fmt.Errorf("failed to determine suitable backend")
Expand Down
18 changes: 9 additions & 9 deletions shared/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,24 +88,24 @@ func guessIngress() (string, error) {
}

// Restart restarts the pod.
func Restart(ServerFilter string) error {
if err := Stop(ServerFilter); err != nil {
return fmt.Errorf("cannot stop %s: %s", ServerFilter, err)
func Restart(filter string) error {
if err := Stop(filter); err != nil {
return fmt.Errorf("cannot stop %s: %s", filter, err)
}
return Start(ServerFilter)
return Start(filter)
}

// Start starts the pod.
func Start(ServerFilter string) error {
func Start(filter string) error {
// if something is running, we don't need to set replicas to 1
if _, err := GetNode("uyuni"); err != nil {
return ReplicasTo(ServerFilter, 1)
if _, err := GetNode(filter); err != nil {
return ReplicasTo(filter, 1)
}
log.Debug().Msgf("Already running")
return nil
}

// Stop stop the pod.
func Stop(ServerFilter string) error {
return ReplicasTo(ServerFilter, 0)
func Stop(filter string) error {
return ReplicasTo(filter, 0)
}
12 changes: 6 additions & 6 deletions shared/kubernetes/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func GetDeploymentStatus(namespace string, name string) (*DeploymentStatus, erro
// ReplicasTo set the replica for an app to the given value.
// Scale the number of replicas of the server.
func ReplicasTo(filter string, replica uint) error {
args := []string{"scale", "deploy", "uyuni", "--replicas"}
args := []string{"scale", "deploy", filter, "--replicas"}
log.Debug().Msgf("Setting replicas for pod in %s to %d", filter, replica)
args = append(args, fmt.Sprint(replica))

Expand All @@ -152,7 +152,7 @@ func ReplicasTo(filter string, replica uint) error {
return fmt.Errorf("cannot run kubectl %s: %s", args, err)
}

pods, err := getPods(ServerFilter)
pods, err := getPods(filter)
if err != nil {
return fmt.Errorf("cannot get pods for %s: %s", filter, err)
}
Expand Down Expand Up @@ -339,9 +339,9 @@ func waitForPod(podname string) error {
}

// GetNode return the node where the app is running.
func GetNode(appName string) (string, error) {
func GetNode(filter string) (string, error) {
nodeName := ""
cmdArgs := []string{"get", "pod", "-lapp=" + appName, "-o", "jsonpath={.items[*].spec.nodeName}"}
cmdArgs := []string{"get", "pod", filter, "-o", "jsonpath={.items[*].spec.nodeName}"}
for i := 0; i < 60; i++ {
out, err := utils.RunCmdOutput(zerolog.DebugLevel, "kubectl", cmdArgs...)
if err == nil {
Expand All @@ -350,9 +350,9 @@ func GetNode(appName string) (string, error) {
}
}
if len(nodeName) > 0 {
log.Debug().Msgf("Node name for app %s is: %s", appName, nodeName)
log.Debug().Msgf("Node name matching filter %s is: %s", filter, nodeName)
} else {
return "", fmt.Errorf("cannot find Node name for app %s", appName)
return "", fmt.Errorf("cannot find node name matching filter %s", filter)
}
return nodeName, nil
}
Expand Down
1 change: 1 addition & 0 deletions uyuni-tools.changes.cbosdo.pxy-start
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Add mgrpxy start, stop and restart commands

0 comments on commit e296d24

Please sign in to comment.