1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-15 17:51:20 +00:00
kyverno/pkg/toggle/context.go
Charles-Edouard Brétéché 441641515a
feat: add lazy loading feature flag (#7680)
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
2023-06-27 13:44:15 +08:00

45 lines
888 B
Go

package toggle
import (
"context"
)
var defaults Toggles = defaultToggles{}
type Toggles interface {
ProtectManagedResources() bool
ForceFailurePolicyIgnore() bool
EnableDeferredLoading() bool
}
type defaultToggles struct{}
func (defaultToggles) ProtectManagedResources() bool {
return ProtectManagedResources.enabled()
}
func (defaultToggles) ForceFailurePolicyIgnore() bool {
return ForceFailurePolicyIgnore.enabled()
}
func (defaultToggles) EnableDeferredLoading() bool {
return EnableDeferredLoading.enabled()
}
type contextKey struct{}
func NewContext(ctx context.Context, toggles Toggles) context.Context {
if ctx == nil {
return nil
}
return context.WithValue(ctx, contextKey{}, toggles)
}
func FromContext(ctx context.Context) Toggles {
if ctx != nil {
if toggles, ok := ctx.Value(contextKey{}).(Toggles); ok {
return toggles
}
}
return defaults
}