Merge pull request #9 from ibuildthecloud/master

Save secret to k8s on start
This commit is contained in:
Darren Shepherd 2019-11-10 03:52:54 +00:00 committed by GitHub
commit aaa5bc0d2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 5 deletions

View File

@ -68,7 +68,7 @@ func (l *listener) Accept() (net.Conn, error) {
return conn, err
}
addr := conn.RemoteAddr()
addr := conn.LocalAddr()
if addr == nil {
return conn, nil
}
@ -79,7 +79,11 @@ func (l *listener) Accept() (net.Conn, error) {
return conn, nil
}
return conn, l.updateCert(host)
if err := l.updateCert(host); err != nil {
logrus.Infof("failed to create TLS cert for: %s", host)
}
return conn, nil
}
func (l *listener) getCertificate(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {

View File

@ -73,6 +73,11 @@ func (s *storage) init(secrets v1controller.SecretController) {
return secret, nil
})
s.secrets = secrets
secret, err := s.storage.Get()
if err == nil && secret != nil {
s.saveInK8s(secret)
}
}
func (s *storage) Get() (*v1.Secret, error) {
@ -115,10 +120,10 @@ func (s *storage) saveInK8s(secret *v1.Secret) (*v1.Secret, error) {
targetSecret.Data = secret.Data
if targetSecret.UID == "" {
logrus.Infof("Creating new TLS secret for %v", targetSecret.Annotations)
logrus.Infof("Creating new TLS secret for %v (count: %d)", targetSecret.Name, len(targetSecret.Data)-1)
return s.secrets.Create(targetSecret)
} else {
logrus.Infof("Updating TLS secret for %v", targetSecret.Annotations)
logrus.Infof("Updating TLS secret for %v (count: %d)", targetSecret.Name, len(targetSecret.Data)-1)
return s.secrets.Update(targetSecret)
}
}
@ -127,7 +132,16 @@ func (s *storage) Update(secret *v1.Secret) (err error) {
s.Lock()
defer s.Unlock()
secret, err = s.saveInK8s(secret)
for i := 0; i < 3; i++ {
secret, err = s.saveInK8s(secret)
if errors.IsConflict(err) {
continue
} else if err != nil {
return err
}
break
}
if err != nil {
return err
}