FeatureFlags: Add helper to check against a set of flags (#97240)

This commit is contained in:
Ryan McKinley
2024-12-02 22:41:41 +03:00
committed by GitHub
parent 3448384e0d
commit 20a27da636
8 changed files with 36 additions and 13 deletions

View File

@@ -22,6 +22,14 @@ var (
_ builder.OpenAPIPostProcessor = (*DashboardsAPIBuilder)(nil) _ builder.OpenAPIPostProcessor = (*DashboardsAPIBuilder)(nil)
) )
func FeatureEnabled(features featuremgmt.FeatureToggles) bool {
return featuremgmt.AnyEnabled(features,
featuremgmt.FlagGrafanaAPIServerWithExperimentalAPIs,
featuremgmt.FlagKubernetesDashboardsAPI,
featuremgmt.FlagKubernetesDashboards, // << the UI will call the new apis
featuremgmt.FlagProvisioning)
}
// This is used just so wire has something unique to return // This is used just so wire has something unique to return
type DashboardsAPIBuilder struct{} type DashboardsAPIBuilder struct{}
@@ -29,7 +37,7 @@ func RegisterAPIService(
features featuremgmt.FeatureToggles, features featuremgmt.FeatureToggles,
apiregistration builder.APIRegistrar, apiregistration builder.APIRegistrar,
) *DashboardsAPIBuilder { ) *DashboardsAPIBuilder {
if !features.IsEnabledGlobally(featuremgmt.FlagGrafanaAPIServerWithExperimentalAPIs) && !features.IsEnabledGlobally(featuremgmt.FlagKubernetesDashboardsAPI) { if !FeatureEnabled(features) {
return nil // skip registration unless opting into experimental apis or dashboards in the k8s api return nil // skip registration unless opting into experimental apis or dashboards in the k8s api
} }
builder := &DashboardsAPIBuilder{} builder := &DashboardsAPIBuilder{}

View File

@@ -63,7 +63,7 @@ func RegisterAPIService(cfg *setting.Cfg, features featuremgmt.FeatureToggles,
tracing *tracing.TracingService, tracing *tracing.TracingService,
unified resource.ResourceClient, unified resource.ResourceClient,
) *DashboardsAPIBuilder { ) *DashboardsAPIBuilder {
if !features.IsEnabledGlobally(featuremgmt.FlagGrafanaAPIServerWithExperimentalAPIs) && !features.IsEnabledGlobally(featuremgmt.FlagKubernetesDashboardsAPI) { if !dashboard.FeatureEnabled(features) {
return nil // skip registration unless opting into experimental apis or dashboards in the k8s api return nil // skip registration unless opting into experimental apis or dashboards in the k8s api
} }

View File

@@ -59,7 +59,7 @@ func RegisterAPIService(cfg *setting.Cfg, features featuremgmt.FeatureToggles,
tracing *tracing.TracingService, tracing *tracing.TracingService,
unified resource.ResourceClient, unified resource.ResourceClient,
) *DashboardsAPIBuilder { ) *DashboardsAPIBuilder {
if !features.IsEnabledGlobally(featuremgmt.FlagGrafanaAPIServerWithExperimentalAPIs) && !features.IsEnabledGlobally(featuremgmt.FlagKubernetesDashboardsAPI) { if !dashboard.FeatureEnabled(features) {
return nil // skip registration unless opting into experimental apis or dashboards in the k8s api return nil // skip registration unless opting into experimental apis or dashboards in the k8s api
} }

View File

@@ -59,7 +59,7 @@ func RegisterAPIService(cfg *setting.Cfg, features featuremgmt.FeatureToggles,
tracing *tracing.TracingService, tracing *tracing.TracingService,
unified resource.ResourceClient, unified resource.ResourceClient,
) *DashboardsAPIBuilder { ) *DashboardsAPIBuilder {
if !features.IsEnabledGlobally(featuremgmt.FlagGrafanaAPIServerWithExperimentalAPIs) && !features.IsEnabledGlobally(featuremgmt.FlagKubernetesDashboardsAPI) { if !dashboard.FeatureEnabled(features) {
return nil // skip registration unless opting into experimental apis or dashboards in the k8s api return nil // skip registration unless opting into experimental apis or dashboards in the k8s api
} }

View File

@@ -5,7 +5,6 @@ import (
"errors" "errors"
"slices" "slices"
grafanarest "github.com/grafana/grafana/pkg/apiserver/rest"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
@@ -17,6 +16,8 @@ import (
common "k8s.io/kube-openapi/pkg/common" common "k8s.io/kube-openapi/pkg/common"
"k8s.io/kube-openapi/pkg/spec3" "k8s.io/kube-openapi/pkg/spec3"
grafanarest "github.com/grafana/grafana/pkg/apiserver/rest"
"github.com/grafana/grafana/pkg/apimachinery/identity" "github.com/grafana/grafana/pkg/apimachinery/identity"
"github.com/grafana/grafana/pkg/apimachinery/utils" "github.com/grafana/grafana/pkg/apimachinery/utils"
"github.com/grafana/grafana/pkg/apis/folder/v0alpha1" "github.com/grafana/grafana/pkg/apis/folder/v0alpha1"
@@ -55,8 +56,11 @@ func RegisterAPIService(cfg *setting.Cfg,
accessControl accesscontrol.AccessControl, accessControl accesscontrol.AccessControl,
registerer prometheus.Registerer, registerer prometheus.Registerer,
) *FolderAPIBuilder { ) *FolderAPIBuilder {
if !features.IsEnabledGlobally(featuremgmt.FlagKubernetesFolders) && !features.IsEnabledGlobally(featuremgmt.FlagGrafanaAPIServerTestingWithExperimentalAPIs) { if !featuremgmt.AnyEnabled(features,
return nil // skip registration unless opting into Kubernetes folders or unless we want to customise registration when testing featuremgmt.FlagKubernetesFolders,
featuremgmt.FlagGrafanaAPIServerTestingWithExperimentalAPIs,
featuremgmt.FlagProvisioning) {
return nil // skip registration unless opting into Kubernetes folders or unless we want to customize registration when testing
} }
builder := &FolderAPIBuilder{ builder := &FolderAPIBuilder{

View File

@@ -28,8 +28,10 @@ func NewPeakQAPIBuilder() *PeakQAPIBuilder {
} }
func RegisterAPIService(features featuremgmt.FeatureToggles, apiregistration builder.APIRegistrar, reg prometheus.Registerer) *PeakQAPIBuilder { func RegisterAPIService(features featuremgmt.FeatureToggles, apiregistration builder.APIRegistrar, reg prometheus.Registerer) *PeakQAPIBuilder {
if !((features.IsEnabledGlobally(featuremgmt.FlagQueryService) && features.IsEnabledGlobally(featuremgmt.FlagQueryLibrary)) || if !featuremgmt.AnyEnabled(features,
features.IsEnabledGlobally(featuremgmt.FlagGrafanaAPIServerWithExperimentalAPIs)) { featuremgmt.FlagQueryService,
featuremgmt.FlagQueryLibrary,
featuremgmt.FlagGrafanaAPIServerWithExperimentalAPIs) {
return nil // skip registration unless explicitly added (or all experimental are added) return nil // skip registration unless explicitly added (or all experimental are added)
} }
builder := NewPeakQAPIBuilder() builder := NewPeakQAPIBuilder()

View File

@@ -3,6 +3,7 @@ package scope
import ( import (
"fmt" "fmt"
"github.com/prometheus/client_golang/prometheus"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/authorization/authorizer" "k8s.io/apiserver/pkg/authorization/authorizer"
@@ -12,8 +13,6 @@ import (
"k8s.io/kube-openapi/pkg/spec3" "k8s.io/kube-openapi/pkg/spec3"
"k8s.io/kube-openapi/pkg/validation/spec" "k8s.io/kube-openapi/pkg/validation/spec"
"github.com/prometheus/client_golang/prometheus"
scope "github.com/grafana/grafana/pkg/apis/scope/v0alpha1" scope "github.com/grafana/grafana/pkg/apis/scope/v0alpha1"
"github.com/grafana/grafana/pkg/services/apiserver/builder" "github.com/grafana/grafana/pkg/services/apiserver/builder"
"github.com/grafana/grafana/pkg/services/featuremgmt" "github.com/grafana/grafana/pkg/services/featuremgmt"
@@ -29,8 +28,9 @@ func NewScopeAPIBuilder() *ScopeAPIBuilder {
} }
func RegisterAPIService(features featuremgmt.FeatureToggles, apiregistration builder.APIRegistrar, reg prometheus.Registerer) *ScopeAPIBuilder { func RegisterAPIService(features featuremgmt.FeatureToggles, apiregistration builder.APIRegistrar, reg prometheus.Registerer) *ScopeAPIBuilder {
if !(features.IsEnabledGlobally(featuremgmt.FlagScopeApi) || if !featuremgmt.AnyEnabled(features,
features.IsEnabledGlobally(featuremgmt.FlagGrafanaAPIServerWithExperimentalAPIs)) { featuremgmt.FlagScopeApi,
featuremgmt.FlagGrafanaAPIServerWithExperimentalAPIs) {
return nil // skip registration unless opting into experimental apis return nil // skip registration unless opting into experimental apis
} }
builder := NewScopeAPIBuilder() builder := NewScopeAPIBuilder()

View File

@@ -23,6 +23,15 @@ type FeatureToggles interface {
GetEnabled(ctx context.Context) map[string]bool GetEnabled(ctx context.Context) map[string]bool
} }
func AnyEnabled(f FeatureToggles, flags ...string) bool {
for _, flag := range flags {
if f.IsEnabledGlobally(flag) {
return true
}
}
return false
}
// FeatureFlagStage indicates the quality level // FeatureFlagStage indicates the quality level
type FeatureFlagStage int type FeatureFlagStage int