Skip to content

Commit 3f5431a

Browse files
clsvgkechhors
authored
K8SPSMDB-1276: Optimize TLS and CA certificate file writes in getMongoUri function (#1736)
* Optimize TLS and CA certificate file writes in getMongoUri function * extracted common logic to functions for checking and writing cert files --------- Co-authored-by: George Kechagias <geo.kechagias@gmail.com> Co-authored-by: Viacheslav Sarzhan <slava.sarzhan@percona.com>
1 parent e6f8cbe commit 3f5431a

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

pkg/psmdb/backup/pbm.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package backup
22

33
import (
4+
"bytes"
45
"context"
56
"fmt"
67
"net/url"
@@ -117,27 +118,45 @@ func getMongoUri(ctx context.Context, k8sclient client.Client, cr *api.PerconaSe
117118
return "", errors.Wrap(err, "get ssl secret")
118119
}
119120

121+
isCertFileOutdated := func(certData []byte, certFilePath string) (bool, error) {
122+
_, err := os.Stat(certFilePath)
123+
if os.IsNotExist(err) {
124+
return true, nil
125+
}
126+
127+
fileData, err := os.ReadFile(certFilePath)
128+
if err != nil {
129+
return true, err
130+
}
131+
132+
return !bytes.Equal(fileData, certData), nil
133+
}
134+
135+
writeCertFileIfOutdated := func(certData []byte, filePath string) error {
136+
if isCertOutdated, err := isCertFileOutdated(certData, filePath); err != nil {
137+
return err
138+
} else if isCertOutdated {
139+
return os.WriteFile(filePath, certData, 0o600)
140+
}
141+
return nil
142+
}
143+
120144
tlsKey := sslSecret.Data["tls.key"]
121145
tlsCert := sslSecret.Data["tls.crt"]
122146
tlsPemFile := fmt.Sprintf("/tmp/%s-%s-tls.pem", cr.Namespace, cr.Name)
123-
f, err := os.OpenFile(tlsPemFile, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0o600)
147+
tlsPem := append(tlsKey, tlsCert...)
148+
149+
err = writeCertFileIfOutdated(tlsPem, tlsPemFile)
124150
if err != nil {
125-
return "", errors.Wrapf(err, "open %s", tlsPemFile)
126-
}
127-
defer f.Close()
128-
if _, err := f.Write(append(tlsKey, tlsCert...)); err != nil {
129-
return "", errors.Wrapf(err, "write TLS key and certificate to %s", tlsPemFile)
151+
return "", errors.Wrapf(err, "error checking and writing TLS key and certificate to file %s", tlsPemFile)
130152
}
131153

132154
caCert := sslSecret.Data["ca.crt"]
133155
caCertFile := fmt.Sprintf("/tmp/%s-%s-ca.crt", cr.Namespace, cr.Name)
134-
f, err = os.OpenFile(caCertFile, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0o600)
156+
157+
err = writeCertFileIfOutdated(caCert, caCertFile)
135158
if err != nil {
136-
return "", errors.Wrapf(err, "open %s", caCertFile)
137-
}
138-
defer f.Close()
139-
if _, err := f.Write(caCert); err != nil {
140-
return "", errors.Wrapf(err, "write CA certificate to %s", caCertFile)
159+
return "", errors.Wrapf(err, "error checking and writing CA certificate to file %s", tlsPemFile)
141160
}
142161

143162
murl += fmt.Sprintf(

0 commit comments

Comments
 (0)