Skip to content

Commit

Permalink
Stop copying admin.conf to $HOME (#936)
Browse files Browse the repository at this point in the history
* Stop copying admin.conf and use it directly

As $HOME is not always persisted (Flatcar/Coreos)

Signed-off-by: Artiom Diomin <artiom@loodse.com>

* Get rid of kubectl --kubeconfig

Signed-off-by: Artiom Diomin <artiom@loodse.com>
  • Loading branch information
kron4eg authored Jun 26, 2020
1 parent 0f64d4e commit d3fa740
Show file tree
Hide file tree
Showing 10 changed files with 13 additions and 91 deletions.
8 changes: 5 additions & 3 deletions pkg/addons/addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ import (
)

const (
addonLabel = "kubeone.io/addon"

kubectlApplyScript = `kubectl apply -f {{.FILE_NAME}} --prune -l "%s"`
addonLabel = "kubeone.io/addon"
kubectlApplyScript = `
sudo KUBECONFIG=/etc/kubernetes/admin.conf \
kubectl apply -f {{.FILE_NAME}} --prune -l "%s"
`
)

// TemplateData is data available in the addons render template
Expand Down
36 changes: 0 additions & 36 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ limitations under the License.
package runner

import (
"fmt"
"os"
"strings"
"time"

"github.com/koron-go/prefixw"
"github.com/pkg/errors"
Expand Down Expand Up @@ -78,36 +75,3 @@ func (r *Runner) Run(cmd string, variables TemplateVariables) (string, string, e

return r.RunRaw(cmd)
}

// WaitForPod waits for the availability of the given Kubernetes element.
func (r *Runner) WaitForPod(namespace string, name string, timeout time.Duration) error {
cmd := fmt.Sprintf(`sudo kubectl --kubeconfig=/etc/kubernetes/admin.conf -n "%s" get pod "%s" -o jsonpath='{.status.phase}' --ignore-not-found`, namespace, name)
if !r.WaitForCondition(cmd, timeout, IsRunning) {
return errors.Errorf("timed out while waiting for %s/%s to come up for %v", namespace, name, timeout)
}

return nil
}

type validatorFunc func(stdout string) bool

// IsRunning checks if the given output represents the "Running" status of a Kubernetes pod.
func IsRunning(stdout string) bool {
return strings.ToLower(stdout) == "running"
}

// WaitForCondition waits for something to be true.
func (r *Runner) WaitForCondition(cmd string, timeout time.Duration, validator validatorFunc) bool {
cutoff := time.Now().Add(timeout)

for time.Now().Before(cutoff) {
stdout, _, _ := r.Run(cmd, nil)
if validator(stdout) {
return true
}

time.Sleep(1 * time.Second)
}

return false
}
10 changes: 0 additions & 10 deletions pkg/scripts/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ limitations under the License.
package scripts

const (
kubernetesAdminConfigScript = `
mkdir -p $HOME/.kube/
sudo cp /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
`

cloudConfigScriptTemplate = `
sudo mkdir -p /etc/systemd/system/kubelet.service.d/ /etc/kubernetes
sudo mv {{ .WORK_DIR }}/cfg/cloud-config /etc/kubernetes/cloud-config
Expand All @@ -49,10 +43,6 @@ fi
`
)

func KubernetesAdminConfig() (string, error) {
return Render(kubernetesAdminConfigScript, nil)
}

func SaveCloudConfig(workdir string) (string, error) {
return Render(cloudConfigScriptTemplate, Data{
"WORK_DIR": workdir,
Expand Down
11 changes: 0 additions & 11 deletions pkg/scripts/configs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,6 @@ import (
"github.com/kubermatic/kubeone/pkg/testhelper"
)

func TestKubernetesAdminConfig(t *testing.T) {
t.Parallel()

got, err := KubernetesAdminConfig()
if err != nil {
t.Fatalf("KubernetesAdminConfig() error = %v", err)
}

testhelper.DiffOutput(t, testhelper.FSGoldenName(t), got, *updateFlag)
}

func TestSaveCloudConfig(t *testing.T) {
t.Parallel()

Expand Down
8 changes: 4 additions & 4 deletions pkg/scripts/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ package scripts

const (
drainNodeScriptTemplate = `
export KUBECONFIG=/etc/kubernetes/admin.conf
sudo kubectl drain {{ .NODE_NAME }} --ignore-daemonsets --delete-local-data
sudo KUBECONFIG=/etc/kubernetes/admin.conf \
kubectl drain {{ .NODE_NAME }} --ignore-daemonsets --delete-local-data
`

uncordonNodeScriptTemplate = `
export KUBECONFIG=/etc/kubernetes/admin.conf
sudo kubectl uncordon {{ .NODE_NAME }}
sudo KUBECONFIG=/etc/kubernetes/admin.conf \
kubectl uncordon {{ .NODE_NAME }}
`
)

Expand Down
4 changes: 2 additions & 2 deletions pkg/scripts/testdata/TestDrainNode.golden
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
set -xeu pipefail
export "PATH=$PATH:/sbin:/usr/local/bin:/opt/bin"

export KUBECONFIG=/etc/kubernetes/admin.conf
sudo kubectl drain testNode1 --ignore-daemonsets --delete-local-data
sudo KUBECONFIG=/etc/kubernetes/admin.conf \
kubectl drain testNode1 --ignore-daemonsets --delete-local-data
6 changes: 0 additions & 6 deletions pkg/scripts/testdata/TestKubernetesAdminConfig.golden

This file was deleted.

4 changes: 2 additions & 2 deletions pkg/scripts/testdata/TestUncordonNode.golden
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
set -xeu pipefail
export "PATH=$PATH:/sbin:/usr/local/bin:/opt/bin"

export KUBECONFIG=/etc/kubernetes/admin.conf
sudo kubectl uncordon testNode2
sudo KUBECONFIG=/etc/kubernetes/admin.conf \
kubectl uncordon testNode2
16 changes: 0 additions & 16 deletions pkg/tasks/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,10 @@ import (

"github.com/pkg/errors"

kubeoneapi "github.com/kubermatic/kubeone/pkg/apis/kubeone"
"github.com/kubermatic/kubeone/pkg/kubeconfig"
"github.com/kubermatic/kubeone/pkg/scripts"
"github.com/kubermatic/kubeone/pkg/ssh"
"github.com/kubermatic/kubeone/pkg/state"
)

func copyKubeconfig(s *state.State) error {
return s.RunTaskOnNodes(s.Cluster.ControlPlane.Hosts, func(s *state.State, _ *kubeoneapi.HostConfig, conn ssh.Connection) error {
s.Logger.Infoln("Copying Kubeconfig to home directory…")
cmd, err := scripts.KubernetesAdminConfig()
if err != nil {
return err
}

_, _, err = s.Runner.RunRaw(cmd)
return err
}, state.RunParallel)
}

func saveKubeconfig(s *state.State) error {
s.Logger.Info("Downloading kubeconfig…")

Expand Down
1 change: 0 additions & 1 deletion pkg/tasks/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ func WithFullInstall(t Tasks) Tasks {
{Fn: kubeconfig.BuildKubernetesClientset, ErrMsg: "failed to build kubernetes clientset"},
{Fn: repairClusterIfNeeded, ErrMsg: "failed to repair cluster"},
{Fn: joinControlplaneNode, ErrMsg: "failed to join other masters a cluster"},
{Fn: copyKubeconfig, ErrMsg: "failed to copy kubeconfig to home directory"},
{Fn: saveKubeconfig, ErrMsg: "failed to save kubeconfig to the local machine"},
}...).
append(kubernetesResources()...).
Expand Down

0 comments on commit d3fa740

Please sign in to comment.