-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
77 lines (61 loc) · 1.71 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
package main
import (
"errors"
"fmt"
"os"
"strings"
"time"
"github.com/itzmanish/ecr-token-renew/pkg/aws"
"github.com/itzmanish/ecr-token-renew/pkg/k8s"
)
const (
envVarDockerSecretName = "DOCKER_SECRET_NAME"
envVarTargetNamespace = "TARGET_NAMESPACE"
envVarRegistries = "DOCKER_REGISTRIES"
)
func checkErr(err error) {
if err != nil {
panic(err)
}
}
func main() {
fmt.Println("Running at " + time.Now().UTC().String())
name := os.Getenv(envVarDockerSecretName)
if name == "" {
panic(fmt.Sprintf("Environment variable %s is required", envVarDockerSecretName))
}
namespaceList := os.Getenv(envVarTargetNamespace)
if namespaceList == "" {
namespaceList = "default"
}
fmt.Println("Fetching auth data from AWS... ")
credentials, err := aws.GetDockerCredentials()
checkErr(err)
servers := getServerList(credentials.Server)
fmt.Printf("Docker Registries: %s\n", strings.Join(servers, ","))
namespaces, err := k8s.GetNamespaces(namespaceList)
checkErr(err)
fmt.Printf("Updating kubernetes secret [%s] in %d namespaces\n", name, len(namespaces))
failed := false
for _, ns := range namespaces {
fmt.Printf("Updating secret in namespace [%s]... ", ns)
err = k8s.UpdatePassword(ns, name, credentials.Username, credentials.Password, servers)
if nil != err {
fmt.Printf("failed: %s\n", err)
failed = true
} else {
fmt.Println("success")
}
}
if failed {
panic(errors.New("failed to create one of more Docker login secrets"))
}
fmt.Println("Job complete.")
}
func getServerList(defaultServer string) []string {
addedServersSetting := os.Getenv(envVarRegistries)
if addedServersSetting == "" {
return []string{defaultServer}
}
return strings.Split(addedServersSetting, ",")
}