Skip to content

Commit

Permalink
Copy kubeconfig to HOME; Don't use sudo for kubectl (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
xmudrii authored and kubermatic-bot committed Jan 21, 2019
1 parent d2b8e97 commit 3ec5c69
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pkg/installer/installation/ark.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func deployArk(ctx *util.Context) error {

ctx.Logger.Infoln("Deploying Ark…")

_, _, err = ctx.Runner.Run(`sudo kubectl --kubeconfig /etc/kubernetes/admin.conf apply -f "{{ .WORK_DIR }}/ark/ark.yaml"`, util.TemplateVariables{
_, _, err = ctx.Runner.Run(`kubectl apply -f "{{ .WORK_DIR }}/ark/ark.yaml"`, util.TemplateVariables{
"WORK_DIR": ctx.WorkDir,
})

Expand Down
2 changes: 1 addition & 1 deletion pkg/installer/installation/cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func applyCNI(ctx *util.Context, cni string) error {
func applyCanalCNI(ctx *util.Context) error {
return ctx.RunTaskOnLeader(func(ctx *util.Context, _ *config.HostConfig, conn ssh.Connection) error {
ctx.Logger.Infoln("Applying canal CNI plugin…")
_, _, err := ctx.Runner.Run(`sudo kubectl apply -f {{ .WORK_DIR }}/canal.yaml`, util.TemplateVariables{
_, _, err := ctx.Runner.Run(`kubectl apply -f {{ .WORK_DIR }}/canal.yaml`, util.TemplateVariables{
"WORK_DIR": ctx.WorkDir,
})

Expand Down
3 changes: 3 additions & 0 deletions pkg/installer/installation/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ func Install(ctx *util.Context) error {
if err := joinControlplaneNode(ctx); err != nil {
return fmt.Errorf("unable to join other masters a cluster: %v", err)
}
if err := copyKubeconfig(ctx); err != nil {
return fmt.Errorf("unable to copy kubeconfig to home directory: %v", err)
}
if err := applyCNI(ctx, "canal"); err != nil {
return fmt.Errorf("failed to install cni plugin canal: %v", err)
}
Expand Down
1 change: 0 additions & 1 deletion pkg/installer/installation/kubeadm_leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

const (
kubeadmCertCommand = `
grep -q KUBECONFIG /etc/environment || { echo 'export KUBECONFIG=/etc/kubernetes/admin.conf' | sudo tee -a /etc/environment; }
if [[ -d ./{{ .WORK_DIR }}/pki ]]; then
sudo rsync -av ./{{ .WORK_DIR }}/pki/ /etc/kubernetes/pki/
rm -rf ./{{ .WORK_DIR }}/pki
Expand Down
24 changes: 24 additions & 0 deletions pkg/installer/installation/kubeconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package installation

import (
"github.com/kubermatic/kubeone/pkg/config"
"github.com/kubermatic/kubeone/pkg/installer/util"
"github.com/kubermatic/kubeone/pkg/ssh"
)

func copyKubeconfig(ctx *util.Context) error {
return ctx.RunTaskOnAllNodes(func(ctx *util.Context, _ *config.HostConfig, conn ssh.Connection) error {
ctx.Logger.Infoln("Copying Kubeconfig to home directory…")

_, _, err := ctx.Runner.Run(`
mkdir -p $HOME/.kube/
sudo cp /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -u) $HOME/.kube/config
`, util.TemplateVariables{})
if err != nil {
return err
}

return nil
}, true)
}
4 changes: 2 additions & 2 deletions pkg/installer/installation/machine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func installMachineController(ctx *util.Context) error {
ctx.Logger.Infoln("Installing machine-controller…")

_, _, err = ctx.Runner.Run(`
sudo kubectl apply -f ./{{ .WORK_DIR }}/machine-controller.yaml
sudo kubectl apply -f ./{{ .WORK_DIR }}/machine-controller-webhook.yaml
kubectl apply -f ./{{ .WORK_DIR }}/machine-controller.yaml
kubectl apply -f ./{{ .WORK_DIR }}/machine-controller-webhook.yaml
`, util.TemplateVariables{
"WORK_DIR": ctx.WorkDir,
})
Expand Down
12 changes: 6 additions & 6 deletions pkg/installer/installation/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ func resetNode(ctx *util.Context, _ *config.HostConfig, conn ssh.Connection) err
}

const destroyScript = `
if sudo kubectl cluster-info > /dev/null; then
sudo kubectl annotate --all --overwrite node kubermatic.io/skip-eviction=true
sudo kubectl delete machinedeployment -n "{{ .MACHINE_NS }}" --all
sudo kubectl delete machineset -n "{{ .MACHINE_NS }}" --all
sudo kubectl delete machine -n "{{ .MACHINE_NS }}" --all
if kubectl cluster-info > /dev/null; then
kubectl annotate --all --overwrite node kubermatic.io/skip-eviction=true
kubectl delete machinedeployment -n "{{ .MACHINE_NS }}" --all
kubectl delete machineset -n "{{ .MACHINE_NS }}" --all
kubectl delete machine -n "{{ .MACHINE_NS }}" --all
for try in {1..30}; do
if sudo kubectl get machine -n "{{ .MACHINE_NS }}" 2>&1 | grep -q 'No resources found.'; then
if kubectl get machine -n "{{ .MACHINE_NS }}" 2>&1 | grep -q 'No resources found.'; then
exit 0
fi
sleep 10s
Expand Down
6 changes: 3 additions & 3 deletions pkg/installer/installation/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func createWorkerMachines(ctx *util.Context) error {
ctx.Logger.Infoln("Waiting for machine-controller to come up…")

cmd := fmt.Sprintf(
`sudo kubectl -n "%s" get pods -l '%s=%s' -o jsonpath='{.items[0].status.phase}'`,
`kubectl -n "%s" get pods -l '%s=%s' -o jsonpath='{.items[0].status.phase}'`,
machinecontroller.WebhookNamespace,
machinecontroller.WebhookAppLabelKey,
machinecontroller.WebhookAppLabelValue,
Expand All @@ -30,7 +30,7 @@ func createWorkerMachines(ctx *util.Context) error {
}

cmd = fmt.Sprintf(
`sudo kubectl -n "%s" get pods -l '%s=%s' -o jsonpath='{.items[0].status.phase}'`,
`kubectl -n "%s" get pods -l '%s=%s' -o jsonpath='{.items[0].status.phase}'`,
machinecontroller.MachineControllerNamespace,
machinecontroller.MachineControllerAppLabelKey,
machinecontroller.MachineControllerAppLabelValue,
Expand All @@ -43,7 +43,7 @@ func createWorkerMachines(ctx *util.Context) error {
time.Sleep(10 * time.Second)

ctx.Logger.Infoln("Creating worker machines…")
_, _, err := ctx.Runner.Run(`sudo kubectl apply -f ./{{ .WORK_DIR }}/workers.yaml`, util.TemplateVariables{
_, _, err := ctx.Runner.Run(`kubectl apply -f ./{{ .WORK_DIR }}/workers.yaml`, util.TemplateVariables{
"WORK_DIR": ctx.WorkDir,
})

Expand Down

0 comments on commit 3ec5c69

Please sign in to comment.