Skip to content

Commit 12e7368

Browse files
Merge pull request #378 from erikgb/target-reconciler
refactor: establish target.Reconciler
2 parents b02e2b0 + f3ddd4a commit 12e7368

File tree

7 files changed

+118
-106
lines changed

7 files changed

+118
-106
lines changed

pkg/bundle/bundle.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737

3838
trustapi "github.com/cert-manager/trust-manager/pkg/apis/trust/v1alpha1"
3939
"github.com/cert-manager/trust-manager/pkg/bundle/internal/ssa_client"
40+
"github.com/cert-manager/trust-manager/pkg/bundle/internal/target"
4041
"github.com/cert-manager/trust-manager/pkg/fspkg"
4142
)
4243

@@ -67,10 +68,6 @@ type bundle struct {
6768
// a cache-backed Kubernetes client
6869
client client.Client
6970

70-
// targetCache is a cache.Cache that holds cached ConfigMap and Secret
71-
// resources that are used as targets for Bundles.
72-
targetCache client.Reader
73-
7471
// defaultPackage holds the loaded 'default' certificate package, if one was specified
7572
// at startup.
7673
defaultPackage *fspkg.Package
@@ -84,9 +81,7 @@ type bundle struct {
8481
// Options holds options for the Bundle controller.
8582
Options
8683

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

9287
// Reconcile is the top level function for reconciling over synced Bundles.
@@ -253,7 +248,7 @@ func (b *bundle) reconcileBundle(ctx context.Context, req ctrl.Request) (result
253248
Kind: string(kind),
254249
},
255250
}
256-
err := b.targetCache.List(ctx, targetList, &client.ListOptions{
251+
err := b.targetReconciler.Cache.List(ctx, targetList, &client.ListOptions{
257252
LabelSelector: labels.SelectorFromSet(map[string]string{
258253
trustapi.BundleLabelKey: bundle.Name,
259254
}),
@@ -303,12 +298,12 @@ func (b *bundle) reconcileBundle(ctx context.Context, req ctrl.Request) (result
303298

304299
if target.Kind == configMapTarget {
305300
syncFunc = func(targetLog logr.Logger, target targetResource, shouldExist bool) (bool, error) {
306-
return b.syncConfigMapTarget(ctx, targetLog, &bundle, target.NamespacedName, resolvedBundle.targetData, shouldExist)
301+
return b.targetReconciler.SyncConfigMap(ctx, targetLog, &bundle, target.NamespacedName, resolvedBundle.Data, shouldExist)
307302
}
308303
}
309304
if target.Kind == secretTarget {
310305
syncFunc = func(targetLog logr.Logger, target targetResource, shouldExist bool) (bool, error) {
311-
return b.syncSecretTarget(ctx, targetLog, &bundle, target.NamespacedName, resolvedBundle.targetData, shouldExist)
306+
return b.targetReconciler.SyncSecret(ctx, targetLog, &bundle, target.NamespacedName, resolvedBundle.Data, shouldExist)
312307
}
313308
}
314309

pkg/bundle/bundle_test.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141

4242
trustapi "github.com/cert-manager/trust-manager/pkg/apis/trust/v1alpha1"
4343
"github.com/cert-manager/trust-manager/pkg/bundle/internal/ssa_client"
44+
"github.com/cert-manager/trust-manager/pkg/bundle/internal/target"
4445
"github.com/cert-manager/trust-manager/pkg/bundle/internal/truststore"
4546
"github.com/cert-manager/trust-manager/pkg/fspkg"
4647
"github.com/cert-manager/trust-manager/pkg/util"
@@ -1312,22 +1313,25 @@ func Test_Reconcile(t *testing.T) {
13121313

13131314
log, ctx := ktesting.NewTestContext(t)
13141315
b := &bundle{
1315-
client: fakeClient,
1316-
targetCache: fakeClient,
1317-
recorder: fakeRecorder,
1318-
clock: fixedclock,
1316+
client: fakeClient,
1317+
recorder: fakeRecorder,
1318+
clock: fixedclock,
13191319
Options: Options{
13201320
Log: log,
13211321
Namespace: trustNamespace,
13221322
SecretTargetsEnabled: !test.disableSecretTargets,
13231323
FilterExpiredCerts: true,
13241324
},
1325-
patchResourceOverwrite: func(ctx context.Context, obj interface{}) error {
1326-
logMutex.Lock()
1327-
defer logMutex.Unlock()
1325+
targetReconciler: &target.Reconciler{
1326+
Client: fakeClient,
1327+
Cache: fakeClient,
1328+
PatchResourceOverwrite: func(ctx context.Context, obj interface{}) error {
1329+
logMutex.Lock()
1330+
defer logMutex.Unlock()
13281331

1329-
resourcePatches = append(resourcePatches, obj)
1330-
return nil
1332+
resourcePatches = append(resourcePatches, obj)
1333+
return nil
1334+
},
13311335
},
13321336
}
13331337

pkg/bundle/controller.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"sigs.k8s.io/controller-runtime/pkg/source"
3838

3939
trustapi "github.com/cert-manager/trust-manager/pkg/apis/trust/v1alpha1"
40+
"github.com/cert-manager/trust-manager/pkg/bundle/internal/target"
4041
"github.com/cert-manager/trust-manager/pkg/fspkg"
4142
)
4243

@@ -52,11 +53,14 @@ func AddBundleController(
5253
targetCache cache.Cache,
5354
) error {
5455
b := &bundle{
55-
client: mgr.GetClient(),
56-
targetCache: targetCache,
57-
recorder: mgr.GetEventRecorderFor("bundles"),
58-
clock: clock.RealClock{},
59-
Options: opts,
56+
client: mgr.GetClient(),
57+
recorder: mgr.GetEventRecorderFor("bundles"),
58+
clock: clock.RealClock{},
59+
Options: opts,
60+
targetReconciler: &target.Reconciler{
61+
Client: mgr.GetClient(),
62+
Cache: targetCache,
63+
},
6064
}
6165

6266
if b.Options.DefaultPackageLocation != "" {

0 commit comments

Comments
 (0)