Skip to content

fix(vendor/scoped): bump k8s version to 1.24, go version to 1.18 and fix scoped client #2794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 17, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions pkg/lib/scoped/token_retriever.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -50,29 +49,45 @@ func (r *BearerTokenRetriever) Retrieve(reference *corev1.ObjectReference) (toke
}

func getAPISecret(logger logrus.FieldLogger, kubeclient operatorclient.ClientInterface, sa *corev1.ServiceAccount) (APISecret *corev1.Secret, err error) {
for _, ref := range sa.Secrets {
// corev1.ObjectReference only has Name populated.
secret, getErr := kubeclient.KubernetesInterface().CoreV1().Secrets(sa.GetNamespace()).Get(context.TODO(), ref.Name, metav1.GetOptions{})
if getErr != nil {
if apierrors.IsNotFound(getErr) {
logger.Warnf("skipping secret %s - %v", ref.Name, getErr)
continue
}
seList, err := kubeclient.KubernetesInterface().CoreV1().Secrets(sa.GetNamespace()).List(context.TODO(), metav1.ListOptions{})
if err != nil {
logger.Errorf("unable to retrieve list of secrets in the namespace %s - %v", sa.GetNamespace(), err)
return nil, err
}
secrets := filterSecretsBySAName(sa.Name, seList)

err = getErr
break
for _, ref := range sa.Secrets {
if _, ok := secrets[ref.Name]; !ok {
logger.Warnf("skipping secret %s: secret not found", ref.Name)
continue
}
}
Comment on lines +59 to +64
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're iterating over the list of SA secrets, purely for logging purposes, here? Is that right?


for _, secret := range secrets {
// Validate that this is a token for API access.
if !IsServiceAccountToken(secret, sa) {
logger.Warnf("skipping secret %s - %v", ref.Name, getErr)
logger.Warnf("skipping secret %s: not token secret", secret.Name)
continue
}

// The first eligible secret that has an API access token is returned.
APISecret = secret
break
}

return
}

// filterSecretsBySAName returna a maps of secrets that are associated with a
// specific ServiceAccount via annotations kubernetes.io/service-account.name
func filterSecretsBySAName(saName string, secrets *corev1.SecretList) map[string]*corev1.Secret {
secretMap := make(map[string]*corev1.Secret)
for _, ref := range secrets.Items {
annotations := ref.GetAnnotations()
value := annotations[corev1.ServiceAccountNameKey]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to nil check the annotations map here before indexing for the SA name key, or do maps handle this instruction fine?

if value == saName {
secretMap[ref.Name] = &ref
}
}

return secretMap
}