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.
This commit is contained in:
Rafael Fernández López 2018-10-04 13:16:04 +02:00
parent 7136a3e4b2
commit 503c6c7b85
No known key found for this signature in database
GPG Key ID: 8902294E78418CF9
3 changed files with 62 additions and 18 deletions

View File

@ -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)

View File

@ -54,13 +54,16 @@ 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
expectedError bool
}{
{
cfg: &kubeadmapi.InitConfiguration{
ClusterConfiguration: kubeadmapi.ClusterConfiguration{ ClusterConfiguration: kubeadmapi.ClusterConfiguration{
KubernetesVersion: "v1.7.0", KubernetesVersion: "v1.7.0",
Etcd: kubeadmapi.Etcd{ Etcd: kubeadmapi.Etcd{
@ -70,18 +73,45 @@ func TestCreateLocalEtcdStaticPodManifestFile(t *testing.T) {
}, },
}, },
}, },
},
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,
},
} }
for _, test := range tests {
// Execute createStaticPodFunction // Execute createStaticPodFunction
manifestPath := filepath.Join(tmpdir, kubeadmconstants.ManifestsSubDirName) manifestPath := filepath.Join(tmpdir, kubeadmconstants.ManifestsSubDirName)
err := CreateLocalEtcdStaticPodManifestFile(manifestPath, cfg) err := CreateLocalEtcdStaticPodManifestFile(manifestPath, test.cfg)
if err != nil {
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 // Assert expected files are there
testutil.AssertFilesCount(t, manifestPath, 1) testutil.AssertFilesCount(t, manifestPath, 1)
testutil.AssertFileExists(t, manifestPath, kubeadmconstants.Etcd+".yaml") 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) { func TestGetEtcdCommand(t *testing.T) {

View File

@ -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{})