Skip to content

Commit f20a8ee

Browse files
committed
Optimize getBundlePEM
This was the top allocation on the heap. See if we can optimize it a bit Jira: OSPRH-17235
1 parent dfbf3b7 commit f20a8ee

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

pkg/openstack/ca.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"math"
1111
"os"
12+
"strings"
1213
"time"
1314

1415
certmgrv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
@@ -458,6 +459,15 @@ func ReconcileCAs(ctx context.Context, instance *corev1.OpenStackControlPlane, h
458459
return ctrl.Result{}, err
459460
}
460461

462+
caBundlePEM, err := bundle.getBundlePEM()
463+
if err != nil {
464+
return ctrl.Result{}, fmt.Errorf("failed to create CA bundle PEM: %w", err)
465+
}
466+
caOnlyBundlePEM, err := caOnlyBundle.getBundlePEM()
467+
if err != nil {
468+
return ctrl.Result{}, fmt.Errorf("failed to create CA only bundle PEM: %w", err)
469+
}
470+
461471
saSecretTemplate := []util.Template{
462472
{
463473
Name: tls.CABundleSecret,
@@ -471,8 +481,8 @@ func ReconcileCAs(ctx context.Context, instance *corev1.OpenStackControlPlane, h
471481
},
472482
ConfigOptions: nil,
473483
CustomData: map[string]string{
474-
tls.CABundleKey: bundle.getBundlePEM(),
475-
tls.InternalCABundleKey: caOnlyBundle.getBundlePEM(),
484+
tls.CABundleKey: caBundlePEM,
485+
tls.InternalCABundleKey: caOnlyBundlePEM,
476486
},
477487
SkipSetOwner: true, // TODO: (mschuppert) instead add e.g. keystoneapi to secret to prevent keystoneapi on cleanup to switch to not ready
478488
},
@@ -796,16 +806,25 @@ func (cab *caBundle) getCertsFromPEM(PEMdata []byte) error {
796806
return nil
797807
}
798808

799-
// Create PEM bundle from certificates
800-
func (cab *caBundle) getBundlePEM() string {
801-
var bundleData string
809+
func (cab *caBundle) getBundlePEM() (string, error) {
810+
var b strings.Builder
802811

803812
for _, cert := range cab.certs {
804-
bundleData += "# " + cert.cert.Issuer.CommonName + "\n" +
805-
string(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: cert.cert.Raw}))
813+
b.WriteString("# ")
814+
b.WriteString(cert.cert.Issuer.CommonName)
815+
b.WriteByte('\n')
816+
817+
block := &pem.Block{
818+
Type: "CERTIFICATE",
819+
Bytes: cert.cert.Raw,
820+
}
821+
822+
if err := pem.Encode(&b, block); err != nil {
823+
return "", fmt.Errorf("failed to encode bundle PEM for %w", err)
824+
}
806825
}
807826

808-
return bundleData
827+
return b.String(), nil
809828
}
810829

811830
func addIssuerLabelAnnotation(

0 commit comments

Comments
 (0)