Skip to content

Commit

Permalink
Inject Kubeclient
Browse files Browse the repository at this point in the history
Signed-off-by: Pierangelo Di Pilato <pierdipi@redhat.com>
  • Loading branch information
pierDipi committed May 22, 2024
1 parent f3edb9d commit a513f14
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
18 changes: 16 additions & 2 deletions pkg/apis/sources/v1/sinkbinding_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"strings"

"go.uber.org/zap"
"k8s.io/client-go/kubernetes"
corev1listers "k8s.io/client-go/listers/core/v1"
kubeclient "knative.dev/pkg/client/injection/kube/client"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -202,7 +202,7 @@ func (sb *SinkBinding) Do(ctx context.Context, ps *duckv1.WithPod) {
Version: SchemeGroupVersion.Version,
Kind: "SinkBinding",
}
bundles, err := eventingtls.PropagateTrustBundles(ctx, kubeclient.Get(ctx), GetTrustBundleConfigMapLister(ctx), gvk, sb)
bundles, err := eventingtls.PropagateTrustBundles(ctx, getKubeClient(ctx), GetTrustBundleConfigMapLister(ctx), gvk, sb)
if err != nil {
logging.FromContext(ctx).Errorw("Failed to propagate trust bundles", zap.Error(err))
}
Expand Down Expand Up @@ -328,6 +328,20 @@ func (sb *SinkBinding) Undo(ctx context.Context, ps *duckv1.WithPod) {
}
}

type kubeClientKey struct{}

func WithKubeClient(ctx context.Context, k kubernetes.Interface) context.Context {
return context.WithValue(ctx, kubeClientKey{}, k)
}

func getKubeClient(ctx context.Context) kubernetes.Interface {
k := ctx.Value(kubeClientKey{})
if k == nil {
return nil
}
return k.(kubernetes.Interface)
}

type configMapListerKey struct{}

func WithTrustBundleConfigMapLister(ctx context.Context, lister corev1listers.ConfigMapLister) context.Context {
Expand Down
3 changes: 2 additions & 1 deletion pkg/reconciler/sinkbinding/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,10 @@ func ListAll(ctx context.Context, handler cache.ResourceEventHandler) psbinding.

func WithContextFactory(ctx context.Context, lister corev1listers.ConfigMapLister, handler func(types.NamespacedName)) psbinding.BindableContext {
r := resolver.NewURIResolverFromTracker(ctx, tracker.New(handler, controller.GetTrackerLease(ctx)))
k := kubeclient.Get(ctx)

return func(ctx context.Context, b psbinding.Bindable) (context.Context, error) {
return v1.WithTrustBundleConfigMapLister(v1.WithURIResolver(ctx, r), lister), nil
return v1.WithKubeClient(v1.WithTrustBundleConfigMapLister(v1.WithURIResolver(ctx, r), lister), k), nil
}
}

Expand Down

0 comments on commit a513f14

Please sign in to comment.