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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 36 additions and 13 deletions

View File

@ -22,6 +22,14 @@ var (
_ 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
type DashboardsAPIBuilder struct{}
@ -29,7 +37,7 @@ func RegisterAPIService(
features featuremgmt.FeatureToggles,
apiregistration builder.APIRegistrar,
) *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
}
builder := &DashboardsAPIBuilder{}

View File

@ -63,7 +63,7 @@ func RegisterAPIService(cfg *setting.Cfg, features featuremgmt.FeatureToggles,
tracing *tracing.TracingService,
unified resource.ResourceClient,
) *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
}

View File

@ -59,7 +59,7 @@ func RegisterAPIService(cfg *setting.Cfg, features featuremgmt.FeatureToggles,
tracing *tracing.TracingService,
unified resource.ResourceClient,
) *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
}

View File

@ -59,7 +59,7 @@ func RegisterAPIService(cfg *setting.Cfg, features featuremgmt.FeatureToggles,
tracing *tracing.TracingService,
unified resource.ResourceClient,
) *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
}

View File

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

View File

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

View File

@ -3,6 +3,7 @@ package scope
import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/authorization/authorizer"
@ -12,8 +13,6 @@ import (
"k8s.io/kube-openapi/pkg/spec3"
"k8s.io/kube-openapi/pkg/validation/spec"
"github.com/prometheus/client_golang/prometheus"
scope "github.com/grafana/grafana/pkg/apis/scope/v0alpha1"
"github.com/grafana/grafana/pkg/services/apiserver/builder"
"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 {
if !(features.IsEnabledGlobally(featuremgmt.FlagScopeApi) ||
features.IsEnabledGlobally(featuremgmt.FlagGrafanaAPIServerWithExperimentalAPIs)) {
if !featuremgmt.AnyEnabled(features,
featuremgmt.FlagScopeApi,
featuremgmt.FlagGrafanaAPIServerWithExperimentalAPIs) {
return nil // skip registration unless opting into experimental apis
}
builder := NewScopeAPIBuilder()

View File

@ -23,6 +23,15 @@ type FeatureToggles interface {
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
type FeatureFlagStage int