Skip to content

Commit

Permalink
Check feature flags when initialising dual writer
Browse files Browse the repository at this point in the history
  • Loading branch information
suntala committed May 10, 2024
1 parent 33a5c6d commit dfa06b8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
30 changes: 16 additions & 14 deletions pkg/apiserver/rest/dualwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rest
import (
"context"

"github.com/grafana/grafana/pkg/services/featuremgmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/registry/rest"
Expand Down Expand Up @@ -80,22 +81,23 @@ const (
Mode4
)

// #TODO replace the keys with Kind type
var CurrentMode = map[string]DualWriterMode{
"playlist": Mode2,
}

//TODO: make CurrentMode customisable and specific to each entity
// change DualWriter signature to get the current mode as an argument

// NewDualWriter returns a new DualWriter.
func NewDualWriter(kind string, legacy LegacyStorage, storage Storage) DualWriter {
var mode DualWriterMode
if val, ok := CurrentMode[kind]; ok {
mode = val
}
func NewDualWriter(kind string, features featuremgmt.FeatureToggles, legacy LegacyStorage, storage Storage) DualWriter {
// #TODO replace string with Kind type
switch kind {
case "playlist":
if features.IsEnabledGlobally(featuremgmt.FlagDualWritePlaylistsMode3) {
return NewDualWriterMode3(legacy, storage)
}

if features.IsEnabledGlobally(featuremgmt.FlagDualWritePlaylistsMode2) {
return NewDualWriterMode2(legacy, storage)
}

return selectDualWriter(mode, legacy, storage)
return NewDualWriterMode1(legacy, storage)
default:
return NewDualWriterMode1(legacy, storage)
}
}

type updateWrapper struct {
Expand Down
6 changes: 5 additions & 1 deletion pkg/registry/apis/playlist/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
grafanarest "github.com/grafana/grafana/pkg/apiserver/rest"
"github.com/grafana/grafana/pkg/services/apiserver/endpoints/request"
"github.com/grafana/grafana/pkg/services/apiserver/utils"
"github.com/grafana/grafana/pkg/services/featuremgmt"
playlistsvc "github.com/grafana/grafana/pkg/services/playlist"
"github.com/grafana/grafana/pkg/setting"
)
Expand All @@ -30,16 +31,19 @@ type PlaylistAPIBuilder struct {
service playlistsvc.Service
namespacer request.NamespaceMapper
gv schema.GroupVersion
features featuremgmt.FeatureToggles
}

func RegisterAPIService(p playlistsvc.Service,
apiregistration builder.APIRegistrar,
cfg *setting.Cfg,
features featuremgmt.FeatureToggles,
) *PlaylistAPIBuilder {
builder := &PlaylistAPIBuilder{
service: p,
namespacer: request.GetNamespaceMapper(cfg),
gv: playlist.PlaylistResourceInfo.GroupVersion(),
features: features,
}
apiregistration.RegisterAPI(builder)
return builder
Expand Down Expand Up @@ -118,7 +122,7 @@ func (b *PlaylistAPIBuilder) GetAPIGroupInfo(
if err != nil {
return nil, err
}
storage[resource.StoragePath()] = grafanarest.NewDualWriter("playlist", legacyStore, store)
storage[resource.StoragePath()] = grafanarest.NewDualWriter("playlist", b.features, legacyStore, store)
}

apiGroupInfo.VersionedResourcesStorageMap[playlist.VERSION] = storage
Expand Down

0 comments on commit dfa06b8

Please sign in to comment.