Skip to content

Commit

Permalink
add unit test for poolcoordinator cert manager (#1140)
Browse files Browse the repository at this point in the history
Signed-off-by: Congrool <chpzhangyifei@zju.edu.cn>
  • Loading branch information
Congrool authored Jan 12, 2023
1 parent b4a47b9 commit 7964a5c
Show file tree
Hide file tree
Showing 2 changed files with 390 additions and 11 deletions.
19 changes: 8 additions & 11 deletions pkg/yurthub/poolcoordinator/certmanager/certmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,17 @@ var certFileNames = map[CertFileType]string{
YurthubClientKey: "pool-coordinator-yurthub-client.key",
}

func NewCertManager(caFilePath string, yurtClient kubernetes.Interface, informerFactory informers.SharedInformerFactory) (*CertManager, error) {
func NewCertManager(pkiDir string, yurtClient kubernetes.Interface, informerFactory informers.SharedInformerFactory) (*CertManager, error) {
store := fs.FileSystemOperator{}
dir, _ := filepath.Split(caFilePath)
if err := store.CreateDir(dir); err != nil && err != fs.ErrExists {
return nil, fmt.Errorf("failed to create dir %s, %v", dir, err)
if err := store.CreateDir(pkiDir); err != nil && err != fs.ErrExists {
return nil, fmt.Errorf("failed to create dir %s, %v", pkiDir, err)
}

certMgr := &CertManager{
pkiDir: dir,
pkiDir: pkiDir,
store: store,
}

// try to use last cert files when restart.
certPath, keyPath := certMgr.GetFilePath(YurthubClientCert), certMgr.GetFilePath(YurthubClientKey)
if cert, err := tls.LoadX509KeyPair(certPath, keyPath); err == nil {
certMgr.cert = &cert
}

secretInformerFunc := func(client kubernetes.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
tweakListOptions := func(options *metav1.ListOptions) {
options.FieldSelector = fields.Set{"metadata.name": constants.PoolCoordinatorClientSecretName}.String()
Expand Down Expand Up @@ -100,6 +93,9 @@ type CertManager struct {
pkiDir string
cert *tls.Certificate
store fs.FileSystemOperator

// Used for unit test.
secret *corev1.Secret
}

func (c *CertManager) Current() *tls.Certificate {
Expand Down Expand Up @@ -145,6 +141,7 @@ func (c *CertManager) updateCerts(secret *corev1.Secret) {
klog.Errorf("failed to update client cert, %v", err)
}
c.cert = &cert
c.secret = secret.DeepCopy()
}

func (c *CertManager) deleteCerts() {
Expand Down
Loading

0 comments on commit 7964a5c

Please sign in to comment.