Skip to content

Commit e20778c

Browse files
authored
fix: call TokenRequest API when service account token secret is missing (#3377)
* fix: call TokenRequest API when service account token secret is missing Beyond Kubernetes 1.22, the service account token secret is not automatically, created. Therefore, when OLM is not able to find the service account token secret, it should request one from the k8s api server. Ref: https://kubernetes.io/docs/reference/access-authn-authz/service-accounts-admin/#manual-secret-management-for-serviceaccounts Signed-off-by: Edmund Ochieng <[email protected]> * fix: return error Signed-off-by: Edmund Ochieng <[email protected]> * handle error when creating sa token from TokenRequest API fails Signed-off-by: Edmund Ochieng <[email protected]> * move return from inner loop Move the return to line 48. This will ensure a value is returned whether we successully create a service account token from the TokenRequest API or get an error Signed-off-by: Edmund Ochieng <[email protected]> --------- Signed-off-by: Edmund Ochieng <[email protected]>
1 parent 183a7f2 commit e20778c

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

pkg/lib/scoped/token_retriever.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
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+
5173
func 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

Comments
 (0)