Skip to content

Commit

Permalink
Add Support for FlatcarContainerLinux (#221)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
bephinix and kke committed Oct 18, 2021
1 parent 13a8468 commit 0106928
Show file tree
Hide file tree
Showing 17 changed files with 129 additions and 19 deletions.
25 changes: 20 additions & 5 deletions configurer/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package configurer

import (
"fmt"
"path"
"regexp"
"strconv"
"strings"
Expand All @@ -10,8 +11,18 @@ import (
"github.com/k0sproject/rig/os"
)

// Static Constants Interface for overriding by distro-specific structs
type PathFuncs interface {
K0sBinaryPath() string
K0sConfigPath() string
K0sJoinTokenPath() string
KubeconfigPath() string
}

// Linux is a base module for various linux OS support packages
type Linux struct{}
type Linux struct {
PathFuncs
}

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

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

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

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

return h.Execf(`install -m 0750 -o root -g adm "%s" "%s"`, tmp, l.PathFuncs.K0sBinaryPath(), exec.Sudo(h))
}

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

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

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

// HTTPStatus makes a HTTP GET request to the url and returns the status code or an error
Expand Down
4 changes: 3 additions & 1 deletion configurer/linux/alpine.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ func init() {
return os.ID == "alpine"
},
func() interface{} {
return Alpine{}
linuxType := &Alpine{}
linuxType.PathFuncs = interface{}(linuxType).(configurer.PathFuncs)
return linuxType
},
)
}
Expand Down
4 changes: 3 additions & 1 deletion configurer/linux/archlinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ func init() {
return os.ID == "arch" || os.IDLike == "arch"
},
func() interface{} {
return &Archlinux{}
linuxType := &Archlinux{}
linuxType.PathFuncs = interface{}(linuxType).(configurer.PathFuncs)
return linuxType
},
)
}
4 changes: 3 additions & 1 deletion configurer/linux/debian.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ func init() {
return os.ID == "debian"
},
func() interface{} {
return &Debian{}
linuxType := &Debian{}
linuxType.PathFuncs = interface{}(linuxType).(configurer.PathFuncs)
return linuxType
},
)
}
4 changes: 3 additions & 1 deletion configurer/linux/enterpriselinux/almalinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ func init() {
return os.ID == "almalinux"
},
func() interface{} {
return AlmaLinux{}
linuxType := &AlmaLinux{}
linuxType.PathFuncs = interface{}(linuxType).(configurer.PathFuncs)
return linuxType
},
)
}
4 changes: 3 additions & 1 deletion configurer/linux/enterpriselinux/amazon.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ func init() {
return os.ID == "amzn"
},
func() interface{} {
return AmazonLinux{}
linuxType := &AmazonLinux{}
linuxType.PathFuncs = interface{}(linuxType).(configurer.PathFuncs)
return linuxType
},
)
}
4 changes: 3 additions & 1 deletion configurer/linux/enterpriselinux/centos.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ func init() {
return os.ID == "centos"
},
func() interface{} {
return CentOS{}
linuxType := &CentOS{}
linuxType.PathFuncs = interface{}(linuxType).(configurer.PathFuncs)
return linuxType
},
)
}
4 changes: 3 additions & 1 deletion configurer/linux/enterpriselinux/fedora.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ func init() {
return os.ID == "fedora"
},
func() interface{} {
return Fedora{}
linuxType := &Fedora{}
linuxType.PathFuncs = interface{}(linuxType).(configurer.PathFuncs)
return linuxType
},
)
}
4 changes: 3 additions & 1 deletion configurer/linux/enterpriselinux/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ func init() {
return os.ID == "ol"
},
func() interface{} {
return OracleLinux{}
linuxType := &OracleLinux{}
linuxType.PathFuncs = interface{}(linuxType).(configurer.PathFuncs)
return linuxType
},
)
}
5 changes: 4 additions & 1 deletion configurer/linux/enterpriselinux/rhel.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package enterpriselinux

import (
"github.com/k0sproject/k0sctl/configurer"
k0slinux "github.com/k0sproject/k0sctl/configurer/linux"
"github.com/k0sproject/rig"
"github.com/k0sproject/rig/os/registry"
Expand All @@ -17,7 +18,9 @@ func init() {
return os.ID == "rhel"
},
func() interface{} {
return RHEL{}
linuxType := &RHEL{}
linuxType.PathFuncs = interface{}(linuxType).(configurer.PathFuncs)
return linuxType
},
)
}
4 changes: 3 additions & 1 deletion configurer/linux/enterpriselinux/rocky.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ func init() {
return os.ID == "rocky"
},
func() interface{} {
return RockyLinux{}
linuxType := &RockyLinux{}
linuxType.PathFuncs = interface{}(linuxType).(configurer.PathFuncs)
return linuxType
},
)
}
36 changes: 36 additions & 0 deletions configurer/linux/flatcar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package linux

