Skip to content

Commit

Permalink
CoreOS host architecture detection fix (#882)
Browse files Browse the repository at this point in the history
* Fix missing host detection on CoreOS

Signed-off-by: Artiom Diomin <[email protected]>

* golden tests for pkg/scripts

Signed-off-by: Artiom Diomin <[email protected]>

* fix boilerplate

Signed-off-by: Artiom Diomin <[email protected]>

* fix linting

Signed-off-by: Artiom Diomin <[email protected]>
  • Loading branch information
kron4eg authored Apr 27, 2020
1 parent 3923dd4 commit 68aa7b6
Show file tree
Hide file tree
Showing 55 changed files with 1,639 additions and 58 deletions.
19 changes: 9 additions & 10 deletions pkg/scripts/ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@ package scripts

const (
copyPKIHomeScriptTemplate = `
mkdir -p ./{{ .WORK_DIR }}/pki/etcd
sudo cp /etc/kubernetes/pki/ca.crt ./{{ .WORK_DIR }}/pki/
sudo cp /etc/kubernetes/pki/ca.key ./{{ .WORK_DIR }}/pki/
sudo cp /etc/kubernetes/pki/sa.key ./{{ .WORK_DIR }}/pki/
sudo cp /etc/kubernetes/pki/sa.pub ./{{ .WORK_DIR }}/pki/
sudo cp /etc/kubernetes/pki/front-proxy-ca.crt ./{{ .WORK_DIR }}/pki/
sudo cp /etc/kubernetes/pki/front-proxy-ca.key ./{{ .WORK_DIR }}/pki/
sudo cp /etc/kubernetes/pki/etcd/ca.{crt,key} ./{{ .WORK_DIR }}/pki/etcd/
sudo chown -R "$(id -u):$(id -g)" ./{{ .WORK_DIR }}
mkdir -p {{ .WORK_DIR }}/pki/etcd
sudo cp /etc/kubernetes/pki/ca.crt {{ .WORK_DIR }}/pki/
sudo cp /etc/kubernetes/pki/ca.key {{ .WORK_DIR }}/pki/
sudo cp /etc/kubernetes/pki/sa.key {{ .WORK_DIR }}/pki/
sudo cp /etc/kubernetes/pki/sa.pub {{ .WORK_DIR }}/pki/
sudo cp /etc/kubernetes/pki/front-proxy-ca.crt {{ .WORK_DIR }}/pki/
sudo cp /etc/kubernetes/pki/front-proxy-ca.key {{ .WORK_DIR }}/pki/
sudo cp /etc/kubernetes/pki/etcd/ca.{crt,key} {{ .WORK_DIR }}/pki/etcd/
sudo chown -R "$(id -u):$(id -g)" {{ .WORK_DIR }}
`
)

Expand Down
48 changes: 48 additions & 0 deletions pkg/scripts/ca_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright 2019 The KubeOne Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package scripts

import (
"testing"

"github.com/kubermatic/kubeone/pkg/testhelper"
)

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

tests := []struct {
name string
workdir string
err error
}{
{name: "kubeone1", workdir: "test-dir1"},
{name: "kubeone2", workdir: "./subdir/test"},
}

for _, tt := range tests {
tt := tt
t.Run(tt.workdir, func(t *testing.T) {
got, err := CopyPKIHome(tt.workdir)
if err != tt.err {
t.Fatalf("CopyPKIHome() error = %v, wantErr %v", err, tt.err)
}

testhelper.DiffOutput(t, testhelper.FSGoldenName(t), got, *updateFlag)
})
}
}
6 changes: 3 additions & 3 deletions pkg/scripts/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ 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
sudo mv {{ .WORK_DIR }}/cfg/cloud-config /etc/kubernetes/cloud-config
sudo chown root:root /etc/kubernetes/cloud-config
sudo chmod 600 /etc/kubernetes/cloud-config
`

auditPolicyScriptTemplate = `
if [[ -f "./{{ .WORK_DIR }}/cfg/audit-policy.yaml" ]]; then
if [[ -f "{{ .WORK_DIR }}/cfg/audit-policy.yaml" ]]; then
sudo mkdir -p /etc/kubernetes/audit
sudo mv ./{{ .WORK_DIR }}/cfg/audit-policy.yaml /etc/kubernetes/audit/policy.yaml
sudo mv {{ .WORK_DIR }}/cfg/audit-policy.yaml /etc/kubernetes/audit/policy.yaml
sudo chown root:root /etc/kubernetes/audit/policy.yaml
fi
`
Expand Down
86 changes: 86 additions & 0 deletions pkg/scripts/configs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
Copyright 2019 The KubeOne Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package scripts

import (
"testing"

"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()

tests := []struct {
name string
workdir string
wantErr error
}{
{name: "kubeone1", workdir: "test-dir1"},
{name: "kubeone2", workdir: "./subdir/test"},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
got, err := SaveCloudConfig(tt.workdir)
if err != tt.wantErr {
t.Errorf("SaveCloudConfig() error = %v, wantErr %v", err, tt.wantErr)
return
}

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

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

tests := []struct {
name string
workdir string
err error
}{
{name: "kubeone1", workdir: "test-dir1"},
{name: "kubeone2", workdir: "./subdir/test"},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
got, err := SaveAuditPolicyConfig(tt.workdir)
if err != tt.err {
t.Errorf("SaveAuditPolicyConfig() error = %v, wantErr %v", err, tt.err)
return
}

testhelper.DiffOutput(t, testhelper.FSGoldenName(t), got, *updateFlag)
})
}
}
33 changes: 33 additions & 0 deletions pkg/scripts/discovery_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright 2019 The KubeOne Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package scripts

import (
"testing"

"github.com/kubermatic/kubeone/pkg/testhelper"
)

func TestVerifyPrerequisites(t *testing.T) {
got, err := VerifyPrerequisites()
if err != nil {
t.Errorf("VerifyPrerequisites() error = %v", err)
return
}

testhelper.DiffOutput(t, testhelper.FSGoldenName(t), got, *updateFlag)
}
35 changes: 35 additions & 0 deletions pkg/scripts/funcs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright 2019 The KubeOne Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package scripts

func detectHostArch() string {
return `
HOST_ARCH=""
case $(uname -m) in
x86_64)
HOST_ARCH="amd64"
;;
aarch64)
HOST_ARCH="arm64"
;;
*)
echo "unsupported CPU architecture, exiting"
exit 1
;;
esac
`
}
20 changes: 11 additions & 9 deletions pkg/scripts/kubeadm.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,26 @@ const (
if [[ -f /etc/kubernetes/admin.conf ]]; then exit 0; fi
sudo kubeadm join {{ .VERBOSE }} \
--config=./{{ .WORK_DIR }}/cfg/master_{{ .NODE_ID }}.yaml \
--config={{ .WORK_DIR }}/cfg/master_{{ .NODE_ID }}.yaml \
--ignore-preflight-errors=DirAvailable--var-lib-etcd
`

