K8s: Add grafana-apiserver config (#76649)

Co-authored-by: Kristin Laemmert <mildwonkey@users.noreply.github.com>
This commit is contained in:
Todd Treece 2023-10-17 11:29:06 -04:00 committed by GitHub
parent 7da2cc9610
commit 863f25acf7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 86 additions and 19 deletions

View File

@ -0,0 +1,38 @@
package grafanaapiserver
import (
"fmt"
"path/filepath"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/setting"
)
type config struct {
enabled bool
devMode bool
host string
appURL string
etcdServers []string
dataPath string
logLevel int
}
func newConfig(cfg *setting.Cfg) *config {
defaultLogLevel := 0
if cfg.Env == setting.Dev {
defaultLogLevel = 10
}
return &config{
enabled: cfg.IsFeatureToggleEnabled(featuremgmt.FlagGrafanaAPIServer),
devMode: cfg.Env == setting.Dev,
dataPath: filepath.Join(cfg.DataPath, "grafana-apiserver"),
host: fmt.Sprintf("%s:%s", cfg.HTTPAddr, cfg.HTTPPort),
logLevel: cfg.SectionWithEnvOverrides("grafana-apiserver").Key("log_level").MustInt(defaultLogLevel),
etcdServers: cfg.SectionWithEnvOverrides("grafana-apiserver").Key("etcd_servers").Strings(","),
appURL: cfg.AppURL,
}
}

View File

@ -0,0 +1,36 @@
package grafanaapiserver
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/setting"
)
func TestNewConfig(t *testing.T) {
cfg := setting.NewCfg()
cfg.Env = setting.Dev
cfg.DataPath = "/tmp/grafana"
cfg.HTTPAddr = "test"
cfg.HTTPPort = "4000"
cfg.IsFeatureToggleEnabled = func(_ string) bool { return true }
cfg.AppURL = "http://test:4000"
section := cfg.Raw.Section("grafana-apiserver")
section.Key("log_level").SetValue("5")
section.Key("etcd_servers").SetValue("http://localhost:2379")
actual := newConfig(cfg)
expected := &config{
enabled: true,
devMode: true,
etcdServers: []string{"http://localhost:2379"},
appURL: "http://test:4000",
host: "test:4000",
dataPath: "/tmp/grafana/grafana-apiserver",
logLevel: 5,
}
require.Equal(t, expected, actual)
}

View File

@ -37,7 +37,6 @@ import (
"github.com/grafana/grafana/pkg/modules"
"github.com/grafana/grafana/pkg/registry"
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/web"
)
@ -91,12 +90,9 @@ type RestConfigProvider interface {
type service struct {
*services.BasicService
restConfig *clientrest.Config
etcdServers []string
config *config
restConfig *clientrest.Config
enabled bool
logLevel int
dataPath string
stopCh chan struct{}
stoppedCh chan error
@ -113,14 +109,11 @@ func ProvideService(
authz authorizer.Authorizer,
) (*service, error) {
s := &service{
logLevel: cfg.SectionWithEnvOverrides("grafana-apiserver").Key("log_level").MustInt(10),
etcdServers: cfg.SectionWithEnvOverrides("grafana-apiserver").Key("etcd_servers").Strings(","),
enabled: cfg.IsFeatureToggleEnabled(featuremgmt.FlagGrafanaAPIServer),
rr: rr,
dataPath: path.Join(cfg.DataPath, "k8s"),
stopCh: make(chan struct{}),
builders: []APIGroupBuilder{},
authorizer: authz,
config: newConfig(cfg),
rr: rr,
stopCh: make(chan struct{}),
builders: []APIGroupBuilder{},
authorizer: authz,
}
// This will be used when running as a dskit service
@ -157,7 +150,7 @@ func (s *service) GetRestConfig() *clientrest.Config {
}
func (s *service) IsDisabled() bool {
return !s.enabled
return !s.config.enabled
}
// Run is an adapter for the BackgroundService interface.
@ -173,14 +166,14 @@ func (s *service) RegisterAPI(builder APIGroupBuilder) {
}
func (s *service) start(ctx context.Context) error {
logger := logr.New(newLogAdapter(s.logLevel))
logger := logr.New(newLogAdapter(s.config.logLevel))
klog.SetLoggerWithOptions(logger, klog.ContextualLogger(true))
o := options.NewRecommendedOptions("", unstructured.UnstructuredJSONScheme)
o.SecureServing.BindPort = 6443
o.Authentication.RemoteKubeConfigFileOptional = true
o.Authorization.RemoteKubeConfigFileOptional = true
o.Etcd.StorageConfig.Transport.ServerList = s.etcdServers
o.Etcd.StorageConfig.Transport.ServerList = s.config.etcdServers
o.Admission = nil
o.CoreAPI = nil
@ -190,7 +183,7 @@ func (s *service) start(ctx context.Context) error {
// Get the util to get the paths to pre-generated certs
certUtil := certgenerator.CertUtil{
K8sDataPath: s.dataPath,
K8sDataPath: s.config.dataPath,
}
if err := certUtil.InitializeCACertPKI(); err != nil {
@ -357,7 +350,7 @@ func (s *service) writeKubeConfiguration(restConfig *clientrest.Config) error {
CurrentContext: "default-context",
AuthInfos: authinfos,
}
return clientcmd.WriteToFile(clientConfig, path.Join(s.dataPath, "grafana.kubeconfig"))
return clientcmd.WriteToFile(clientConfig, path.Join(s.config.dataPath, "grafana.kubeconfig"))
}
func newAuthenticator(cert *x509.Certificate) (authenticator.Request, error) {