2023-06-12 15:36:12 +00:00
|
|
|
package toggle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
|
|
|
|
|
|
|
var defaults Toggles = defaultToggles{}
|
|
|
|
|
|
|
|
type Toggles interface {
|
|
|
|
ProtectManagedResources() bool
|
|
|
|
ForceFailurePolicyIgnore() bool
|
2023-06-27 05:44:15 +00:00
|
|
|
EnableDeferredLoading() bool
|
2023-11-01 13:52:03 +00:00
|
|
|
GenerateValidatingAdmissionPolicy() bool
|
2023-06-12 15:36:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type defaultToggles struct{}
|
|
|
|
|
|
|
|
func (defaultToggles) ProtectManagedResources() bool {
|
|
|
|
return ProtectManagedResources.enabled()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (defaultToggles) ForceFailurePolicyIgnore() bool {
|
|
|
|
return ForceFailurePolicyIgnore.enabled()
|
|
|
|
}
|
|
|
|
|
2023-06-27 05:44:15 +00:00
|
|
|
func (defaultToggles) EnableDeferredLoading() bool {
|
|
|
|
return EnableDeferredLoading.enabled()
|
|
|
|
}
|
|
|
|
|
2023-11-01 13:52:03 +00:00
|
|
|
func (defaultToggles) GenerateValidatingAdmissionPolicy() bool {
|
|
|
|
return GenerateValidatingAdmissionPolicy.enabled()
|
|
|
|
}
|
|
|
|
|
2023-06-12 15:36:12 +00:00
|
|
|
type contextKey struct{}
|
|
|
|
|
|
|
|
func NewContext(ctx context.Context, toggles Toggles) context.Context {
|
2023-06-27 05:44:15 +00:00
|
|
|
if ctx == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2023-06-12 15:36:12 +00:00
|
|
|
return context.WithValue(ctx, contextKey{}, toggles)
|
|
|
|
}
|
|
|
|
|
|
|
|
func FromContext(ctx context.Context) Toggles {
|
2023-06-27 05:44:15 +00:00
|
|
|
if ctx != nil {
|
|
|
|
if toggles, ok := ctx.Value(contextKey{}).(Toggles); ok {
|
|
|
|
return toggles
|
|
|
|
}
|
2023-06-12 15:36:12 +00:00
|
|
|
}
|
|
|
|
return defaults
|
|
|
|
}
|