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.
parent 7136a3e4
...@@ -37,6 +37,9 @@ const ( ...@@ -37,6 +37,9 @@ const (
// CreateLocalEtcdStaticPodManifestFile will write local etcd static pod manifest file. // CreateLocalEtcdStaticPodManifestFile will write local etcd static pod manifest file.
func CreateLocalEtcdStaticPodManifestFile(manifestDir string, cfg *kubeadmapi.InitConfiguration) error { 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") glog.V(1).Infoln("creating local etcd static pod manifest file")
// gets etcd StaticPodSpec, actualized for the current InitConfiguration // gets etcd StaticPodSpec, actualized for the current InitConfiguration
spec := GetEtcdPodSpec(cfg) spec := GetEtcdPodSpec(cfg)
......
...@@ -54,34 +54,64 @@ func TestGetEtcdPodSpec(t *testing.T) { ...@@ -54,34 +54,64 @@ func TestGetEtcdPodSpec(t *testing.T) {
} }
func TestCreateLocalEtcdStaticPodManifestFile(t *testing.T) { func TestCreateLocalEtcdStaticPodManifestFile(t *testing.T) {
// Create temp folder for the test case // Create temp folder for the test case
tmpdir := testutil.SetupTempDir(t) tmpdir := testutil.SetupTempDir(t)
defer os.RemoveAll(tmpdir) defer os.RemoveAll(tmpdir)
// Creates a Master Configuration var tests = []struct {
cfg := &kubeadmapi.InitConfiguration{ cfg *kubeadmapi.InitConfiguration
ClusterConfiguration: kubeadmapi.ClusterConfiguration{ expectedError bool
KubernetesVersion: "v1.7.0", }{
Etcd: kubeadmapi.Etcd{ {
Local: &kubeadmapi.LocalEtcd{ cfg: &kubeadmapi.InitConfiguration{
DataDir: "/var/lib/etcd", ClusterConfiguration: kubeadmapi.ClusterConfiguration{
Image: "k8s.gcr.io/etcd", 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 for _, test := range tests {
manifestPath := filepath.Join(tmpdir, kubeadmconstants.ManifestsSubDirName) // Execute createStaticPodFunction
err := CreateLocalEtcdStaticPodManifestFile(manifestPath, cfg) manifestPath := filepath.Join(tmpdir, kubeadmconstants.ManifestsSubDirName)
if err != nil { err := CreateLocalEtcdStaticPodManifestFile(manifestPath, test.cfg)
t.Errorf("Error executing CreateEtcdStaticPodManifestFile: %v", err)
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")
}
} }
// Assert expected files are there
testutil.AssertFilesCount(t, manifestPath, 1)
testutil.AssertFileExists(t, manifestPath, kubeadmconstants.Etcd+".yaml")
} }
func TestGetEtcdCommand(t *testing.T) { func TestGetEtcdCommand(t *testing.T) {
......
...@@ -143,6 +143,17 @@ func AssertFileExists(t *testing.T, dirName string, fileNames ...string) { ...@@ -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 // GetDefaultInternalConfig returns a defaulted kubeadmapi.InitConfiguration
func GetDefaultInternalConfig(t *testing.T) *kubeadmapi.InitConfiguration { func GetDefaultInternalConfig(t *testing.T) *kubeadmapi.InitConfiguration {
internalcfg, err := configutil.ConfigFileAndDefaultsToInternalConfig("", &kubeadmapiv1alpha3.InitConfiguration{}) internalcfg, err := configutil.ConfigFileAndDefaultsToInternalConfig("", &kubeadmapiv1alpha3.InitConfiguration{})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment