Skip to content

Commit ee128a7

Browse files
committed
Initial start on code migration
Signed-off-by: Erik Godding Boye <[email protected]>
1 parent 4817e2d commit ee128a7

File tree

4 files changed

+40
-42
lines changed

4 files changed

+40
-42
lines changed

pkg/bundle/bundle.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"slices"
2424
"strings"
2525

26+
trustmanagerapi "github.com/cert-manager/trust-manager/pkg/apis/trustmanager/v1alpha2"
2627
corev1 "k8s.io/api/core/v1"
2728
apierrors "k8s.io/apimachinery/pkg/api/errors"
2829
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -89,7 +90,7 @@ func (b *bundle) reconcileBundle(ctx context.Context, req ctrl.Request) (statusP
8990
ctx = logf.IntoContext(ctx, log)
9091
log.V(2).Info("syncing bundle")
9192

92-
var bundle trustapi.Bundle
93+
var bundle trustmanagerapi.ClusterBundle
9394
err := b.client.Get(ctx, req.NamespacedName, &bundle)
9495
if apierrors.IsNotFound(err) {
9596
log.V(2).Info("bundle no longer exists, ignoring")
@@ -172,7 +173,7 @@ func (b *bundle) reconcileBundle(ctx context.Context, req ctrl.Request) (statusP
172173

173174
targetResources := map[target.Resource]struct{}{}
174175

175-
namespaceSelector, err := b.bundleTargetNamespaceSelector(&bundle)
176+
namespaceSelector, err := metav1.LabelSelectorAsSelector((&bundle).Spec.Target.NamespaceSelector)
176177
if err != nil {
177178
b.recorder.Eventf(&bundle, corev1.EventTypeWarning, "NamespaceSelectorError", "Failed to build namespace match labels selector: %s", err)
178179
return nil, fmt.Errorf("failed to build NamespaceSelector: %w", err)
@@ -346,16 +347,3 @@ func (b *bundle) reconcileBundle(ctx context.Context, req ctrl.Request) (statusP
346347

347348
return statusPatch, nil
348349
}
349-
350-
func (b *bundle) bundleTargetNamespaceSelector(bundleObj *trustapi.Bundle) (labels.Selector, error) {
351-
nsSelector := bundleObj.Spec.Target.NamespaceSelector
352-
353-
// LabelSelectorAsSelector returns a Selector selecting nothing if LabelSelector is nil,
354-
// while our current default is to select everything. But this is subject to change.
355-
// See https://github.com/cert-manager/trust-manager/issues/39
356-
if nsSelector == nil {
357-
return labels.Everything(), nil
358-
}
359-
360-
return metav1.LabelSelectorAsSelector(nsSelector)
361-
}

pkg/bundle/controller.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"os"
2323

24+
trustmanagerapi "github.com/cert-manager/trust-manager/pkg/apis/trustmanager/v1alpha2"
2425
corev1 "k8s.io/api/core/v1"
2526
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2627
"k8s.io/apimachinery/pkg/labels"
@@ -205,8 +206,9 @@ func addBundleController(
205206
// Watch all Namespaces. Cache whole Namespaces to include Phase Status.
206207
// Reconcile all Bundles on a Namespace change.
207208
Watches(&corev1.Namespace{}, b.enqueueRequestsFromBundleFunc(
208-
func(obj client.Object, bundle trustapi.Bundle) bool {
209-
namespaceSelector, err := b.bundleTargetNamespaceSelector(&bundle)
209+
func(obj client.Object, bundle trustmanagerapi.ClusterBundle) bool {
210+
var r labels.Selector = metav1.LabelSelectorAsSelector((&bundle).Spec.Target.NamespaceSelector)
211+
namespaceSelector, err := r
210212
if err != nil {
211213
// We have an invalid selector, so we can skip this Bundle.
212214
return false
@@ -250,7 +252,7 @@ func addBundleController(
250252
// enqueueRequestsFromBundleFunc returns an event handler for watching Bundle dependants.
251253
// It will invoke the provided function for all Bundles and trigger a Bundle reconcile if the
252254
// functions returns true.
253-
func (b *bundle) enqueueRequestsFromBundleFunc(fn func(obj client.Object, bundle trustapi.Bundle) bool) handler.EventHandler {
255+
func (b *bundle) enqueueRequestsFromBundleFunc(fn func(obj client.Object, bundle trustmanagerapi.ClusterBundle) bool) handler.EventHandler {
254256
return handler.EnqueueRequestsFromMapFunc(
255257
func(ctx context.Context, obj client.Object) []reconcile.Request {
256258
// If an error happens here, and we do nothing, we run the risk of
@@ -273,8 +275,8 @@ func (b *bundle) enqueueRequestsFromBundleFunc(fn func(obj client.Object, bundle
273275

274276
// mustBundleList will return a BundleList of all Bundles in the cluster. If an
275277
// error occurs, will exit error the program.
276-
func (b *bundle) mustBundleList(ctx context.Context) *trustapi.BundleList {
277-
var bundleList trustapi.BundleList
278+
func (b *bundle) mustBundleList(ctx context.Context) *trustmanagerapi.ClusterBundleList {
279+
var bundleList trustmanagerapi.ClusterBundleList
278280
if err := b.client.List(ctx, &bundleList); err != nil {
279281
logf.FromContext(ctx).Error(err, "failed to list all Bundles, exiting error")
280282
os.Exit(-1)

pkg/bundle/controller/bundle_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type BundleReconciler struct {
4141
// SetupWithManager sets up the controller with the Manager.
4242
func (r *BundleReconciler) SetupWithManager(mgr ctrl.Manager) error {
4343
return ctrl.NewControllerManagedBy(mgr).
44-
For(&trustapi.Bundle{}).
44+
For(&trustapi.Bundle{}). //lint:ignore SA1019
4545
Owns(&trustmanagerapi.ClusterBundle{}).
4646
Complete(r)
4747
}

pkg/bundle/internal/source/source.go

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ import (
2020
"context"
2121
"fmt"
2222

23+
trustmanagerapi "github.com/cert-manager/trust-manager/pkg/apis/trustmanager/v1alpha2"
2324
corev1 "k8s.io/api/core/v1"
2425
apierrors "k8s.io/apimachinery/pkg/api/errors"
2526
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"k8s.io/utils/ptr"
2628
"sigs.k8s.io/controller-runtime/pkg/client"
2729
logf "sigs.k8s.io/controller-runtime/pkg/log"
2830

@@ -66,32 +68,38 @@ func (b *BundleBuilder) BuildBundle(ctx context.Context, sources []trustapi.Bund
6668
util.WithLogger(logf.FromContext(ctx).WithName("cert-pool")),
6769
)
6870

69-
for _, source := range sources {
71+
for _, source := range spec.Sources {
7072
var certSource bundleSource
7173

72-
switch {
73-
case source.ConfigMap != nil:
74-
certSource = &configMapBundleSource{b.Reader, b.Namespace, source.ConfigMap}
74+
switch source.Kind {
75+
case "ConfigMap":
76+
certSource = &configMapBundleSource{b.Reader, b.Namespace, source}
7577

76-
case source.Secret != nil:
77-
certSource = &secretBundleSource{b.Reader, b.Namespace, source.Secret}
78+
case "Secret":
79+
certSource = &secretBundleSource{b.Reader, b.Namespace, source}
7880

79-
case source.InLine != nil:
80-
certSource = &inlineBundleSource{*source.InLine}
81-
82-
case source.UseDefaultCAs != nil:
83-
if !*source.UseDefaultCAs {
84-
continue
85-
}
86-
if b.DefaultPackage == nil {
87-
return BundleData{}, NotFoundError{fmt.Errorf("no default package was specified when trust-manager was started; default CAs not available")}
88-
}
89-
certSource = &defaultCAsBundleSource{b.DefaultPackage.Bundle}
90-
resolvedBundle.DefaultCAPackageStringID = b.DefaultPackage.StringID()
9181
default:
92-
panic(fmt.Sprintf("don't know how to process source: %+v", source))
82+
panic(fmt.Sprintf("don't know how to process source of kind: %q", source.Kind))
83+
}
84+
85+
if err := certSource.addToCertPool(ctx, certPool); err != nil {
86+
return BundleData{}, err
9387
}
88+
}
89+
90+
if spec.InLineCAs != nil {
91+
certSource := &inlineBundleSource{*spec.InLineCAs}
92+
if err := certSource.addToCertPool(ctx, certPool); err != nil {
93+
return BundleData{}, err
94+
}
95+
}
9496

97+
if ptr.Deref(spec.IncludeDefaultCAs, false) {
98+
if b.DefaultPackage == nil {
99+
return BundleData{}, NotFoundError{fmt.Errorf("no default package was specified when trust-manager was started; default CAs not available")}
100+
}
101+
certSource := &defaultCAsBundleSource{b.DefaultPackage.Bundle}
102+
resolvedBundle.DefaultCAPackageStringID = b.DefaultPackage.StringID()
95103
if err := certSource.addToCertPool(ctx, resolvedBundle.CertPool); err != nil {
96104
return BundleData{}, err
97105
}
@@ -134,7 +142,7 @@ func (s defaultCAsBundleSource) addToCertPool(_ context.Context, pool *util.Cert
134142
type configMapBundleSource struct {
135143
client.Reader
136144
Namespace string
137-
ref *trustapi.SourceObjectKeySelector
145+
ref trustmanagerapi.BundleSource
138146
}
139147

140148
func (b configMapBundleSource) addToCertPool(ctx context.Context, pool *util.CertPool) error {
@@ -197,7 +205,7 @@ func (b configMapBundleSource) addToCertPool(ctx context.Context, pool *util.Cer
197205
type secretBundleSource struct {
198206
client.Reader
199207
Namespace string
200-
ref *trustapi.SourceObjectKeySelector
208+
ref trustmanagerapi.BundleSource
201209
}
202210

203211
func (b secretBundleSource) addToCertPool(ctx context.Context, pool *util.CertPool) error {

0 commit comments

Comments
 (0)