github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/apply/processor/create.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 processor 16 17 import ( 18 "fmt" 19 20 v1 "github.com/alibaba/sealer/types/api/v1" 21 "github.com/alibaba/sealer/utils/ssh" 22 23 "github.com/alibaba/sealer/utils/platform" 24 25 "github.com/alibaba/sealer/pkg/clusterfile" 26 "github.com/alibaba/sealer/pkg/config" 27 "github.com/alibaba/sealer/pkg/filesystem" 28 "github.com/alibaba/sealer/pkg/filesystem/cloudimage" 29 "github.com/alibaba/sealer/pkg/guest" 30 "github.com/alibaba/sealer/pkg/image" 31 "github.com/alibaba/sealer/pkg/plugin" 32 "github.com/alibaba/sealer/pkg/runtime" 33 v2 "github.com/alibaba/sealer/types/api/v2" 34 "github.com/alibaba/sealer/utils" 35 ) 36 37 type CreateProcessor struct { 38 ClusterFile clusterfile.Interface 39 ImageManager image.Service 40 cloudImageMounter cloudimage.Interface 41 Runtime runtime.Interface 42 Guest guest.Interface 43 Config config.Interface 44 Plugins plugin.Plugins 45 } 46 47 func (c *CreateProcessor) GetPipeLine() ([]func(cluster *v2.Cluster) error, error) { 48 var todoList []func(cluster *v2.Cluster) error 49 todoList = append(todoList, 50 c.MountImage, 51 c.PreProcess, 52 c.GetPhasePluginFunc(plugin.PhaseOriginally), 53 c.RunConfig, 54 c.MountRootfs, 55 c.GetPhasePluginFunc(plugin.PhasePreInit), 56 c.Init, 57 c.Join, 58 c.GetPhasePluginFunc(plugin.PhasePreGuest), 59 c.RunGuest, 60 c.UnMountImage, 61 c.GetPhasePluginFunc(plugin.PhasePostInstall), 62 ) 63 return todoList, nil 64 } 65 66 func (c *CreateProcessor) PreProcess(cluster *v2.Cluster) error { 67 c.Config = config.NewConfiguration(cluster) 68 c.initPlugin(cluster) 69 return utils.SaveClusterInfoToFile(cluster, cluster.Name) 70 } 71 72 func (c *CreateProcessor) initPlugin(cluster *v2.Cluster) { 73 c.Plugins = plugin.NewPlugins(cluster, c.ClusterFile.GetPlugins()) 74 } 75 76 func (c *CreateProcessor) MountImage(cluster *v2.Cluster) error { 77 platsMap, err := ssh.GetClusterPlatform(cluster) 78 if err != nil { 79 return err 80 } 81 plats := []*v1.Platform{platform.GetDefaultPlatform()} 82 for _, v := range platsMap { 83 plat := v 84 plats = append(plats, &plat) 85 } 86 if err = c.ImageManager.PullIfNotExist(cluster.Spec.Image, plats); err != nil { 87 return err 88 } 89 if err = c.cloudImageMounter.MountImage(cluster); err != nil { 90 return err 91 } 92 runTime, err := runtime.NewDefaultRuntime(cluster, c.ClusterFile.GetKubeadmConfig()) 93 if err != nil { 94 return fmt.Errorf("failed to init runtime, %v", err) 95 } 96 c.Runtime = runTime 97 return nil 98 } 99 100 func (c *CreateProcessor) RunConfig(cluster *v2.Cluster) error { 101 return c.Config.Dump(c.ClusterFile.GetConfigs()) 102 } 103 104 func (c *CreateProcessor) MountRootfs(cluster *v2.Cluster) error { 105 hosts := append(cluster.GetMasterIPList(), cluster.GetNodeIPList()...) 106 regConfig := runtime.GetRegistryConfig(platform.DefaultMountCloudImageDir(cluster.Name), cluster.GetMaster0IP()) 107 if utils.NotInIPList(regConfig.IP, hosts) { 108 hosts = append(hosts, regConfig.IP) 109 } 110 111 fs, err := filesystem.NewFilesystem(platform.DefaultMountCloudImageDir(cluster.Name)) 112 if err != nil { 113 return err 114 } 115 116 return fs.MountRootfs(cluster, hosts, true) 117 } 118 119 func (c *CreateProcessor) Init(cluster *v2.Cluster) error { 120 return c.Runtime.Init(cluster) 121 } 122 123 func (c *CreateProcessor) Join(cluster *v2.Cluster) error { 124 err := c.Runtime.JoinMasters(cluster.GetMasterIPList()[1:]) 125 if err != nil { 126 return err 127 } 128 err = c.Runtime.JoinNodes(cluster.GetNodeIPList()) 129 if err != nil { 130 return err 131 } 132 return utils.SaveClusterInfoToFile(cluster, cluster.Name) 133 } 134 135 func (c *CreateProcessor) RunGuest(cluster *v2.Cluster) error { 136 return c.Guest.Apply(cluster) 137 } 138 func (c *CreateProcessor) UnMountImage(cluster *v2.Cluster) error { 139 return c.cloudImageMounter.UnMountImage(cluster) 140 } 141 142 func (c *CreateProcessor) GetPhasePluginFunc(phase plugin.Phase) func(cluster *v2.Cluster) error { 143 return func(cluster *v2.Cluster) error { 144 if phase == plugin.PhasePreInit { 145 if err := c.Plugins.Load(); err != nil { 146 return err 147 } 148 } 149 return c.Plugins.Run(cluster.GetAllIPList(), phase) 150 } 151 } 152 153 func NewCreateProcessor(clusterFile clusterfile.Interface) (Processor, error) { 154 imgSvc, err := image.NewImageService() 155 if err != nil { 156 return nil, err 157 } 158 159 mounter, err := filesystem.NewCloudImageMounter() 160 if err != nil { 161 return nil, err 162 } 163 164 gs, err := guest.NewGuestManager() 165 if err != nil { 166 return nil, err 167 } 168 169 return &CreateProcessor{ 170 ClusterFile: clusterFile, 171 ImageManager: imgSvc, 172 cloudImageMounter: mounter, 173 Guest: gs, 174 }, nil 175 }