github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/runtime/k0s/init.go (about) 1 // Copyright © 2022 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 k0s 16 17 import ( 18 "context" 19 "fmt" 20 "net" 21 "path/filepath" 22 23 "github.com/sirupsen/logrus" 24 "golang.org/x/sync/errgroup" 25 ) 26 27 // generateConfigOnMaster0 generate the k0s.yaml to /etc/k0s/k0s.yaml and lead the controller node install 28 func (k *Runtime) generateConfigOnMaster0(master0 net.IP, registryInfo string) error { 29 if err := k.generateK0sConfig(master0); err != nil { 30 return fmt.Errorf("failed to generate config: %v", err) 31 } 32 logrus.Infof("k0s config created under /etc/k0s") 33 34 if err := k.modifyConfigRepo(master0, registryInfo); err != nil { 35 return fmt.Errorf("failed to modify config to private repository: %s", err) 36 } 37 return nil 38 } 39 40 // GenerateCert generate k0s join token for 100 years. 41 func (k *Runtime) generateJoinToken(master0 net.IP) error { 42 workerTokenCreateCMD := fmt.Sprintf("k0s token create --role=%s --expiry=876000h > %s", WorkerRole, DefaultK0sWorkerJoin) 43 controllerTokenCreateCMD := fmt.Sprintf("k0s token create --role=%s --expiry=876000h > %s", ControllerRole, DefaultK0sControllerJoin) 44 return k.infra.CmdAsync(master0, nil, workerTokenCreateCMD, controllerTokenCreateCMD) 45 } 46 47 func (k *Runtime) generateK0sConfig(master0 net.IP) error { 48 mkdirCMD := "mkdir -p /etc/k0s" 49 if _, err := k.infra.Cmd(master0, nil, mkdirCMD); err != nil { 50 return err 51 } 52 53 configCreateCMD := fmt.Sprintf("k0s config create > %s", DefaultK0sConfigPath) 54 if _, err := k.infra.Cmd(master0, nil, configCreateCMD); err != nil { 55 return err 56 } 57 58 return nil 59 } 60 61 func (k *Runtime) modifyConfigRepo(master0 net.IP, registryInfo string) error { 62 addRepoCMD := fmt.Sprintf("sed -i '/ images/ a\\ repository: %s' %s", registryInfo, DefaultK0sConfigPath) 63 _, err := k.infra.Cmd(master0, nil, addRepoCMD) 64 if err != nil { 65 return err 66 } 67 return nil 68 } 69 70 func (k *Runtime) bootstrapMaster0(master0 net.IP) error { 71 bootstrapCMD := fmt.Sprintf("k0s install controller -c %s --cri-socket %s", DefaultK0sConfigPath, ExternalCRIAddress) 72 if _, err := k.infra.Cmd(master0, nil, bootstrapCMD); err != nil { 73 return err 74 } 75 76 startSvcCMD := "k0s start" 77 if _, err := k.infra.Cmd(master0, nil, startSvcCMD); err != nil { 78 return err 79 } 80 81 if err := k.WaitK0sReady(master0); err != nil { 82 return err 83 } 84 // fetch kubeconfig 85 if _, err := k.infra.Cmd(master0, nil, "rm -rf .kube/config && mkdir -p /root/.kube && cp /var/lib/k0s/pki/admin.conf /root/.kube/config"); err != nil { 86 return err 87 } 88 logrus.Infof("k0s start successfully on master0") 89 return nil 90 } 91 92 // initKube prepare install environment. 93 func (k *Runtime) initKube(hosts []net.IP) error { 94 initKubeletCmd := fmt.Sprintf("cd %s && export RegistryURL=%s && bash %s", filepath.Join(k.infra.GetClusterRootfsPath(), "scripts"), k.registryInfo.URL, "init-kube.sh") 95 eg, _ := errgroup.WithContext(context.Background()) 96 for _, h := range hosts { 97 host := h 98 eg.Go(func() error { 99 if err := k.infra.CmdAsync(host, nil, initKubeletCmd); err != nil { 100 return fmt.Errorf("failed to init Kubelet Service on (%s): %s", host, err.Error()) 101 } 102 return nil 103 }) 104 } 105 if err := eg.Wait(); err != nil { 106 return err 107 } 108 return nil 109 }