Skip to content

Commit

Permalink
Refactor rbac.GetClientTokenSecret
Browse files Browse the repository at this point in the history
...to serviceaccount.GetTokenSecretFor as the function isn't really
related to RBAC. Plus we already have unit tests in serviceaccount
so we get code coverage credit :)

Signed-off-by: Tom Pantelis <tompantelis@gmail.com>
  • Loading branch information
tpantelis authored and dfarrell07 committed Aug 29, 2023
1 parent 006fcef commit 16bb828
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 58 deletions.
53 changes: 0 additions & 53 deletions internal/rbac/rbac.go

This file was deleted.

4 changes: 2 additions & 2 deletions pkg/broker/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"github.com/submariner-io/admiral/pkg/reporter"
"github.com/submariner-io/subctl/internal/component"
"github.com/submariner-io/subctl/internal/constants"
"github.com/submariner-io/subctl/internal/rbac"
"github.com/submariner-io/subctl/pkg/serviceaccount"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/utils/set"
Expand Down Expand Up @@ -61,7 +61,7 @@ func WriteInfoToFile(restConfig *rest.Config, brokerNamespace string, ipsecPSK [

data := &Info{}

data.ClientToken, err = rbac.GetClientTokenSecret(context.TODO(), kubeClient, brokerNamespace, constants.SubmarinerBrokerAdminSA)
data.ClientToken, err = serviceaccount.GetTokenSecretFor(context.TODO(), kubeClient, brokerNamespace, constants.SubmarinerBrokerAdminSA)
if err != nil {
return errors.Wrap(err, "error getting broker client secret")
}
Expand Down
29 changes: 26 additions & 3 deletions pkg/serviceaccount/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ import (
"github.com/pkg/errors"
"github.com/submariner-io/admiral/pkg/resource"
"github.com/submariner-io/admiral/pkg/util"
"github.com/submariner-io/subctl/internal/rbac"
"github.com/submariner-io/subctl/pkg/secret"
"github.com/submariner-io/submariner-operator/pkg/embeddedyamls"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
)
Expand Down Expand Up @@ -76,13 +77,13 @@ func EnsureFromYAML(ctx context.Context, kubeClient kubernetes.Interface, namesp
}

func EnsureTokenSecret(ctx context.Context, client kubernetes.Interface, namespace, saName string) (*corev1.Secret, error) {
saSecret, err := rbac.GetClientTokenSecret(ctx, client, namespace, saName)
saSecret, err := GetTokenSecretFor(ctx, client, namespace, saName)
if err == nil {
return saSecret, nil
}

if !apierrors.IsNotFound(err) {
return nil, err //nolint:wrapcheck // No need to wrap
return nil, err
}

newSecret := &corev1.Secret{
Expand Down Expand Up @@ -129,3 +130,25 @@ func EnsureTokenSecret(ctx context.Context, client kubernetes.Interface, namespa

return saSecret, err //nolint:wrapcheck // No need to wrap here
}

func GetTokenSecretFor(ctx context.Context, kubeClient kubernetes.Interface, namespace, serviceAccountName string,
) (*corev1.Secret, error) {
saSecrets, err := kubeClient.CoreV1().Secrets(namespace).List(ctx, metav1.ListOptions{
FieldSelector: fields.OneTermEqualSelector("type", string(corev1.SecretTypeServiceAccountToken)).String(),
})
if err != nil {
return nil, errors.Wrapf(err, "failed to list secrets of type %q in namespace %q",
corev1.SecretTypeServiceAccountToken, namespace)
}

for i := range saSecrets.Items {
if saSecrets.Items[i].Annotations[corev1.ServiceAccountNameKey] == serviceAccountName {
return &saSecrets.Items[i], nil
}
}

return nil, apierrors.NewNotFound(schema.GroupResource{
Group: corev1.SchemeGroupVersion.Group,
Resource: "secrets",
}, serviceAccountName)
}

0 comments on commit 16bb828

Please sign in to comment.