-
Notifications
You must be signed in to change notification settings - Fork 4k
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
[WIP] Add functionality to dynamically reload the VPA certs #3462
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,11 +17,24 @@ limitations under the License. | |
package main | ||
|
||
import ( | ||
"crypto/tls" | ||
"io/ioutil" | ||
"sync" | ||
|
||
"github.com/fsnotify/fsnotify" | ||
"k8s.io/klog" | ||
) | ||
|
||
// KeypairReloader structs holds cert path and certs | ||
type KeypairReloader struct { | ||
certMu sync.RWMutex | ||
cert *tls.Certificate | ||
caCert []byte | ||
certPath string | ||
keyPath string | ||
caPath string | ||
} | ||
|
||
type certsContainer struct { | ||
caCert, serverKey, serverCert []byte | ||
} | ||
|
@@ -41,10 +54,75 @@ func readFile(filePath string) []byte { | |
return res | ||
} | ||
|
||
func initCerts(config certsConfig) certsContainer { | ||
res := certsContainer{} | ||
res.caCert = readFile(*config.clientCaFile) | ||
res.serverCert = readFile(*config.tlsCertFile) | ||
res.serverKey = readFile(*config.tlsPrivateKey) | ||
return res | ||
// NewKeypairReloader will load certs on first run and trigger a goroutine for fsnotify watcher | ||
func NewKeypairReloader(config certsConfig) (*KeypairReloader, error) { | ||
result := &KeypairReloader{ | ||
certPath: *config.tlsCertFile, | ||
keyPath: *config.tlsPrivateKey, | ||
caCert: readFile(*config.clientCaFile), | ||
} | ||
cert, err := tls.LoadX509KeyPair(*config.tlsCertFile, *config.tlsPrivateKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
result.cert = &cert | ||
|
||
// creates a new file watcher | ||
watcher, err := fsnotify.NewWatcher() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer func() { | ||
if err != nil { | ||
watcher.Close() | ||
} | ||
}() | ||
|
||
if err := watcher.Add("/etc/tls-certs"); err != nil { | ||
return nil, err | ||
} | ||
|
||
go func() { | ||
for { | ||
select { | ||
// watch for events | ||
case event := <-watcher.Events: | ||
// fsnotify.create events will tell us if there are new certs | ||
if event.Op&fsnotify.Create == fsnotify.Create { | ||
klog.Info("Reloading certs") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add verbosity level to the log? And the log could be "New certificate found, reloading certs" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure 👍 |
||
if err := result.reload(); err != nil { | ||
klog.Infof("Could not load new certs: %v", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably be Warning |
||
} | ||
} | ||
|
||
// watch for errors | ||
case err := <-watcher.Errors: | ||
klog.Infof("error: %v", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need more descriptive log here. Probably also warning or error level. |
||
} | ||
} | ||
}() | ||
|
||
return result, nil | ||
} | ||
|
||
// reload loads updated cert and key whenever they are updated | ||
func (kpr *KeypairReloader) reload() error { | ||
newCert, err := tls.LoadX509KeyPair(kpr.certPath, kpr.keyPath) | ||
if err != nil { | ||
return err | ||
} | ||
kpr.certMu.Lock() | ||
defer kpr.certMu.Unlock() | ||
kpr.cert = &newCert | ||
return nil | ||
} | ||
|
||
// GetCertificateFunc will return function which will be used as tls.Config.GetCertificate | ||
func (kpr *KeypairReloader) GetCertificateFunc() func(*tls.ClientHelloInfo) (*tls.Certificate, error) { | ||
return func(clientHello *tls.ClientHelloInfo) (*tls.Certificate, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since clientHello is unused, you can replace it with underscore |
||
kpr.certMu.RLock() | ||
defer kpr.certMu.RUnlock() | ||
return kpr.cert, nil | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ limitations under the License. | |
package main | ||
|
||
import ( | ||
"crypto/tls" | ||
"flag" | ||
"fmt" | ||
|
||
|
@@ -78,7 +79,11 @@ func main() { | |
metrics.Initialize(*address, healthCheck) | ||
metrics_admission.Register() | ||
|
||
certs := initCerts(*certsConfiguration) | ||
// load certs | ||
kpr, err := NewKeypairReloader(*certsConfiguration) | ||
if err != nil { | ||
klog.Fatal(err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More info in the log needed, like "Failed to load certificate on startup: %v", err |
||
} | ||
|
||
config, err := rest.InClusterConfig() | ||
if err != nil { | ||
|
@@ -128,13 +133,16 @@ func main() { | |
}) | ||
clientset := getClient() | ||
server := &http.Server{ | ||
Addr: fmt.Sprintf(":%d", *port), | ||
TLSConfig: configTLS(clientset, certs.serverCert, certs.serverKey), | ||
Addr: fmt.Sprintf(":%d", *port), | ||
} | ||
// this will check if there are new certs before every tls handshake | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, this will return up-to-date cert before every handshake, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, that's correct, I've updated the comment :) |
||
t := &tls.Config{GetCertificate: kpr.GetCertificateFunc()} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. server.TLSConfig = &tls.Config{GetCertificate: kpr.GetCertificateFunc()} you can also do it above: One more thing is that the KeypairReloader could have a GetTLSConfig function and then we could simply have: server := &http.Server{ |
||
server.TLSConfig = t | ||
|
||
url := fmt.Sprintf("%v:%v", *webhookAddress, *webhookPort) | ||
go func() { | ||
if *registerWebhook { | ||
selfRegistration(clientset, certs.caCert, namespace, *serviceName, url, *registerByURL, int32(*webhookTimeout)) | ||
selfRegistration(clientset, kpr.caCert, namespace, *serviceName, url, *registerByURL, int32(*webhookTimeout)) | ||
} | ||
// Start status updates after the webhook is initialized. | ||
statusUpdater.Run(stopCh) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You probably don't want this hardcoded. You should get the directories of both the cert and key and set watches on them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 I think you want those as flags passed in to the KeyPairReloader
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, I will update it 👍