forked from neutryno/imagepullsecret-serviceaccount-patcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
126 lines (110 loc) · 3.73 KB
/
main.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"context"
"encoding/json"
"flag"
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"os"
"strings"
"time"
)
type PrivateRegistrySecret struct {
name string
}
func main() {
// creates the in-cluster config
config, err := rest.InClusterConfig()
if err != nil {
panic(err.Error())
}
// creates the client
client, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
var privateRegistrySecretNames string
flag.StringVar(&privateRegistrySecretNames, "registrysecretnames", LookupEnv("REGISTRY_SECRET_NAMES"), "Comma separated names of secrets that shall be used as ImagePullSecrets.")
var privateRegistries []PrivateRegistrySecret // an empty list
for _, i := range strings.Split(privateRegistrySecretNames, ",") {
privateRegistries = append(privateRegistries, PrivateRegistrySecret{name: i})
}
log.Info("Service-Account Patcher started")
var patched = 0
for {
// get all service accounts in all namespaces
serviceAccounts, err := client.CoreV1().ServiceAccounts("").List(context.TODO(), metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
for _, sa := range serviceAccounts.Items {
log.WithField("serviceaccount", sa.Name).WithField("namespace", sa.Namespace).
Debug("Processing ServiceAccount")
for _, privateReg := range privateRegistries {
log.WithField("registryname", privateReg.name).Debug("Processing Secret")
if includeImagePullSecret(&sa, privateReg.name) {
log.WithField("serviceaccount", sa.Name).
WithField("namespace", sa.Namespace).
WithField("imagePullSecrets", sa.ImagePullSecrets).
Debug("ServiceAccount has ImagePullSecrets")
} else {
log.WithField("serviceaccount", sa.Name).
WithField("namespace", sa.Namespace).
WithField("imagePullSecret", privateReg.name).
Info("ServiceAccount does not have ImagePullSecret")
patch, err := getPatchString(&sa, privateReg.name)
if err != nil {
panic(err.Error())
}
result, err := client.CoreV1().ServiceAccounts(sa.Namespace).
Patch(context.TODO(), sa.Name, types.StrategicMergePatchType, patch, metav1.PatchOptions{})
log.WithField("serviceaccount", result.Name).
WithField("namespace", sa.Namespace).
WithField("imagePullSecrets", result.ImagePullSecrets).
Info("ServiceAccount patched")
if err != nil {
panic(err.Error())
}
patched++
}
}
}
log.WithField("numberOfPatchedServiceAccounts", patched).
Info("Patched ServiceAccounts")
patched = 0
time.Sleep(10 * time.Second)
}
}
// the below is taken from https://github.com/titansoft-pte-ltd/imagepullsecret-patcher
type patch struct {
ImagePullSecrets []corev1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
}
func includeImagePullSecret(sa *corev1.ServiceAccount, secretName string) bool {
for _, imagePullSecret := range sa.ImagePullSecrets {
if imagePullSecret.Name == secretName {
return true
}
}
return false
}
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)
}
// LookupEnvOrString lookup ENV string with given key,
func LookupEnv(key string) string {
if val, ok := os.LookupEnv(key); ok {
return val
}
panic("Please provide environment variable REGISTRY_SECRET_NAMES, with comma separated secret names.")
}