6
6
7
7
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
8
8
"github.com/sirupsen/logrus"
9
+ authv1 "k8s.io/api/authentication/v1"
9
10
corev1 "k8s.io/api/core/v1"
10
11
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11
12
)
@@ -36,7 +37,14 @@ func (r *BearerTokenRetriever) Retrieve(reference *corev1.ObjectReference) (toke
36
37
}
37
38
38
39
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
+ }
40
48
return
41
49
}
42
50
@@ -48,6 +56,20 @@ func (r *BearerTokenRetriever) Retrieve(reference *corev1.ObjectReference) (toke
48
56
return
49
57
}
50
58
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
+
51
73
func getAPISecret (logger logrus.FieldLogger , kubeclient operatorclient.ClientInterface , sa * corev1.ServiceAccount ) (APISecret * corev1.Secret , err error ) {
52
74
seList , err := kubeclient .KubernetesInterface ().CoreV1 ().Secrets (sa .GetNamespace ()).List (context .TODO (), metav1.ListOptions {})
53
75
if err != nil {
0 commit comments