Merge pull request #1855 from hanweisen/karmadainit

when type wrong etcd-storage-mode throw error
This commit is contained in:
karmada-bot 2022-06-02 12:02:52 +08:00 committed by GitHub
commit 08fe6a6df0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 7 deletions

View File

@ -2,6 +2,7 @@ package cmdinit
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"k8s.io/klog/v2"
@ -57,7 +58,7 @@ func NewCmdInit(parentCommand string) *cobra.Command {
flags.StringVar(&opts.Context, "context", "", "The name of the kubeconfig context to use")
// etcd
flags.StringVarP(&opts.EtcdStorageMode, "etcd-storage-mode", "", "emptyDir",
"etcd data storage mode(emptyDir,hostPath,PVC). value is PVC, specify --storage-classes-name")
fmt.Sprintf("etcd data storage mode(%s). value is PVC, specify --storage-classes-name", strings.Join(kubernetes.SupportedStorageMode(), ",")))
flags.StringVarP(&opts.EtcdImage, "etcd-image", "", "", "etcd image")
flags.StringVarP(&opts.EtcdInitImage, "etcd-init-image", "", "docker.io/alpine:3.15.1", "etcd init container image")
flags.Int32VarP(&opts.EtcdReplicas, "etcd-replicas", "", 1, "etcd replica set, cluster 3,5...singular")

View File

@ -47,6 +47,12 @@ var (
defaultKubeControllerManagerImage = "kube-controller-manager:v1.22.10"
)
const (
etcdStorageModePVC = "PVC"
etcdStorageModeEmptyDir = "emptyDir"
etcdStorageModeHostPath = "hostPath"
)
// CommandInitOption holds all flags options for init.
type CommandInitOption struct {
KubeImageRegistry string
@ -87,22 +93,31 @@ type CommandInitOption struct {
// Validate Check that there are enough flags to run the command.
func (i *CommandInitOption) Validate(parentCommand string) error {
if i.EtcdStorageMode == "hostPath" && i.EtcdHostDataPath == "" {
if i.EtcdStorageMode == etcdStorageModeHostPath && i.EtcdHostDataPath == "" {
return fmt.Errorf("when etcd storage mode is hostPath, dataPath is not empty. See '%s init --help'", parentCommand)
}
if i.EtcdStorageMode == "hostPath" && i.EtcdNodeSelectorLabels != "" && utils.StringToMap(i.EtcdNodeSelectorLabels) == nil {
if i.EtcdStorageMode == etcdStorageModeHostPath && i.EtcdNodeSelectorLabels != "" && utils.StringToMap(i.EtcdNodeSelectorLabels) == nil {
return fmt.Errorf("the label does not seem to be 'key=value'")
}
if i.EtcdStorageMode == "hostPath" && i.EtcdReplicas != 1 {
if i.EtcdStorageMode == etcdStorageModeHostPath && i.EtcdReplicas != 1 {
return fmt.Errorf("for data security,when etcd storage mode is hostPath,etcd-replicas can only be 1")
}
if i.EtcdStorageMode == "PVC" && i.StorageClassesName == "" {
if i.EtcdStorageMode == etcdStorageModePVC && i.StorageClassesName == "" {
return fmt.Errorf("when etcd storage mode is PVC, storageClassesName is not empty. See '%s init --help'", parentCommand)
}
supportedStorageMode := SupportedStorageMode()
if i.EtcdStorageMode != "" {
for _, mode := range supportedStorageMode {
if i.EtcdStorageMode == mode {
return nil
}
}
return fmt.Errorf("unsupported etcd-storage-mode %s. See '%s init --help'", i.EtcdStorageMode, parentCommand)
}
return nil
}
@ -509,3 +524,8 @@ func homeDir() string {
}
return os.Getenv("USERPROFILE") // windows
}
// SupportedStorageMode Return install etcd supported storage mode
func SupportedStorageMode() []string {
return []string{etcdStorageModeEmptyDir, etcdStorageModeHostPath, etcdStorageModePVC}
}

View File

@ -58,7 +58,7 @@ func (i CommandInitOption) etcdVolume() (*[]corev1.Volume, *corev1.PersistentVol
Volumes = append(Volumes, secretVolume, configVolume)
switch i.EtcdStorageMode {
case "PVC":
case etcdStorageModePVC:
mode := corev1.PersistentVolumeFilesystem
persistentVolumeClaim := corev1.PersistentVolumeClaim{
TypeMeta: metav1.TypeMeta{
@ -83,7 +83,7 @@ func (i CommandInitOption) etcdVolume() (*[]corev1.Volume, *corev1.PersistentVol
}
return &Volumes, &persistentVolumeClaim
case "hostPath":
case etcdStorageModeHostPath:
t := corev1.HostPathDirectoryOrCreate
hostPath := corev1.Volume{
Name: etcdContainerDataVolumeMountName,