From 5558daf3e59bf47947eb5abcdcb1ec3a4f0ac387 Mon Sep 17 00:00:00 2001 From: James Nesbitt Date: Mon, 12 Aug 2024 09:14:04 +0300 Subject: [PATCH] PRODENG-2717 More MKE/MSR config checks before running functionality (#498) * PRODENG-2717 docker authentication only with MKE - docker authentication relies on MKE configuration, so it can only happen if MKE is going to be installed. - more checking of config before running MKE/MSR functionality - some renaming MSR->MSR2 of steps, variables Signed-off-by: James Nesbitt --- pkg/mke/mke.go | 5 +++++ pkg/product/mke/describe.go | 2 +- pkg/product/mke/phase/authenticate_docker.go | 6 +++++- pkg/product/mke/phase/describe.go | 18 +++++++++++++----- pkg/product/mke/phase/info.go | 5 +++++ pkg/product/mke/phase/install_msr2.go | 3 +++ pkg/product/mke/phase/join_msr2_replicas.go | 5 +++++ pkg/product/mke/phase/label_nodes.go | 5 +++++ pkg/product/mke/phase/uninstall_msr2.go | 13 +++++++++---- pkg/product/mke/phase/upgrade_msr2.go | 3 +++ pkg/product/mke/reset.go | 2 +- 11 files changed, 55 insertions(+), 12 deletions(-) diff --git a/pkg/mke/mke.go b/pkg/mke/mke.go index 92136de6..8d1e3c04 100644 --- a/pkg/mke/mke.go +++ b/pkg/mke/mke.go @@ -253,10 +253,15 @@ func KubeAndHelmFromConfig(config *api.ClusterConfig) (*kubeclient.KubeClient, * var ( errNoManagersInConfig = errors.New("no managers found in config") errInvalidConfig = errors.New("invalid config") + errNoMKE = errors.New("no MKE installation") ) // DownloadBundle downloads the client bundle from MKE to local storage. func DownloadBundle(config *api.ClusterConfig) error { + if config.Spec.MKE == nil { + return errNoMKE + } + if len(config.Spec.Managers()) == 0 { return errNoManagersInConfig } diff --git a/pkg/product/mke/describe.go b/pkg/product/mke/describe.go index f9ed30c4..f252ae70 100644 --- a/pkg/product/mke/describe.go +++ b/pkg/product/mke/describe.go @@ -39,7 +39,7 @@ func (p *MKE) Describe(reportName string) error { &de.DetectOS{}, &de.GatherFacts{}, &common.Disconnect{}, - &de.Describe{MKE: mke, MSR: msr}, + &de.Describe{MKE: mke, MSR2: msr}, ) if err := phaseManager.Run(); err != nil { diff --git a/pkg/product/mke/phase/authenticate_docker.go b/pkg/product/mke/phase/authenticate_docker.go index 6ee9bbb6..06b470a5 100644 --- a/pkg/product/mke/phase/authenticate_docker.go +++ b/pkg/product/mke/phase/authenticate_docker.go @@ -14,8 +14,12 @@ type AuthenticateDocker struct { phase.BasicPhase } -// ShouldRun is true when registry credentials are set. +// ShouldRun is true when registry credentials are set and MKE is included. func (p *AuthenticateDocker) ShouldRun() bool { + if p.Config.Spec.MKE == nil { + return false + } + return os.Getenv("REGISTRY_USERNAME") != "" && os.Getenv("REGISTRY_PASSWORD") != "" } diff --git a/pkg/product/mke/phase/describe.go b/pkg/product/mke/phase/describe.go index a121f2db..802a0c28 100644 --- a/pkg/product/mke/phase/describe.go +++ b/pkg/product/mke/phase/describe.go @@ -14,8 +14,8 @@ import ( type Describe struct { phase.BasicPhase - MKE bool - MSR bool + MKE bool + MSR2 bool } // Title for the phase. @@ -28,8 +28,8 @@ func (p *Describe) Run() error { switch { case p.MKE: p.mkeReport() - case p.MSR: - p.msrReport() + case p.MSR2: + p.msr2Reports() default: p.hostReport() } @@ -38,6 +38,10 @@ func (p *Describe) Run() error { } func (p *Describe) mkeReport() { + if p.Config.Spec.MKE == nil { + fmt.Println("No MKE installation") + return + } if !p.Config.Spec.MKE.Metadata.Installed { fmt.Println("Not installed") return @@ -61,7 +65,11 @@ func (p *Describe) mkeReport() { tabWriter.Flush() } -func (p *Describe) msrReport() { +func (p *Describe) msr2Reports() { + if p.Config.Spec.MSR2 == nil { + fmt.Println("No MSR2 installation") + return + } // Configure the columns to start. tabWriter := new(tabwriter.Writer) // minwidth, tabwidth, padding, padchar, flags diff --git a/pkg/product/mke/phase/info.go b/pkg/product/mke/phase/info.go index f13c46cf..f1f886ee 100644 --- a/pkg/product/mke/phase/info.go +++ b/pkg/product/mke/phase/info.go @@ -16,6 +16,11 @@ func (p *Info) Title() string { return "Cluster info" } +// ShouldRun is true when MKE is included. +func (p *Info) ShouldRun() bool { + return p.Config.Spec.MKE != nil +} + // Run ... func (p *Info) Run() error { log.Info("Cluster is now configured.") diff --git a/pkg/product/mke/phase/install_msr2.go b/pkg/product/mke/phase/install_msr2.go index 98ce8bd9..e1237cea 100644 --- a/pkg/product/mke/phase/install_msr2.go +++ b/pkg/product/mke/phase/install_msr2.go @@ -30,6 +30,9 @@ func (p *InstallMSR2) Title() string { // ShouldRun should return true only when there is an installation to be // performed. func (p *InstallMSR2) ShouldRun() bool { + if !p.Config.Spec.ContainsMSR2() { + return false + } p.leader = p.Config.Spec.MSR2Leader() return p.Config.Spec.ContainsMSR2() && (p.leader.MSR2Metadata == nil || !p.leader.MSR2Metadata.Installed) } diff --git a/pkg/product/mke/phase/join_msr2_replicas.go b/pkg/product/mke/phase/join_msr2_replicas.go index b1303be4..789f1e93 100644 --- a/pkg/product/mke/phase/join_msr2_replicas.go +++ b/pkg/product/mke/phase/join_msr2_replicas.go @@ -47,6 +47,11 @@ func (p *JoinMSR2Replicas) Title() string { return "Join MSR2 Replicas" } +// ShouldRun should return true only when there is a configured installation. +func (p *JoinMSR2Replicas) ShouldRun() bool { + return p.Config.Spec.ContainsMSR2() +} + // Run joins all the workers nodes to swarm if not already part of it. func (p *JoinMSR2Replicas) Run() error { msrLeader := p.Config.Spec.MSR2Leader() diff --git a/pkg/product/mke/phase/label_nodes.go b/pkg/product/mke/phase/label_nodes.go index f920420b..b8f0fe2d 100644 --- a/pkg/product/mke/phase/label_nodes.go +++ b/pkg/product/mke/phase/label_nodes.go @@ -23,6 +23,11 @@ func (p *LabelNodes) Title() string { return "Label nodes" } +// ShouldRun is true when MKE is included. +func (p *LabelNodes) ShouldRun() bool { + return p.Config.Spec.MKE != nil +} + // Run labels all nodes with launchpad label. func (p *LabelNodes) Run() error { swarmLeader := p.Config.Spec.SwarmLeader() diff --git a/pkg/product/mke/phase/uninstall_msr2.go b/pkg/product/mke/phase/uninstall_msr2.go index 88bf881c..7a9ff7e7 100644 --- a/pkg/product/mke/phase/uninstall_msr2.go +++ b/pkg/product/mke/phase/uninstall_msr2.go @@ -9,19 +9,24 @@ import ( log "github.com/sirupsen/logrus" ) -// UninstallMSR is the phase implementation for running MSR uninstall. -type UninstallMSR struct { +// UninstallMSR2 is the phase implementation for running MSR uninstall. +type UninstallMSR2 struct { phase.Analytics phase.BasicPhase } // Title prints the phase title. -func (p *UninstallMSR) Title() string { +func (p *UninstallMSR2) Title() string { return "Uninstall MSR2 components" } +// ShouldRun should return true only when there is a configured installation. +func (p *UninstallMSR2) ShouldRun() bool { + return p.Config.Spec.ContainsMSR2() +} + // Run an uninstall via msr.Cleanup. -func (p *UninstallMSR) Run() error { +func (p *UninstallMSR2) Run() error { swarmLeader := p.Config.Spec.SwarmLeader() msrLeader := p.Config.Spec.MSR2Leader() if msrLeader == nil || !msrLeader.MSR2Metadata.Installed { diff --git a/pkg/product/mke/phase/upgrade_msr2.go b/pkg/product/mke/phase/upgrade_msr2.go index b55fe849..e14ef08b 100644 --- a/pkg/product/mke/phase/upgrade_msr2.go +++ b/pkg/product/mke/phase/upgrade_msr2.go @@ -35,6 +35,9 @@ func (p *UpgradeMSR2) Title() string { // ShouldRun should return true only when there is an upgrade to be performed. func (p *UpgradeMSR2) ShouldRun() bool { + if !p.Config.Spec.ContainsMSR2() { + return false + } h := p.Config.Spec.MSR2Leader() return p.Config.Spec.ContainsMSR() && h.MSR2Metadata != nil && h.MSR2Metadata.InstalledVersion != p.Config.Spec.MSR2.Version } diff --git a/pkg/product/mke/reset.go b/pkg/product/mke/reset.go index 711763bf..66d3b3ce 100644 --- a/pkg/product/mke/reset.go +++ b/pkg/product/mke/reset.go @@ -22,7 +22,7 @@ func (p *MKE) Reset() error { // begin MSR phases if p.ClusterConfig.Spec.ContainsMSR2() { - phaseManager.AddPhase(&mke.UninstallMSR{}) + phaseManager.AddPhase(&mke.UninstallMSR2{}) } else if p.ClusterConfig.Spec.ContainsMSR3() { phaseManager.AddPhase(&mke.UninstallMSR3{}) }