github.com/redhat-appstudio/e2e-tests@v0.0.0-20240520140907-9709f6f59323/pkg/clients/common/service_account.go (about)

     1  package common
     2  
     3  import (
     4  	"context"
     5  
     6  	corev1 "k8s.io/api/core/v1"
     7  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     8  	"k8s.io/apimachinery/pkg/util/wait"
     9  	"sigs.k8s.io/controller-runtime/pkg/client"
    10  )
    11  
    12  func (s *SuiteController) GetServiceAccount(saName, namespace string) (*corev1.ServiceAccount, error) {
    13  	return s.KubeInterface().CoreV1().ServiceAccounts(namespace).Get(context.Background(), saName, metav1.GetOptions{})
    14  }
    15  
    16  func (s *SuiteController) ServiceAccountPresent(saName, namespace string) wait.ConditionFunc {
    17  	return func() (bool, error) {
    18  		_, err := s.GetServiceAccount(saName, namespace)
    19  		if err != nil {
    20  			return false, nil
    21  		}
    22  		return true, nil
    23  	}
    24  }
    25  
    26  // CreateServiceAccount creates a service account with the provided name and namespace using the given list of secrets.
    27  func (s *SuiteController) CreateServiceAccount(name, namespace string, serviceAccountSecretList []corev1.ObjectReference, labels map[string]string) (*corev1.ServiceAccount, error) {
    28  	serviceAccount := &corev1.ServiceAccount{
    29  		ObjectMeta: metav1.ObjectMeta{
    30  			Name:      name,
    31  			Namespace: namespace,
    32  			Labels:    labels,
    33  		},
    34  		Secrets: serviceAccountSecretList,
    35  	}
    36  	return s.KubeInterface().CoreV1().ServiceAccounts(namespace).Create(context.Background(), serviceAccount, metav1.CreateOptions{})
    37  }
    38  
    39  // DeleteAllServiceAccountsInASpecificNamespace deletes all ServiceAccount from a given namespace
    40  func (h *SuiteController) DeleteAllServiceAccountsInASpecificNamespace(namespace string) error {
    41  	return h.KubeRest().DeleteAllOf(context.Background(), &corev1.ServiceAccount{}, client.InNamespace(namespace))
    42  }