Skip to content

Commit

Permalink
yurthub verify bootstrap token on start and do cleanup if necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
gnunu committed Nov 26, 2021
1 parent 76bd622 commit 3fa1ecd
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions pkg/yurthub/certificate/hubself/cert_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"path/filepath"
"strings"
"time"
"os"

certificates "k8s.io/api/certificates/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -116,8 +117,47 @@ func NewYurtHubCertManager(cfg *config.YurtHubConfiguration) (interfaces.YurtCer
return ycm, nil
}

func removeDirContents(dir string) error {
files, err := ioutil.ReadDir(dir)
if err != nil {
return err
}
for _, d := range files {
err = os.RemoveAll(filepath.Join(dir, d.Name()))
if err != nil {
return err
}
}
return nil
}

func (ycm *yurtHubCertManager) verifyOrCleanup() {
if len(ycm.joinToken) == 0 {
return
}

bcf := ycm.getBootstrapConfFile()
if existed, _ := util.FileExists(bcf); existed {
curKubeConfig, err := util.LoadKubeConfig(bcf)
if err == nil && curKubeConfig != nil {
if curKubeConfig.AuthInfos[bootstrapUser] != nil {
if curKubeConfig.AuthInfos[bootstrapUser].Token == ycm.joinToken {
klog.Infof("join token for %s bootstrap conf file is not changed", ycm.hubName)
return
}
}
}
}

klog.Infof("clean up any stale files")
removeDirContents(ycm.rootDir)
}

// Start init certificate manager and certs for hub agent
func (ycm *yurtHubCertManager) Start() {
// 0. verify, cleanup if needed
ycm.verifyOrCleanup()

// 1. create ca file for hub certificate manager
err := ycm.initCaCert()
if err != nil {
Expand Down

0 comments on commit 3fa1ecd

Please sign in to comment.