refactor the cluster struct

This commit is contained in:
pixiake 2020-05-07 09:39:42 +08:00
parent c2d7894121
commit 3eaee9ed4f
7 changed files with 55 additions and 59 deletions

View File

@ -15,28 +15,27 @@ import (
var (
K2ClusterObjTempl = template.Must(template.New("K2Cluster").Parse(
dedent.Dedent(`apiVersion: kubekey.io/v1alpha1
dedent.Dedent(`apiVersion: kubekey.kubesphere.io/v1alpha1
kind: K2Cluster
metadata:
name: demo
spec:
hosts:
- hostName: node1
sshAddress: 172.16.0.2
internalAddress: 172.16.0.2
port: "22"
user: ubuntu
password: Qcloud@123
sshKeyPath: ""
role:
- etcd
- master
- worker
lbKubeapiserver:
- {name: node1, address: 172.16.0.2, internalAddress: 172.16.0.2, user: ubuntu, password: Qcloud@123}
- {name: node2, address: 172.16.0.2, internalAddress: 172.16.0.2, user: ubuntu, password: Qcloud@123}
roleGroups:
etcd:
- node1
master:
- node1
worker:
- node1
- node2
controlPlaneEndpoint:
domain: lb.kubesphere.local
address: ""
port: "6443"
kubeCluster:
kubernetes:
version: v1.17.4
imageRepo: kubekey
clusterName: cluster.local

View File

@ -21,7 +21,7 @@ spec:
domain: lb.kubesphere.local
address: ""
port: "6443"
kubeCluster:
kubernetes:
version: v1.17.4
imageRepo: kubekey
clusterName: cluster.local
@ -32,13 +32,9 @@ spec:
registry:
registryMirrors: []
insecureRegistries: []
plugins:
localVolume:
enabled: true
isDefaultClass: true
storage:
defaultStorageClass: localVolume
nfsClient:
enabled: true
isDefaultClass: false
nfsServer: 172.16.0.2
nfsPath: /mnt/nfs
nfsVrs3Enabled: false

View File

@ -66,13 +66,13 @@ type K2ClusterList struct {
//}
type HostCfg struct {
Name string `json:"name,omitempty"`
Address string `json:"address,omitempty"`
InternalAddress string `json:"internalAddress,omitempty"`
Port string `json:"port,omitempty"`
User string `json:"user,omitempty"`
Password string `json:"password,omitempty"`
PrivateKeyPath string `json:"privateKeyPath,omitempty"`
Name string `yaml:"name,omitempty" json:"name,omitempty"`
Address string `yaml:"address,omitempty" json:"address,omitempty"`
InternalAddress string `yaml:"internalAddress,omitempty" json:"internalAddress,omitempty"`
Port string `yaml:"port,omitempty" json:"port,omitempty"`
User string `yaml:"user,omitempty" json:"user,omitempty"`
Password string `yaml:"password,omitempty" json:"password,omitempty"`
PrivateKeyPath string `yaml:"privateKeyPath,omitempty" json:"privateKeyPath,omitempty"`
ID int `json:"-"`
IsEtcd bool `json:"-"`
IsMaster bool `json:"-"`
@ -92,6 +92,7 @@ type HostGroups struct {
Master []HostCfg
Worker []HostCfg
K8s []HostCfg
Client []HostCfg
}
type NetworkConfig struct {
@ -147,7 +148,8 @@ func (cfg *K2ClusterSpec) GenerateCertSANs() []string {
func (cfg *K2ClusterSpec) GroupHosts() *HostGroups {
clusterHostsGroups := HostGroups{}
etcdGroup, masterGroup, workerGroup := cfg.ParseRolesList()
for _, host := range cfg.Hosts {
for index, host := range cfg.Hosts {
host.ID = index
for _, hostName := range etcdGroup {
if host.Name == hostName {
host.IsEtcd = true
@ -175,12 +177,13 @@ func (cfg *K2ClusterSpec) GroupHosts() *HostGroups {
clusterHostsGroups.All = append(clusterHostsGroups.All, host)
}
for _, host := range cfg.Hosts {
for _, host := range clusterHostsGroups.All {
if host.IsMaster || host.IsWorker {
clusterHostsGroups.K8s = append(clusterHostsGroups.K8s, host)
}
}
clusterHostsGroups.Client = append(clusterHostsGroups.Client, clusterHostsGroups.Master[0])
return &clusterHostsGroups
}

View File

@ -15,7 +15,6 @@ import (
func SyncKubeBinaries(mgr *manager.Manager) error {
mgr.Logger.Infoln("Syncing kube binaries")
return mgr.RunTaskOnK8sNodes(syncKubeBinaries, true)
}
@ -35,6 +34,7 @@ func syncKubeBinaries(mgr *manager.Manager, node *kubekeyapi.HostCfg, conn ssh.C
binaryList := []string{kubeadm, kubelet, kubectl, helm, kubecni}
for _, binary := range binaryList {
fmt.Println(binary)
err2 := mgr.Runner.ScpFile(fmt.Sprintf("%s/%s", filepath, binary), fmt.Sprintf("%s/%s", "/tmp/kubekey", binary))
if err2 != nil {
return errors.Wrap(errors.WithStack(err2), fmt.Sprintf("failed to sync binarys"))
@ -61,7 +61,7 @@ func syncKubeBinaries(mgr *manager.Manager, node *kubekeyapi.HostCfg, conn ssh.C
func ConfigureKubeletService(mgr *manager.Manager) error {
mgr.Logger.Infoln("Configure kubelet service")
return mgr.RunTaskOnAllNodes(setKubelet, true)
return mgr.RunTaskOnK8sNodes(setKubelet, true)
}
func setKubelet(mgr *manager.Manager, node *kubekeyapi.HostCfg, conn ssh.Connection) error {

View File

@ -13,7 +13,7 @@ import (
func InitOS(mgr *manager.Manager) error {
mgr.Logger.Infoln("Initialize operating system")
return mgr.RunTaskOnAllNodes(initOsOnNode, false)
return mgr.RunTaskOnAllNodes(initOsOnNode, true)
}
func initOsOnNode(mgr *manager.Manager, node *kubekeyapi.HostCfg, conn ssh.Connection) error {

View File

@ -38,24 +38,27 @@ func ParseClusterCfg(clusterCfgPath string, logger *log.Logger) (*kubekeyapi.K2C
}
}
defaultK2Cluster := SetDefaultK2Cluster(&clusterCfg)
return defaultK2Cluster, nil
//defaultK2Cluster := SetDefaultK2Cluster(&clusterCfg)
//return defaultK2Cluster, nil
return &clusterCfg, nil
}
func SetDefaultK2Cluster(obj *kubekeyapi.K2Cluster) *kubekeyapi.K2Cluster {
defaultCluster := &kubekeyapi.K2Cluster{}
defaultCluster.APIVersion = obj.APIVersion
defaultCluster.Kind = obj.APIVersion
defaultCluster.Spec = SetDefaultK2ClusterSpec(&obj.Spec)
return defaultCluster
}
//func SetDefaultK2Cluster(obj *kubekeyapi.K2Cluster) *kubekeyapi.K2Cluster {
// //fmt.Println(obj)
// out, _ := json.MarshalIndent(obj, "", " ")
// fmt.Println(string(out))
// defaultCluster := &kubekeyapi.K2Cluster{}
// defaultCluster.APIVersion = obj.APIVersion
// defaultCluster.Kind = obj.APIVersion
// defaultCluster.Spec = SetDefaultK2ClusterSpec(&obj.Spec)
// return defaultCluster
//}
func SetDefaultK2ClusterSpec(cfg *kubekeyapi.K2ClusterSpec) kubekeyapi.K2ClusterSpec {
func SetDefaultK2ClusterSpec(cfg *kubekeyapi.K2ClusterSpec, masterGroup []kubekeyapi.HostCfg) *kubekeyapi.K2ClusterSpec {
clusterCfg := kubekeyapi.K2ClusterSpec{}
clusterCfg.Hosts = SetDefaultHostsCfg(cfg)
clusterCfg.ControlPlaneEndpoint = SetDefaultLBCfg(cfg)
clusterCfg.ControlPlaneEndpoint = SetDefaultLBCfg(cfg, masterGroup)
clusterCfg.Network = SetDefaultNetworkCfg(cfg)
clusterCfg.Kubernetes = SetDefaultClusterCfg(cfg)
clusterCfg.Registry = cfg.Registry
@ -69,7 +72,7 @@ func SetDefaultK2ClusterSpec(cfg *kubekeyapi.K2ClusterSpec) kubekeyapi.K2Cluster
if cfg.Kubernetes.Version == "" {
clusterCfg.Kubernetes.Version = kubekeyapi.DefaultKubeVersion
}
return clusterCfg
return &clusterCfg
}
func SetDefaultHostsCfg(cfg *kubekeyapi.K2ClusterSpec) []kubekeyapi.HostCfg {
@ -77,8 +80,7 @@ func SetDefaultHostsCfg(cfg *kubekeyapi.K2ClusterSpec) []kubekeyapi.HostCfg {
if len(cfg.Hosts) == 0 {
return nil
}
for index, host := range cfg.Hosts {
host.ID = index
for _, host := range cfg.Hosts {
if len(host.Address) == 0 && len(host.InternalAddress) > 0 {
host.Address = host.InternalAddress
@ -95,21 +97,14 @@ func SetDefaultHostsCfg(cfg *kubekeyapi.K2ClusterSpec) []kubekeyapi.HostCfg {
hostscfg = append(hostscfg, host)
}
fmt.Sprintln(hostscfg)
return hostscfg
}
func SetDefaultLBCfg(cfg *kubekeyapi.K2ClusterSpec) kubekeyapi.ControlPlaneEndpoint {
masterHosts := []kubekeyapi.HostCfg{}
hosts := SetDefaultHostsCfg(cfg)
for _, host := range hosts {
if host.IsMaster {
masterHosts = append(masterHosts, host)
}
}
func SetDefaultLBCfg(cfg *kubekeyapi.K2ClusterSpec, masterGroup []kubekeyapi.HostCfg) kubekeyapi.ControlPlaneEndpoint {
if cfg.ControlPlaneEndpoint.Address == "" {
cfg.ControlPlaneEndpoint.Address = masterHosts[0].InternalAddress
cfg.ControlPlaneEndpoint.Address = masterGroup[0].InternalAddress
}
if cfg.ControlPlaneEndpoint.Domain == "" {
cfg.ControlPlaneEndpoint.Domain = kubekeyapi.DefaultLBDomain

View File

@ -3,6 +3,7 @@ package install
import (
"fmt"
kubekeyapi "github.com/kubesphere/kubekey/pkg/apis/kubekey/v1alpha1"
"github.com/kubesphere/kubekey/pkg/config"
"github.com/kubesphere/kubekey/pkg/util/manager"
"github.com/kubesphere/kubekey/pkg/util/ssh"
log "github.com/sirupsen/logrus"
@ -33,12 +34,14 @@ func (executor *Executor) Execute() error {
func (executor *Executor) createManager() (*manager.Manager, error) {
mgr := &manager.Manager{}
hostGroups := executor.cluster.GroupHosts()
fmt.Println(hostGroups)
mgr.AllNodes = hostGroups.All
mgr.EtcdNodes = hostGroups.Etcd
mgr.MasterNodes = hostGroups.Master
mgr.WorkerNodes = hostGroups.Worker
mgr.K8sNodes = hostGroups.K8s
mgr.Cluster = executor.cluster
mgr.ClientNode = hostGroups.Client
mgr.Cluster = config.SetDefaultK2ClusterSpec(executor.cluster, hostGroups.Master)
mgr.ClusterHosts = GenerateHosts(hostGroups, executor.cluster)
mgr.Connector = ssh.NewConnector()
mgr.Logger = executor.logger