66
77 "github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
88 "github.com/sirupsen/logrus"
9+ authv1 "k8s.io/api/authentication/v1"
910 corev1 "k8s.io/api/core/v1"
1011 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1112)
@@ -36,7 +37,14 @@ func (r *BearerTokenRetriever) Retrieve(reference *corev1.ObjectReference) (toke
3637 }
3738
3839 if secret == nil {
39- err = fmt .Errorf ("the service account does not have any API secret sa=%s/%s" , sa .GetNamespace (), sa .GetName ())
40+ token , err = requestSAToken (r .kubeclient , sa )
41+ if err != nil {
42+ err = fmt .Errorf ("creating service account token from TokenRequest API for sa=%s/%s; %v" ,
43+ sa .GetNamespace (),
44+ sa .GetName (),
45+ err ,
46+ )
47+ }
4048 return
4149 }
4250
@@ -48,6 +56,20 @@ func (r *BearerTokenRetriever) Retrieve(reference *corev1.ObjectReference) (toke
4856 return
4957}
5058
59+ // requestSAToken requests for a service account token from the Kubernetes API server whenever the Operator
60+ // Lifecycle manager is unable to find a service account token secret
61+ func requestSAToken (kubeclient operatorclient.ClientInterface , sa * corev1.ServiceAccount ) (string , error ) {
62+ req := new (authv1.TokenRequest )
63+ req , err := kubeclient .KubernetesInterface ().
64+ CoreV1 ().ServiceAccounts (sa .GetNamespace ()).
65+ CreateToken (context .Background (), sa .GetName (), req , metav1.CreateOptions {})
66+ if err != nil {
67+ return "" , err
68+ }
69+
70+ return req .Status .Token , nil
71+ }
72+
5173func getAPISecret (logger logrus.FieldLogger , kubeclient operatorclient.ClientInterface , sa * corev1.ServiceAccount ) (APISecret * corev1.Secret , err error ) {
5274 seList , err := kubeclient .KubernetesInterface ().CoreV1 ().Secrets (sa .GetNamespace ()).List (context .TODO (), metav1.ListOptions {})
5375 if err != nil {
0 commit comments