kubeadmWorkerJoinScriptTemplate = `
if [[ -f /etc/kubernetes/kubelet.conf ]]; then exit 0; fi
sudo kubeadm join {{ .VERBOSE }} \
--config=./{{ .WORK_DIR }}/cfg/worker_{{ .NODE_ID }}.yaml
--config={{ .WORK_DIR }}/cfg/worker_{{ .NODE_ID }}.yaml
`

kubeadmCertScriptTemplate = `
if [[ -d ./{{ .WORK_DIR }}/pki ]]; then
sudo rsync -av ./{{ .WORK_DIR }}/pki/ /etc/kubernetes/pki/
sudo chown -R root:root /etc/kubernetes
rm -rf ./{{ .WORK_DIR }}/pki
if [[ -d {{ .WORK_DIR }}/pki ]]; then
sudo rsync -av {{ .WORK_DIR }}/pki/ /etc/kubernetes/pki/
sudo chown -R root:root /etc/kubernetes
rm -rf {{ .WORK_DIR }}/pki
fi
sudo kubeadm {{ .VERBOSE }} init phase certs all --config=./{{ .WORK_DIR }}/cfg/master_{{ .NODE_ID }}.yaml
sudo kubeadm {{ .VERBOSE }} \
init phase certs all \
--config={{ .WORK_DIR }}/cfg/master_{{ .NODE_ID }}.yaml
`

kubeadmInitScriptTemplate = `
Expand All @@ -47,7 +49,7 @@ if [[ -f /etc/kubernetes/admin.conf ]]; then
exit 0;
fi
sudo kubeadm {{ .VERBOSE }} \
init --config=./{{ .WORK_DIR }}/cfg/master_{{ .NODE_ID }}.yaml \
init --config={{ .WORK_DIR }}/cfg/master_{{ .NODE_ID }}.yaml \
--ignore-preflight-errors=DirAvailable--var-lib-etcd
`

Expand All @@ -59,7 +61,7 @@ rm -rf "{{ .WORK_DIR }}"
`

kubeadmUpgradeLeaderScriptTemplate = `
sudo {{ .KUBEADM_UPGRADE }} --config=./{{ .WORK_DIR }}/cfg/master_0.yaml`
sudo {{ .KUBEADM_UPGRADE }} --config={{ .WORK_DIR }}/cfg/master_0.yaml`
)

func KubeadmJoin(workdir string, nodeID int, verboseFlag string) (string, error) {
Expand Down
Loading

0 comments on commit 68aa7b6

Please sign in to comment.