Skip to content

Commit 0106928

Browse files
bephinixkke
andauthored
Add Support for FlatcarContainerLinux (#221)
* Provide string constant functions through dedicated interface Using a dedicated interface enables distro-specific structs to 'override' functions returning string constants such as config folders or paths. * Create path for k0s binary if it does not already exists * Add support for FlatcarContainerLinux * Rename struct holding path functions for linux * Add test for linux path functions Co-authored-by: Kimmo Lehto <[email protected]>
1 parent 13a8468 commit 0106928

File tree

17 files changed

+129
-19
lines changed

17 files changed

+129
-19
lines changed

configurer/linux.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package configurer
22

33
import (
44
"fmt"
5+
"path"
56
"regexp"
67
"strconv"
78
"strings"
@@ -10,8 +11,18 @@ import (
1011
"github.com/k0sproject/rig/os"
1112
)
1213

14+
// Static Constants Interface for overriding by distro-specific structs
15+
type PathFuncs interface {
16+
K0sBinaryPath() string
17+
K0sConfigPath() string
18+
K0sJoinTokenPath() string
19+
KubeconfigPath() string
20+
}
21+
1322
// Linux is a base module for various linux OS support packages
14-
type Linux struct{}
23+
type Linux struct {
24+
PathFuncs
25+
}
1526

1627
// NOTE The Linux struct does not embed rig/os.Linux because it will confuse
1728
// go as the distro-configurers' parents embed it too. This means you can't
@@ -46,7 +57,7 @@ func (l Linux) Chmod(h os.Host, path, chmod string) error {
4657

4758
// K0sCmdf can be used to construct k0s commands in sprintf style.
4859
func (l Linux) K0sCmdf(template string, args ...interface{}) string {
49-
return fmt.Sprintf("%s %s", l.K0sBinaryPath(), fmt.Sprintf(template, args...))
60+
return fmt.Sprintf("%s %s", l.PathFuncs.K0sBinaryPath(), fmt.Sprintf(template, args...))
5061
}
5162

5263
// K0sBinaryPath returns the location of k0s binary
@@ -87,12 +98,16 @@ func (l Linux) DownloadK0s(h os.Host, version, arch string) error {
8798
return err
8899
}
89100

90-
return h.Execf(`install -m 0750 -o root -g adm "%s" "%s"`, tmp, l.K0sBinaryPath(), exec.Sudo(h))
101+
if err := h.Execf(`install -m 0755 -o root -g root -d "%s"`, path.Dir(l.PathFuncs.K0sBinaryPath()), exec.Sudo(h)); err != nil {
102+
return err
103+
}
104+
105+
return h.Execf(`install -m 0750 -o root -g adm "%s" "%s"`, tmp, l.PathFuncs.K0sBinaryPath(), exec.Sudo(h))
91106
}
92107

93108
// ReplaceK0sTokenPath replaces the config path in the service stub
94109
func (l Linux) ReplaceK0sTokenPath(h os.Host, spath string) error {
95-
return h.Exec(fmt.Sprintf("sed -i 's^REPLACEME^%s^g' %s", l.K0sJoinTokenPath(), spath))
110+
return h.Exec(fmt.Sprintf("sed -i 's^REPLACEME^%s^g' %s", l.PathFuncs.K0sJoinTokenPath(), spath))
96111
}
97112

98113
// FileContains returns true if a file contains the substring
@@ -117,7 +132,7 @@ func (l Linux) KubeconfigPath() string {
117132

118133
// KubectlCmdf returns a command line in sprintf manner for running kubectl on the host using the kubeconfig from KubeconfigPath
119134
func (l Linux) KubectlCmdf(s string, args ...interface{}) string {
120-
return l.K0sCmdf(`kubectl --kubeconfig "%s" %s`, l.KubeconfigPath(), fmt.Sprintf(s, args...))
135+
return l.K0sCmdf(`kubectl --kubeconfig "%s" %s`, l.PathFuncs.KubeconfigPath(), fmt.Sprintf(s, args...))
121136
}
122137

123138
// HTTPStatus makes a HTTP GET request to the url and returns the status code or an error

configurer/linux/alpine.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ func init() {
2727
return os.ID == "alpine"
2828
},
2929
func() interface{} {
30-
return Alpine{}
30+
linuxType := &Alpine{}
31+
linuxType.PathFuncs = interface{}(linuxType).(configurer.PathFuncs)
32+
return linuxType
3133
},
3234
)
3335
}

configurer/linux/archlinux.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ func init() {
1919
return os.ID == "arch" || os.IDLike == "arch"
2020
},
2121
func() interface{} {
22-
return &Archlinux{}
22+
linuxType := &Archlinux{}
23+
linuxType.PathFuncs = interface{}(linuxType).(configurer.PathFuncs)
24+
return linuxType
2325
},
2426
)
2527
}

