Skip to content

Commit 44829ce

Browse files
refactor: rename and export patchSecret func (#3377)
* refactor: rename and export patchSecret func * Update pkg/certs/rotate.go Co-authored-by: Florian MEDJA <[email protected]> --------- Co-authored-by: Florian MEDJA <[email protected]> (cherry picked from commit 39a49e0)
1 parent 7685b1a commit 44829ce

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

pkg/certs/rotate.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package certs
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"io"
78
"io/fs"
@@ -98,8 +99,8 @@ func Rotate(ctx context.Context,
9899
return nil
99100
}
100101

101-
// Patch the secret so in case of a restart without persistence we don't loose data.
102-
return patchSecret(ctx, vConfig.HostNamespace, CertSecretName(vConfig.Name), pkiPath, vConfig.HostClient)
102+
// Sync the secret so in case of a restart without persistence we don't loose data.
103+
return SyncSecret(ctx, vConfig.HostNamespace, CertSecretName(vConfig.Name), pkiPath, vConfig.HostClient)
103104
}
104105

105106
func backupDirectory(src, dst string) error {
@@ -204,7 +205,10 @@ func excludeSAFiles(name string) bool {
204205
return false
205206
}
206207

207-
func patchSecret(ctx context.Context, secretNamespace, secretName, pkiPath string, client kubernetes.Interface) error {
208+
// SyncSecret patches the certs secret by bringing it in sync with the content of the PKI directory.
209+
// The PKI directory is the source of truth here. Meaning, new or updated certs/keys will be created or updated in the secret.
210+
// Deleted certs/keys will not be added to the secret.
211+
func SyncSecret(ctx context.Context, secretNamespace, secretName, pkiPath string, client kubernetes.Interface) error {
208212
secret, err := client.CoreV1().Secrets(secretNamespace).Get(ctx, secretName, metav1.GetOptions{})
209213
if err != nil {
210214
return fmt.Errorf("getting cert secret %s: %w", secretName, err)
@@ -214,7 +218,11 @@ func patchSecret(ctx context.Context, secretNamespace, secretName, pkiPath strin
214218
for k, v := range certMap {
215219
d, err := os.ReadFile(filepath.Join(pkiPath, k))
216220
if err != nil {
217-
return fmt.Errorf("reading file %s: %w", k, err)
221+
if !errors.Is(err, os.ErrNotExist) {
222+
return fmt.Errorf("reading file %s: %w", filepath.Join(pkiPath, k), err)
223+
}
224+
// If the cert/key referenced in certMap does not exist in the PKI directory, don't add it to the secret.
225+
continue
218226
}
219227

220228
data[v] = d

0 commit comments

Comments
 (0)