external etcd, refactor based on review
Signed-off-by: lizhen6 <lizhen6@360.cn>
This commit is contained in:
parent
e0111919cc
commit
9257a8d4dd
|
@ -231,19 +231,24 @@ func getExternalEtcdServerConfig(ctx context.Context, host kubernetes.Interface,
|
|||
}
|
||||
// should be only one container, but it may be injected others by mutating webhook of host cluster,
|
||||
// anyway, a for can handle all cases.
|
||||
for _, container := range apiserver.Spec.Template.Spec.Containers {
|
||||
var apiServerContainer *corev1.Container
|
||||
for i, container := range apiserver.Spec.Template.Spec.Containers {
|
||||
if container.Name == karmadaAPIServerDeploymentAndServiceName {
|
||||
for _, cmd := range container.Command {
|
||||
if strings.HasPrefix(cmd, etcdServerArgPrefix) {
|
||||
servers = cmd[etcdServerArgPrefixLength:]
|
||||
} else if strings.HasPrefix(cmd, etcdKeyPrefixArgPrefix) {
|
||||
prefix = cmd[etcdKeyPrefixArgPrefixLength:]
|
||||
}
|
||||
if servers != "" && prefix != "" {
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
apiServerContainer = &apiserver.Spec.Template.Spec.Containers[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
if apiServerContainer == nil {
|
||||
return
|
||||
}
|
||||
for _, cmd := range apiServerContainer.Command {
|
||||
if strings.HasPrefix(cmd, etcdServerArgPrefix) {
|
||||
servers = cmd[etcdServerArgPrefixLength:]
|
||||
} else if strings.HasPrefix(cmd, etcdKeyPrefixArgPrefix) {
|
||||
prefix = cmd[etcdKeyPrefixArgPrefixLength:]
|
||||
}
|
||||
if servers != "" && prefix != "" {
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
|
|
|
@ -196,6 +196,10 @@ func (i *CommandInitOption) validateExternalEtcd(_ string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (i *CommandInitOption) isExternalEtcdProvided() bool {
|
||||
return i.ExternalEtcdServers != ""
|
||||
}
|
||||
|
||||
// Validate Check that there are enough flags to run the command.
|
||||
//
|
||||
//nolint:gocyclo
|
||||
|
@ -206,7 +210,7 @@ func (i *CommandInitOption) Validate(parentCommand string) error {
|
|||
}
|
||||
}
|
||||
|
||||
if i.ExternalEtcdServers != "" {
|
||||
if i.isExternalEtcdProvided() {
|
||||
return i.validateExternalEtcd(parentCommand)
|
||||
} else {
|
||||
return i.validateBundledEtcd(parentCommand)
|
||||
|
@ -232,7 +236,7 @@ func (i *CommandInitOption) Complete() error {
|
|||
return fmt.Errorf("nodePort of karmada apiserver %v already exist", i.KarmadaAPIServerNodePort)
|
||||
}
|
||||
|
||||
if i.ExternalEtcdServers == "" && i.EtcdStorageMode == "hostPath" && i.EtcdNodeSelectorLabels == "" {
|
||||
if !i.isExternalEtcdProvided() && i.EtcdStorageMode == "hostPath" && i.EtcdNodeSelectorLabels == "" {
|
||||
if err := i.AddNodeSelectorLabels(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -243,7 +247,7 @@ func (i *CommandInitOption) Complete() error {
|
|||
}
|
||||
klog.Infof("karmada apiserver ip: %s", i.KarmadaAPIServerIP)
|
||||
|
||||
if i.ExternalEtcdServers == "" && i.EtcdStorageMode == "hostPath" && i.EtcdNodeSelectorLabels != "" {
|
||||
if !i.isExternalEtcdProvided() && i.EtcdStorageMode == "hostPath" && i.EtcdNodeSelectorLabels != "" {
|
||||
if !i.isNodeExist(i.EtcdNodeSelectorLabels) {
|
||||
return fmt.Errorf("no node found by label %s", i.EtcdNodeSelectorLabels)
|
||||
}
|
||||
|
@ -271,7 +275,7 @@ func (i *CommandInitOption) genCerts() error {
|
|||
notAfter := time.Now().Add(i.CertValidity).UTC()
|
||||
|
||||
var etcdServerCertConfig, etcdClientCertCfg *cert.CertsConfig
|
||||
if i.ExternalEtcdServers == "" {
|
||||
if !i.isExternalEtcdProvided() {
|
||||
etcdServerCertDNS := []string{
|
||||
"localhost",
|
||||
}
|
||||
|
@ -400,7 +404,7 @@ func (i *CommandInitOption) createCertsSecrets() error {
|
|||
}
|
||||
|
||||
func (i *CommandInitOption) initKarmadaAPIServer() error {
|
||||
if i.ExternalEtcdServers == "" {
|
||||
if !i.isExternalEtcdProvided() {
|
||||
if err := util.CreateOrUpdateService(i.KubeClientSet, i.makeEtcdService(etcdStatefulSetAndServiceName)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -497,7 +501,7 @@ func (i *CommandInitOption) initKarmadaComponent() error {
|
|||
}
|
||||
|
||||
func (i *CommandInitOption) readExternalEtcdCert(name string) (isExternalEtcdCert bool, err error) {
|
||||
if i.ExternalEtcdServers == "" {
|
||||
if !i.isExternalEtcdProvided() {
|
||||
return
|
||||
}
|
||||
var getCertAndKey func(*CommandInitOption) ([]byte, []byte, error)
|
||||
|
|
Loading…
Reference in New Issue