kubelatte-ce
Форк от sbertech/kubelatte-ce
296 строк · 9.8 Кб
1package env
2
3import (
4"encoding/json"
5"fmt"
6"gitverse.ru/synapse/kubelatte/pkg/util/types"
7"os"
8"strconv"
9"strings"
10"time"
11)
12
13var (
14trueBool = BoolValue{
15Title: []string{"enabled", "true"},
16Value: true,
17}
18falseBool = BoolValue{
19Title: []string{"disabled", "false"},
20Value: false,
21}
22)
23
24var (
25KbltMutator bool
26KbltValidator bool
27KbltCreator bool
28KbltSideEffect bool
29PodMutationValidation bool
30LocalNamespaceMode bool
31OwnerRefSideEffect bool
32KbltPrefixEnabled bool
33KbltPermissionsStartOnly bool
34KbltPermissionsLogOnly bool
35KbltPermissionsCheck bool
36SideEffectRecreate bool
37KbltMainPort int
38KbltLoggerPort int
39OperatorNamespace string
40ClusterName string
41CreationMode string
42KbltLabelException string
43KbltCreatorLabelKey string
44KbltCreatorLabelValue string
45NamespaceSelectorTI string
46NamespaceSelectorCommonResource string
47PrefixSideEffectRecreate string
48ServiceAnnotationPrefix string
49LogLevel string
50LogsPath string
51LogsFileName string
52DirPlugin string
53ServerCert string
54SertCa string
55LogFileNameInjector string
56ServerKey string
57TLSPort int
58WithoutCert bool
59
60AnnotTag = "sideEffect.resource/tag"
61AnnotSideEffConfigName = "sideEffect.resource/sideEffectConfigName"
62AnnotTriggerNamespace = "sideEffect.resource/triggerNamespace"
63AnnotTriggerName = "sideEffect.resource/triggerName"
64AnnotParentHash = "sideEffect.resource/parentHash"
65
66TimeoutSideEffectParentPublicationWaiting time.Duration
67KbltPermissionsCheckPeriod time.Duration
68NSSelectorTI *types.NamespaceSelector
69NSSelectorCommon *types.NamespaceSelector
70
71KbltLabelExceptionValues []string
72
73m Manager
74)
75
76type Manager struct {
77Events []Event
78}
79
80type Event struct {
81IsCritical bool
82Message string
83}
84
85type BoolValue struct {
86Title []string
87Value bool
88}
89
90func GetManager() *Manager {
91return &m
92}
93
94func initCommon(m *Manager) {
95KbltMainPort = m.GetInt("KBLT_PORT_MAIN", 9443, 0, false)
96KbltSideEffect = m.GetBool("KBLT_SIDE_EFFECT", false, false, true, trueBool, falseBool)
97SideEffectRecreate = m.GetBool("KBLT_SIDE_EFFECT_RECREATE", false, false, true, trueBool, falseBool)
98KbltMutator = m.GetBool("KBLT_MUTATOR", false, false, true, trueBool, falseBool)
99KbltValidator = m.GetBool("KBLT_VALIDATOR", false, false, true, trueBool, falseBool)
100
101if KbltSideEffect {
102ServiceAnnotationPrefix = m.GetString("SERVICE_ANNOTATION_PREFIX", "kblt.")
103
104AnnotTag = ServiceAnnotationPrefix + AnnotTag
105AnnotSideEffConfigName = ServiceAnnotationPrefix + AnnotSideEffConfigName
106AnnotTriggerNamespace = ServiceAnnotationPrefix + AnnotTriggerNamespace
107AnnotTriggerName = ServiceAnnotationPrefix + AnnotTriggerName
108AnnotParentHash = ServiceAnnotationPrefix + AnnotParentHash
109}
110
111initLogs(m)
112}
113
114func InitEnvOperator() {
115initCommon(&m)
116KbltCreator = m.GetBool("KBLT_CREATOR", false, true, true, trueBool, falseBool)
117CreationMode = m.GetString("KBLT_CREATION_MODE", "namespaced")
118KbltCreatorLabelKey = m.GetString("KBLT_CREATOR_LABEL_KEY", "kblt.creation.resource")
119KbltCreatorLabelValue = m.GetString("KBLT_CREATOR_LABEL_VALUE", "true")
120defineNamespaceSelectorTI(&m)
121OwnerRefSideEffect = m.GetBool("OWNER_REF_SIDE_EFFECT", true, false, true, trueBool, falseBool)
122PrefixSideEffectRecreate = m.GetString("PREFIX_SIDE_EFFECT_RECREATE", "kblt")
123PrefixSideEffectRecreate = strings.ReplaceAll(PrefixSideEffectRecreate, "/", "")
124PrefixSideEffectRecreate += "/"
125TimeoutSideEffectParentPublicationWaiting = m.GetDuration("TIMEOUT_SECONDS_SIDE_EFFECT_PARENT_PUBLISHING_WAITING", 5*time.Second, 5*time.Second, true)
126
127LocalNamespaceMode = m.GetBool("LOCAL_NAMESPACE_MODE", true, false, false, trueBool, falseBool)
128OperatorNamespace = m.GetString("NAMESPACE", "")
129ClusterName = m.GetString("CLUSTER_NAME", "")
130defineNamespaceSelectorCommon(&m)
131KbltPermissionsCheck = m.GetBool("KBLT_PERMISSIONS_CHECK", false, false, true, trueBool, falseBool)
132KbltPermissionsStartOnly = m.GetBool("KBLT_PERMISSIONS_START_ONLY", false, false, true, trueBool, falseBool)
133KbltPermissionsLogOnly = m.GetBool("KBLT_PERMISSIONS_LOG_ONLY", false, false, true, trueBool, falseBool)
134KbltPermissionsCheckPeriod = m.GetDuration("KBLT_PERMISSIONS_CHECK_PERIOD", 3*time.Minute, 3*time.Minute, true)
135
136WithoutCert = m.GetBool("NO_CERT", false, false, true, trueBool, falseBool)
137ServerKey = m.GetString("SERVER_KEY", "")
138ServerCert = m.GetString("SERVER_CERT", "")
139SertCa = m.GetString("SERVER_CA", "")
140KbltPermissionsCheck = m.GetBool("KBLT_PERMISSIONS_CHECK", false, false, true, trueBool, falseBool)
141KbltPermissionsStartOnly = m.GetBool("KBLT_PERMISSIONS_START_ONLY", false, false, true, trueBool, falseBool)
142KbltPermissionsLogOnly = m.GetBool("KBLT_PERMISSIONS_LOG_ONLY", false, false, true, trueBool, falseBool)
143KbltPermissionsCheckPeriod = m.GetDuration("KBLT_PERMISSIONS_CHECK_PERIOD", 3*time.Minute, 3*time.Minute, true)
144
145}
146
147func InitEnvWebhookServer() {
148initCommon(&m)
149PodMutationValidation = m.GetBool("KBLT_VALIDATION", true, false, true, trueBool, falseBool)
150TLSPort = m.GetInt("TLS_PORT", 0, 0, false)
151DirPlugin = m.GetString("KBLT_PLUGINS_DIR", "")
152
153KbltPrefixEnabled = m.GetBool("KBLT_ENABLED_TRIGGER_PREFIX", true, false, true, trueBool, falseBool)
154KbltLabelException = m.GetString("KBLT_PREFIX_EXCEPTION", "")
155
156if KbltLabelException != "false" && KbltLabelException != "" {
157if KbltLabelException == "true" {
158KbltLabelExceptionValues = []string{"istiod"}
159} else {
160KbltLabelExceptionValues = []string{"istiod"}
161KbltLabelExceptionValues = append(KbltLabelExceptionValues, strings.Split(KbltLabelException, ",")...)
162}
163} else {
164KbltLabelExceptionValues = []string{}
165}
166}
167
168func initLogs(m *Manager) {
169LogLevel = m.GetString("LOG_LEVEL", "info")
170LogsPath = m.GetString("LOGS_PATH", ".logs")
171LogsFileName = m.GetString("LOG_FILE_NAME", "trace.log")
172KbltLoggerPort = m.GetInt("KBLT_PORT_LOG", KbltMainPort+1, KbltMainPort+1, true)
173}
174
175func defineNamespaceSelectorTI(m *Manager) {
176NamespaceSelectorTI = m.GetString("KBLT_NAMESPACE_SLR_TI", "{\"labelSelector\": {\"matchExpressions\": [{\"key\":\"kubelatte-injection\",\"operator\": \"In\",\"values\": [\"enabled\"]}]}}")
177selector := types.NamespaceSelector{}
178err := json.Unmarshal([]byte(NamespaceSelectorTI), &selector)
179if err != nil {
180m.Events = append(m.Events, Event{
181IsCritical: true,
182Message: fmt.Sprintf("KBLT_NAMESPACE_SLR_TI Unable to parse selector: %s", NamespaceSelectorTI),
183})
184NSSelectorTI = &selector
185}
186NSSelectorTI = &selector
187}
188
189func defineNamespaceSelectorCommon(m *Manager) {
190NamespaceSelectorCommonResource = m.GetString("KBLT_NAMESPACE_SLR_CMN", "")
191if NamespaceSelectorCommonResource == "" {
192return
193}
194selector := types.NamespaceSelector{}
195err := json.Unmarshal([]byte(NamespaceSelectorCommonResource), &selector)
196if err != nil {
197m.Events = append(m.Events, Event{
198IsCritical: true,
199Message: fmt.Sprintf("KBLT_NAMESPACE_SLR_TI Unable to parse selector: %s", NamespaceSelectorTI),
200})
201}
202NSSelectorCommon = &selector
203}
204
205func (m *Manager) GetBool(key string, ifEmpty, ifIncorrect, enableIncorrect bool, enabledValues ...BoolValue) bool {
206val := os.Getenv(key)
207if val == "" {
208m.Events = append(m.Events, Event{
209IsCritical: false,
210Message: fmt.Sprintf("%s is empty. Used default value %v", key, ifEmpty),
211})
212return ifEmpty
213}
214for _, v := range enabledValues {
215for _, boolKey := range v.Title {
216if val == boolKey {
217return v.Value
218}
219}
220}
221if enableIncorrect {
222m.Events = append(m.Events, Event{
223IsCritical: false,
224Message: fmt.Sprintf("%s unable parse %s to bool value. Used default value %v", key, val, ifIncorrect),
225})
226return ifIncorrect
227}
228m.Events = append(m.Events, Event{
229IsCritical: true,
230Message: fmt.Sprintf("%s isn't possible to use value %s", key, val),
231})
232return false
233}
234
235func (m *Manager) GetString(key, defaultVal string) string {
236val := os.Getenv(key)
237if val == "" {
238return defaultVal
239}
240return val
241}
242
243func (m *Manager) GetInt(key string, ifEmpty, ifIncorrect int, enableIncorrect bool) int {
244val := os.Getenv(key)
245if val == "" {
246m.Events = append(m.Events, Event{
247IsCritical: false,
248Message: fmt.Sprintf("%s is empty. Used default value %v", key, ifEmpty),
249})
250return ifEmpty
251}
252res, err := strconv.Atoi(val)
253if err == nil {
254return res
255}
256if enableIncorrect {
257m.Events = append(m.Events, Event{
258IsCritical: false,
259Message: fmt.Sprintf("%s unable parse %s to int value. Used default value %v", key, val, ifIncorrect),
260})
261return ifIncorrect
262}
263m.Events = append(m.Events, Event{
264IsCritical: true,
265Message: fmt.Sprintf("%s isn't possible to use value %s", key, val),
266})
267return 0
268}
269
270func (m *Manager) GetDuration(key string, ifEmpty, ifIncorrect time.Duration, enableIncorrect bool) time.Duration {
271val := os.Getenv(key)
272if val == "" {
273m.Events = append(m.Events, Event{
274IsCritical: false,
275Message: fmt.Sprintf("%s is empty. Used default value %v", key, ifEmpty),
276})
277return ifEmpty
278}
279res, err := time.ParseDuration(val)
280if err == nil {
281return res
282}
283if enableIncorrect {
284m.Events = append(m.Events, Event{
285IsCritical: false,
286Message: fmt.Sprintf("%s unable parse %s to duration value. Used default value %v", key, val, ifIncorrect),
287})
288return ifIncorrect
289}
290
291m.Events = append(m.Events, Event{
292IsCritical: true,
293Message: fmt.Sprintf("%s isn't possible to use value %s", key, val),
294})
295return time.Duration(0)
296}
297