-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathenvvars.go
166 lines (150 loc) · 4.7 KB
/
envvars.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
// Copyright (c) 2019 Red Hat, Inc.
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//
// Contributors:
// Red Hat, Inc. - initial API and implementation
//
package cfg
import (
"encoding/json"
"log"
"os"
"strconv"
"strings"
corev1 "k8s.io/api/core/v1"
)
// Env vars used for configuration
const (
intervalEnvVar = "CACHING_INTERVAL_HOURS"
daemonsetNameEnvVar = "DAEMONSET_NAME"
namespaceEnvVar = "NAMESPACE"
imagesEnvVar = "IMAGES"
cachingMemRequestEnvVar = "CACHING_MEMORY_REQUEST"
cachingMemLimitEnvVar = "CACHING_MEMORY_LIMIT"
cachingCpuRequestEnvVar = "CACHING_CPU_REQUEST"
cachingCpuLimitEnvVar = "CACHING_CPU_LIMIT"
nodeSelectorEnvVar = "NODE_SELECTOR"
imagePullSecretsEnvVar = "IMAGE_PULL_SECRETS"
affinityEnvVar = "AFFINITY"
kipImageEnvVar = "KIP_IMAGE"
tolerationsEnvVar = "TOLERATIONS"
)
// Default values where applicable
const (
defaultDeploymentName = "kubernetes-image-puller"
defaultDaemonsetName = "kubernetes-image-puller"
defaultNamespace = "k8s-image-puller"
defaultCachingMemRequest = "1Mi"
defaultCachingMemLimit = "5Mi"
defaultCachingInterval = 1
defaultCachingCpuRequest = ".05"
defaultCachingCpuLimit = ".2"
defaultNodeSelector = "{}"
defaultImagePullSecret = ""
defaultAffinity = "{}"
defaultImage = "quay.io/eclipse/kubernetes-image-puller:next"
defaultTolerations = "[]"
)
func getCachingInterval() int {
cachingIntervalStr := getEnvVarOrExit(intervalEnvVar)
interval, err := strconv.Atoi(cachingIntervalStr)
if err != nil {
log.Printf(
"Could not parse env var %s to integer. Value is %s. Using default of %d",
intervalEnvVar,
cachingIntervalStr,
defaultCachingInterval)
return defaultCachingInterval
}
return interval
}
func processImagesEnvVar() map[string]string {
rawImages := getEnvVarOrExit(imagesEnvVar)
rawImages = strings.TrimSpace(rawImages)
images := strings.Split(rawImages, ";")
for i, image := range images {
images[i] = strings.TrimSpace(image)
}
// If last element is empty, remove it
if images[len(images)-1] == "" {
images = images[:len(images)-1]
}
log.Printf("Processing images from configuration...")
var imagesMap = make(map[string]string)
for _, image := range images {
log.Printf("Image: %s", image)
nameAndImage := strings.Split(image, "=")
if len(nameAndImage) != 2 {
log.Printf("Malformed image name/tag: %s. Ignoring.", image)
continue
}
imagesMap[nameAndImage[0]] = nameAndImage[1]
}
return imagesMap
}
func processNodeSelectorEnvVar() map[string]string {
rawNodeSelector := getEnvVarOrDefault(nodeSelectorEnvVar, defaultNodeSelector)
nodeSelector := make(map[string]string)
err := json.Unmarshal([]byte(rawNodeSelector), &nodeSelector)
if err != nil {
log.Fatalf("Failed to unmarshal node selector json: %s", err)
}
return nodeSelector
}
func processImagePullSecretsEnvVar() []string {
rawImagePullSecrets := getEnvVarOrDefault(imagePullSecretsEnvVar, defaultImagePullSecret)
rawImagePullSecrets = strings.TrimSpace(rawImagePullSecrets)
pullSecrets := strings.Split(rawImagePullSecrets, ";")
for i, secret := range pullSecrets {
pullSecrets[i] = strings.TrimSpace(secret)
}
// If last element is empty, remove it
if pullSecrets[len(pullSecrets)-1] == "" {
pullSecrets = pullSecrets[:len(pullSecrets)-1]
}
return pullSecrets
}
func processAffinityEnvVar() *corev1.Affinity {
rawAffinity := getEnvVarOrDefault(affinityEnvVar, defaultAffinity)
affinity := &corev1.Affinity{}
if err := json.Unmarshal([]byte(rawAffinity), affinity); err != nil {
log.Fatalf("Failed to unmarshal affinity json: %s", err)
}
return affinity
}
func processTolerationsEnvVar() []corev1.Toleration {
rawTolerations := getEnvVarOrDefault(tolerationsEnvVar, defaultTolerations)
var tolerations []corev1.Toleration
if err := json.Unmarshal([]byte(rawTolerations), &tolerations); err != nil {
log.Fatalf("Failed to unmarshal tolerations json: %s", err)
}
return tolerations
}
func getEnvVarOrExit(envVar string) string {
val := os.Getenv(envVar)
if val == "" {
log.Fatalf("Env var %s unset. Aborting", envVar)
}
return val
}
func getEnvVarOrDefault(envVar, defaultValue string) string {
val := os.Getenv(envVar)
if val == "" {
log.Printf("No value found for %s. Using default value of %s", envVar, defaultValue)
val = defaultValue
}
return val
}
func getEnvVarOrDefaultBool(envVar string, defaultValue bool) bool {
envvar := os.Getenv(envVar)
val, err := strconv.ParseBool(envvar)
if err != nil {
val = defaultValue
}
return val
}