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
2024-05-31 19:29:59 +02:00
grafanarest "github.com/grafana/grafana/pkg/apiserver/rest"
2024-02-01 17:27:30 -05:00
"github.com/spf13/pflag"
genericapiserver "k8s.io/apiserver/pkg/server"
"k8s.io/apiserver/pkg/server/options"
)
type StorageType string
const (
StorageTypeFile StorageType = "file"
StorageTypeEtcd StorageType = "etcd"
StorageTypeLegacy StorageType = "legacy"
StorageTypeUnified StorageType = "unified"
StorageTypeUnifiedGrpc StorageType = "unified-grpc"
)
type StorageOptions struct {
2024-05-31 19:29:59 +02:00
StorageType StorageType
DataPath string
Address string
DualWriterDesiredModes map [ string ] grafanarest . DualWriterMode
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 {
case StorageTypeFile , StorageTypeEtcd , StorageTypeLegacy , StorageTypeUnified , StorageTypeUnifiedGrpc :
// no-op
default :
errs = append ( errs , fmt . Errorf ( "--grafana-apiserver-storage-type must be one of %s, %s, %s, %s, %s" , StorageTypeFile , StorageTypeEtcd , StorageTypeLegacy , StorageTypeUnified , StorageTypeUnifiedGrpc ) )
}
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
}