Skip to content

Commit

Permalink
start/stop/restart command for k8s
Browse files Browse the repository at this point in the history
  • Loading branch information
mbussolotto committed Feb 20, 2024
1 parent b442d04 commit dedd01b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 12 deletions.
5 changes: 2 additions & 3 deletions mgradm/cmd/restart/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
package restart

import (
"fmt"

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

Expand All @@ -19,5 +18,5 @@ func kubernetesRestart(
cmd *cobra.Command,
args []string,
) error {
return fmt.Errorf("not implemented")
return kubernetes.RestartService(kubernetes.ServerFilter)
}
5 changes: 2 additions & 3 deletions mgradm/cmd/start/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
package start

import (
"fmt"

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

Expand All @@ -19,5 +18,5 @@ func kubernetesStart(
cmd *cobra.Command,
args []string,
) error {
return fmt.Errorf("not implemented")
return kubernetes.StartService(kubernetes.ServerFilter)
}
5 changes: 2 additions & 3 deletions mgradm/cmd/stop/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
package stop

import (
"fmt"

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

Expand All @@ -19,5 +18,5 @@ func kubernetesStop(
cmd *cobra.Command,
args []string,
) error {
return fmt.Errorf("not implemented")
return kubernetes.StopService(kubernetes.ServerFilter)
}
24 changes: 24 additions & 0 deletions shared/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package kubernetes

import (
"fmt"
"os"
"strings"

Expand Down Expand Up @@ -87,3 +88,26 @@ func guessIngress() string {

return ingress
}

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

// StartService starts the pod.
func StartService(ServerFilter 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)
}
log.Debug().Msgf("Already running")
return nil
}

// StopService stop the pod.
func StopService(ServerFilter string) error {
return ReplicasTo(ServerFilter, 0)
}
4 changes: 2 additions & 2 deletions shared/kubernetes/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func waitForReplica(podname string, replica uint) error {
if replica == 0 {
return waitForReplicaZero(podname)
}
cmdArgs := []string{"get", "rs", podname, "--output=custom-columns=DESIRED:.status.replicas", "--no-headers"}
cmdArgs := []string{"get", "pod", podname, "--output=custom-columns=STATUS:.status.phase", "--no-headers"}

var err error

Expand All @@ -231,7 +231,7 @@ func waitForReplica(podname string, replica uint) error {
if err != nil {
return fmt.Errorf("cannot execute %s: %s", strings.Join(cmdArgs, string(" ")), err)
}
if string(outStr) == fmt.Sprint(replica) {
if string(outStr) == "Running" {
log.Debug().Msgf("%s pod replica is now %d", podname, replica)
break
}
Expand Down
2 changes: 1 addition & 1 deletion shared/utils/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func RunCmdOutput(logLevel zerolog.Level, command string, args ...string) ([]byt

output, err := exec.Command(command, args...).Output()
if err != nil {
log.Err(err).Msgf("Command returned Error: %s", output)
log.Warn().Msgf("Command returned with error: %s. Be sure to handle the error if required", err)
} else if len(output) > 0 {
log.Debug().Msgf("Command output: %s", output)
}
Expand Down

0 comments on commit dedd01b

Please sign in to comment.