@@ -663,6 +663,21 @@ func (r *GitRepositoryReconciler) getAuthOpts(ctx context.Context, obj *sourcev1
663663 getCreds = func () (* authutils.GitCredentials , error ) {
664664 var opts []auth.Option
665665
666+ if obj .Spec .ServiceAccountName != "" {
667+ // Check object-level workload identity feature gate.
668+ if ! auth .IsObjectLevelWorkloadIdentityEnabled () {
669+ const gate = auth .FeatureGateObjectLevelWorkloadIdentity
670+ const msgFmt = "to use spec.serviceAccountName for provider authentication please enable the %s feature gate in the controller"
671+ err := serror .NewStalling (fmt .Errorf (msgFmt , gate ), meta .FeatureGateDisabledReason )
672+ conditions .MarkTrue (obj , sourcev1 .FetchFailedCondition , meta .FeatureGateDisabledReason , "%s" , err )
673+ return nil , err
674+ }
675+ serviceAccount := client.ObjectKey {
676+ Name : obj .Spec .ServiceAccountName ,
677+ Namespace : obj .GetNamespace (),
678+ }
679+ opts = append (opts , auth .WithServiceAccount (serviceAccount , r .Client ))
680+ }
666681 if r .TokenCache != nil {
667682 involvedObject := cache.InvolvedObject {
668683 Kind : sourcev1 .GitRepositoryKind ,
@@ -742,6 +757,14 @@ func (r *GitRepositoryReconciler) getAuthOpts(ctx context.Context, obj *sourcev1
742757 if getCreds != nil {
743758 creds , err := getCreds ()
744759 if err != nil {
760+ // Check if it's already a structured error and preserve it
761+ if se , ok := err .(* serror.Stalling ); ok {
762+ return nil , se
763+ }
764+ if ge , ok := err .(* serror.Generic ); ok {
765+ return nil , ge
766+ }
767+
745768 e := serror .NewGeneric (
746769 fmt .Errorf ("failed to configure authentication options: %w" , err ),
747770 sourcev1 .AuthenticationFailedReason ,
0 commit comments