github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/apply/processor/gen.go (about) 1 /* 2 Copyright © 2022 Alibaba Group Holding Ltd. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package processor 18 19 import ( 20 "fmt" 21 "strconv" 22 23 "github.com/alibaba/sealer/utils/platform" 24 25 "github.com/alibaba/sealer/utils/ssh" 26 27 "github.com/alibaba/sealer/pkg/filesystem" 28 "github.com/alibaba/sealer/pkg/filesystem/cloudimage" 29 "github.com/alibaba/sealer/pkg/image" 30 "github.com/alibaba/sealer/pkg/runtime" 31 32 "github.com/alibaba/sealer/common" 33 "github.com/alibaba/sealer/pkg/client/k8s" 34 apiv1 "github.com/alibaba/sealer/types/api/v1" 35 v2 "github.com/alibaba/sealer/types/api/v2" 36 "github.com/alibaba/sealer/utils" 37 v1 "k8s.io/api/core/v1" 38 ) 39 40 const ( 41 masterLabel = "node-role.kubernetes.io/master" 42 ) 43 44 type ParserArg struct { 45 Name string 46 Passwd string 47 Image string 48 Port uint16 49 Pk string 50 PkPassword string 51 } 52 53 type GenerateProcessor struct { 54 Runtime *runtime.KubeadmRuntime 55 ImageManager image.Service 56 ImageMounter cloudimage.Interface 57 } 58 59 func NewGenerateProcessor() (Processor, error) { 60 imageMounter, err := filesystem.NewCloudImageMounter() 61 if err != nil { 62 return nil, err 63 } 64 imgSvc, err := image.NewImageService() 65 if err != nil { 66 return nil, err 67 } 68 return &GenerateProcessor{ 69 ImageManager: imgSvc, 70 ImageMounter: imageMounter, 71 }, nil 72 } 73 74 func (g *GenerateProcessor) init(cluster *v2.Cluster) error { 75 fileName := fmt.Sprintf("%s/.sealer/%s/Clusterfile", common.GetHomeDir(), cluster.Name) 76 if err := utils.MarshalYamlToFile(fileName, cluster); err != nil { 77 return err 78 } 79 return nil 80 } 81 82 func (g *GenerateProcessor) GetPipeLine() ([]func(cluster *v2.Cluster) error, error) { 83 var todoList []func(cluster *v2.Cluster) error 84 todoList = append(todoList, 85 g.init, 86 g.MountImage, 87 g.MountRootfs, 88 g.ApplyRegistry, 89 g.UnmountImage, 90 ) 91 return todoList, nil 92 } 93 94 func GenerateCluster(arg *ParserArg) (*v2.Cluster, error) { 95 var nodeip, masterip []string 96 cluster := &v2.Cluster{} 97 98 cluster.Kind = common.Kind 99 cluster.APIVersion = common.APIVersion 100 cluster.Name = arg.Name 101 cluster.Spec.Image = arg.Image 102 cluster.Spec.SSH.Passwd = arg.Passwd 103 cluster.Spec.SSH.Port = strconv.Itoa(int(arg.Port)) 104 cluster.Spec.SSH.Pk = arg.Pk 105 cluster.Spec.SSH.PkPasswd = arg.PkPassword 106 107 c, err := k8s.Newk8sClient() 108 if err != nil { 109 return nil, fmt.Errorf("generate clusterfile failed, %s", err) 110 } 111 112 all, err := c.ListNodes() 113 if err != nil { 114 return nil, fmt.Errorf("generate clusterfile failed, %s", err) 115 } 116 for _, n := range all.Items { 117 for _, v := range n.Status.Addresses { 118 if _, ok := n.Labels[masterLabel]; ok { 119 if v.Type == v1.NodeInternalIP { 120 masterip = append(masterip, v.Address) 121 } 122 } else if v.Type == v1.NodeInternalIP { 123 nodeip = append(nodeip, v.Address) 124 } 125 } 126 } 127 128 masterHosts := v2.Host{ 129 IPS: masterip, 130 Roles: []string{common.MASTER}, 131 } 132 133 nodeHosts := v2.Host{ 134 IPS: nodeip, 135 Roles: []string{common.NODE}, 136 } 137 138 cluster.Spec.Hosts = append(cluster.Spec.Hosts, masterHosts, nodeHosts) 139 return cluster, nil 140 } 141 142 func (g *GenerateProcessor) MountRootfs(cluster *v2.Cluster) error { 143 fs, err := filesystem.NewFilesystem(common.DefaultTheClusterRootfsDir(cluster.Name)) 144 if err != nil { 145 return err 146 } 147 hosts := append(cluster.GetMasterIPList(), cluster.GetNodeIPList()...) 148 regConfig := runtime.GetRegistryConfig(common.DefaultTheClusterRootfsDir(cluster.Name), cluster.GetMaster0IP()) 149 if utils.NotInIPList(regConfig.IP, hosts) { 150 hosts = append(hosts, regConfig.IP) 151 } 152 return fs.MountRootfs(cluster, hosts, false) 153 } 154 155 func (g *GenerateProcessor) MountImage(cluster *v2.Cluster) error { 156 platsMap, err := ssh.GetClusterPlatform(cluster) 157 if err != nil { 158 return err 159 } 160 plats := []*apiv1.Platform{platform.GetDefaultPlatform()} 161 for _, v := range platsMap { 162 plat := v 163 plats = append(plats, &plat) 164 } 165 err = g.ImageManager.PullIfNotExist(cluster.Spec.Image, plats) 166 if err != nil { 167 return err 168 } 169 if err = g.ImageMounter.MountImage(cluster); err != nil { 170 return err 171 } 172 runt, err := runtime.NewDefaultRuntime(cluster, nil) 173 if err != nil { 174 return err 175 } 176 g.Runtime = runt.(*runtime.KubeadmRuntime) 177 return nil 178 } 179 180 func (g *GenerateProcessor) UnmountImage(cluster *v2.Cluster) error { 181 return g.ImageMounter.UnMountImage(cluster) 182 } 183 184 func (g *GenerateProcessor) ApplyRegistry(cluster *v2.Cluster) error { 185 runt, err := runtime.NewDefaultRuntime(cluster, nil) 186 if err != nil { 187 return err 188 } 189 err = runt.(*runtime.KubeadmRuntime).GenerateRegistryCert() 190 if err != nil { 191 return err 192 } 193 return g.Runtime.ApplyRegistry() 194 }