Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions internal/bootstrap/gcp/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ type GCPBootstrapper struct {
NodeClient node.NodeClient
PortalClient portal.Portal
GitHubClient github.GitHubClient

// packageFilename is the Codesphere installer package filename on the jumpbox,
// populated by EnsureCodespherePackage and reused by the k0s and Codesphere install steps.
packageFilename string
}

type CodesphereEnvironment struct {
Expand Down Expand Up @@ -332,6 +336,16 @@ func (b *GCPBootstrapper) Bootstrap() error {
}

if b.Env.InstallVersion != "" || b.Env.InstallLocal != "" {
err = b.stlog.Step("Ensure Codesphere package on jumpbox", b.EnsureCodespherePackage)
if err != nil {
return fmt.Errorf("failed to ensure Codesphere package on jumpbox: %w", err)
}

err = b.stlog.Step("Install k0s", b.InstallK0s)
if err != nil {
return fmt.Errorf("failed to install k0s: %w", err)
}

err = b.stlog.Step("Install Codesphere", b.InstallCodesphere)
if err != nil {
return fmt.Errorf("failed to install Codesphere: %w", err)
Expand Down Expand Up @@ -958,13 +972,30 @@ func (b *GCPBootstrapper) EnsureDNSRecords() error {
return nil
}

func (b *GCPBootstrapper) InstallCodesphere() error {
// EnsureCodespherePackage downloads or copies the Codesphere installer package onto the
// jumpbox and records its filename for the subsequent k0s and Codesphere install steps.
func (b *GCPBootstrapper) EnsureCodespherePackage() error {
fullPackageFilename, err := b.ensureCodespherePackageOnJumpbox()
if err != nil {
return fmt.Errorf("failed to ensure Codesphere package on jumpbox: %w", err)
}
b.packageFilename = fullPackageFilename
return nil
}

err = b.runInstallCommand(fullPackageFilename)
// InstallK0s deploys the k0s cluster from the jumpbox using the Codesphere package's
// bundled k0s binary. The bootstrap now owns cluster creation (instead of the installer's
// internal "kubernetes" step, which is skipped in runInstallCommand), so this must run
// before the Codesphere application install. EnsureCodespherePackage must run first.
func (b *GCPBootstrapper) InstallK0s() error {
b.stlog.Logf("Installing k0s cluster...")
installCmd := fmt.Sprintf("oms install k0s --install-config %s --package %s",
remoteInstallConfigPath, b.packageFilename)
return b.Env.Jumpbox.RunSSHCommand("root", installCmd)
}
Comment on lines +990 to +995

func (b *GCPBootstrapper) InstallCodesphere() error {
err := b.runInstallCommand(b.packageFilename)
if err != nil {
return fmt.Errorf("failed to install Codesphere from jumpbox: %w", err)
}
Expand Down Expand Up @@ -1015,7 +1046,11 @@ func (b *GCPBootstrapper) runInstallCommand(packageFilename string) error {
}

func (b *GCPBootstrapper) generateSkipStepsArg() string {
skipSteps := b.Env.InstallSkipSteps
skipSteps := append([]string{}, b.Env.InstallSkipSteps...)
// The k0s cluster is created by the standalone "Install k0s" bootstrap step, so the
// installer must skip its own "kubernetes" (cluster creation) step. The post-cluster
// component steps (set-up-cluster, codesphere, ms-backends) still run.
skipSteps = append(skipSteps, "kubernetes")
if b.Env.RegistryType == RegistryTypeGitHub {
skipSteps = append(skipSteps, "load-container-images")
}
Expand Down
93 changes: 59 additions & 34 deletions internal/bootstrap/gcp/gcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1273,28 +1273,31 @@ var _ = Describe("GCP Bootstrapper", func() {
})
})

Describe("InstallCodesphere", func() {
Describe("Package, k0s and Codesphere install", func() {
BeforeEach(func() {
csEnv.InstallVersion = "v1.2.3"
csEnv.InstallHash = "abc1234567890"
})
Describe("Valid InstallCodesphere", func() {
Describe("Valid install sequence", func() {
Context("Direct GitHub access", func() {
BeforeEach(func() {
csEnv.GitHubPAT = "fake-pat"
csEnv.RegistryUser = "fake-user"
csEnv.RegistryType = "github"
})
It("downloads and installs lite package", func() {
It("downloads the package, installs k0s and installs the lite package", func() {
// Expect download package
nodeClient.EXPECT().RunCommand(mock.MatchedBy(jumpboxMatcher), "root", "oms download package -f installer-lite.tar.gz -H abc1234567890 v1.2.3").Return(nil)
Expect(bs.EnsureCodespherePackage()).To(Succeed())

// Expect install codesphere
nodeClient.EXPECT().RunCommand(mock.MatchedBy(jumpboxMatcher), "root",
"oms install codesphere -c /etc/codesphere/config.yaml -k /etc/codesphere/secrets/age_key.txt -p v1.2.3-abc1234567890-installer-lite.tar.gz -s load-container-images").Return(nil)
// Expect standalone k0s install before Codesphere
nodeClient.EXPECT().RunCommand(mock.MatchedBy(jumpboxMatcher), "root", "oms install k0s --install-config /etc/codesphere/config.yaml --package v1.2.3-abc1234567890-installer-lite.tar.gz").Return(nil)
Expect(bs.InstallK0s()).To(Succeed())

err := bs.InstallCodesphere()
Expect(err).NotTo(HaveOccurred())
// Expect install codesphere with the kubernetes step skipped
nodeClient.EXPECT().RunCommand(mock.MatchedBy(jumpboxMatcher), "root",
"oms install codesphere -c /etc/codesphere/config.yaml -k /etc/codesphere/secrets/age_key.txt -p v1.2.3-abc1234567890-installer-lite.tar.gz -s kubernetes,load-container-images").Return(nil)
Expect(bs.InstallCodesphere()).To(Succeed())
})
})

Expand All @@ -1303,24 +1306,27 @@ var _ = Describe("GCP Bootstrapper", func() {
// Simulate that ValidateInput has populated the hash
csEnv.InstallHash = "def9876543210"
})
It("downloads and installs codesphere", func() {
// Expect download package
It("downloads the package, installs k0s and installs codesphere", func() {
nodeClient.EXPECT().RunCommand(mock.MatchedBy(jumpboxMatcher), "root", "oms download package -f installer.tar.gz -H def9876543210 v1.2.3").Return(nil)
Expect(bs.EnsureCodespherePackage()).To(Succeed())

// Expect install codesphere
nodeClient.EXPECT().RunCommand(mock.MatchedBy(jumpboxMatcher), "root", "oms install codesphere -c /etc/codesphere/config.yaml -k /etc/codesphere/secrets/age_key.txt -p v1.2.3-def9876543210-installer.tar.gz").Return(nil)
nodeClient.EXPECT().RunCommand(mock.MatchedBy(jumpboxMatcher), "root", "oms install k0s --install-config /etc/codesphere/config.yaml --package v1.2.3-def9876543210-installer.tar.gz").Return(nil)
Expect(bs.InstallK0s()).To(Succeed())

err := bs.InstallCodesphere()
Expect(err).NotTo(HaveOccurred())
nodeClient.EXPECT().RunCommand(mock.MatchedBy(jumpboxMatcher), "root", "oms install codesphere -c /etc/codesphere/config.yaml -k /etc/codesphere/secrets/age_key.txt -p v1.2.3-def9876543210-installer.tar.gz -s kubernetes").Return(nil)
Expect(bs.InstallCodesphere()).To(Succeed())
})
})

It("downloads and installs codesphere with hash", func() {
It("downloads, installs k0s and codesphere with hash", func() {
nodeClient.EXPECT().RunCommand(mock.MatchedBy(jumpboxMatcher), "root", "oms download package -f installer.tar.gz -H abc1234567890 v1.2.3").Return(nil)
nodeClient.EXPECT().RunCommand(mock.MatchedBy(jumpboxMatcher), "root", "oms install codesphere -c /etc/codesphere/config.yaml -k /etc/codesphere/secrets/age_key.txt -p v1.2.3-abc1234567890-installer.tar.gz").Return(nil)
Expect(bs.EnsureCodespherePackage()).To(Succeed())

err := bs.InstallCodesphere()
Expect(err).NotTo(HaveOccurred())
nodeClient.EXPECT().RunCommand(mock.MatchedBy(jumpboxMatcher), "root", "oms install k0s --install-config /etc/codesphere/config.yaml --package v1.2.3-abc1234567890-installer.tar.gz").Return(nil)
Expect(bs.InstallK0s()).To(Succeed())

nodeClient.EXPECT().RunCommand(mock.MatchedBy(jumpboxMatcher), "root", "oms install codesphere -c /etc/codesphere/config.yaml -k /etc/codesphere/secrets/age_key.txt -p v1.2.3-abc1234567890-installer.tar.gz -s kubernetes").Return(nil)
Expect(bs.InstallCodesphere()).To(Succeed())
})

Context("with local package", func() {
Expand All @@ -1333,27 +1339,33 @@ var _ = Describe("GCP Bootstrapper", func() {
BeforeEach(func() {
csEnv.RegistryType = gcp.RegistryTypeGitHub
})
It("installs codesphere from local package", func() {
It("installs k0s and codesphere from local package", func() {
nodeClient.EXPECT().CopyFile(mock.Anything, csEnv.InstallLocal, "/root/local-installer-lite.tar.gz").Return(nil)
nodeClient.EXPECT().RunCommand(mock.MatchedBy(jumpboxMatcher), "root",
"oms install codesphere -c /etc/codesphere/config.yaml -k /etc/codesphere/secrets/age_key.txt -p local-installer-lite.tar.gz -s load-container-images").Return(nil)
Expect(bs.EnsureCodespherePackage()).To(Succeed())

err := bs.InstallCodesphere()
Expect(err).NotTo(HaveOccurred())
nodeClient.EXPECT().RunCommand(mock.MatchedBy(jumpboxMatcher), "root", "oms install k0s --install-config /etc/codesphere/config.yaml --package local-installer-lite.tar.gz").Return(nil)
Expect(bs.InstallK0s()).To(Succeed())

nodeClient.EXPECT().RunCommand(mock.MatchedBy(jumpboxMatcher), "root",
"oms install codesphere -c /etc/codesphere/config.yaml -k /etc/codesphere/secrets/age_key.txt -p local-installer-lite.tar.gz -s kubernetes,load-container-images").Return(nil)
Expect(bs.InstallCodesphere()).To(Succeed())
})
})
Context("using the local registry", func() {
BeforeEach(func() {
csEnv.RegistryType = gcp.RegistryTypeLocalContainer
csEnv.InstallLocal = "fake-installer-lite.tar.gz"
})
It("installs codesphere from local package", func() {
It("installs k0s and codesphere from local package", func() {
nodeClient.EXPECT().CopyFile(mock.Anything, csEnv.InstallLocal, "/root/local-installer.tar.gz").Return(nil)
nodeClient.EXPECT().RunCommand(mock.MatchedBy(jumpboxMatcher), "root",
"oms install codesphere -c /etc/codesphere/config.yaml -k /etc/codesphere/secrets/age_key.txt -p local-installer.tar.gz").Return(nil)
Expect(bs.EnsureCodespherePackage()).To(Succeed())

err := bs.InstallCodesphere()
Expect(err).NotTo(HaveOccurred())
nodeClient.EXPECT().RunCommand(mock.MatchedBy(jumpboxMatcher), "root", "oms install k0s --install-config /etc/codesphere/config.yaml --package local-installer.tar.gz").Return(nil)
Expect(bs.InstallK0s()).To(Succeed())

nodeClient.EXPECT().RunCommand(mock.MatchedBy(jumpboxMatcher), "root",
"oms install codesphere -c /etc/codesphere/config.yaml -k /etc/codesphere/secrets/age_key.txt -p local-installer.tar.gz -s kubernetes").Return(nil)
Expect(bs.InstallCodesphere()).To(Succeed())
})
})
})
Expand All @@ -1365,8 +1377,8 @@ var _ = Describe("GCP Bootstrapper", func() {
// Simulate that ValidateInput has not populated the hash
csEnv.InstallHash = ""
})
It("fails", func() {
err := bs.InstallCodesphere()
It("fails to ensure the package", func() {
err := bs.EnsureCodespherePackage()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("install hash must be set when install version is set"))
})
Expand All @@ -1377,8 +1389,8 @@ var _ = Describe("GCP Bootstrapper", func() {
csEnv.InstallVersion = ""
csEnv.InstallHash = ""
})
It("fails", func() {
err := bs.InstallCodesphere()
It("fails to ensure the package", func() {
err := bs.EnsureCodespherePackage()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("either install version or a local package must be specified"))
})
Expand All @@ -1387,14 +1399,27 @@ var _ = Describe("GCP Bootstrapper", func() {
It("fails when download package fails", func() {
nodeClient.EXPECT().RunCommand(mock.MatchedBy(jumpboxMatcher), "root", "oms download package -f installer.tar.gz -H abc1234567890 v1.2.3").Return(fmt.Errorf("download error"))

err := bs.InstallCodesphere()
err := bs.EnsureCodespherePackage()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("failed to download Codesphere package from jumpbox"))
})

It("fails when k0s install fails", func() {
nodeClient.EXPECT().RunCommand(mock.MatchedBy(jumpboxMatcher), "root", "oms download package -f installer.tar.gz -H abc1234567890 v1.2.3").Return(nil).Once()
Expect(bs.EnsureCodespherePackage()).To(Succeed())

nodeClient.EXPECT().RunCommand(mock.MatchedBy(jumpboxMatcher), "root", "oms install k0s --install-config /etc/codesphere/config.yaml --package v1.2.3-abc1234567890-installer.tar.gz").Return(fmt.Errorf("k0s error")).Once()

err := bs.InstallK0s()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("k0s error"))
})

It("fails when install codesphere fails", func() {
nodeClient.EXPECT().RunCommand(mock.MatchedBy(jumpboxMatcher), "root", "oms download package -f installer.tar.gz -H abc1234567890 v1.2.3").Return(nil).Once()
nodeClient.EXPECT().RunCommand(mock.MatchedBy(jumpboxMatcher), "root", "oms install codesphere -c /etc/codesphere/config.yaml -k /etc/codesphere/secrets/age_key.txt -p v1.2.3-abc1234567890-installer.tar.gz").Return(fmt.Errorf("install error")).Once()
Expect(bs.EnsureCodespherePackage()).To(Succeed())

nodeClient.EXPECT().RunCommand(mock.MatchedBy(jumpboxMatcher), "root", "oms install codesphere -c /etc/codesphere/config.yaml -k /etc/codesphere/secrets/age_key.txt -p v1.2.3-abc1234567890-installer.tar.gz -s kubernetes").Return(fmt.Errorf("install error")).Once()

err := bs.InstallCodesphere()
Expect(err).To(HaveOccurred())
Expand Down
Loading
Loading