configurer/linux/debian.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ func init() {
1919
return os.ID == "debian"
2020
},
2121
func() interface{} {
22-
return &Debian{}
22+
linuxType := &Debian{}
23+
linuxType.PathFuncs = interface{}(linuxType).(configurer.PathFuncs)
24+
return linuxType
2325
},
2426
)
2527
}

configurer/linux/enterpriselinux/almalinux.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ func init() {
1919
return os.ID == "almalinux"
2020
},
2121
func() interface{} {
22-
return AlmaLinux{}
22+
linuxType := &AlmaLinux{}
23+
linuxType.PathFuncs = interface{}(linuxType).(configurer.PathFuncs)
24+
return linuxType
2325
},
2426
)
2527
}

configurer/linux/enterpriselinux/amazon.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ func init() {
2727
return os.ID == "amzn"
2828
},
2929
func() interface{} {
30-
return AmazonLinux{}
30+
linuxType := &AmazonLinux{}
31+
linuxType.PathFuncs = interface{}(linuxType).(configurer.PathFuncs)
32+
return linuxType
3133
},
3234
)
3335
}

configurer/linux/enterpriselinux/centos.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ func init() {
1919
return os.ID == "centos"
2020
},
2121
func() interface{} {
22-
return CentOS{}
22+
linuxType := &CentOS{}
23+
linuxType.PathFuncs = interface{}(linuxType).(configurer.PathFuncs)
24+
return linuxType
2325
},
2426
)
2527
}

configurer/linux/enterpriselinux/fedora.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ func init() {
1919
return os.ID == "fedora"
2020
},
2121
func() interface{} {
22-
return Fedora{}
22+
linuxType := &Fedora{}
23+
linuxType.PathFuncs = interface{}(linuxType).(configurer.PathFuncs)
24+
return linuxType
2325
},
2426
)
2527
}

configurer/linux/enterpriselinux/oracle.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ func init() {
1919
return os.ID == "ol"
2020
},
2121
func() interface{} {
22-
return OracleLinux{}
22+
linuxType := &OracleLinux{}
23+
linuxType.PathFuncs = interface{}(linuxType).(configurer.PathFuncs)
24+
return linuxType
2325
},
2426
)
2527
}

configurer/linux/enterpriselinux/rhel.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package enterpriselinux
22

33
import (
4+
"github.com/k0sproject/k0sctl/configurer"
45
k0slinux "github.com/k0sproject/k0sctl/configurer/linux"
56
"github.com/k0sproject/rig"
67
"github.com/k0sproject/rig/os/registry"
@@ -17,7 +18,9 @@ func init() {
1718
return os.ID == "rhel"
1819
},
1920
func() interface{} {
20-
return RHEL{}
21+
linuxType := &RHEL{}
22+
linuxType.PathFuncs = interface{}(linuxType).(configurer.PathFuncs)
23+
return linuxType
2124
},
2225
)
2326
}

0 commit comments

Comments
 (0)