2023-06-12 17:36:12 +02:00
|
|
|
package toggle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
|
|
|
|
|
|
|
var defaults Toggles = defaultToggles{}
|
|
|
|
|
|
|
|
type Toggles interface {
|
|
|
|
ProtectManagedResources() bool
|
|
|
|
ForceFailurePolicyIgnore() bool
|
2023-06-27 07:44:15 +02:00
|
|
|
EnableDeferredLoading() bool
|
2023-11-01 15:52:03 +02:00
|
|
|
GenerateValidatingAdmissionPolicy() bool
|
2024-09-25 16:11:43 +03:00
|
|
|
DumpMutatePatches() bool
|
2024-11-08 16:42:24 +05:30
|
|
|
AutogenV2() bool
|
2023-06-12 17:36:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type defaultToggles struct{}
|
|
|
|
|
|
|
|
func (defaultToggles) ProtectManagedResources() bool {
|
|
|
|
return ProtectManagedResources.enabled()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (defaultToggles) ForceFailurePolicyIgnore() bool {
|
|
|
|
return ForceFailurePolicyIgnore.enabled()
|
|
|
|
}
|
|
|
|
|
2023-06-27 07:44:15 +02:00
|
|
|
func (defaultToggles) EnableDeferredLoading() bool {
|
|
|
|
return EnableDeferredLoading.enabled()
|
|
|
|
}
|
|
|
|
|
2023-11-01 15:52:03 +02:00
|
|
|
func (defaultToggles) GenerateValidatingAdmissionPolicy() bool {
|
|
|
|
return GenerateValidatingAdmissionPolicy.enabled()
|
|
|
|
}
|
|
|
|
|
2024-09-25 16:11:43 +03:00
|
|
|
func (defaultToggles) DumpMutatePatches() bool {
|
|
|
|
return DumpMutatePatches.enabled()
|
|
|
|
}
|
|
|
|
|
2024-11-08 16:42:24 +05:30
|
|
|
func (defaultToggles) AutogenV2() bool {
|
|
|
|
return AutogenV2.enabled()
|
|
|
|
}
|
|
|
|
|
2023-06-12 17:36:12 +02:00
|
|
|
type contextKey struct{}
|
|
|
|
|
|
|
|
func NewContext(ctx context.Context, toggles Toggles) context.Context {
|
2023-06-27 07:44:15 +02:00
|
|
|
if ctx == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2023-06-12 17:36:12 +02:00
|
|
|
return context.WithValue(ctx, contextKey{}, toggles)
|
|
|
|
}
|
|
|
|
|
|
|
|
func FromContext(ctx context.Context) Toggles {
|
2023-06-27 07:44:15 +02:00
|
|
|
if ctx != nil {
|
|
|
|
if toggles, ok := ctx.Value(contextKey{}).(Toggles); ok {
|
|
|
|
return toggles
|
|
|
|
}
|
2023-06-12 17:36:12 +02:00
|
|
|
}
|
|
|
|
return defaults
|
|
|
|
}
|