Skip to content

Commit

Permalink
refactor: dedicated struct for build source
Browse files Browse the repository at this point in the history
Signed-off-by: Erik Godding Boye <[email protected]>
  • Loading branch information
erikgb committed Nov 17, 2024
1 parent 41dc93d commit e0a65ac
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
9 changes: 3 additions & 6 deletions pkg/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ 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/target"
"github.com/cert-manager/trust-manager/pkg/fspkg"
)

// Options hold options for the Bundle controller.
Expand Down Expand Up @@ -68,10 +67,6 @@ type bundle struct {
// a cache-backed Kubernetes client
client client.Client

// defaultPackage holds the loaded 'default' certificate package, if one was specified
// at startup.
defaultPackage *fspkg.Package

// recorder is used for create Kubernetes Events for reconciled Bundles.
recorder record.EventRecorder

Expand All @@ -81,6 +76,8 @@ type bundle struct {
// Options holds options for the Bundle controller.
Options

sourceDataBuilder *bundleDataBuilder

targetReconciler *target.Reconciler
}

Expand Down Expand Up @@ -135,7 +132,7 @@ func (b *bundle) reconcileBundle(ctx context.Context, req ctrl.Request) (result
statusPatch = &trustapi.BundleStatus{
DefaultCAPackageVersion: bundle.Status.DefaultCAPackageVersion,
}
resolvedBundle, err := b.buildSourceBundle(ctx, bundle.Spec.Sources, bundle.Spec.Target.AdditionalFormats)
resolvedBundle, err := b.sourceDataBuilder.buildSourceBundle(ctx, bundle.Spec.Sources, bundle.Spec.Target.AdditionalFormats)

// If any source is not found, update the Bundle status to an unready state.
if errors.As(err, &notFoundError{}) {
Expand Down
17 changes: 11 additions & 6 deletions pkg/bundle/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1311,15 +1311,20 @@ func Test_Reconcile(t *testing.T) {
)

log, ctx := ktesting.NewTestContext(t)
opts := Options{
Log: log,
Namespace: trustNamespace,
SecretTargetsEnabled: !test.disableSecretTargets,
FilterExpiredCerts: true,
}
b := &bundle{
client: fakeClient,
recorder: fakeRecorder,
clock: fixedclock,
Options: Options{
Log: log,
Namespace: trustNamespace,
SecretTargetsEnabled: !test.disableSecretTargets,
FilterExpiredCerts: true,
Options: opts,
sourceDataBuilder: &bundleDataBuilder{
client: fakeClient,
Options: opts,
},
targetReconciler: &target.Reconciler{
Client: fakeClient,
Expand All @@ -1335,7 +1340,7 @@ func Test_Reconcile(t *testing.T) {
}

if test.configureDefaultPackage {
b.defaultPackage = testDefaultPackage.Clone()
b.sourceDataBuilder.defaultPackage = testDefaultPackage.Clone()
}
resp, result, err := b.reconcileBundle(ctx, ctrl.Request{NamespacedName: types.NamespacedName{Name: bundleName}})
if (err != nil) != test.expError {
Expand Down
6 changes: 5 additions & 1 deletion pkg/bundle/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func AddBundleController(
recorder: mgr.GetEventRecorderFor("bundles"),
clock: clock.RealClock{},
Options: opts,
sourceDataBuilder: &bundleDataBuilder{
client: mgr.GetClient(),
Options: opts,
},
targetReconciler: &target.Reconciler{
Client: mgr.GetClient(),
Cache: targetCache,
Expand All @@ -69,7 +73,7 @@ func AddBundleController(
return fmt.Errorf("must load default package successfully when default package location is set: %w", err)
}

b.defaultPackage = &pkg
b.sourceDataBuilder.defaultPackage = &pkg

b.Options.Log.Info("successfully loaded default package from filesystem", "path", b.Options.DefaultPackageLocation)
}
Expand Down
19 changes: 16 additions & 3 deletions pkg/bundle/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

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

Expand All @@ -47,10 +48,22 @@ type bundleData struct {
defaultCAPackageStringID string
}

type bundleDataBuilder struct {
// a cache-backed Kubernetes client
client client.Client

// defaultPackage holds the loaded 'default' certificate package, if one was specified
// at startup.
defaultPackage *fspkg.Package

// Options holds options for the Bundle controller.
Options
}

// buildSourceBundle retrieves and concatenates all source bundle data for this Bundle object.
// Each source data is validated and pruned to ensure that all certificates within are valid, and
// is each bundle is concatenated together with a new line character.
func (b *bundle) buildSourceBundle(ctx context.Context, sources []trustapi.BundleSource, formats *trustapi.AdditionalFormats) (bundleData, error) {
func (b *bundleDataBuilder) buildSourceBundle(ctx context.Context, sources []trustapi.BundleSource, formats *trustapi.AdditionalFormats) (bundleData, error) {
var resolvedBundle bundleData
certPool := util.NewCertPool(util.WithFilteredExpiredCerts(b.FilterExpiredCerts))

Expand Down Expand Up @@ -111,7 +124,7 @@ func (b *bundle) buildSourceBundle(ctx context.Context, sources []trustapi.Bundl
}

// configMapBundle returns the data in the source ConfigMap within the trust Namespace.
func (b *bundle) configMapBundle(ctx context.Context, ref *trustapi.SourceObjectKeySelector) (string, error) {
func (b *bundleDataBuilder) configMapBundle(ctx context.Context, ref *trustapi.SourceObjectKeySelector) (string, error) {
// this slice will contain a single ConfigMap if we fetch by name
// or potentially multiple ConfigMaps if we fetch by label selector
var configMaps []corev1.ConfigMap
Expand Down Expand Up @@ -165,7 +178,7 @@ func (b *bundle) configMapBundle(ctx context.Context, ref *trustapi.SourceObject
}

// secretBundle returns the data in the source Secret within the trust Namespace.
func (b *bundle) secretBundle(ctx context.Context, ref *trustapi.SourceObjectKeySelector) (string, error) {
func (b *bundleDataBuilder) secretBundle(ctx context.Context, ref *trustapi.SourceObjectKeySelector) (string, error) {
// this slice will contain a single Secret if we fetch by name
// or potentially multiple Secrets if we fetch by label selector
var secrets []corev1.Secret
Expand Down
2 changes: 1 addition & 1 deletion pkg/bundle/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ func Test_buildSourceBundle(t *testing.T) {
WithScheme(trustapi.GlobalScheme).
Build()

b := &bundle{
b := &bundleDataBuilder{
client: fakeClient,
defaultPackage: &fspkg.Package{
Name: "testpkg",
Expand Down

0 comments on commit e0a65ac

Please sign in to comment.