forked from titansoft-pte-ltd/imagepullsecret-patcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_account.go
35 lines (29 loc) · 851 Bytes
/
service_account.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
package main
import (
"encoding/json"
corev1 "k8s.io/api/core/v1"
)
const (
defaultServiceAccountName = "default"
)
func includeImagePullSecret(sa *corev1.ServiceAccount, secretName string) bool {
for _, imagePullSecret := range sa.ImagePullSecrets {
if imagePullSecret.Name == secretName {
return true
}
}
return false
}
type patch struct {
ImagePullSecrets []corev1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
}
func getPatchString(sa *corev1.ServiceAccount, secretName string) ([]byte, error) {
saPatch := patch{
// copy the slice
ImagePullSecrets: append([]corev1.LocalObjectReference(nil), sa.ImagePullSecrets...),
}
if !includeImagePullSecret(sa, secretName) {
saPatch.ImagePullSecrets = append(saPatch.ImagePullSecrets, corev1.LocalObjectReference{Name: secretName})
}
return json.Marshal(saPatch)
}