From 503c6c7b85e3261eb004120a0b46cd8a371c7cd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Fern=C3=A1ndez=20L=C3=B3pez?= Date: Thu, 4 Oct 2018 13:16:04 +0200 Subject: [PATCH] kubeadm: do not panic if etcd local alpha phase is called when an external etcd config is used If etcd local alpha phase is called manually while the kubeadm configuration points to an external etcd cluster kubeadm panics. --- cmd/kubeadm/app/phases/etcd/local.go | 3 ++ cmd/kubeadm/app/phases/etcd/local_test.go | 66 ++++++++++++++++------- cmd/kubeadm/test/util.go | 11 ++++ 3 files changed, 62 insertions(+), 18 deletions(-) diff --git a/cmd/kubeadm/app/phases/etcd/local.go b/cmd/kubeadm/app/phases/etcd/local.go index 57ecb6dd19f..43ed4892a24 100644 --- a/cmd/kubeadm/app/phases/etcd/local.go +++ b/cmd/kubeadm/app/phases/etcd/local.go @@ -37,6 +37,9 @@ const ( // CreateLocalEtcdStaticPodManifestFile will write local etcd static pod manifest file. func CreateLocalEtcdStaticPodManifestFile(manifestDir string, cfg *kubeadmapi.InitConfiguration) error { + if cfg.ClusterConfiguration.Etcd.External != nil { + return fmt.Errorf("etcd static pod manifest cannot be generated for cluster using external etcd") + } glog.V(1).Infoln("creating local etcd static pod manifest file") // gets etcd StaticPodSpec, actualized for the current InitConfiguration spec := GetEtcdPodSpec(cfg) diff --git a/cmd/kubeadm/app/phases/etcd/local_test.go b/cmd/kubeadm/app/phases/etcd/local_test.go index 1401b091d8b..d755a636791 100644 --- a/cmd/kubeadm/app/phases/etcd/local_test.go +++ b/cmd/kubeadm/app/phases/etcd/local_test.go @@ -54,34 +54,64 @@ func TestGetEtcdPodSpec(t *testing.T) { } func TestCreateLocalEtcdStaticPodManifestFile(t *testing.T) { - // Create temp folder for the test case tmpdir := testutil.SetupTempDir(t) defer os.RemoveAll(tmpdir) - // Creates a Master Configuration - cfg := &kubeadmapi.InitConfiguration{ - ClusterConfiguration: kubeadmapi.ClusterConfiguration{ - KubernetesVersion: "v1.7.0", - Etcd: kubeadmapi.Etcd{ - Local: &kubeadmapi.LocalEtcd{ - DataDir: "/var/lib/etcd", - Image: "k8s.gcr.io/etcd", + var tests = []struct { + cfg *kubeadmapi.InitConfiguration + expectedError bool + }{ + { + cfg: &kubeadmapi.InitConfiguration{ + ClusterConfiguration: kubeadmapi.ClusterConfiguration{ + KubernetesVersion: "v1.7.0", + Etcd: kubeadmapi.Etcd{ + Local: &kubeadmapi.LocalEtcd{ + DataDir: "/var/lib/etcd", + Image: "k8s.gcr.io/etcd", + }, + }, }, }, + expectedError: false, + }, + { + cfg: &kubeadmapi.InitConfiguration{ + ClusterConfiguration: kubeadmapi.ClusterConfiguration{ + KubernetesVersion: "v1.7.0", + Etcd: kubeadmapi.Etcd{ + External: &kubeadmapi.ExternalEtcd{ + Endpoints: []string{ + "https://etcd-instance:2379", + }, + CAFile: "/etc/kubernetes/pki/etcd/ca.crt", + CertFile: "/etc/kubernetes/pki/etcd/apiserver-etcd-client.crt", + KeyFile: "/etc/kubernetes/pki/etcd/apiserver-etcd-client.key", + }, + }, + }, + }, + expectedError: true, }, } - // Execute createStaticPodFunction - manifestPath := filepath.Join(tmpdir, kubeadmconstants.ManifestsSubDirName) - err := CreateLocalEtcdStaticPodManifestFile(manifestPath, cfg) - if err != nil { - t.Errorf("Error executing CreateEtcdStaticPodManifestFile: %v", err) - } + for _, test := range tests { + // Execute createStaticPodFunction + manifestPath := filepath.Join(tmpdir, kubeadmconstants.ManifestsSubDirName) + err := CreateLocalEtcdStaticPodManifestFile(manifestPath, test.cfg) - // Assert expected files are there - testutil.AssertFilesCount(t, manifestPath, 1) - testutil.AssertFileExists(t, manifestPath, kubeadmconstants.Etcd+".yaml") + if !test.expectedError { + if err != nil { + t.Errorf("CreateLocalEtcdStaticPodManifestFile failed when not expected: %v", err) + } + // Assert expected files are there + testutil.AssertFilesCount(t, manifestPath, 1) + testutil.AssertFileExists(t, manifestPath, kubeadmconstants.Etcd+".yaml") + } else { + testutil.AssertError(t, err, "etcd static pod manifest cannot be generated for cluster using external etcd") + } + } } func TestGetEtcdCommand(t *testing.T) { diff --git a/cmd/kubeadm/test/util.go b/cmd/kubeadm/test/util.go index 07bc630766a..fa98e68f57a 100644 --- a/cmd/kubeadm/test/util.go +++ b/cmd/kubeadm/test/util.go @@ -143,6 +143,17 @@ func AssertFileExists(t *testing.T, dirName string, fileNames ...string) { } } +// AssertError checks that the provided error matches the expected output +func AssertError(t *testing.T, err error, expected string) { + if err == nil { + t.Errorf("no error was found, but '%s' was expected", expected) + return + } + if err.Error() != expected { + t.Errorf("error '%s' does not match expected error: '%s'", err.Error(), expected) + } +} + // GetDefaultInternalConfig returns a defaulted kubeadmapi.InitConfiguration func GetDefaultInternalConfig(t *testing.T) *kubeadmapi.InitConfiguration { internalcfg, err := configutil.ConfigFileAndDefaultsToInternalConfig("", &kubeadmapiv1alpha3.InitConfiguration{})