Skip to content

Commit

Permalink
Use the new help functions in mgradm uninstall command
Browse files Browse the repository at this point in the history
  • Loading branch information
cbosdo committed Feb 7, 2024
1 parent 2eb72da commit 44e71f2
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 44 deletions.
20 changes: 14 additions & 6 deletions mgradm/cmd/uninstall/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,25 @@ package uninstall
import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/uyuni-project/uyuni-tools/shared/kubernetes"
"github.com/uyuni-project/uyuni-tools/shared/types"
"github.com/uyuni-project/uyuni-tools/shared/utils"
)

func uninstallForKubernetes(dryRun bool) {
func uninstallForKubernetes(
globalFlags *types.GlobalFlags,
flags *uninstallFlags,
cmd *cobra.Command,
args []string,
) error {
clusterInfos := kubernetes.CheckCluster()
kubeconfig := clusterInfos.GetKubeconfig()

// TODO Find all the PVs related to the server if we want to delete them

// Uninstall uyuni
namespace := kubernetes.HelmUninstall(kubeconfig, "uyuni", "", dryRun)
namespace := kubernetes.HelmUninstall(kubeconfig, "uyuni", "", flags.DryRun)

// Remove the remaining configmap and secrets
if namespace != "" {
Expand All @@ -31,7 +38,7 @@ func uninstallForKubernetes(dryRun bool) {
caSecret = ""
}

if dryRun {
if flags.DryRun {
log.Info().Msgf("Would run kubectl delete -n %s configmap uyuni-ca", namespace)
log.Info().Msgf("Would run kubectl delete -n %s secret uyuni-cert %s", namespace, caSecret)
} else {
Expand All @@ -58,17 +65,18 @@ func uninstallForKubernetes(dryRun bool) {
// Since some storage plugins don't handle Delete policy, we may need to check for error events to avoid infinite loop

// Uninstall cert-manager if we installed it
kubernetes.HelmUninstall(kubeconfig, "cert-manager", "-linstalledby=mgradm", dryRun)
kubernetes.HelmUninstall(kubeconfig, "cert-manager", "-linstalledby=mgradm", flags.DryRun)

// Remove the K3s Traefik config
if clusterInfos.IsK3s() {
kubernetes.UninstallK3sTraefikConfig(dryRun)
kubernetes.UninstallK3sTraefikConfig(flags.DryRun)
}

// Remove the rke2 nginx config
if clusterInfos.IsRke2() {
kubernetes.UninstallRke2NginxConfig(dryRun)
kubernetes.UninstallRke2NginxConfig(flags.DryRun)
}
return nil
}

const kubernetesHelp = kubernetes.UninstallHelp
11 changes: 6 additions & 5 deletions mgradm/cmd/uninstall/nokubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@

package uninstall

func uninstallForKubernetes(dryRun bool) {
}

func helmUninstall(kubeconfig string, deployment string, filter string, dryRun bool) string {
return ""
func uninstallForKubernetes(
globalFlags *types.GlobalFlags,
flags *uninstallFlags,
cmd *cobra.Command,
args []string,
) {
}

const kubernetesHelp = ""
25 changes: 17 additions & 8 deletions mgradm/cmd/uninstall/podman.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,45 @@
// SPDX-FileCopyrightText: 2023 SUSE LLC
// SPDX-FileCopyrightText: 2024 SUSE LLC
//
// SPDX-License-Identifier: Apache-2.0

package uninstall

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

func uninstallForPodman(dryRun bool, purge bool) {
func uninstallForPodman(
globalFlags *types.GlobalFlags,
flags *uninstallFlags,
cmd *cobra.Command,
args []string,
) error {

// Uninstall the service
podman.UninstallService("uyuni-server", dryRun)
podman.UninstallService("uyuni-server", flags.DryRun)

// Force stop the pod
podman.DeleteContainer(podman.ServerContainerName, dryRun)
podman.DeleteContainer(podman.ServerContainerName, flags.DryRun)

// Remove the volumes
if purge {
if flags.PurgeVolumes {
volumes := []string{"cgroup"}
for volume := range utils.VOLUMES {
volumes = append(volumes, volume)
}
for _, volume := range volumes {
podman.DeleteVolume(volume, dryRun)
podman.DeleteVolume(volume, flags.DryRun)
}
log.Info().Msg("All volumes removed")
}

podman.DeleteNetwork(dryRun)
podman.DeleteNetwork(flags.DryRun)

podman.ReloadDaemon(dryRun)
podman.ReloadDaemon(flags.DryRun)

return nil
}
50 changes: 25 additions & 25 deletions mgradm/cmd/uninstall/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,49 @@
package uninstall

import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/uyuni-project/uyuni-tools/shared"
"github.com/uyuni-project/uyuni-tools/shared/kubernetes"
"github.com/uyuni-project/uyuni-tools/shared/podman"
"github.com/uyuni-project/uyuni-tools/shared/types"
"github.com/uyuni-project/uyuni-tools/shared/utils"
)

type uninstallFlags struct {
Backend string
DryRun bool
PurgeVolumes bool
}

func NewCommand(globalFlags *types.GlobalFlags) *cobra.Command {
uninstallCmd := &cobra.Command{
Use: "uninstall",
Short: "uninstall a server",
Long: "Uninstall a server and optionally the corresponding volumes." + kubernetesHelp,
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
dryRun, _ := cmd.Flags().GetBool("dry-run")
purge, _ := cmd.Flags().GetBool("purge-volumes")

backend := "podman"
if utils.KubernetesBuilt {
backend, _ = cmd.Flags().GetString("backend")
}

cnx := shared.NewConnection(backend, podman.ServerContainerName, kubernetes.ServerFilter)
command, err := cnx.GetCommand()
if err != nil {
log.Fatal().Err(err).Msg("Failed to determine suitable backend")
}
switch command {
case "podman":
uninstallForPodman(dryRun, purge)
case "kubectl":
uninstallForKubernetes(dryRun)
}
RunE: func(cmd *cobra.Command, args []string) error {
var flags uninstallFlags
return utils.CommandHelper(globalFlags, cmd, args, &flags, uninstall)
},
}
uninstallCmd.Flags().BoolP("dry-run", "n", false, "Only show what would be done")
uninstallCmd.Flags().Bool("purge-volumes", false, "Also remove the volume")
uninstallCmd.Flags().BoolP("dryRun", "n", false, "Only show what would be done")
uninstallCmd.Flags().Bool("purgeVolumes", false, "Also remove the volume")

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

return uninstallCmd
}

func uninstall(
globalFlags *types.GlobalFlags,
flags *uninstallFlags,
cmd *cobra.Command,
args []string,
) error {
fn, err := shared.ChoosePodmanOrKubernetes(cmd.Flags(), uninstallForPodman, uninstallForKubernetes)
if err != nil {
return err
}

return fn(globalFlags, flags, cmd, args)
}

0 comments on commit 44e71f2

Please sign in to comment.