Skip to content

Commit

Permalink
Output logs every 15 seconds if the certs don't yet exist in the cont…
Browse files Browse the repository at this point in the history
…ainer

Signed-off-by: Yuki Iwai <yuki.iwai.tz@gmail.com>
  • Loading branch information
tenzen-y committed Aug 15, 2023
1 parent 3410d0f commit 9d85d60
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
37 changes: 26 additions & 11 deletions pkg/certgenerator/v1beta1/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (c *CertGenerator) Start(ctx context.Context) error {
Jitter: 1,
Steps: 10,
Cap: time.Minute * 5,
}, ensureCertMounted); err != nil {
}, ensureCertMounted(time.Now())); err != nil {
return err
}
// Sending an empty data to a certsReady means it starts to register controllers to the manager.
Expand All @@ -86,17 +86,32 @@ func (c *CertGenerator) Start(ctx context.Context) error {
}

// ensureCertMounted ensures that the generated certs are mounted inside the container.
func ensureCertMounted(context.Context) (bool, error) {
certFile := filepath.Join(consts.CertDir, serverCertName)
if _, err := os.Stat(certFile); err != nil {
return false, nil
}
keyFile := filepath.Join(consts.CertDir, serverKeyName)
if _, err := os.Stat(keyFile); err != nil {
return false, nil
func ensureCertMounted(start time.Time) func(context.Context) (bool, error) {
return func(ctx context.Context) (bool, error) {
now := time.Now()
outputLog := false
if now.Sub(start) >= 15*time.Second {
start = now
outputLog = true
}

certFile := filepath.Join(consts.CertDir, serverCertName)
if _, err := os.Stat(certFile); err != nil {
if outputLog {
klog.Infof("Public key file %q doesn't exist in the container yet", certFile)
}
return false, nil
}
keyFile := filepath.Join(consts.CertDir, serverKeyName)
if _, err := os.Stat(keyFile); err != nil {
if outputLog {
klog.Infof("Private key file %q doesn't exist in the container yet", keyFile)
}
return false, nil
}
klog.Info("Succeeded to be mounted certs inside the container.")
return true, nil
}
klog.Info("Succeeded to be mounted certs inside the container.")
return true, nil
}

func (c *CertGenerator) NeedLeaderElection() bool {
Expand Down
4 changes: 3 additions & 1 deletion pkg/certgenerator/v1beta1/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"path/filepath"
"strings"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
Expand Down Expand Up @@ -263,7 +264,8 @@ func TestEnsureCertMounted(t *testing.T) {
t.Fatalf("Failed to create tls.crt: %v", err)
}
}
got, _ := ensureCertMounted(context.Background())
ensureFunc := ensureCertMounted(time.Now())
got, _ := ensureFunc(context.Background())
if tc.wantExist != got {
t.Errorf("Unexpected value from ensureCertMounted: \n(want: %v, got: %v)\n", tc.wantExist, got)
}
Expand Down

0 comments on commit 9d85d60

Please sign in to comment.