github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/apply/run.go (about) 1 // Copyright © 2021 Alibaba Group Holding Ltd. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package apply 16 17 import ( 18 "net" 19 "strconv" 20 "strings" 21 22 v1 "github.com/alibaba/sealer/types/api/v1" 23 24 "github.com/alibaba/sealer/apply/applydriver" 25 26 "github.com/alibaba/sealer/common" 27 v2 "github.com/alibaba/sealer/types/api/v2" 28 "github.com/alibaba/sealer/utils" 29 ) 30 31 type ClusterArgs struct { 32 cluster *v2.Cluster 33 imageName string 34 runArgs *common.RunArgs 35 hosts []v2.Host 36 } 37 38 func IsIPList(args string) bool { 39 ipList := strings.Split(args, ",") 40 41 for _, i := range ipList { 42 if !strings.Contains(i, ":") { 43 return net.ParseIP(i) != nil 44 } 45 if _, err := net.ResolveTCPAddr("tcp", i); err != nil { 46 return false 47 } 48 } 49 return true 50 } 51 52 func PreProcessIPList(joinArgs *common.RunArgs) error { 53 if err := utils.AssemblyIPList(&joinArgs.Masters); err != nil { 54 return err 55 } 56 if err := utils.AssemblyIPList(&joinArgs.Nodes); err != nil { 57 return err 58 } 59 return nil 60 } 61 62 func (c *ClusterArgs) SetClusterArgs() error { 63 c.cluster.APIVersion = common.APIVersion 64 c.cluster.Kind = common.Cluster 65 c.cluster.Name = c.runArgs.ClusterName 66 c.cluster.Spec.Image = c.imageName 67 c.cluster.Spec.SSH.User = c.runArgs.User 68 c.cluster.Spec.SSH.Pk = c.runArgs.Pk 69 c.cluster.Spec.SSH.PkPasswd = c.runArgs.PkPassword 70 c.cluster.Spec.SSH.Port = strconv.Itoa(int(c.runArgs.Port)) 71 c.cluster.Spec.Env = append(c.cluster.Spec.Env, c.runArgs.CustomEnv...) 72 c.cluster.Spec.CMDArgs = append(c.cluster.Spec.CMDArgs, c.runArgs.CMDArgs...) 73 if c.runArgs.Password != "" { 74 c.cluster.Spec.SSH.Passwd = c.runArgs.Password 75 } 76 err := PreProcessIPList(c.runArgs) 77 if err != nil { 78 return err 79 } 80 if IsIPList(c.runArgs.Masters) && (IsIPList(c.runArgs.Nodes) || c.runArgs.Nodes == "") { 81 masters := strings.Split(c.runArgs.Masters, ",") 82 nodes := strings.Split(c.runArgs.Nodes, ",") 83 c.hosts = []v2.Host{} 84 c.setHostWithIpsPort(masters, common.MASTER) 85 if len(nodes) != 0 { 86 c.setHostWithIpsPort(nodes, common.NODE) 87 } 88 c.cluster.Spec.Hosts = c.hosts 89 } else { 90 ip, err := utils.GetLocalDefaultIP() 91 if err != nil { 92 return err 93 } 94 c.cluster.Spec.Hosts = []v2.Host{ 95 { 96 IPS: []string{ip}, 97 Roles: []string{common.MASTER}, 98 }, 99 } 100 } 101 return err 102 } 103 104 func (c *ClusterArgs) setHostWithIpsPort(ips []string, role string) { 105 //map[ssh port]*host 106 hostMap := map[string]*v2.Host{} 107 for i := range ips { 108 ip, port := utils.GetHostIPAndPortOrDefault(ips[i], strconv.Itoa(int(c.runArgs.Port))) 109 if _, ok := hostMap[port]; !ok { 110 hostMap[port] = &v2.Host{IPS: []string{ip}, Roles: []string{role}, SSH: v1.SSH{Port: port}} 111 continue 112 } 113 hostMap[port].IPS = append(hostMap[port].IPS, ip) 114 } 115 _, master0Port := utils.GetHostIPAndPortOrDefault(ips[0], strconv.Itoa(int(c.runArgs.Port))) 116 for port, host := range hostMap { 117 host.IPS = removeIPListDuplicatesAndEmpty(host.IPS) 118 if port == master0Port && role == common.MASTER { 119 c.hosts = append([]v2.Host{*host}, c.hosts...) 120 continue 121 } 122 c.hosts = append(c.hosts, *host) 123 } 124 } 125 126 func NewApplierFromArgs(imageName string, runArgs *common.RunArgs) (applydriver.Interface, error) { 127 c := &ClusterArgs{ 128 cluster: &v2.Cluster{}, 129 imageName: imageName, 130 runArgs: runArgs, 131 } 132 if err := c.SetClusterArgs(); err != nil { 133 return nil, err 134 } 135 return NewApplier(c.cluster) 136 }