From 8b5b13a79642a97bea9d90cc2097ee04b6494698 Mon Sep 17 00:00:00 2001 From: Dimitar Date: Tue, 21 Jan 2025 11:15:27 -0800 Subject: [PATCH] PRODENG-2826 MCR Uninstall now swarm drains and prunes volumes Signed-off-by: Dimitar --- pkg/mcr/mcr.go | 21 +++++++++++++++++++++ pkg/product/mke/phase/uninstall_mcr.go | 18 ++++++++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/pkg/mcr/mcr.go b/pkg/mcr/mcr.go index 8d41b356..c9235cee 100644 --- a/pkg/mcr/mcr.go +++ b/pkg/mcr/mcr.go @@ -4,6 +4,8 @@ import ( "fmt" "github.com/Mirantis/mcc/pkg/product/mke/api" + "github.com/Mirantis/mcc/pkg/swarm" + log "github.com/sirupsen/logrus" ) @@ -37,3 +39,22 @@ func EnsureMCRVersion(h *api.Host, specMcrVersion string, forceMCRRestart bool) return nil } + +// DrainNode drains a node from the workload via docker drain command. +func DrainNode(lead *api.Host, h *api.Host) error { + nodeID, err := swarm.NodeID(h) + if err != nil { + return fmt.Errorf("failed to get node ID for %s: %w", h, err) + } + + drainCmd := lead.Configurer.DockerCommandf("node update --availability drain %s", nodeID) + if err := lead.Exec(drainCmd); err != nil { + return fmt.Errorf("%s: failed to run MKE uninstaller: %w", lead, err) + } + if err := lead.Exec(drainCmd); err != nil { + return fmt.Errorf("failed to drain node %s: %w", nodeID, err) + } + + log.Infof("%s: node %s drained", lead, nodeID) + return nil +} diff --git a/pkg/product/mke/phase/uninstall_mcr.go b/pkg/product/mke/phase/uninstall_mcr.go index 9e82f4ab..5117076a 100644 --- a/pkg/product/mke/phase/uninstall_mcr.go +++ b/pkg/product/mke/phase/uninstall_mcr.go @@ -3,6 +3,7 @@ package phase import ( "fmt" + "github.com/Mirantis/mcc/pkg/mcr" "github.com/Mirantis/mcc/pkg/phase" "github.com/Mirantis/mcc/pkg/product/mke/api" log "github.com/sirupsen/logrus" @@ -27,10 +28,23 @@ func (p *UninstallMCR) Run() error { return nil } -func (p *UninstallMCR) uninstallMCR(h *api.Host, c *api.ClusterConfig) error { +func (p *UninstallMCR) uninstallMCR(h *api.Host, config *api.ClusterConfig) error { log.Infof("%s: uninstalling container runtime", h) - if err := h.Configurer.UninstallMCR(h, h.Metadata.MCRInstallScript, c.Spec.MCR); err != nil { + leader := config.Spec.SwarmLeader() + + if err := mcr.DrainNode(leader, h); err != nil { + return fmt.Errorf("%s: drain node: %w", h, err) + } + + uVolumeCmd := h.Configurer.DockerCommandf("volume prune -f") + log.Infof("%s: unmounted dangling volumes", h) + + if err := h.Exec(uVolumeCmd); err != nil { + return fmt.Errorf("%s: failed to unmount dangling volumes: %w", h, err) + } + + if err := h.Configurer.UninstallMCR(h, h.Metadata.MCRInstallScript, config.Spec.MCR); err != nil { return fmt.Errorf("%s: uninstall container runtime: %w", h, err) }