-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathscale_resolvers.go
243 lines (210 loc) · 7.89 KB
/
scale_resolvers.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package resolver
import (
"context"
"fmt"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
kedav1alpha1 "github.com/kedacore/keda/api/v1alpha1"
)
func ResolveContainerEnv(client client.Client, logger logr.Logger, podSpec *corev1.PodSpec, containerName, namespace string) (map[string]string, error) {
if len(podSpec.Containers) < 1 {
return nil, fmt.Errorf("Target object doesn't have containers")
}
var container corev1.Container
if containerName != "" {
for _, c := range podSpec.Containers {
if c.Name == containerName {
container = c
break
}
}
if &container == nil {
return nil, fmt.Errorf("Couldn't find container with name %s on Target object", containerName)
}
} else {
container = podSpec.Containers[0]
}
return resolveEnv(client, logger, &container, namespace)
}
func ResolveAuthRef(client client.Client, logger logr.Logger, triggerAuthRef *kedav1alpha1.ScaledObjectAuthRef, podSpec *corev1.PodSpec, namespace string) (map[string]string, string) {
result := make(map[string]string)
podIdentity := ""
if triggerAuthRef != nil && triggerAuthRef.Name != "" {
triggerAuth := &kedav1alpha1.TriggerAuthentication{}
err := client.Get(context.TODO(), types.NamespacedName{Name: triggerAuthRef.Name, Namespace: namespace}, triggerAuth)
if err != nil {
logger.Error(err, "Error getting triggerAuth", "triggerAuthRef.Name", triggerAuthRef.Name)
} else {
podIdentity = string(triggerAuth.Spec.PodIdentity.Provider)
if triggerAuth.Spec.Env != nil {
for _, e := range triggerAuth.Spec.Env {
env, err := ResolveContainerEnv(client, logger, podSpec, e.ContainerName, namespace)
if err != nil {
result[e.Parameter] = ""
} else {
result[e.Parameter] = env[e.Name]
}
}
}
if triggerAuth.Spec.SecretTargetRef != nil {
for _, e := range triggerAuth.Spec.SecretTargetRef {
result[e.Parameter] = resolveAuthSecret(client, logger, e.Name, namespace, e.Key)
}
}
if triggerAuth.Spec.HashiCorpVault.Secrets != nil {
vault := NewHashicorpVaultHandler(&triggerAuth.Spec.HashiCorpVault)
err := vault.Initialize(logger)
if err != nil {
logger.Error(err, "Error authenticate to Vault", "triggerAuthRef.Name", triggerAuthRef.Name)
} else {
for _, e := range triggerAuth.Spec.HashiCorpVault.Secrets {
secret, err := vault.Read(e.Path)
if err != nil {
logger.Error(err, "Error trying to read secret from Vault", "triggerAuthRef.Name", triggerAuthRef.Name,
"secret.path", e.Path)
continue
}
result[e.Parameter] = resolveVaultSecret(logger, secret.Data, e.Key)
}
vault.Stop()
}
}
}
}
return result, podIdentity
}
func resolveEnv(client client.Client, logger logr.Logger, container *corev1.Container, namespace string) (map[string]string, error) {
resolved := make(map[string]string)
if container.EnvFrom != nil {
for _, source := range container.EnvFrom {
if source.ConfigMapRef != nil {
if configMap, err := resolveConfigMap(client, source.ConfigMapRef, namespace); err == nil {
for k, v := range configMap {
resolved[k] = v
}
} else if source.ConfigMapRef.Optional != nil && *source.ConfigMapRef.Optional {
// ignore error when ConfigMap is marked as optional
continue
} else {
return nil, fmt.Errorf("error reading config ref %s on namespace %s/: %s", source.ConfigMapRef, namespace, err)
}
} else if source.SecretRef != nil {
if secretsMap, err := resolveSecretMap(client, source.SecretRef, namespace); err == nil {
for k, v := range secretsMap {
resolved[k] = v
}
} else if source.SecretRef.Optional != nil && *source.SecretRef.Optional {
// ignore error when Secret is marked as optional
continue
} else {
return nil, fmt.Errorf("error reading secret ref %s on namespace %s: %s", source.SecretRef, namespace, err)
}
}
}
}
if container.Env != nil {
for _, envVar := range container.Env {
var value string
var err error
// env is either a name/value pair or an EnvVarSource
if envVar.Value != "" {
value = envVar.Value
} else if envVar.ValueFrom != nil {
// env is an EnvVarSource, that can be on of the 4 below
if envVar.ValueFrom.SecretKeyRef != nil {
// env is a secret selector
value, err = resolveSecretValue(client, envVar.ValueFrom.SecretKeyRef, envVar.ValueFrom.SecretKeyRef.Key, namespace)
if err != nil {
return nil, fmt.Errorf("error resolving secret name %s for env %s in namespace %s",
envVar.ValueFrom.SecretKeyRef,
envVar.Name,
namespace)
}
} else if envVar.ValueFrom.ConfigMapKeyRef != nil {
// env is a configMap selector
value, err = resolveConfigValue(client, envVar.ValueFrom.ConfigMapKeyRef, envVar.ValueFrom.ConfigMapKeyRef.Key, namespace)
if err != nil {
return nil, fmt.Errorf("error resolving config %s for env %s in namespace %s",
envVar.ValueFrom.ConfigMapKeyRef,
envVar.Name,
namespace)
}
} else {
logger.V(1).Info("cannot resolve env %s to a value. fieldRef and resourceFieldRef env are skipped", envVar.Name)
continue
}
}
resolved[envVar.Name] = value
}
}
return resolved, nil
}
func resolveConfigMap(client client.Client, configMapRef *corev1.ConfigMapEnvSource, namespace string) (map[string]string, error) {
configMap := &corev1.ConfigMap{}
err := client.Get(context.TODO(), types.NamespacedName{Name: configMapRef.Name, Namespace: namespace}, configMap)
if err != nil {
return nil, err
}
return configMap.Data, nil
}
func resolveSecretMap(client client.Client, secretMapRef *corev1.SecretEnvSource, namespace string) (map[string]string, error) {
secret := &corev1.Secret{}
err := client.Get(context.TODO(), types.NamespacedName{Name: secretMapRef.Name, Namespace: namespace}, secret)
if err != nil {
return nil, err
}
secretsStr := make(map[string]string)
for k, v := range secret.Data {
secretsStr[k] = string(v)
}
return secretsStr, nil
}
func resolveSecretValue(client client.Client, secretKeyRef *corev1.SecretKeySelector, keyName, namespace string) (string, error) {
secret := &corev1.Secret{}
err := client.Get(context.TODO(), types.NamespacedName{Name: secretKeyRef.Name, Namespace: namespace}, secret)
if err != nil {
return "", err
}
return string(secret.Data[keyName]), nil
}
func resolveConfigValue(client client.Client, configKeyRef *corev1.ConfigMapKeySelector, keyName, namespace string) (string, error) {
configMap := &corev1.ConfigMap{}
err := client.Get(context.TODO(), types.NamespacedName{Name: configKeyRef.Name, Namespace: namespace}, configMap)
if err != nil {
return "", err
}
return string(configMap.Data[keyName]), nil
}
func resolveAuthSecret(client client.Client, logger logr.Logger, name, namespace, key string) string {
if name == "" || namespace == "" || key == "" {
logger.Error(fmt.Errorf("error trying to get secret"), "name, namespace and key are required", "Secret.Namespace", namespace, "Secret.Name", name, "key", key)
return ""
}
secret := &corev1.Secret{}
err := client.Get(context.TODO(), types.NamespacedName{Name: name, Namespace: namespace}, secret)
if err != nil {
logger.Error(err, "Error trying to get secret from namespace", "Secret.Namespace", namespace, "Secret.Name", name)
return ""
}
result := secret.Data[key]
if result == nil {
return ""
}
return string(result)
}
func resolveVaultSecret(logger logr.Logger, data map[string]interface{}, key string) string {
if v2Data, ok := data["data"].(map[string]interface{}); ok {
if value, ok := v2Data[key]; ok {
if s, ok := value.(string); ok {
return s
}
} else {
logger.Error(fmt.Errorf("key '%s' not found", key), "Error trying to get key from Vault secret")
return ""
}
}
logger.Error(fmt.Errorf("unable to convert Vault Data value"), "Error trying to convert Data secret vaule")
return ""
}