1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-15 17:51:20 +00:00
kyverno/pkg/toggle/toggle.go
Charles-Edouard Brétéché e4bf66e756
feat: remove policy mutation for auto-gen rules (#5123)
* feat: remove policy mutation code

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* Fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* changelog

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Co-authored-by: shuting <shuting@nirmata.com>
2022-10-25 23:43:46 +00:00

72 lines
1.7 KiB
Go

package toggle
import (
"os"
"strconv"
)
const (
// protect managed resource
ProtectManagedResourcesFlagName = "protectManagedResources"
ProtectManagedResourcesDescription = "Set the flag to 'true', to enable managed resources protection."
protectManagedResourcesEnvVar = "FLAG_PROTECT_MANAGED_RESOURCES"
defaultProtectManagedResources = false
// 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
)
var (
ProtectManagedResources = newToggle(defaultProtectManagedResources, protectManagedResourcesEnvVar)
ForceFailurePolicyIgnore = newToggle(defaultForceFailurePolicyIgnore, forceFailurePolicyIgnoreEnvVar)
)
type Toggle interface {
Enabled() bool
Parse(string) error
}
type toggle struct {
value *bool
defaultValue bool
envVar string
}
func newToggle(defaultValue bool, envVar string) *toggle {
return &toggle{
defaultValue: defaultValue,
envVar: envVar,
}
}
func (t *toggle) Parse(in string) error {
if value, err := getBool(in); err != nil {
return err
} else {
t.value = value
return nil
}
}
func (t *toggle) Enabled() bool {
if t.value != nil {
return *t.value
}
if value, err := getBool(os.Getenv(t.envVar)); err == nil && value != nil {
return *value
}
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
}