Skip to content

Commit

Permalink
refactor: establish target.Reconciler
Browse files Browse the repository at this point in the history
Signed-off-by: Erik Godding Boye <[email protected]>
  • Loading branch information
erikgb committed Sep 10, 2024
1 parent 12ed5f3 commit adf3daf
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 213 deletions.
15 changes: 5 additions & 10 deletions pkg/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ 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"
)

Expand Down Expand Up @@ -67,10 +68,6 @@ type bundle struct {
// a cache-backed Kubernetes client
client client.Client

// targetCache is a cache.Cache that holds cached ConfigMap and Secret
// resources that are used as targets for Bundles.
targetCache client.Reader

// defaultPackage holds the loaded 'default' certificate package, if one was specified
// at startup.
defaultPackage *fspkg.Package
Expand All @@ -84,9 +81,7 @@ type bundle struct {
// Options holds options for the Bundle controller.
Options

// patchResourceOverwrite allows use to override the patchResource function
// it is used for testing purposes
patchResourceOverwrite func(ctx context.Context, obj interface{}) error
targetReconciler *target.Reconciler
}

// Reconcile is the top level function for reconciling over synced Bundles.
Expand Down Expand Up @@ -253,7 +248,7 @@ func (b *bundle) reconcileBundle(ctx context.Context, req ctrl.Request) (result
Kind: string(kind),
},
}
err := b.targetCache.List(ctx, targetList, &client.ListOptions{
err := b.targetReconciler.Cache.List(ctx, targetList, &client.ListOptions{
LabelSelector: labels.SelectorFromSet(map[string]string{
trustapi.BundleLabelKey: bundle.Name,
}),
Expand Down Expand Up @@ -303,12 +298,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.NamespacedName, resolvedBundle.targetData, shouldExist)
return b.targetReconciler.SyncConfigMap(ctx, targetLog, &bundle, target.NamespacedName, resolvedBundle.Data, shouldExist)
}
}
if target.Kind == secretTarget {
syncFunc = func(targetLog logr.Logger, target targetResource, shouldExist bool) (bool, error) {
return b.syncSecretTarget(ctx, targetLog, &bundle, target.NamespacedName, resolvedBundle.targetData, shouldExist)
return b.targetReconciler.SyncSecret(ctx, targetLog, &bundle, target.NamespacedName, resolvedBundle.Data, shouldExist)
}
}

Expand Down
22 changes: 13 additions & 9 deletions pkg/bundle/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ 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/bundle/internal/truststore"
"github.com/cert-manager/trust-manager/pkg/fspkg"
"github.com/cert-manager/trust-manager/pkg/util"
Expand Down Expand Up @@ -1312,22 +1313,25 @@ func Test_Reconcile(t *testing.T) {

log, ctx := ktesting.NewTestContext(t)
b := &bundle{
client: fakeClient,
targetCache: fakeClient,
recorder: fakeRecorder,
clock: fixedclock,
client: fakeClient,
recorder: fakeRecorder,
clock: fixedclock,
Options: Options{
Log: log,
Namespace: trustNamespace,
SecretTargetsEnabled: !test.disableSecretTargets,
FilterExpiredCerts: true,
},
patchResourceOverwrite: func(ctx context.Context, obj interface{}) error {
logMutex.Lock()
defer logMutex.Unlock()
targetReconciler: &target.Reconciler{
Client: fakeClient,
Cache: fakeClient,
PatchResourceOverwrite: func(ctx context.Context, obj interface{}) error {
logMutex.Lock()
defer logMutex.Unlock()

resourcePatches = append(resourcePatches, obj)
return nil
resourcePatches = append(resourcePatches, obj)
return nil
},
},
}

Expand Down
14 changes: 9 additions & 5 deletions pkg/bundle/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/source"

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"
)

Expand All @@ -52,11 +53,14 @@ func AddBundleController(
targetCache cache.Cache,
) error {
b := &bundle{
client: mgr.GetClient(),
targetCache: targetCache,
recorder: mgr.GetEventRecorderFor("bundles"),
clock: clock.RealClock{},
Options: opts,
client: mgr.GetClient(),
recorder: mgr.GetEventRecorderFor("bundles"),
clock: clock.RealClock{},
Options: opts,
targetReconciler: &target.Reconciler{
Client: mgr.GetClient(),
Cache: targetCache,
},
}

if b.Options.DefaultPackageLocation != "" {
Expand Down
Loading

0 comments on commit adf3daf

Please sign in to comment.