Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to the VPA admission-controller to reload it's certificate v2 #6665

Merged
merged 6 commits into from
Jun 25, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions vertical-pod-autoscaler/pkg/admission-controller/certs.go
Original file line number Diff line number Diff line change
@@ -42,14 +42,14 @@ func readFile(filePath string) []byte {
return res
}

type CertReloader struct {
type certReloader struct {
tlsCertPath string
tlsKeyPath string
cert *tls.Certificate
mu sync.RWMutex
}

func (cr *CertReloader) Start(stop <-chan struct{}) error {
func (cr *certReloader) start(stop <-chan struct{}) error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
@@ -68,7 +68,7 @@ func (cr *CertReloader) Start(stop <-chan struct{}) error {
case event := <-watcher.Events:
if event.Has(fsnotify.Create) || event.Has(fsnotify.Write) {
klog.V(2).Info("New certificate found, reloading")
if err := cr.Load(); err != nil {
if err := cr.load(); err != nil {
klog.Errorf("Failed to reload certificate: %s", err)
}
}
@@ -82,7 +82,7 @@ func (cr *CertReloader) Start(stop <-chan struct{}) error {
return nil
}

func (cr *CertReloader) Load() error {
func (cr *certReloader) load() error {
cert, err := tls.LoadX509KeyPair(cr.tlsCertPath, cr.tlsKeyPath)
if err != nil {
return err
@@ -93,7 +93,7 @@ func (cr *CertReloader) Load() error {
return nil
}

func (cr *CertReloader) GetCertificate(_ *tls.ClientHelloInfo) (*tls.Certificate, error) {
func (cr *certReloader) getCertificate(_ *tls.ClientHelloInfo) (*tls.Certificate, error) {
kwiesmueller marked this conversation as resolved.
Show resolved Hide resolved
cr.mu.RLock()
defer cr.mu.RUnlock()
return cr.cert, nil
Original file line number Diff line number Diff line change
@@ -122,7 +122,7 @@ func TestKeypairReloader(t *testing.T) {
t.Error(err)
}

reloader := CertReloader{
reloader := certReloader{
tlsCertPath: certPath,
tlsKeyPath: keyPath,
}
@@ -131,7 +131,7 @@ func TestKeypairReloader(t *testing.T) {
}
stop := make(chan struct{})
defer close(stop)
if err = reloader.Start(stop); err != nil {
if err = reloader.start(stop); err != nil {
t.Error(err)
}

@@ -143,7 +143,7 @@ func TestKeypairReloader(t *testing.T) {
t.Error(err)
}
time.Sleep(40 * time.Millisecond)
Nuckal777 marked this conversation as resolved.
Show resolved Hide resolved
tlsCert, err := reloader.GetCertificate(nil)
tlsCert, err := reloader.getCertificate(nil)
if err != nil {
t.Error(err)
}
8 changes: 4 additions & 4 deletions vertical-pod-autoscaler/pkg/admission-controller/config.go
Original file line number Diff line number Diff line change
@@ -67,17 +67,17 @@ func configTLS(cfg certsConfig, minTlsVersion, ciphers string, stop <-chan struc
CipherSuites: ciphersuites,
}
if *cfg.reload {
cr := CertReloader{
cr := certReloader{
tlsCertPath: *cfg.tlsCertFile,
tlsKeyPath: *cfg.tlsPrivateKey,
}
if err := cr.Load(); err != nil {
if err := cr.load(); err != nil {
klog.Fatal(err)
}
if err := cr.Start(stop); err != nil {
if err := cr.start(stop); err != nil {
klog.Fatal(err)
}
config.GetCertificate = cr.GetCertificate
config.GetCertificate = cr.getCertificate
} else {
cert, err := tls.LoadX509KeyPair(*cfg.tlsCertFile, *cfg.tlsPrivateKey)
if err != nil {