2024-02-01 17:27:30 -05:00
package options
import (
"fmt"
2024-03-11 10:23:03 +01:00
"net"
2024-02-01 17:27:30 -05:00
"github.com/spf13/pflag"
genericapiserver "k8s.io/apiserver/pkg/server"
"k8s.io/apiserver/pkg/server/options"
2024-07-09 15:08:13 -07:00
2024-08-30 10:59:42 +01:00
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/setting"
2024-02-01 17:27:30 -05:00
)
type StorageType string
const (
2024-07-25 18:17:39 +02:00
StorageTypeFile StorageType = "file"
StorageTypeEtcd StorageType = "etcd"
StorageTypeLegacy StorageType = "legacy"
StorageTypeUnified StorageType = "unified"
StorageTypeUnifiedGrpc StorageType = "unified-grpc"
2024-02-01 17:27:30 -05:00
)
type StorageOptions struct {
2024-08-27 13:31:40 -03:00
StorageType StorageType
DataPath string
Address string
2024-08-30 10:59:42 +01:00
UnifiedStorageConfig map [ string ] setting . UnifiedStorageConfig
2024-08-27 13:31:40 -03:00
DualWriterDataSyncJobEnabled map [ string ] bool
2024-02-01 17:27:30 -05:00
}
func NewStorageOptions ( ) * StorageOptions {
return & StorageOptions {
StorageType : StorageTypeLegacy ,
2024-03-11 10:23:03 +01:00
Address : "localhost:10000" ,
2024-02-01 17:27:30 -05:00
}
}
func ( o * StorageOptions ) AddFlags ( fs * pflag . FlagSet ) {
fs . StringVar ( ( * string ) ( & o . StorageType ) , "grafana-apiserver-storage-type" , string ( o . StorageType ) , "Storage type" )
2024-03-05 10:34:47 -08:00
fs . StringVar ( & o . DataPath , "grafana-apiserver-storage-path" , o . DataPath , "Storage path for file storage" )
2024-03-11 10:23:03 +01:00
fs . StringVar ( & o . Address , "grafana-apiserver-storage-address" , o . Address , "Remote grpc address endpoint" )
2024-02-01 17:27:30 -05:00
}
func ( o * StorageOptions ) Validate ( ) [ ] error {
errs := [ ] error { }
switch o . StorageType {
2024-07-25 18:17:39 +02:00
case StorageTypeFile , StorageTypeEtcd , StorageTypeLegacy , StorageTypeUnified , StorageTypeUnifiedGrpc :
2024-02-01 17:27:30 -05:00
// no-op
default :
2024-07-25 18:17:39 +02:00
errs = append ( errs , fmt . Errorf ( "--grafana-apiserver-storage-type must be one of %s, %s, %s, %s, %s" , StorageTypeFile , StorageTypeEtcd , StorageTypeLegacy , StorageTypeUnified , StorageTypeUnifiedGrpc ) )
2024-02-01 17:27:30 -05:00
}
2024-03-11 10:23:03 +01:00
if _ , _ , err := net . SplitHostPort ( o . Address ) ; err != nil {
errs = append ( errs , fmt . Errorf ( "--grafana-apiserver-storage-address must be a valid network address: %v" , err ) )
}
2024-02-01 17:27:30 -05:00
return errs
}
func ( o * StorageOptions ) ApplyTo ( serverConfig * genericapiserver . RecommendedConfig , etcdOptions * options . EtcdOptions ) error {
// TODO: move storage setup here
return nil
}
2024-08-30 10:59:42 +01:00
// EnforceFeatureToggleAfterMode1 makes sure there is a feature toggle set for resources with DualWriterMode > 1.
// This is needed to ensure that we use the K8s client before enabling dual writing.
func ( o * StorageOptions ) EnforceFeatureToggleAfterMode1 ( features featuremgmt . FeatureToggles ) error {
if o . StorageType != StorageTypeLegacy {
for rg , s := range o . UnifiedStorageConfig {
if s . DualWriterMode > 1 {
switch rg {
case "playlists.playlist.grafana.app" :
if ! features . IsEnabledGlobally ( featuremgmt . FlagKubernetesPlaylists ) {
return fmt . Errorf ( "feature toggle FlagKubernetesPlaylists to be set" )
}
}
}
}
}
return nil
}