Skip to content

Commit

Permalink
Fix alpine support part 2 (#104)
Browse files Browse the repository at this point in the history
* Fix alpine support (again)

* Lint

* Maybe now

* Install sudo

* Add a test to validate alpine implements the configurer

* More alpine fixes

- Use exteded regexp instead perl regex, since the latter is not
  implemented in busybox grep

- Install kubectl with apk because:
  - It installs slightly faster
  - avoids bashism in checksum check
  - avoids problems with picky busybox sha256sum implementation

- Don't install packages as virtual since it gives unexpected behavior
  (next install will replace the previously installed packages), and we
  don't uninstall the packages, so there is no point with `-t`

Co-authored-by: Natanael Copa <[email protected]>
  • Loading branch information
kke and ncopa authored Mar 11, 2021
1 parent def3973 commit 9535e37
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 24 deletions.
2 changes: 1 addition & 1 deletion configurer/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ const sbinPath = `PATH=/usr/local/sbin:/usr/sbin:/sbin:$PATH`

// PrivateInterface tries to find a private network interface
func (l Linux) PrivateInterface(h os.Host) (string, error) {
output, err := h.ExecOutput(fmt.Sprintf(`%s; (ip route list scope global | grep -P "\b(172|10|192\.168)\.") || (ip route list | grep -m1 default)`, sbinPath))
output, err := h.ExecOutput(fmt.Sprintf(`%s; (ip route list scope global | grep -E "\b(172|10|192\.168)\.") || (ip route list | grep -m1 default)`, sbinPath))
if err == nil {
re := regexp.MustCompile(`\bdev (\w+)`)
match := re.FindSubmatch([]byte(output))
Expand Down
40 changes: 38 additions & 2 deletions configurer/linux/alpine.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,32 @@ import (
"github.com/k0sproject/rig/os/registry"
)

// BaseLinux for tricking go interfaces
type BaseLinux struct {
configurer.Linux
}

var kubectlInstallScript = []string{
`curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"`,
`curl -LO "https://dl.k8s.io/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256"`,
`echo "$(<kubectl.sha256) kubectl" | sha256sum --check`,
`sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl`,
}

// InstallKubectl installs kubectl using the curl method
func (l BaseLinux) InstallKubectl(h os.Host) error {
for _, c := range kubectlInstallScript {
if err := h.Exec(c); err != nil {
return err
}
}
return nil
}

// Alpine provides OS support for Alpine Linux
type Alpine struct {
configurer.Linux
os.Linux
BaseLinux
}

func init() {
Expand All @@ -27,5 +50,18 @@ func init() {

// InstallPackage installs packages via slackpkg
func (l Alpine) InstallPackage(h os.Host, pkg ...string) error {
return h.Execf("sudo apk add -U -t k0sctl %s", strings.Join(pkg, " "))
return h.Execf("sudo apk add --update %s", strings.Join(pkg, " "))
}

// InstallKubectl installs kubectl using the alpine edge/testing repo
func (l Alpine) InstallKubectl(h os.Host) error {
return l.InstallPackage(h, "--repository https://dl-cdn.alpinelinux.org/alpine/edge/testing kubectl")
}

func (l Alpine) Prepare(h os.Host) error {
if !l.CommandExist(h, "sudo") {
return h.Exec("apk add --update sudo")
}

return l.InstallPackage(h, "findutils", "coreutils")
}
12 changes: 12 additions & 0 deletions configurer/linux/alpine_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package linux

import (
"testing"

"github.com/k0sproject/k0sctl/config/cluster"
)

func TestAlpineConfigurerInterface(t *testing.T) {
h := cluster.Host{}
h.Configurer = Alpine{}
}
2 changes: 2 additions & 0 deletions configurer/linux/enterpriselinux.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package linux

import (
"github.com/k0sproject/k0sctl/configurer"
"github.com/k0sproject/rig/os"
"github.com/k0sproject/rig/os/linux"
)

// EnterpriseLinux is a base package for several RHEL-like enterprise linux distributions
type EnterpriseLinux struct {
linux.EnterpriseLinux
configurer.Linux
}

// InstallKubectl installs kubectl using the gcloud kubernetes repo
Expand Down
4 changes: 2 additions & 2 deletions configurer/linux/slackware.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package linux
import (
"strings"

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

// Slackware provides OS support for Slackware Linux
type Slackware struct {
configurer.Linux
BaseLinux
os.Linux
}

func init() {
Expand Down
21 changes: 2 additions & 19 deletions configurer/linux/sles.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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 @@ -11,7 +10,8 @@ import (
// SLES provides OS support for Suse SUSE Linux Enterprise Server
type SLES struct {
linux.SLES
configurer.Linux
os.Linux
BaseLinux
}

func init() {
Expand All @@ -24,20 +24,3 @@ func init() {
},
)
}

var kubectlInstallScript = []string{
`curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"`,
`curl -LO "https://dl.k8s.io/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256"`,
`echo "$(<kubectl.sha256) kubectl" | sha256sum --check`,
`sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl`,
}

// InstallKubectl installs kubectl using the curl method
func (l SLES) InstallKubectl(h os.Host) error {
for _, c := range kubectlInstallScript {
if err := h.Exec(c); err != nil {
return err
}
}
return nil
}
11 changes: 11 additions & 0 deletions phase/prepare_hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"

"github.com/k0sproject/k0sctl/config/cluster"
"github.com/k0sproject/rig/os"
log "github.com/sirupsen/logrus"
)

Expand All @@ -22,7 +23,17 @@ func (p *PrepareHosts) Run() error {
return p.Config.Spec.Hosts.ParallelEach(p.prepareHost)
}

type prepare interface {
Prepare(os.Host) error
}

func (p *PrepareHosts) prepareHost(h *cluster.Host) error {
if c, ok := h.Configurer.(prepare); ok {
if err := c.Prepare(h); err != nil {
return err
}
}

if len(h.Environment) > 0 {
log.Infof("%s: updating environment", h)
if err := h.Configurer.UpdateEnvironment(h, h.Environment); err != nil {
Expand Down

0 comments on commit 9535e37

Please sign in to comment.