-
Notifications
You must be signed in to change notification settings - Fork 20
/
serviceAccountHelper.go
60 lines (53 loc) · 1.57 KB
/
serviceAccountHelper.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
package k8s
import (
"time"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type serviceAccountHelper struct {
factory ClientFactory
cache *v1.ServiceAccount
}
//newServiceAccountHelper creates ServiceAccountManager
func newServiceAccountHelper(factory ClientFactory, cache *v1.ServiceAccount) *serviceAccountHelper {
return &serviceAccountHelper{
factory: factory,
cache: cache,
}
}
// Reload performs an update of the cached service account resource object
// via the underlying client.
func (a *serviceAccountHelper) Reload() error {
client := a.factory.CoreV1().ServiceAccounts(a.cache.GetNamespace())
storedObj, err := client.Get(a.cache.GetName(), metav1.GetOptions{})
if err != nil {
return err
}
a.cache = storedObj
return nil
}
// GetServiceAccountSecretNameRepeat returns the name of the default-token of the service account
func (a *serviceAccountHelper) GetServiceAccountSecretNameRepeat() string {
duration, _ := time.ParseDuration("100ms")
for {
result := a.GetServiceAccountSecretName()
if result != "" {
return result
}
time.Sleep(duration)
a.Reload()
}
}
// GetServiceAccountSecretName returns the name of the default-token of the service account
func (a *serviceAccountHelper) GetServiceAccountSecretName() string {
for _, secretRef := range a.cache.Secrets {
client := a.factory.CoreV1().Secrets(a.cache.GetNamespace())
secret, err := client.Get(secretRef.Name, metav1.GetOptions{})
if err == nil &&
secret != nil &&
secret.Type == v1.SecretTypeServiceAccountToken {
return secret.Name
}
}
return ""
}