Apps: Fix status key functions (#108915)

This commit is contained in:
Todd Treece
2025-07-30 17:11:39 -04:00
committed by GitHub
parent 0f53290ca7
commit 8729ea2dba
24 changed files with 129 additions and 102 deletions
+1 -1
View File
@@ -5,7 +5,7 @@ go 1.24.5
require (
github.com/google/go-cmp v0.7.0
github.com/grafana/authlib/types v0.0.0-20250710201142-9542f2f28d43
github.com/grafana/grafana-app-sdk/logging v0.40.0
github.com/grafana/grafana-app-sdk/logging v0.40.1
github.com/grafana/grafana/pkg/apimachinery v0.0.0-20250514132646-acbc7b54ed9e
github.com/prometheus/client_golang v1.22.0
github.com/stretchr/testify v1.10.0
+2 -2
View File
@@ -84,8 +84,8 @@ github.com/grafana/authlib/types v0.0.0-20250710201142-9542f2f28d43 h1:NlkGMnVi/
github.com/grafana/authlib/types v0.0.0-20250710201142-9542f2f28d43/go.mod h1:qeWYbnWzaYGl88JlL9+DsP1GT2Cudm58rLtx13fKZdw=
github.com/grafana/dskit v0.0.0-20250611075409-46f51e1ce914 h1:qcSGhr691f1mmPHwg2svGyO40Ex92G02aOyHzP6XHCE=
github.com/grafana/dskit v0.0.0-20250611075409-46f51e1ce914/go.mod h1:OiN4P4aC6LwLzLbEupH3Ue83VfQoNMfG48rsna8jI/E=
github.com/grafana/grafana-app-sdk/logging v0.40.0 h1:3LHA0pFM9mGGFyq6qoHvJOGEe0H0OVhzzlOKd4Ehu+E=
github.com/grafana/grafana-app-sdk/logging v0.40.0/go.mod h1:otUD9XpJD7A5sCLb8mcs9hIXGdeV6lnhzVwe747g4RU=
github.com/grafana/grafana-app-sdk/logging v0.40.1 h1:ru+GqbaQk6jthA5l2Yo1WI/JbNXKNQmLiqNrxz7HGP4=
github.com/grafana/grafana-app-sdk/logging v0.40.1/go.mod h1:otUD9XpJD7A5sCLb8mcs9hIXGdeV6lnhzVwe747g4RU=
github.com/grafana/grafana/pkg/apimachinery v0.0.0-20250514132646-acbc7b54ed9e h1:BTKk7LHuG1kmAkucwTA7DuMbKpKvJTKrGdBmUNO4dfQ=
github.com/grafana/grafana/pkg/apimachinery v0.0.0-20250514132646-acbc7b54ed9e/go.mod h1:IA4SOwun8QyST9c5UNs/fN37XL6boXXDvRYFcFwbipg=
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI=
@@ -134,7 +134,6 @@ func InstallAPIs(
builderMetrics *builder.BuilderMetrics,
) error {
logger := logging.FromContext(ctx)
for _, installer := range appInstallers {
logger.Debug("Installing APIs for app installer", "app", installer.ManifestData().AppName)
wrapper := &serverWrapper{
+47 -31
View File
@@ -8,6 +8,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/registry/generic"
genericregistry "k8s.io/apiserver/pkg/registry/generic/registry"
genericrest "k8s.io/apiserver/pkg/registry/rest"
genericapiserver "k8s.io/apiserver/pkg/server"
appsdkapiserver "github.com/grafana/grafana-app-sdk/k8s/apiserver"
@@ -38,17 +39,9 @@ type serverWrapper struct {
func (s *serverWrapper) InstallAPIGroup(apiGroupInfo *genericapiserver.APIGroupInfo) error {
log := logging.FromContext(s.ctx)
legacyProvider, ok := s.installer.(LegacyStorageProvider)
if !ok {
return s.GenericAPIServer.InstallAPIGroup(apiGroupInfo)
}
for v, storageMap := range apiGroupInfo.VersionedResourcesStorageMap {
for storagePath, restStorage := range storageMap {
genericStorage, ok := restStorage.(*genericregistry.Store)
if !ok {
log.Error("Expected generic registry store", "storagePath", storagePath, "version", v)
continue
}
legacyProvider, dualWriteSupported := s.installer.(LegacyStorageProvider)
resource, err := getResourceFromStoragePath(storagePath)
if err != nil {
return err
@@ -57,29 +50,28 @@ func (s *serverWrapper) InstallAPIGroup(apiGroupInfo *genericapiserver.APIGroupI
Group: s.installer.ManifestData().Group,
Resource: resource,
}
genericStorage.KeyRootFunc = grafanaregistry.KeyRootFunc(gr)
genericStorage.KeyFunc = grafanaregistry.NamespaceKeyFunc(gr)
genericStorage.UpdateStrategy = &updateStrategyWrapper{
RESTUpdateStrategy: genericStorage.UpdateStrategy,
storage := s.configureStorage(gr, dualWriteSupported, restStorage)
if unifiedStorage, ok := storage.(grafanarest.Storage); ok && dualWriteSupported {
log.Debug("Configuring dual writer for storage", "resource", gr.String(), "version", v, "storagePath", storagePath)
dw, err := NewDualWriter(
s.ctx,
gr,
s.storageOpts,
legacyProvider.GetLegacyStorage(gr.WithVersion(v)),
unifiedStorage,
s.kvStore,
s.lock,
s.namespaceMapper,
s.dualWriteService,
s.dualWriterMetrics,
s.builderMetrics,
)
if err != nil {
return err
}
storage = dw
}
dw, err := NewDualWriter(
s.ctx,
gr,
s.storageOpts,
legacyProvider.GetLegacyStorage(gr.WithVersion(v)),
grafanarest.Storage(genericStorage),
s.kvStore,
s.lock,
s.namespaceMapper,
s.dualWriteService,
s.dualWriterMetrics,
s.builderMetrics,
)
if err != nil {
return err
}
apiGroupInfo.VersionedResourcesStorageMap[v][storagePath] = dw
apiGroupInfo.VersionedResourcesStorageMap[v][storagePath] = storage
}
}
@@ -93,3 +85,27 @@ func getResourceFromStoragePath(storagePath string) (string, error) {
}
return parts[0], nil
}
func (s *serverWrapper) configureStorage(gr schema.GroupResource, dualWriteSupported bool, storage genericrest.Storage) genericrest.Storage {
if gs, ok := storage.(*genericregistry.Store); ok {
// if dual write is supported, we need to modify the update strategy
// this is not needed for the status store
if dualWriteSupported {
gs.UpdateStrategy = &updateStrategyWrapper{
RESTUpdateStrategy: gs.UpdateStrategy,
}
}
gs.KeyFunc = grafanaregistry.NamespaceKeyFunc(gr)
gs.KeyRootFunc = grafanaregistry.KeyRootFunc(gr)
return gs
}
// if the storage is a status store, we need to extract the underlying generic registry store
if statusStore, ok := storage.(*appsdkapiserver.StatusREST); ok {
statusStore.Store.KeyFunc = grafanaregistry.NamespaceKeyFunc(gr)
statusStore.Store.KeyRootFunc = grafanaregistry.KeyRootFunc(gr)
return statusStore
}
return storage
}