|
1 | 1 | // Package defaults holds default values for the helmvm binary. For sake of |
2 | | -// keeping everything simple this packages panics if some error occurs as |
| 2 | +// keeping everything simple this packages exits(1) if some error occurs as |
3 | 3 | // these should not happen in the first place. |
4 | 4 | package defaults |
5 | 5 |
|
6 | | -import ( |
7 | | - "fmt" |
8 | | - "net" |
9 | | - "os" |
10 | | - "path/filepath" |
11 | | - "runtime" |
12 | | - "strings" |
13 | | - |
14 | | - "github.com/gosimple/slug" |
| 6 | +var ( |
| 7 | + // K0sVersion holds the version of k0s binary we are embedding. this is |
| 8 | + // set at compile time via ldflags. |
| 9 | + K0sVersion = "0.0.0" |
| 10 | + // provider holds a global reference to the default provider. |
| 11 | + provider *DefaultsProvider |
15 | 12 | ) |
16 | 13 |
|
17 | | -// K0sVersion holds the version of k0s binary we are embedding. this is set |
18 | | -// at compile time via ldflags. |
19 | | -var K0sVersion = "0.0.0" |
20 | | - |
21 | | -func init() { |
22 | | - if err := os.MkdirAll(K0sctlBinsSubDir(), 0755); err != nil { |
23 | | - panic(fmt.Errorf("unable to create basedir: %w", err)) |
24 | | - } |
25 | | - if err := os.MkdirAll(ConfigSubDir(), 0755); err != nil { |
26 | | - panic(fmt.Errorf("unable to create config dir: %w", err)) |
27 | | - } |
28 | | - if err := os.MkdirAll(HelmVMBinsSubDir(), 0755); err != nil { |
29 | | - panic(fmt.Errorf("unable to create helmvm bin dir: %w", err)) |
30 | | - } |
31 | | - if err := os.MkdirAll(HelmVMLogsSubDir(), 0755); err != nil { |
32 | | - panic(fmt.Errorf("unable to create helmvm logs dir: %w", err)) |
| 14 | +// def returns a global reference to the default provider. creates one if not |
| 15 | +// already created. |
| 16 | +func def() *DefaultsProvider { |
| 17 | + if provider == nil { |
| 18 | + provider = NewProvider("") |
33 | 19 | } |
| 20 | + return provider |
34 | 21 | } |
35 | 22 |
|
36 | | -const ( |
37 | | - k0sBinsSubDirDarwin = "Library/Caches/k0sctl/k0s/linux/amd64" |
38 | | - k0sBinsSubDirLinux = ".cache/k0sctl/k0s/linux/amd64" |
39 | | -) |
40 | | - |
41 | | -// BinaryName returns the binary name, this is useful for places where we |
42 | | -// need to present the name of the binary to the user (the name may vary if |
43 | | -// the binary is renamed). We make sure the name does not contain invalid |
44 | | -// characters for a filename. |
| 23 | +// BinaryName calls BinaryName on the default provider. |
45 | 24 | func BinaryName() string { |
46 | | - exe, err := os.Executable() |
47 | | - if err != nil { |
48 | | - panic(err) |
49 | | - } |
50 | | - base := filepath.Base(exe) |
51 | | - return slug.Make(base) |
| 25 | + return def().BinaryName() |
52 | 26 | } |
53 | 27 |
|
54 | | -// K0sctlBinsSubDir returns the path to the directory where k0sctl binaries |
55 | | -// are stored. This is a subdirectory of the user's home directory. Follows |
56 | | -// the k0sctl directory convention. |
| 28 | +// K0sctlBinsSubDir calls K0sctlBinsSubDir on the default provider. |
57 | 29 | func K0sctlBinsSubDir() string { |
58 | | - home, err := os.UserHomeDir() |
59 | | - if err != nil { |
60 | | - panic(err) |
61 | | - } |
62 | | - if runtime.GOOS == "darwin" { |
63 | | - return filepath.Join(home, k0sBinsSubDirDarwin) |
64 | | - } |
65 | | - return filepath.Join(home, k0sBinsSubDirLinux) |
| 30 | + return def().K0sctlBinsSubDir() |
66 | 31 | } |
67 | 32 |
|
68 | | -// HelmVMBinsSubDir returns the path to the directory where helmvm binaries |
69 | | -// are stored. This is a subdirectory of the user's home directory. |
| 33 | +// HelmVMBinsSubDir calls HelmVMBinsSubDir on the default provider. |
70 | 34 | func HelmVMBinsSubDir() string { |
71 | | - home, err := os.UserHomeDir() |
72 | | - if err != nil { |
73 | | - panic(err) |
74 | | - } |
75 | | - hidden := fmt.Sprintf(".%s", BinaryName()) |
76 | | - return filepath.Join(home, hidden, "bin") |
| 35 | + return def().HelmVMBinsSubDir() |
77 | 36 | } |
78 | 37 |
|
79 | | -// HelmVMLogsSubDir returns the path to the directory where helmvm logs are |
80 | | -// stored. This is a subdirectory of the user's home directory. |
| 38 | +// HelmVMLogsSubDir calls HelmVMLogsSubDir on the default provider. |
81 | 39 | func HelmVMLogsSubDir() string { |
82 | | - home, err := os.UserHomeDir() |
83 | | - if err != nil { |
84 | | - panic(err) |
85 | | - } |
86 | | - hidden := fmt.Sprintf(".%s", BinaryName()) |
87 | | - return filepath.Join(home, hidden, "logs") |
| 40 | + return def().HelmVMLogsSubDir() |
88 | 41 | } |
89 | 42 |
|
90 | | -// K0sctlApplyLogPath returns the path to the k0sctl apply log file. |
| 43 | +// K0sctlApplyLogPath calls K0sctlApplyLogPath on the default provider. |
91 | 44 | func K0sctlApplyLogPath() string { |
92 | | - home, err := os.UserHomeDir() |
93 | | - if err != nil { |
94 | | - panic(err) |
95 | | - } |
96 | | - return filepath.Join(home, ".cache", "k0sctl", "k0sctl.log") |
| 45 | + return def().K0sctlApplyLogPath() |
97 | 46 | } |
98 | 47 |
|
99 | | -// SSHKeyPath returns the path to the SSH managed by helmvm installation. |
| 48 | +// SSHKeyPath calls SSHKeyPath on the default provider. |
100 | 49 | func SSHKeyPath() string { |
101 | | - home, err := os.UserHomeDir() |
102 | | - if err != nil { |
103 | | - panic(err) |
104 | | - } |
105 | | - return filepath.Join(home, ".ssh", "helmvm_rsa") |
| 50 | + return def().SSHKeyPath() |
106 | 51 | } |
107 | 52 |
|
108 | | -// SSHAuthorizedKeysPath returns the path to the authorized_hosts file. |
| 53 | +// SSHAuthorizedKeysPath calls SSHAuthorizedKeysPath on the default provider. |
109 | 54 | func SSHAuthorizedKeysPath() string { |
110 | | - home, err := os.UserHomeDir() |
111 | | - if err != nil { |
112 | | - panic(err) |
113 | | - } |
114 | | - return filepath.Join(home, ".ssh", "authorized_keys") |
| 55 | + return def().SSHAuthorizedKeysPath() |
115 | 56 | } |
116 | 57 |
|
117 | | -// ConfigSubDir returns the path to the directory where k0sctl configuration |
118 | | -// files are stored. This is a subdirectory of the user's home directory. |
| 58 | +// ConfigSubDir calls ConfigSubDir on the default provider. |
119 | 59 | func ConfigSubDir() string { |
120 | | - home, err := os.UserHomeDir() |
121 | | - if err != nil { |
122 | | - panic(err) |
123 | | - } |
124 | | - hidden := fmt.Sprintf(".%s", BinaryName()) |
125 | | - return filepath.Join(home, hidden, "etc") |
| 60 | + return def().ConfigSubDir() |
126 | 61 | } |
127 | 62 |
|
128 | | -// K0sBinaryPath returns the path to the k0s binary. |
| 63 | +// K0sBinaryPath calls K0sBinaryPath on the default provider. |
129 | 64 | func K0sBinaryPath() string { |
130 | | - return PathToK0sctlBinary(fmt.Sprintf("k0s-%s", K0sVersion)) |
| 65 | + return def().K0sBinaryPath() |
131 | 66 | } |
132 | 67 |
|
133 | | -// PathToK0sctlBinary is an utility function that returns the full path to |
134 | | -// a materialized binary that belongs to k0sctl. This function does not check |
135 | | -// if the file exists. |
| 68 | +// PathToK0sctlBinary calls PathToK0sctlBinary on the default provider. |
136 | 69 | func PathToK0sctlBinary(name string) string { |
137 | | - return filepath.Join(K0sctlBinsSubDir(), name) |
| 70 | + return def().PathToK0sctlBinary(name) |
138 | 71 | } |
139 | 72 |
|
140 | | -// PathToHelmVMBinary is an utility function that returns the full path to a |
141 | | -// materialized binary that belongs to helmvm (do not confuse with binaries |
142 | | -// belonging to k0sctl). This function does not check if the file exists. |
| 73 | +// PathToHelmVMBinary calls PathToHelmVMBinary on the default provider. |
143 | 74 | func PathToHelmVMBinary(name string) string { |
144 | | - return filepath.Join(HelmVMBinsSubDir(), name) |
| 75 | + return def().PathToHelmVMBinary(name) |
145 | 76 | } |
146 | 77 |
|
147 | | -// PathToLog returns the full path to a log file. This function does not check |
148 | | -// if the file exists. |
| 78 | +// PathToLog calls PathToLog on the default provider. |
149 | 79 | func PathToLog(name string) string { |
150 | | - return filepath.Join(HelmVMLogsSubDir(), name) |
| 80 | + return def().PathToLog(name) |
151 | 81 | } |
152 | 82 |
|
153 | | -// PathToConfig returns the full path to a configuration file. This function |
154 | | -// does not check if the file exists. |
| 83 | +// PathToConfig calls PathToConfig on the default provider. |
155 | 84 | func PathToConfig(name string) string { |
156 | | - return filepath.Join(ConfigSubDir(), name) |
| 85 | + return def().PathToConfig(name) |
157 | 86 | } |
158 | 87 |
|
159 | | -// FileNameForImage returns an appropriate .tar name for a given image. |
160 | | -// e.g. quay.io/test/test:v1 would return quay.io-test-test-v1.tar. |
| 88 | +// FileNameForImage calls FileNameForImage on the default provider. |
161 | 89 | func FileNameForImage(img string) string { |
162 | | - prefix := strings.ReplaceAll(img, "/", "-") |
163 | | - prefix = strings.ReplaceAll(prefix, ":", "-") |
164 | | - return fmt.Sprintf("%s.tar", prefix) |
| 90 | + return def().FileNameForImage(img) |
165 | 91 | } |
166 | 92 |
|
167 | | -// PreferredNodeIPAddress returns the ip address the node uses when reaching |
168 | | -// the internet. This is useful when the node has multiple interfaces and we |
169 | | -// want to bind to one of the interfaces. |
| 93 | +// PreferredNodeIPAddress calls PreferredNodeIPAddress on the default provider. |
170 | 94 | func PreferredNodeIPAddress() (string, error) { |
171 | | - conn, err := net.Dial("udp", "8.8.8.8:80") |
172 | | - if err != nil { |
173 | | - return "", fmt.Errorf("unable to get local IP: %w", err) |
174 | | - } |
175 | | - defer conn.Close() |
176 | | - addr := conn.LocalAddr().(*net.UDPAddr) |
177 | | - return addr.IP.String(), nil |
| 95 | + return def().PreferredNodeIPAddress() |
178 | 96 | } |
179 | 97 |
|
180 | | -// DecentralizedInstall returns true if the cluster installation has been |
181 | | -// executed in a decentralized way (installing the first node then generating |
182 | | -// a join token and installing the others). |
| 98 | +// DecentralizedInstall calls DecentralizedInstall on the default provider. |
183 | 99 | func DecentralizedInstall() bool { |
184 | | - fpath := PathToConfig(".decentralized") |
185 | | - _, err := os.Stat(fpath) |
186 | | - return err == nil |
| 100 | + return def().DecentralizedInstall() |
187 | 101 | } |
188 | 102 |
|
189 | | -// SetInstallAsDecentralized sets the decentralized install flag inside the |
190 | | -// configuration directory. |
| 103 | +// SetInstallAsDecentralized calls SetInstallAsDecentralized on the default provider. |
191 | 104 | func SetInstallAsDecentralized() error { |
192 | | - fpath := PathToConfig(".decentralized") |
193 | | - fp, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE, 0666) |
194 | | - if err != nil { |
195 | | - return fmt.Errorf("unable to set installation mode: %w", err) |
196 | | - } |
197 | | - defer fp.Close() |
198 | | - return nil |
| 105 | + return def().SetInstallAsDecentralized() |
199 | 106 | } |
0 commit comments