Skip to content

Commit

Permalink
refactor: dedicated target data struct
Browse files Browse the repository at this point in the history
Signed-off-by: Erik Godding Boye <[email protected]>
  • Loading branch information
erikgb committed Aug 15, 2024
1 parent b315a26 commit 25cfd99
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 34 deletions.
4 changes: 2 additions & 2 deletions pkg/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,12 @@ func (b *bundle) reconcileBundle(ctx context.Context, req ctrl.Request) (result

if target.Kind == configMapTarget {
syncFunc = func(targetLog logr.Logger, target targetResource, shouldExist bool) (bool, error) {
return b.syncConfigMapTarget(ctx, targetLog, &bundle, target.Name, target.Namespace, resolvedBundle, shouldExist)
return b.syncConfigMapTarget(ctx, targetLog, &bundle, target.Name, target.Namespace, resolvedBundle.targetData, shouldExist)
}
}
if target.Kind == secretTarget {
syncFunc = func(targetLog logr.Logger, target targetResource, shouldExist bool) (bool, error) {
return b.syncSecretTarget(ctx, targetLog, &bundle, target.Name, target.Namespace, resolvedBundle, shouldExist)
return b.syncSecretTarget(ctx, targetLog, &bundle, target.Name, target.Namespace, resolvedBundle.targetData, shouldExist)
}
}

Expand Down
29 changes: 1 addition & 28 deletions pkg/bundle/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

trustapi "github.com/cert-manager/trust-manager/pkg/apis/trust/v1alpha1"
"github.com/cert-manager/trust-manager/pkg/bundle/internal/truststore"
"github.com/cert-manager/trust-manager/pkg/util"
)

Expand All @@ -37,8 +36,7 @@ type notFoundError struct{ error }
// certificate data from concatenating all the sources together, binary data for any additional formats and
// any metadata from the sources which needs to be exposed on the Bundle resource's status field.
type bundleData struct {
data string
binaryData map[string][]byte
targetData

defaultCAPackageStringID string
}
Expand Down Expand Up @@ -193,28 +191,3 @@ func (b *bundle) secretBundle(ctx context.Context, ref *trustapi.SourceObjectKey
}
return results.String(), nil
}

func (b *bundleData) populateData(pool *util.CertPool, formats *trustapi.AdditionalFormats) error {
b.data = pool.PEM()

if formats != nil {
b.binaryData = make(map[string][]byte)

if formats.JKS != nil {
encoded, err := truststore.NewJKSEncoder(*formats.JKS.Password).Encode(pool)
if err != nil {
return fmt.Errorf("failed to encode JKS: %w", err)
}
b.binaryData[formats.JKS.Key] = encoded
}

if formats.PKCS12 != nil {
encoded, err := truststore.NewPKCS12Encoder(*formats.PKCS12.Password).Encode(pool)
if err != nil {
return fmt.Errorf("failed to encode PKCS12: %w", err)
}
b.binaryData[formats.PKCS12.Key] = encoded
}
}
return nil
}
36 changes: 34 additions & 2 deletions pkg/bundle/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (

trustapi "github.com/cert-manager/trust-manager/pkg/apis/trust/v1alpha1"
"github.com/cert-manager/trust-manager/pkg/bundle/internal/ssa_client"
"github.com/cert-manager/trust-manager/pkg/bundle/internal/truststore"
"github.com/cert-manager/trust-manager/pkg/util"
)

// syncConfigMapTarget syncs the given data to the target ConfigMap in the given namespace.
Expand All @@ -48,7 +50,7 @@ func (b *bundle) syncConfigMapTarget(
bundle *trustapi.Bundle,
name string,
namespace string,
resolvedBundle bundleData,
resolvedBundle targetData,
shouldExist bool,
) (bool, error) {
configMap := &metav1.PartialObjectMetadata{
Expand Down Expand Up @@ -154,7 +156,7 @@ func (b *bundle) syncSecretTarget(
bundle *trustapi.Bundle,
name string,
namespace string,
resolvedBundle bundleData,
resolvedBundle targetData,
shouldExist bool,
) (bool, error) {
secret := &metav1.PartialObjectMetadata{
Expand Down Expand Up @@ -394,3 +396,33 @@ func (b *bundle) patchSecretResource(ctx context.Context, applyConfig *coreapply

return nil
}

type targetData struct {
data string
binaryData map[string][]byte
}

func (b *targetData) populateData(pool *util.CertPool, formats *trustapi.AdditionalFormats) error {
b.data = pool.PEM()

if formats != nil {
b.binaryData = make(map[string][]byte)

if formats.JKS != nil {
encoded, err := truststore.NewJKSEncoder(*formats.JKS.Password).Encode(pool)
if err != nil {
return fmt.Errorf("failed to encode JKS: %w", err)
}
b.binaryData[formats.JKS.Key] = encoded
}

if formats.PKCS12 != nil {
encoded, err := truststore.NewPKCS12Encoder(*formats.PKCS12.Password).Encode(pool)
if err != nil {
return fmt.Errorf("failed to encode PKCS12: %w", err)
}
b.binaryData[formats.PKCS12.Key] = encoded
}
}
return nil
}
4 changes: 2 additions & 2 deletions pkg/bundle/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ func Test_syncConfigMapTarget(t *testing.T) {
AdditionalFormats: &trustapi.AdditionalFormats{},
},
}
resolvedBundle := bundleData{data: data, binaryData: make(map[string][]byte)}
resolvedBundle := targetData{data: data, binaryData: make(map[string][]byte)}
if test.withJKS {
spec.Target.AdditionalFormats.JKS = &trustapi.JKS{
KeySelector: trustapi.KeySelector{
Expand Down Expand Up @@ -1237,7 +1237,7 @@ func Test_syncSecretTarget(t *testing.T) {
AdditionalFormats: &trustapi.AdditionalFormats{},
},
}
resolvedBundle := bundleData{data: data, binaryData: make(map[string][]byte)}
resolvedBundle := targetData{data: data, binaryData: make(map[string][]byte)}
if test.withJKS {
spec.Target.AdditionalFormats.JKS = &trustapi.JKS{
KeySelector: trustapi.KeySelector{
Expand Down

0 comments on commit 25cfd99

Please sign in to comment.