Skip to content

Commit

Permalink
Replace kubectl usage with embedded k0s kubectl (#127)
Browse files Browse the repository at this point in the history
* Replace installed kubectl usage with k0s embedded kubectl usage

Signed-off-by: Jussi Nummelin <[email protected]>

* Use sudo with install when creating the upload target dir

Signed-off-by: Jussi Nummelin <[email protected]>

* Add note to bump up rig version

Signed-off-by: Jussi Nummelin <[email protected]>

* I see dead code

Signed-off-by: Jussi Nummelin <[email protected]>

* Move k0s upload/download phases up so we get proper support for embedded kubectl command for upgrade case

Signed-off-by: Jussi Nummelin <[email protected]>

* Bump rig to 0.3.17 to get fixes for file uploads

Signed-off-by: Jussi Nummelin <[email protected]>
  • Loading branch information
jnummelin committed Apr 23, 2021
1 parent 9dc8830 commit 04f6ac4
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 109 deletions.
6 changes: 3 additions & 3 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ var applyCommand = &cli.Command{
&phase.DetectOS{},
&phase.PrepareHosts{},
&phase.GatherFacts{},
&phase.ValidateHosts{},
&phase.GatherK0sFacts{},
&phase.ValidateFacts{},
&phase.DownloadBinaries{},
&phase.UploadBinaries{},
&phase.DownloadK0s{},
&phase.UploadFiles{},
&phase.ValidateHosts{},
&phase.GatherK0sFacts{},
&phase.ValidateFacts{},
&phase.ConfigureK0s{},
&phase.InitializeK0s{},
&phase.InstallControllers{},
Expand Down
1 change: 0 additions & 1 deletion config/cluster/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ type configurer interface {
DeleteFile(os.Host, string) error
CommandExist(os.Host, string) bool
Hostname(os.Host) string
InstallKubectl(os.Host) error
KubectlCmdf(string, ...interface{}) string
KubeconfigPath() string
IsContainer(os.Host) bool
Expand Down
2 changes: 1 addition & 1 deletion configurer/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,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 fmt.Sprintf(`sudo kubectl --kubeconfig "%s" %s`, l.KubeconfigPath(), fmt.Sprintf(s, args...))
return l.K0sCmdf(`kubectl --kubeconfig "%s" %s`, l.KubeconfigPath(), fmt.Sprintf(s, args...))
}

// HTTPStatus makes a HTTP GET request to the url and returns the status code or an error
Expand Down
22 changes: 0 additions & 22 deletions configurer/linux/alpine.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,6 @@ 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 {
os.Linux
Expand All @@ -53,11 +36,6 @@ func (l Alpine) InstallPackage(h os.Host, pkg ...string) error {
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")
Expand Down
20 changes: 0 additions & 20 deletions configurer/linux/debian.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package linux
import (
"github.com/k0sproject/k0sctl/configurer"
"github.com/k0sproject/rig"
"github.com/k0sproject/rig/os"
"github.com/k0sproject/rig/os/linux"
"github.com/k0sproject/rig/os/registry"
)
Expand All @@ -24,22 +23,3 @@ func init() {
},
)
}

// InstallKubectl installs kubectl using the gcloud kubernetes repo
func (c Debian) InstallKubectl(h os.Host) error {
if err := c.InstallPackage(h, "apt-transport-https", "gnupg2"); err != nil {
return err
}

err := h.Exec(`curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -`)
if err != nil {
return err
}

err = h.Exec(`sudo test -e /etc/apt/sources.list.d/kubernetes.list || (echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list.d/kubernetes.list)`)
if err != nil {
return err
}

return c.InstallPackage(h, "kubectl")
}
19 changes: 0 additions & 19 deletions configurer/linux/enterpriselinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package linux

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

Expand All @@ -11,21 +10,3 @@ type EnterpriseLinux struct {
linux.EnterpriseLinux
configurer.Linux
}

// InstallKubectl installs kubectl using the gcloud kubernetes repo
func (c EnterpriseLinux) InstallKubectl(h os.Host) error {
err := c.WriteFile(h, "/etc/yum.repos.d/kubernetes.repo", `[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
`, "0644")

if err != nil {
return err
}

return c.InstallPackage(h, "kubectl")
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ require (
github.com/go-playground/validator/v10 v10.4.1
github.com/hashicorp/go-version v1.2.1
github.com/k0sproject/dig v0.1.1
github.com/k0sproject/rig v0.3.15
github.com/k0sproject/rig v0.3.17
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/mattn/go-isatty v0.0.12
github.com/segmentio/analytics-go v3.1.0+incompatible
github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18
github.com/sirupsen/logrus v1.7.0
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.0
github.com/urfave/cli/v2 v2.3.0
golang.org/x/sys v0.0.0-20200622214017-ed371f2e16b4
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/k0sproject/dig v0.1.1 h1:TmNoZtsCXF3jDzwSSEEwKjjD7fG5IyG0p8uvK+z1Wyo=
github.com/k0sproject/dig v0.1.1/go.mod h1:rBcqaQlJpcKdt2x/OE/lPvhGU50u/e95CSm5g/r4s78=
github.com/k0sproject/rig v0.3.15 h1:hq5GxDw3PiozvteAqlZZbiKMWTA5r8/0FSEeBirbXxg=
github.com/k0sproject/rig v0.3.15/go.mod h1:2FBHQkR4t9VveNzFF4iNuMGx9T171kKPNuS2PFunASI=
github.com/k0sproject/rig v0.3.17 h1:dsxhZIHcuoi+sIhFrR9xTv5tN9Bp5mu2ejBE8tUtu7o=
github.com/k0sproject/rig v0.3.17/go.mod h1:2FBHQkR4t9VveNzFF4iNuMGx9T171kKPNuS2PFunASI=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
Expand Down Expand Up @@ -213,8 +213,8 @@ github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18 h1:DAYUYH5869yV94
github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18/go.mod h1:nkxAfR/5quYxwPZhyDxgasBMnRtBZd0FCEpawpjMUFg=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
github.com/spf13/pflag v0.0.0-20170130214245-9ff6c6923cff/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand Down
7 changes: 0 additions & 7 deletions phase/prepare_hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,6 @@ func (p *PrepareHosts) prepareHost(h *cluster.Host) error {
}
}

if h.IsController() && !h.Configurer.CommandExist(h, "kubectl") {
log.Infof("%s: installing kubectl", h)
if err := h.Configurer.InstallKubectl(h); err != nil {
return err
}
}

if h.Configurer.IsContainer(h) {
log.Infof("%s: is a container, applying a fix", h)
if err := h.Configurer.FixContainer(h); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion phase/uploadfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (p *UploadFiles) uploadFiles(h *cluster.Host) error {
return err
}

if err := h.Execf("install -d %s -m %s", f.DestinationDir, f.PermMode); err != nil {
if err := h.Execf("sudo install -d %s -m %s", f.DestinationDir, f.PermMode); err != nil {
return err
}

Expand Down
46 changes: 46 additions & 0 deletions smoke-test/k0sctl-calico.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
apiVersion: k0sctl.k0sproject.io/v1beta1
kind: cluster
spec:
hosts:
- role: controller
ssh:
address: "127.0.0.1"
port: 9022
keyPath: ./id_rsa_k0s
- role: worker
ssh:
address: "127.0.0.1"
port: 9023
keyPath: ./id_rsa_k0s
k0s:
version: "0.13.1"
config:
spec:
network:
provider: calico
images:
konnectivity:
image: us.gcr.io/k8s-artifacts-prod/kas-network-proxy/proxy-agent
version: v0.0.13
metricsserver:
image: gcr.io/k8s-staging-metrics-server/metrics-server
version: v0.3.7
kubeproxy:
image: k8s.gcr.io/kube-proxy
version: v1.20.4
coredns:
image: quay.io/jnummelin/coredns
version: 1.7.0
calico:
cni:
image: quay.io/jnummelin/calico-cni
version: v3.16.2
flexvolume:
image: quay.io/jnummelin/calico-pod2daemon-flexvol
version: v3.16.2
node:
image: quay.io/jnummelin/calico-node
version: v3.16.2
kubecontrollers:
image: quay.io/jnummelin/calico-kube-controllers
version: v3.16.2
57 changes: 30 additions & 27 deletions smoke-test/k0sctl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,34 @@ spec:
port: 9023
keyPath: ./id_rsa_k0s
k0s:
version: "0.12.1"
version: "0.13.1"
config:
images:
konnectivity:
image: us.gcr.io/k8s-artifacts-prod/kas-network-proxy/proxy-agent
version: v0.0.13
metricsserver:
image: gcr.io/k8s-staging-metrics-server/metrics-server
version: v0.3.7
kubeproxy:
image: k8s.gcr.io/kube-proxy
version: v1.20.4
coredns:
image: quay.io/jnummelin/coredns
version: 1.7.0
calico:
cni:
image: quay.io/jnummelin/calico-cni
version: v3.16.2
flexvolume:
image: quay.io/jnummelin/calico-pod2daemon-flexvol
version: v3.16.2
node:
image: quay.io/jnummelin/calico-node
version: v3.16.2
kubecontrollers:
image: quay.io/jnummelin/calico-kube-controllers
version: v3.16.2
spec:
network:
provider: calico
images:
konnectivity:
image: us.gcr.io/k8s-artifacts-prod/kas-network-proxy/proxy-agent
version: v0.0.13
metricsserver:
image: gcr.io/k8s-staging-metrics-server/metrics-server
version: v0.3.7
kubeproxy:
image: k8s.gcr.io/kube-proxy
version: v1.20.4
coredns:
image: quay.io/jnummelin/coredns
version: 1.7.0
calico:
cni:
image: quay.io/jnummelin/calico-cni
version: v3.16.2
flexvolume:
image: quay.io/jnummelin/calico-pod2daemon-flexvol
version: v3.16.2
node:
image: quay.io/jnummelin/calico-node
version: v3.16.2
kubecontrollers:
image: quay.io/jnummelin/calico-kube-controllers
version: v3.16.2
5 changes: 3 additions & 2 deletions smoke-test/smoke-upgrade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ createCluster

# k0sctl 0.4.0 does not fall back from /var/cache/k0sctl, so this needs sudo.
sudo ~/.cache/k0sctl_040 apply --config k0sctl_legacy.yaml --debug
../k0sctl apply --config k0sctl.yaml --debug
../k0sctl kubeconfig --config k0sctl.yaml | grep -v -- "-data"
# We need to upgrade with calico specific config as the previous version defaulted to calico
../k0sctl apply --config k0sctl-calico.yaml --debug
../k0sctl kubeconfig --config k0sctl-calico.yaml | grep -v -- "-data"

0 comments on commit 04f6ac4

Please sign in to comment.