forked from platform9/cluster-api-provider-bringyourownhost
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstaller.go
More file actions
86 lines (71 loc) · 2.77 KB
/
Copy pathinstaller.go
File metadata and controls
86 lines (71 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2022 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package installer
import (
"context"
"fmt"
"strings"
"github.com/vmware-tanzu/cluster-api-provider-bringyourownhost/installer/internal/algo"
)
// K8sInstaller represent k8s installer interface
type K8sInstaller interface {
Install() string
Uninstall() string
}
// Error string wrapper for errors returned by the installer
type Error string
func (e Error) Error() string { return string(e) }
// BundleType is used to support various bundles
type BundleType string
const (
// BundleTypeK8s represents a vanilla k8s bundle
BundleTypeK8s BundleType = "k8s"
)
const (
// ErrDetectOs error type when supported OS could not be detected
ErrDetectOs = Error("Error detecting OS")
// ErrOsK8sNotSupported error type when the OS is not supported by the k8s installer
ErrOsK8sNotSupported = Error("No k8s support for OS")
// ErrBundleDownload error type when the bundle download fails
ErrBundleDownload = Error("Error downloading bundle")
// ErrBundleExtract error type when the bundle extraction fails
ErrBundleExtract = Error("Error extracting bundle")
// ErrBundleInstall error type when the bundle installation fails
ErrBundleInstall = Error("Error installing bundle")
// ErrBundleUninstall error type when the bundle uninstallation fails
ErrBundleUninstall = Error("Error uninstalling bundle")
// ErrInstallerCreation error type when installer creation fails
ErrInstallerCreation = Error("Error creating installer")
)
// archOldNameMap keeps the mapping of architecture new name to old name mapping
var archOldNameMap = map[string]string{
"amd64": "x86-64",
}
// NewInstaller will return a new installer
func NewInstaller(ctx context.Context, osDist, arch, k8sVersion string, downloader *bundleDownloader, skipKernelModuleCleanup bool) (K8sInstaller, error) {
bundleArchName := arch
// replacing the arch name to old name to match with the bundle name
if _, exists := archOldNameMap[arch]; exists {
bundleArchName = archOldNameMap[arch]
}
// normalizing os image name and adding arch
osArch := strings.ReplaceAll(osDist, " ", "_") + "_" + bundleArchName
reg := GetSupportedRegistry()
if len(reg.ListK8s(osArch)) == 0 {
return nil, ErrOsK8sNotSupported
}
osbundle := reg.ResolveOsToOsBundle(osArch)
addrs := downloader.GetBundleAddr(osbundle, k8sVersion)
// Use appropriate installer based on OS version
var installer K8sInstaller
var err error
if strings.Contains(osbundle, "Ubuntu_22.04") {
installer, err = algo.NewUbuntu22_04Installer(ctx, arch, addrs, skipKernelModuleCleanup)
} else {
installer, err = algo.NewUbuntu20_04Installer(ctx, arch, addrs, skipKernelModuleCleanup)
}
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrInstallerCreation, err)
}
return installer, nil
}