@@ -51,6 +51,8 @@ import (
5151
5252 eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1"
5353 "github.com/fluxcd/pkg/apis/meta"
54+ "github.com/fluxcd/pkg/auth"
55+ "github.com/fluxcd/pkg/cache"
5456 "github.com/fluxcd/pkg/oci"
5557 "github.com/fluxcd/pkg/runtime/conditions"
5658 helper "github.com/fluxcd/pkg/runtime/controller"
@@ -141,6 +143,7 @@ type OCIRepositoryReconciler struct {
141143
142144 Storage * Storage
143145 ControllerName string
146+ TokenCache * cache.TokenCache
144147 requeueDependency time.Duration
145148
146149 patchOptions []patch.Option
@@ -328,7 +331,7 @@ func (r *OCIRepositoryReconciler) reconcile(ctx context.Context, sp *patch.Seria
328331// If this fails, it records v1beta2.FetchFailedCondition=True on the object and returns early.
329332func (r * OCIRepositoryReconciler ) reconcileSource (ctx context.Context , sp * patch.SerialPatcher ,
330333 obj * ociv1.OCIRepository , metadata * sourcev1.Artifact , dir string ) (sreconcile.Result , error ) {
331- var auth authn.Authenticator
334+ var authenticator authn.Authenticator
332335
333336 ctxTimeout , cancel := context .WithTimeout (ctx , obj .Spec .Timeout .Duration )
334337 defer cancel ()
@@ -363,8 +366,28 @@ func (r *OCIRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
363366 }
364367
365368 if _ , ok := keychain .(soci.Anonymous ); obj .Spec .Provider != ociv1 .GenericOCIProvider && ok {
369+ var opts []auth.Option
370+ if obj .Spec .ServiceAccountName != "" {
371+ serviceAccount := client.ObjectKey {
372+ Name : obj .Spec .ServiceAccountName ,
373+ Namespace : obj .GetNamespace (),
374+ }
375+ opts = append (opts , auth .WithServiceAccount (serviceAccount , r .Client ))
376+ }
377+ if r .TokenCache != nil {
378+ involvedObject := cache.InvolvedObject {
379+ Kind : ociv1 .OCIRepositoryKind ,
380+ Name : obj .GetName (),
381+ Namespace : obj .GetNamespace (),
382+ Operation : cache .OperationReconcile ,
383+ }
384+ opts = append (opts , auth .WithCache (* r .TokenCache , involvedObject ))
385+ }
386+ if proxyURL != nil {
387+ opts = append (opts , auth .WithProxyURL (* proxyURL ))
388+ }
366389 var authErr error
367- auth , authErr = soci .OIDCAuth (ctxTimeout , obj .Spec .URL , obj .Spec .Provider , proxyURL )
390+ authenticator , authErr = soci .OIDCAuth (ctxTimeout , obj .Spec .URL , obj .Spec .Provider , opts ... )
368391 if authErr != nil && ! errors .Is (authErr , oci .ErrUnconfiguredProvider ) {
369392 e := serror .NewGeneric (
370393 fmt .Errorf ("failed to get credential from %s: %w" , obj .Spec .Provider , authErr ),
@@ -386,7 +409,7 @@ func (r *OCIRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
386409 return sreconcile .ResultEmpty , e
387410 }
388411
389- opts := makeRemoteOptions (ctx , transport , keychain , auth )
412+ opts := makeRemoteOptions (ctx , transport , keychain , authenticator )
390413
391414 // Determine which artifact revision to pull
392415 ref , err := r .getArtifactRef (obj , opts )
@@ -446,7 +469,7 @@ func (r *OCIRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
446469 conditions .GetObservedGeneration (obj , sourcev1 .SourceVerifiedCondition ) != obj .Generation ||
447470 conditions .IsFalse (obj , sourcev1 .SourceVerifiedCondition ) {
448471
449- result , err := r .verifySignature (ctx , obj , ref , keychain , auth , transport , opts ... )
472+ result , err := r .verifySignature (ctx , obj , ref , keychain , authenticator , transport , opts ... )
450473 if err != nil {
451474 provider := obj .Spec .Verify .Provider
452475 if obj .Spec .Verify .SecretRef == nil && obj .Spec .Verify .Provider == "cosign" {
@@ -1225,6 +1248,10 @@ func (r *OCIRepositoryReconciler) reconcileDelete(ctx context.Context, obj *ociv
12251248 // Remove our finalizer from the list
12261249 controllerutil .RemoveFinalizer (obj , sourcev1 .SourceFinalizer )
12271250
1251+ // Cleanup caches.
1252+ r .TokenCache .DeleteEventsForObject (ociv1 .OCIRepositoryKind ,
1253+ obj .GetName (), obj .GetNamespace (), cache .OperationReconcile )
1254+
12281255 // Stop reconciliation as the object is being deleted
12291256 return sreconcile .ResultEmpty , nil
12301257}
0 commit comments