/
sa.go
62 lines (51 loc) · 2.12 KB
/
sa.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Jackal Authors
// Package k8s provides a client for interacting with a Kubernetes cluster.
package k8s
import (
"context"
"fmt"
"time"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// GetAllServiceAccounts returns a list of services accounts for all namespaces.
func (k *K8s) GetAllServiceAccounts() (*corev1.ServiceAccountList, error) {
return k.GetServiceAccounts(corev1.NamespaceAll)
}
// GetServiceAccounts returns a list of service accounts in a given namespace.
func (k *K8s) GetServiceAccounts(namespace string) (*corev1.ServiceAccountList, error) {
metaOptions := metav1.ListOptions{}
return k.Clientset.CoreV1().ServiceAccounts(namespace).List(context.TODO(), metaOptions)
}
// GetServiceAccount returns a single service account by namespace and name.
func (k *K8s) GetServiceAccount(namespace, name string) (*corev1.ServiceAccount, error) {
metaOptions := metav1.GetOptions{}
return k.Clientset.CoreV1().ServiceAccounts(namespace).Get(context.TODO(), name, metaOptions)
}
// UpdateServiceAccount updates the given service account in the cluster.
func (k *K8s) UpdateServiceAccount(svcAccount *corev1.ServiceAccount) (*corev1.ServiceAccount, error) {
metaOptions := metav1.UpdateOptions{}
return k.Clientset.CoreV1().ServiceAccounts(svcAccount.Namespace).Update(context.TODO(), svcAccount, metaOptions)
}
// WaitForServiceAccount waits for a service account to be created in the cluster.
func (k *K8s) WaitForServiceAccount(ns, name string, timeout time.Duration) (*corev1.ServiceAccount, error) {
expired := time.After(timeout)
for {
select {
case <-expired:
return nil, fmt.Errorf("timed out waiting for service account %s/%s to exist", ns, name)
default:
sa, err := k.Clientset.CoreV1().ServiceAccounts(ns).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
time.Sleep(1 * time.Second)
continue
}
return nil, fmt.Errorf("error getting service account %s/%s: %w", ns, name, err)
}
return sa, nil
}
}
}