Skip to content

Commit

Permalink
Add tag support to installer (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
ratanasovvmw authored Nov 2, 2021
1 parent 4386112 commit 4aec5ab
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 27 deletions.
20 changes: 13 additions & 7 deletions agent/installer/bundle_downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,29 @@ type bundleDownloader struct {
// If a cache for the bundle exists, nothing is downloaded.
func (bd *bundleDownloader) Download(
normalizedOsVersion,
k8sVersion string) error {
k8sVersion string,
tag string) error {

return bd.DownloadFromRepo(
normalizedOsVersion,
k8sVersion,
tag,
bd.downloadByImgpkg)
}

// DownloadFromRepo downloads the required bundle with the given method.
func (bd *bundleDownloader) DownloadFromRepo(
normalizedOsVersion,
k8sVersion string,
tag string,
downloadByTool func(string, string) error) error {

err := ensureDirExist(bd.downloadPath)
if err != nil {
return err
}

bundleDirPath := bd.GetBundleDirPath(k8sVersion)
bundleDirPath := bd.GetBundleDirPath(k8sVersion, tag)

// cache hit
if checkDirExist(bundleDirPath) {
Expand All @@ -69,7 +72,7 @@ func (bd *bundleDownloader) DownloadFromRepo(
return err
}

bundleAddr := bd.getBundleAddr(normalizedOsVersion, k8sVersion)
bundleAddr := bd.getBundleAddr(normalizedOsVersion, k8sVersion, tag)
err = convertError(downloadByTool(bundleAddr, dir))
if err != nil {
return err
Expand Down Expand Up @@ -113,8 +116,11 @@ func convertError(err error) error {
}

// GetBundleDirPath returns the path to directory containing the required bundle.
func (bd *bundleDownloader) GetBundleDirPath(k8sVersion string) string {
return filepath.Join(bd.downloadPath, k8sVersion)
func (bd *bundleDownloader) GetBundleDirPath(k8sVersion, tag string) string {
// Not storing tag as a subdir of k8s because we can't atomically move
// the temp bundle dir to a non-existing dir.
// Using "-" instead of ":" because Windows doesn't like the latter
return fmt.Sprintf("%s-%s", filepath.Join(bd.downloadPath, k8sVersion), tag)
}

// GetBundleName returns the name of the bundle in normalized format.
Expand All @@ -123,8 +129,8 @@ func GetBundleName(normalizedOsVersion, k8sVersion string) string {
}

// getBundleAddr returns the exact address to the bundle in the repo.
func (bd *bundleDownloader) getBundleAddr(normalizedOsVersion, k8sVersion string) string {
return fmt.Sprintf("%s/%s", bd.repoAddr, GetBundleName(normalizedOsVersion, k8sVersion))
func (bd *bundleDownloader) getBundleAddr(normalizedOsVersion, k8sVersion, tag string) string {
return fmt.Sprintf("%s/%s:%s", bd.repoAddr, GetBundleName(normalizedOsVersion, k8sVersion), tag)
}

// checkDirExist checks if a dirrectory exists.
Expand Down
10 changes: 9 additions & 1 deletion agent/installer/bundle_downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ var _ = Describe("Byohost Installer Tests", func() {
k8sVersion string
)

const testTag = "test-tag"

BeforeEach(func() {
normalizedOsVersion = "Ubuntu_20.04.3_x64"
k8sVersion = "1.22"
Expand All @@ -54,19 +56,20 @@ var _ = Describe("Byohost Installer Tests", func() {
}
})
Context("When given correct arguments", func() {

It("Should download bundle", func() {
// Test download on cache missing
err := bd.DownloadFromRepo(
normalizedOsVersion,
k8sVersion,
testTag,
mi.Get)
Expect(err).ShouldNot((HaveOccurred()))

// Test no download on cache hit
err = bd.DownloadFromRepo(
normalizedOsVersion,
k8sVersion,
testTag,
mi.Get)
Expect(err).ShouldNot((HaveOccurred()))
Expect(mi.callCount).Should(Equal(1))
Expand All @@ -76,6 +79,7 @@ var _ = Describe("Byohost Installer Tests", func() {
err := bd.DownloadFromRepo(
normalizedOsVersion,
k8sVersion,
testTag,
mi.Get)
Expect(err).ShouldNot((HaveOccurred()))
})
Expand All @@ -86,6 +90,7 @@ var _ = Describe("Byohost Installer Tests", func() {
err := bd.DownloadFromRepo(
normalizedOsVersion,
k8sVersion,
testTag,
mi.Get)
Expect(err).Should((HaveOccurred()))
Expect(err.Error()).Should(Equal(ErrBundleDownload.Error()))
Expand All @@ -95,6 +100,7 @@ var _ = Describe("Byohost Installer Tests", func() {
err := bd.DownloadFromRepo(
normalizedOsVersion,
k8sVersion,
testTag,
mi.Get)
Expect(err).Should((HaveOccurred()))
Expect(err.Error()).Should(Equal(ErrBundleDownload.Error()))
Expand All @@ -104,6 +110,7 @@ var _ = Describe("Byohost Installer Tests", func() {
err := bd.DownloadFromRepo(
normalizedOsVersion,
k8sVersion,
testTag,
mi.Get)
Expect(err).Should((HaveOccurred()))
Expect(err.Error()).Should(Equal(ErrBundleDownload.Error()))
Expand All @@ -113,6 +120,7 @@ var _ = Describe("Byohost Installer Tests", func() {
err := bd.DownloadFromRepo(
normalizedOsVersion,
k8sVersion,
testTag,
mi.Get)
Expect(err).Should((HaveOccurred()))
Expect(err.Error()).Should(Equal(ErrBundleExtract.Error()))
Expand Down
5 changes: 3 additions & 2 deletions agent/installer/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var (
cachePathFlag = flag.String("cache-path", ".", "Path to the local bundle cache")
k8sFlag = flag.String("k8s", "1.22.1", "Kubernetes version")
osFlag = flag.String("os", "", "OS. If used with install/uninstall, override os detection")
tagFlag = flag.String("tag", "", "BYOH Bundle tag")
previewOSChangesFlag = flag.Bool("preview-os-changes", false, "Preview the install and uninstall changes for the specified OS")
)

Expand Down Expand Up @@ -116,9 +117,9 @@ func runInstaller(install bool) {
}

if install {
err = i.Install(*k8sFlag)
err = i.Install(*k8sFlag, *tagFlag)
} else {
err = i.Uninstall(*k8sFlag)
err = i.Uninstall(*k8sFlag, *tagFlag)
}
if err != nil {
klogger.Error(err, "error installing/uninstalling")
Expand Down
30 changes: 17 additions & 13 deletions agent/installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,29 @@ func getSupportedRegistry(bd *bundleDownloader, ob algo.OutputBuilder) registry
for _, t := range supportedOsK8s {
a := &algo.BaseK8sInstaller{
K8sStepProvider: t.algo,
BundlePath: bd.getBundlePathDirOrPreview(t.k8s), /* empty means preview mode */
OutputBuilder: ob}
/*BundlePath: will be set when tag is known */
OutputBuilder: ob}
reg.Add(t.os, t.k8s, a)
}

return reg
}

func (bd *bundleDownloader) getBundlePathDirOrPreview(k8s string) string {
func (bd *bundleDownloader) getBundlePathDirOrPreview(k8s, tag string) string {
if bd == nil || bd.downloadPath == "" {
return ""
}

return bd.GetBundleDirPath(k8s)
return bd.GetBundleDirPath(k8s, tag)
}

func (bd *bundleDownloader) DownloadOrPreview(os, k8s string) error {
func (bd *bundleDownloader) DownloadOrPreview(os, k8s, tag string) error {
if bd == nil || bd.downloadPath == "" {
bd.logger.Info("Running in preview mode, skip bundle download")
return nil
}

return bd.Download(os, k8s)
return bd.Download(os, k8s, tag)
}

// New returns an installer that downloads bundles for the current OS from OCI repository with
Expand Down Expand Up @@ -114,8 +114,8 @@ func newUnchecked(currentOs, bundleRepo, downloadPath string, logger logr.Logger
}

// Install installs the specified k8s version on the current OS
func (i *installer) Install(k8sVer string) error {
algoInst, err := i.getAlgoInstallerWithBundle(k8sVer)
func (i *installer) Install(k8sVer, tag string) error {
algoInst, err := i.getAlgoInstallerWithBundle(k8sVer, tag)
if err != nil {
return err
}
Expand All @@ -128,8 +128,8 @@ func (i *installer) Install(k8sVer string) error {
}

// Uninstal uninstalls the specified k8s version on the current OS
func (i *installer) Uninstall(k8sVer string) error {
algoInst, err := i.getAlgoInstallerWithBundle(k8sVer)
func (i *installer) Uninstall(k8sVer, tag string) error {
algoInst, err := i.getAlgoInstallerWithBundle(k8sVer, tag)
if err != nil {
return err
}
Expand All @@ -142,20 +142,24 @@ func (i *installer) Uninstall(k8sVer string) error {
}

// getAlgoInstallerWithBundle returns an algo.Installer instance and downloads its bundle
func (i *installer) getAlgoInstallerWithBundle(k8sVer string) (osk8sInstaller, error) {
func (i *installer) getAlgoInstallerWithBundle(k8sVer, tag string) (osk8sInstaller, error) {
// This OS supports at least 1 k8s version. See New.

algoInst := i.algoRegistry.GetInstaller(i.detectedOs, k8sVer)
if algoInst == nil {
return nil, ErrOsK8sNotSupported
}
// copy installer from registry and set BundlePath including tag
// empty means preview mode
algoInstCopy := *algoInst.(*algo.BaseK8sInstaller)
algoInstCopy.BundlePath = i.bundleDownloader.getBundlePathDirOrPreview(k8sVer, tag)

bdErr := i.bundleDownloader.DownloadOrPreview(i.detectedOs, k8sVer)
bdErr := i.bundleDownloader.DownloadOrPreview(i.detectedOs, k8sVer, tag)
if bdErr != nil {
return nil, bdErr
}

return algoInst, nil
return &algoInstCopy, nil
}

// ListSupportedOS() returns the list of all supported OS-es. Can be invoked on a non-supported OS.
Expand Down
10 changes: 6 additions & 4 deletions agent/installer/installer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
)

var _ = Describe("Byohost Installer Tests", func() {
const testTag = "test-tag"

Context("When installer is created for unsupported OS", func() {
It("Should return error", func() {
_, err := New("repo", "downloadPath", logr.Discard())
Expand All @@ -34,10 +36,10 @@ var _ = Describe("Byohost Installer Tests", func() {
for _, os := range ListSupportedOS() {
i := NewPreviewInstaller(os, nil)

err := i.Install("unsupported-k8s")
err := i.Install("unsupported-k8s", testTag)
Expect(err).Should((HaveOccurred()))

err = i.Uninstall("unsupported-k8s")
err = i.Uninstall("unsupported-k8s", testTag)
Expect(err).Should((HaveOccurred()))
}
})
Expand All @@ -49,15 +51,15 @@ var _ = Describe("Byohost Installer Tests", func() {
{
ob := algo.OutputBuilderCounter{}
i := NewPreviewInstaller(os, &ob)
err := i.Install(k8s)
err := i.Install(k8s, testTag)
Expect(err).ShouldNot((HaveOccurred()))
Expect(ob.LogCalledCnt).Should(Equal(24))
}

{
ob := algo.OutputBuilderCounter{}
i := NewPreviewInstaller(os, &ob)
err := i.Uninstall(k8s)
err := i.Uninstall(k8s, testTag)
Expect(err).ShouldNot((HaveOccurred()))
Expect(ob.LogCalledCnt).Should(Equal(24))
}
Expand Down

0 comments on commit 4aec5ab

Please sign in to comment.