-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
config.go
104 lines (88 loc) · 3.19 KB
/
config.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
/*
Copyright 2022 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package injector
import (
"encoding/json"
"github.com/kelseyhightower/envconfig"
corev1 "k8s.io/api/core/v1"
"github.com/dapr/dapr/utils"
)
// Config represents configuration options for the Dapr Sidecar Injector webhook server.
type Config struct {
TLSCertFile string `envconfig:"TLS_CERT_FILE" required:"true"`
TLSKeyFile string `envconfig:"TLS_KEY_FILE" required:"true"`
SidecarImage string `envconfig:"SIDECAR_IMAGE" required:"true"`
SidecarImagePullPolicy string `envconfig:"SIDECAR_IMAGE_PULL_POLICY"`
Namespace string `envconfig:"NAMESPACE" required:"true"`
KubeClusterDomain string `envconfig:"KUBE_CLUSTER_DOMAIN"`
AllowedServiceAccounts string `envconfig:"ALLOWED_SERVICE_ACCOUNTS"`
IgnoreEntrypointTolerations string `envconfig:"IGNORE_ENTRYPOINT_TOLERATIONS"`
parsedEntrypointTolerations []corev1.Toleration
}
// NewConfigWithDefaults returns a Config object with default values already
// applied. Callers are then free to set custom values for the remaining fields
// and/or override default values.
func NewConfigWithDefaults() Config {
return Config{
SidecarImagePullPolicy: "Always",
}
}
// GetConfig returns configuration derived from environment variables.
func GetConfig() (Config, error) {
// get config from environment variables
c := NewConfigWithDefaults()
err := envconfig.Process("", &c)
if err != nil {
return c, err
}
if c.KubeClusterDomain == "" {
// auto-detect KubeClusterDomain from resolv.conf file
clusterDomain, err := utils.GetKubeClusterDomain()
if err != nil {
log.Errorf("failed to get clusterDomain err:%s, set default:%s", err, utils.DefaultKubeClusterDomain)
c.KubeClusterDomain = utils.DefaultKubeClusterDomain
} else {
c.KubeClusterDomain = clusterDomain
}
}
c.parseTolerationsJSON()
return c, nil
}
func (c Config) GetPullPolicy() corev1.PullPolicy {
switch c.SidecarImagePullPolicy {
case "Always":
return corev1.PullAlways
case "Never":
return corev1.PullNever
case "IfNotPresent":
return corev1.PullIfNotPresent
default:
return corev1.PullIfNotPresent
}
}
func (c *Config) GetIgnoreEntrypointTolerations() []corev1.Toleration {
return c.parsedEntrypointTolerations
}
func (c *Config) parseTolerationsJSON() {
if c.IgnoreEntrypointTolerations == "" {
return
}
// If the string contains an invalid value, log a warning and continue.
ts := []corev1.Toleration{}
err := json.Unmarshal([]byte(c.IgnoreEntrypointTolerations), &ts)
if err != nil {
log.Warnf("couldn't parse entrypoint tolerations (%s): %v", c.IgnoreEntrypointTolerations, err)
return
}
c.parsedEntrypointTolerations = ts
}