import (
"errors"

"github.com/k0sproject/k0sctl/configurer"
"github.com/k0sproject/rig"
"github.com/k0sproject/rig/os"
"github.com/k0sproject/rig/os/registry"
)

type Flatcar struct {
BaseLinux
os.Linux
}

func init() {
registry.RegisterOSModule(
func(os rig.OSVersion) bool {
return os.ID == "flatcar"
},
func() interface{} {
linuxType := &Flatcar{}
linuxType.PathFuncs = interface{}(linuxType).(configurer.PathFuncs)
return linuxType
},
)
}

func (l Flatcar) InstallPackage(h os.Host, pkg ...string) error {
return errors.New("FlatcarContainerLinux does not support installing packages manually")
}

func (l Flatcar) K0sBinaryPath() string {
return "/opt/bin/k0s"
}
26 changes: 26 additions & 0 deletions configurer/linux/linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package linux

import (
"testing"

"github.com/k0sproject/k0sctl/configurer"
"github.com/stretchr/testify/require"
)

// TestPaths tests the slightly weird way to perform function overloading
func TestPaths(t *testing.T) {
fc := &Flatcar{}
fc.PathFuncs = interface{}(fc).(configurer.PathFuncs)

ubuntu := &Ubuntu{}
ubuntu.PathFuncs = interface{}(ubuntu).(configurer.PathFuncs)

require.Equal(t, "/opt/bin/k0s", fc.K0sBinaryPath())
require.Equal(t, "/usr/local/bin/k0s", ubuntu.K0sBinaryPath())

require.Equal(t, "/opt/bin/k0s --help", fc.K0sCmdf("--help"))
require.Equal(t, "/usr/local/bin/k0s --help", ubuntu.K0sCmdf("--help"))

require.Equal(t, "/var/lib/k0s/pki/admin.conf", fc.KubeconfigPath())
require.Equal(t, "/var/lib/k0s/pki/admin.conf", ubuntu.KubeconfigPath())
}
5 changes: 4 additions & 1 deletion configurer/linux/opensuse.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package linux

import (
"github.com/k0sproject/k0sctl/configurer"
"github.com/k0sproject/rig"
"github.com/k0sproject/rig/os/registry"
)
Expand All @@ -16,7 +17,9 @@ func init() {
return os.ID == "opensuse"
},
func() interface{} {
return OpenSUSE{}
linuxType := &OpenSUSE{}
linuxType.PathFuncs = interface{}(linuxType).(configurer.PathFuncs)
return linuxType
},
)
}
5 changes: 4 additions & 1 deletion configurer/linux/slackware.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"

"github.com/k0sproject/k0sctl/configurer"
"github.com/k0sproject/rig"
"github.com/k0sproject/rig/os"
"github.com/k0sproject/rig/os/registry"
Expand All @@ -21,7 +22,9 @@ func init() {
return os.ID == "slackware"
},
func() interface{} {
return Slackware{}
linuxType := &Slackware{}
linuxType.PathFuncs = interface{}(linuxType).(configurer.PathFuncs)
return linuxType
},
)
}
Expand Down
5 changes: 4 additions & 1 deletion configurer/linux/sles.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package linux

import (
"github.com/k0sproject/k0sctl/configurer"
"github.com/k0sproject/rig"
"github.com/k0sproject/rig/os"
"github.com/k0sproject/rig/os/linux"
Expand All @@ -20,7 +21,9 @@ func init() {
return os.ID == "sles"
},
func() interface{} {
return SLES{}
linuxType := &SLES{}
linuxType.PathFuncs = interface{}(linuxType).(configurer.PathFuncs)
return linuxType
},
)
}
5 changes: 4 additions & 1 deletion configurer/linux/ubuntu.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package linux

import (
"github.com/k0sproject/k0sctl/configurer"
"github.com/k0sproject/rig"
"github.com/k0sproject/rig/os/registry"
)
Expand All @@ -16,7 +17,9 @@ func init() {
return os.ID == "ubuntu"
},
func() interface{} {
return &Ubuntu{}
linuxType := &Ubuntu{}
linuxType.PathFuncs = interface{}(linuxType).(configurer.PathFuncs)
return linuxType
},
)
}

0 comments on commit 0106928

Please sign in to comment.