Skip to content

Commit

Permalink
bugfix: prepare yurthub server tls config panic (openyurtio#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
rambohe-ch authored Sep 9, 2021
1 parent 9fbecdc commit eef381f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
4 changes: 2 additions & 2 deletions pkg/yurthub/gc/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (m *GCManager) Run() {
go wait.JitterUntil(func() {
klog.V(2).Infof("start gc events after waiting %v from previous gc", time.Since(m.lastTime))
m.lastTime = time.Now()
cfg := m.restConfigManager.GetRestConfig()
cfg := m.restConfigManager.GetRestConfig(true)
if cfg == nil {
klog.Errorf("could not get rest config, so skip gc")
return
Expand All @@ -96,7 +96,7 @@ func (m *GCManager) gcPodsWhenRestart() error {
}
klog.Infof("list pod keys from storage, total: %d", len(localPodKeys))

cfg := m.restConfigManager.GetRestConfig()
cfg := m.restConfigManager.GetRestConfig(true)
if cfg == nil {
klog.Errorf("could not get rest config, so skip gc pods when restart")
return err
Expand Down
33 changes: 20 additions & 13 deletions pkg/yurthub/kubernetes/rest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,29 @@ func NewRestConfigManager(cfg *config.YurtHubConfiguration, certMgr interfaces.Y
}

// GetRestConfig gets rest client config according to the mode of certificateManager
func (rcm *RestConfigManager) GetRestConfig() *rest.Config {
func (rcm *RestConfigManager) GetRestConfig(needHealthyServer bool) *rest.Config {
certMgrMode := rcm.certMgrMode
switch certMgrMode {
case util.YurtHubCertificateManagerName:
return rcm.getHubselfRestConfig()
return rcm.getHubselfRestConfig(needHealthyServer)
case util.KubeletCertificateManagerName:
return rcm.getKubeletRestConfig(rcm.kubeletRootCAFilePath, rcm.kubeletPairFilePath)
return rcm.getKubeletRestConfig(rcm.kubeletRootCAFilePath, rcm.kubeletPairFilePath, needHealthyServer)
default:
return nil
}
}

// getKubeletRestConfig gets rest client config from kubelet.conf
func (rcm *RestConfigManager) getKubeletRestConfig(kubeletRootCAFilePath, kubeletPairFilePath string) *rest.Config {
healthyServer := rcm.getHealthyServer()
if healthyServer == nil {
klog.Infof("all of remote servers are unhealthy, so return nil for rest config")
return nil
func (rcm *RestConfigManager) getKubeletRestConfig(kubeletRootCAFilePath, kubeletPairFilePath string, needHealthyServer bool) *rest.Config {
healthyServer := rcm.remoteServers[0]
if needHealthyServer {
healthyServer = rcm.getHealthyServer()
if healthyServer == nil {
klog.Infof("all of remote servers are unhealthy, so return nil for rest config")
return nil
}
}

cfg, err := util.LoadKubeletRestClientConfig(healthyServer, kubeletRootCAFilePath, kubeletPairFilePath)
if err != nil {
klog.Errorf("could not load kubelet rest client config, %v", err)
Expand All @@ -80,11 +84,14 @@ func (rcm *RestConfigManager) getKubeletRestConfig(kubeletRootCAFilePath, kubele
}

// getHubselfRestConfig gets rest client config from hub agent conf file.
func (rcm *RestConfigManager) getHubselfRestConfig() *rest.Config {
healthyServer := rcm.getHealthyServer()
if healthyServer == nil {
klog.Infof("all of remote servers are unhealthy, so return nil for rest config")
return nil
func (rcm *RestConfigManager) getHubselfRestConfig(needHealthyServer bool) *rest.Config {
healthyServer := rcm.remoteServers[0]
if needHealthyServer {
healthyServer = rcm.getHealthyServer()
if healthyServer == nil {
klog.Infof("all of remote servers are unhealthy, so return nil for rest config")
return nil
}
}

// certificate expired, rest config can not be used to connect remote server,
Expand Down
2 changes: 1 addition & 1 deletion pkg/yurthub/kubernetes/rest/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func TestGetRestConfig(t *testing.T) {
}

var rc *rest.Config
rc = rcm.GetRestConfig()
rc = rcm.GetRestConfig(true)
if tt.mode == "hubself" {
if rc.Host != u.String() || rc.TLSClientConfig.CertFile != yurthubCurrent || rc.TLSClientConfig.KeyFile != yurthubCurrent {
t.Errorf("The information in rest.Config is not correct: %s", tt.mode)
Expand Down
7 changes: 6 additions & 1 deletion pkg/yurthub/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,12 @@ func healthz(w http.ResponseWriter, _ *http.Request) {
// create a certificate manager for the yurthub server and run the csr approver for both yurthub
// and generate a TLS configuration
func GenUseCertMgrAndTLSConfig(restConfigMgr *rest.RestConfigManager, certificateMgr interfaces.YurtCertificateManager, certDir, proxyServerSecureDummyAddr string, stopCh <-chan struct{}) (*tls.Config, error) {
clientSet, err := kubernetes.NewForConfig(restConfigMgr.GetRestConfig())
cfg := restConfigMgr.GetRestConfig(false)
if cfg == nil {
return nil, fmt.Errorf("failed to prepare rest config based ong hub agent client certificate")
}

clientSet, err := kubernetes.NewForConfig(cfg)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit eef381f

Please sign in to comment.