github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/runtime/k0s/join_masters.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 "fmt" 19 "net" 20 "time" 21 22 "github.com/pkg/errors" 23 "github.com/sirupsen/logrus" 24 ) 25 26 const TimeForWaitingK0sStart = 10 27 28 func (k *Runtime) joinMasters(masters []net.IP, registryInfo string) error { 29 if len(masters) == 0 { 30 return nil 31 } 32 33 if err := k.initKube(masters); err != nil { 34 return err 35 } 36 37 if err := k.WaitSSHReady(6, masters...); err != nil { 38 return errors.Wrap(err, "join masters wait for ssh ready time out") 39 } 40 /**To join a node, following these steps. 41 STEP1: send private registry cert and add registry info into node 42 STEP2: copy k0s join token 43 STEP3: use k0s command to join node with master role. 44 STEP4: k0s create default config 45 STEP5: modify the private image repository field and so on in k0s config 46 STEP6: join node with token 47 STEP7: start the k0scontroller.service 48 */ 49 if err := k.CopyJoinToken(ControllerRole, masters); err != nil { 50 return err 51 } 52 cmds := k.JoinCommand(ControllerRole, registryInfo) 53 if cmds == nil { 54 return fmt.Errorf("failed to get join master command") 55 } 56 57 for _, master := range masters { 58 logrus.Infof("Start to join %s as master", master) 59 if err := k.infra.CmdAsync(master, nil, cmds...); err != nil { 60 return fmt.Errorf("failed to exec command(%s) on master(%s): %v", cmds, master, err) 61 } 62 if err := k.fetchKubeconfig(master); err != nil { 63 return fmt.Errorf("failed to fetch admin.conf on master(%s): %v", master, err) 64 } 65 logrus.Infof("Succeeded in joining %s as master", master) 66 } 67 return nil 68 } 69 70 func (k *Runtime) fetchKubeconfig(m net.IP) error { 71 logrus.Infof("waiting around 10 seconds for fetch k0s admin.conf") 72 // waiting k0s start success. 73 time.Sleep(time.Second * TimeForWaitingK0sStart) 74 if err := k.infra.CmdAsync(m, nil, "rm -rf .kube/config && mkdir -p /root/.kube && cp /var/lib/k0s/pki/admin.conf /root/.kube/config"); err != nil { 75 return err 76 } 77 return nil 78 }