kubelatte-ce
Форк от sbertech/kubelatte-ce
296 строк · 9.8 Кб
1package env2
3import (4"encoding/json"5"fmt"6"gitverse.ru/ktrntrsv/kubelatte-ce/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 bool26KbltValidator bool27KbltCreator bool28KbltSideEffect bool29PodMutationValidation bool30LocalNamespaceMode bool31OwnerRefSideEffect bool32KbltPrefixEnabled bool33KbltPermissionsStartOnly bool34KbltPermissionsLogOnly bool35KbltPermissionsCheck bool36SideEffectRecreate bool37KbltMainPort int38KbltLoggerPort int39OperatorNamespace string40ClusterName string41CreationMode string42KbltLabelException string43KbltCreatorLabelKey string44KbltCreatorLabelValue string45NamespaceSelectorTI string46NamespaceSelectorCommonResource string47PrefixSideEffectRecreate string48ServiceAnnotationPrefix string49LogLevel string50LogsPath string51LogsFileName string52DirPlugin string53ServerCert string54SertCa string55LogFileNameInjector string56ServerKey string57TLSPort int58WithoutCert bool59
60AnnotTag = "sideEffect.resource/tag"61AnnotSideEffConfigName = "sideEffect.resource/sideEffectConfigName"62AnnotTriggerNamespace = "sideEffect.resource/triggerNamespace"63AnnotTriggerName = "sideEffect.resource/triggerName"64AnnotParentHash = "sideEffect.resource/parentHash"65
66TimeoutSideEffectParentPublicationWaiting time.Duration67KbltPermissionsCheckPeriod time.Duration68NSSelectorTI *types.NamespaceSelector69NSSelectorCommon *types.NamespaceSelector70
71KbltLabelExceptionValues []string72
73m Manager
74)
75
76type Manager struct {77Events []Event78}
79
80type Event struct {81IsCritical bool82Message string83}
84
85type BoolValue struct {86Title []string87Value bool88}
89
90func GetManager() *Manager {91return &m92}
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 + AnnotTag105AnnotSideEffConfigName = ServiceAnnotationPrefix + AnnotSideEffConfigName106AnnotTriggerNamespace = ServiceAnnotationPrefix + AnnotTriggerNamespace107AnnotTriggerName = ServiceAnnotationPrefix + AnnotTriggerName108AnnotParentHash = ServiceAnnotationPrefix + AnnotParentHash109}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 = &selector185}186NSSelectorTI = &selector187}
188
189func defineNamespaceSelectorCommon(m *Manager) {190NamespaceSelectorCommonResource = m.GetString("KBLT_NAMESPACE_SLR_CMN", "")191if NamespaceSelectorCommonResource == "" {192return193}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 = &selector203}
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 ifEmpty213}214for _, v := range enabledValues {215for _, boolKey := range v.Title {216if val == boolKey {217return v.Value218}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 ifIncorrect227}228m.Events = append(m.Events, Event{229IsCritical: true,230Message: fmt.Sprintf("%s isn't possible to use value %s", key, val),231})232return false233}
234
235func (m *Manager) GetString(key, defaultVal string) string {236val := os.Getenv(key)237if val == "" {238return defaultVal239}240return val241}
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 ifEmpty251}252res, err := strconv.Atoi(val)253if err == nil {254return res255}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 ifIncorrect262}263m.Events = append(m.Events, Event{264IsCritical: true,265Message: fmt.Sprintf("%s isn't possible to use value %s", key, val),266})267return 0268}
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 ifEmpty278}279res, err := time.ParseDuration(val)280if err == nil {281return res282}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 ifIncorrect289}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