Skip to content

Commit 538bba6

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 7e93488 commit 538bba6

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"
@@ -117,8 +118,8 @@ func Rotate(ctx context.Context,
117118
return nil
118119
}
119120

120-
// Patch the secret so in case of a restart without persistence we don't loose data.
121-
return patchSecret(ctx, vConfig.HostNamespace, CertSecretName(vConfig.Name), pkiPath, vConfig.HostClient)
121+
// Sync the secret so in case of a restart without persistence we don't loose data.
122+
return SyncSecret(ctx, vConfig.HostNamespace, CertSecretName(vConfig.Name), pkiPath, vConfig.HostClient)
122123
}
123124

124125
func backupDirectory(src, dst string) error {
@@ -223,7 +224,10 @@ func excludeSAFiles(name string) bool {
223224
return false
224225
}
225226

226-
func patchSecret(ctx context.Context, secretNamespace, secretName, pkiPath string, client kubernetes.Interface) error {
227+
// SyncSecret patches the certs secret by bringing it in sync with the content of the PKI directory.
228+
// The PKI directory is the source of truth here. Meaning, new or updated certs/keys will be created or updated in the secret.
229+
// Deleted certs/keys will not be added to the secret.
230+
func SyncSecret(ctx context.Context, secretNamespace, secretName, pkiPath string, client kubernetes.Interface) error {
227231
secret, err := client.CoreV1().Secrets(secretNamespace).Get(ctx, secretName, metav1.GetOptions{})
228232
if err != nil {
229233
return fmt.Errorf("getting cert secret %s: %w", secretName, err)
@@ -233,7 +237,11 @@ func patchSecret(ctx context.Context, secretNamespace, secretName, pkiPath strin
233237
for k, v := range certMap {
234238
d, err := os.ReadFile(filepath.Join(pkiPath, k))
235239
if err != nil {
236-
return fmt.Errorf("reading file %s: %w", k, err)
240+
if !errors.Is(err, os.ErrNotExist) {
241+
return fmt.Errorf("reading file %s: %w", filepath.Join(pkiPath, k), err)
242+
}
243+
// If the cert/key referenced in certMap does not exist in the PKI directory, don't add it to the secret.
244+
continue
237245
}
238246

239247
data[v] = d

0 commit comments

Comments
 (0)