11package authentication
22
33import (
4- "fmt"
54 "context"
65 "fmt"
7- "strings"
86 "sync"
97 "time"
108
119 authenticationv1 "k8s.io/api/authentication/v1"
1210 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1311 "k8s.io/apimachinery/pkg/types"
1412 corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
15- "k8s.io/apimachinery/pkg/api/errors"
1613 "k8s.io/utils/ptr"
1714)
1815
@@ -23,16 +20,16 @@ type TokenGetter struct {
2320 mu sync.RWMutex
2421}
2522
26- type TokenGetterOption func (* TokenGetter )
27-
2823type SANotFoundError struct {
2924 Msg string
3025}
3126
32- func (e * SANotFoundError ) Error () string {
33- return fmt .Sprintf (" %s" , e . Msg )
27+ func (e * SANotFoundError ) Error (serviceAccountName string ) string {
28+ return fmt .Sprintf (" Unable to authenticate with Kubernetes cluster using ServiceAccount \" %s \" : ServiceAccount \" %s \" not found." , serviceAccountName , serviceAccountName )
3429}
3530
31+ type TokenGetterOption func (* TokenGetter )
32+
3633const (
3734 rotationThresholdFraction = 0.1
3835 DefaultExpirationDuration = 5 * time .Minute
@@ -95,12 +92,10 @@ func (t *TokenGetter) getToken(ctx context.Context, key types.NamespacedName) (*
9592 req , err := t .client .ServiceAccounts (key .Namespace ).CreateToken (ctx ,
9693 key .Name ,
9794 & authenticationv1.TokenRequest {
98- Spec : authenticationv1.TokenRequestSpec {ExpirationSeconds : ptr .To (int64 (t .expirationDuration / time .Second ))},
95+ Spec : authenticationv1.TokenRequestSpec {ExpirationSeconds : ptr.To [ int64 ] (int64 (t .expirationDuration / time .Second ))},
9996 }, metav1.CreateOptions {})
10097 if err != nil {
101- errMsg := err .Error ()
102- stripErrMsg := errMsg [strings .LastIndex (errMsg , ":" )+ 1 :]
103- saErr := & SANotFoundError {stripErrMsg }
98+ saErr := & SANotFoundError {key .Name }
10499 return nil , saErr
105100 }
106101 return & req .Status , nil
0 commit comments