2022-03-18 16:16:42 +00:00
|
|
|
package toggle
|
|
|
|
|
2022-03-28 14:01:27 +00:00
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
)
|
2022-03-18 16:16:42 +00:00
|
|
|
|
2022-03-28 14:01:27 +00:00
|
|
|
const (
|
2022-09-06 15:43:04 +00:00
|
|
|
// protect managed resource
|
|
|
|
ProtectManagedResourcesFlagName = "protectManagedResources"
|
|
|
|
ProtectManagedResourcesDescription = "Set the flag to 'true', to enable managed resources protection."
|
|
|
|
protectManagedResourcesEnvVar = "FLAG_PROTECT_MANAGED_RESOURCES"
|
|
|
|
defaultProtectManagedResources = false
|
2022-10-21 18:13:36 +00:00
|
|
|
// force failure policy ignore
|
|
|
|
ForceFailurePolicyIgnoreFlagName = "forceFailurePolicyIgnore"
|
|
|
|
ForceFailurePolicyIgnoreDescription = "Set the flag to 'true', to force set Failure Policy to 'ignore'."
|
|
|
|
forceFailurePolicyIgnoreEnvVar = "FLAG_FORCE_FAILURE_POLICY_IGNORE"
|
|
|
|
defaultForceFailurePolicyIgnore = false
|
2023-06-27 05:44:15 +00:00
|
|
|
// enable deferred context loading
|
|
|
|
EnableDeferredLoadingFlagName = "enableDeferredLoading"
|
|
|
|
EnableDeferredLoadingDescription = "enable deferred loading of context variables"
|
|
|
|
enableDeferredLoadingEnvVar = "FLAG_ENABLE_DEFERRED_LOADING"
|
|
|
|
defaultEnableDeferredLoading = true
|
2023-11-01 13:52:03 +00:00
|
|
|
// generate validating admission policies
|
|
|
|
GenerateValidatingAdmissionPolicyFlagName = "generateValidatingAdmissionPolicy"
|
|
|
|
GenerateValidatingAdmissionPolicyDescription = "Set the flag to 'true', to generate validating admission policies."
|
|
|
|
generateValidatingAdmissionPolicyEnvVar = "FLAG_GENERATE_VALIDATING_ADMISSION_POLICY"
|
|
|
|
defaultGenerateValidatingAdmissionPolicy = false
|
2024-09-25 13:11:43 +00:00
|
|
|
// dump mutate patches
|
|
|
|
DumpMutatePatchesFlagName = "dumpPatches"
|
|
|
|
DumpMutatePatchesDescription = "Set the flag to 'true', to dump mutate patches."
|
|
|
|
dumpMutatePatchesEnvVar = "FLAG_DUMP_PATCHES"
|
|
|
|
defaultDumpMutatePatches = false
|
2024-11-08 11:12:24 +00:00
|
|
|
// select autogen
|
|
|
|
AutogenV2FlagName = "autogenV2"
|
|
|
|
AutogenV2Description = "Set the flag to 'true', to enable autogen v2."
|
|
|
|
autogenV2EnvVar = "FLAG_AUTOGEN_V2"
|
|
|
|
defaultAutogenV2 = false
|
2022-03-28 14:01:27 +00:00
|
|
|
)
|
|
|
|
|
2022-07-21 09:01:42 +00:00
|
|
|
var (
|
2023-11-01 13:52:03 +00:00
|
|
|
ProtectManagedResources = newToggle(defaultProtectManagedResources, protectManagedResourcesEnvVar)
|
|
|
|
ForceFailurePolicyIgnore = newToggle(defaultForceFailurePolicyIgnore, forceFailurePolicyIgnoreEnvVar)
|
|
|
|
EnableDeferredLoading = newToggle(defaultEnableDeferredLoading, enableDeferredLoadingEnvVar)
|
|
|
|
GenerateValidatingAdmissionPolicy = newToggle(defaultGenerateValidatingAdmissionPolicy, generateValidatingAdmissionPolicyEnvVar)
|
2024-09-25 13:11:43 +00:00
|
|
|
DumpMutatePatches = newToggle(defaultDumpMutatePatches, dumpMutatePatchesEnvVar)
|
2024-11-08 11:12:24 +00:00
|
|
|
AutogenV2 = newToggle(defaultAutogenV2, autogenV2EnvVar)
|
2022-07-21 09:01:42 +00:00
|
|
|
)
|
2022-03-28 14:01:27 +00:00
|
|
|
|
2023-06-12 15:36:12 +00:00
|
|
|
type ToggleFlag interface {
|
2022-08-31 06:41:14 +00:00
|
|
|
Parse(string) error
|
2022-03-28 14:01:27 +00:00
|
|
|
}
|
|
|
|
|
2023-06-12 15:36:12 +00:00
|
|
|
type Toggle interface {
|
|
|
|
ToggleFlag
|
|
|
|
enabled() bool
|
|
|
|
}
|
|
|
|
|
2022-08-31 06:41:14 +00:00
|
|
|
type toggle struct {
|
|
|
|
value *bool
|
|
|
|
defaultValue bool
|
|
|
|
envVar string
|
2022-03-28 14:01:27 +00:00
|
|
|
}
|
|
|
|
|
2023-06-12 15:36:12 +00:00
|
|
|
func newToggle(defaultValue bool, envVar string) Toggle {
|
2022-08-31 06:41:14 +00:00
|
|
|
return &toggle{
|
|
|
|
defaultValue: defaultValue,
|
|
|
|
envVar: envVar,
|
2022-03-28 14:01:27 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-21 09:01:42 +00:00
|
|
|
|
2022-08-31 06:41:14 +00:00
|
|
|
func (t *toggle) Parse(in string) error {
|
2022-07-21 09:01:42 +00:00
|
|
|
if value, err := getBool(in); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
2022-08-31 06:41:14 +00:00
|
|
|
t.value = value
|
2022-07-21 09:01:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-12 15:36:12 +00:00
|
|
|
func (t *toggle) enabled() bool {
|
2022-08-31 06:41:14 +00:00
|
|
|
if t.value != nil {
|
|
|
|
return *t.value
|
2022-07-21 09:01:42 +00:00
|
|
|
}
|
2022-08-31 06:41:14 +00:00
|
|
|
if value, err := getBool(os.Getenv(t.envVar)); err == nil && value != nil {
|
2022-07-21 09:01:42 +00:00
|
|
|
return *value
|
|
|
|
}
|
2022-08-31 06:41:14 +00:00
|
|
|
return t.defaultValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBool(in string) (*bool, error) {
|
|
|
|
if in == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
value, err := strconv.ParseBool(in)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &value, nil
|
2022-07-21 09:01:42 +00:00
|
|
|
}
|