Chore: Remove standalone apiserver options (#97349)

This commit is contained in:
Todd Treece 2024-12-03 11:42:52 -05:00 committed by GitHub
parent 0ea0c35198
commit ec454e04c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 0 additions and 482 deletions

View File

@ -1,41 +0,0 @@
package options
import (
"time"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/setting"
"github.com/spf13/pflag"
genericapiserver "k8s.io/apiserver/pkg/server"
)
type LoggingOptions struct {
logger log.Logger
Level string
}
func NewLoggingOptions(logger log.Logger) *LoggingOptions {
return &LoggingOptions{
logger: logger,
}
}
func (o *LoggingOptions) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.Level, "grafana.log.level", "debug", "Log level, debug, info, warn, error.")
}
func (o *LoggingOptions) Validate() []error {
return nil
}
func (o *LoggingOptions) ApplyTo(c *genericapiserver.RecommendedConfig) error {
err := log.SetupConsoleLogger(o.Level)
if err != nil {
return err
}
o.logger.Info("Starting grafana-apiserver", "version", setting.BuildVersion, "commit", setting.BuildCommit, "branch", setting.BuildBranch, "compiled", time.Unix(setting.BuildStamp, 0))
o.logger.Debug("Console logging initialized", "logLevel", o.Level)
return nil
}

View File

@ -1,42 +0,0 @@
package options
import (
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/setting"
"github.com/prometheus/client_golang/prometheus"
"github.com/spf13/pflag"
genericapiserver "k8s.io/apiserver/pkg/server"
)
type MetricsOptions struct {
logger log.Logger
Enabled bool
MetricsRegisterer prometheus.Registerer
}
func NewMetricsOptions(logger log.Logger) *MetricsOptions {
return &MetricsOptions{
logger: logger,
}
}
func (o *MetricsOptions) AddFlags(fs *pflag.FlagSet) {
fs.BoolVar(&o.Enabled, "grafana.metrics.enable", false, "Enable metrics and Prometheus /metrics endpoint.")
}
func (o *MetricsOptions) Validate() []error {
return nil
}
func (o *MetricsOptions) ApplyTo(c *genericapiserver.RecommendedConfig) error {
c.EnableMetrics = o.Enabled
o.MetricsRegisterer = metrics.ProvideRegisterer()
metrics.SetBuildInformation(o.MetricsRegisterer, setting.BuildVersion, setting.BuildCommit, setting.BuildBranch, setting.BuildStamp)
if o.Enabled {
o.logger.Debug("Metrics enabled")
}
return nil
}

View File

@ -1,216 +0,0 @@
package options
import (
"fmt"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/services/apiserver/options"
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/runtime"
genericapiserver "k8s.io/apiserver/pkg/server"
genericoptions "k8s.io/apiserver/pkg/server/options"
)
type Options struct {
LoggingOptions *LoggingOptions
ExtraOptions *options.ExtraOptions
RecommendedOptions *genericoptions.RecommendedOptions
TracingOptions *TracingOptions
MetricsOptions *MetricsOptions
ProfilingOptions *ProfilingOptions
ServerRunOptions *genericoptions.ServerRunOptions
StorageOptions *options.StorageOptions
}
func New(logger log.Logger, codec runtime.Codec) *Options {
return &Options{
LoggingOptions: NewLoggingOptions(logger),
ExtraOptions: options.NewExtraOptions(),
RecommendedOptions: options.NewRecommendedOptions(codec),
TracingOptions: NewTracingOptions(logger),
MetricsOptions: NewMetricsOptions(logger),
ProfilingOptions: NewProfilingOptions(logger),
ServerRunOptions: genericoptions.NewServerRunOptions(),
StorageOptions: options.NewStorageOptions(),
}
}
func (o *Options) AddFlags(fs *pflag.FlagSet) {
o.LoggingOptions.AddFlags(fs)
o.ExtraOptions.AddFlags(fs)
o.RecommendedOptions.AddFlags(fs)
o.TracingOptions.AddFlags(fs)
o.MetricsOptions.AddFlags(fs)
o.ProfilingOptions.AddFlags(fs)
o.ServerRunOptions.AddUniversalFlags(fs)
o.internalFlags(fs)
}
func (o *Options) Validate() []error {
if errs := o.LoggingOptions.Validate(); len(errs) != 0 {
return errs
}
if errs := o.ExtraOptions.Validate(); len(errs) != 0 {
return errs
}
if errs := o.TracingOptions.Validate(); len(errs) != 0 {
return errs
}
if errs := o.MetricsOptions.Validate(); len(errs) != 0 {
return errs
}
if errs := o.ProfilingOptions.Validate(); len(errs) != 0 {
return errs
}
if errs := o.ServerRunOptions.Validate(); len(errs) != 0 {
return errs
}
if errs := o.internalFlagsValidate(); len(errs) != 0 {
return errs
}
// NOTE: we don't call validate on the top level recommended options as it doesn't like skipping etcd-servers
// the function is left here for troubleshooting any other config issues
// errors = append(errors, o.RecommendedOptions.Validate()...)
if errs := o.RecommendedOptions.SecureServing.Validate(); len(errs) != 0 {
return errs
}
if o.ExtraOptions.DevMode {
// NOTE: Only consider authn for dev mode - resolves the failure due to missing extension apiserver auth-config
// in parent k8s
if errs := o.RecommendedOptions.Authentication.Validate(); len(errs) != 0 {
return errs
}
}
return nil
}
// A copy of ApplyTo in recommended.go, but for >= 0.28, server pkg in apiserver does a bit extra causing
// a panic when CoreAPI is set to nil
func (o *Options) ModifiedApplyTo(config *genericapiserver.RecommendedConfig) error {
if err := o.RecommendedOptions.Etcd.ApplyTo(&config.Config); err != nil {
return err
}
if err := o.RecommendedOptions.EgressSelector.ApplyTo(&config.Config); err != nil {
return err
}
if err := o.RecommendedOptions.Traces.ApplyTo(config.Config.EgressSelector, &config.Config); err != nil {
return err
}
if err := o.RecommendedOptions.SecureServing.ApplyTo(&config.Config.SecureServing, &config.Config.LoopbackClientConfig); err != nil {
return err
}
if err := o.RecommendedOptions.Authentication.ApplyTo(&config.Config.Authentication, config.SecureServing, config.OpenAPIConfig); err != nil {
return err
}
if err := o.RecommendedOptions.Authorization.ApplyTo(&config.Config.Authorization); err != nil {
return err
}
if err := o.RecommendedOptions.Audit.ApplyTo(&config.Config); err != nil {
return err
}
// TODO: determine whether we need flow control (API priority and fairness)
// We can't assume that a shared informers config was provided in standalone mode and will need a guard
// when enabling below
/* kubeClient, err := kubernetes.NewForConfig(config.ClientConfig)
if err != nil {
return err
}
if err := o.RecommendedOptions.Features.ApplyTo(&config.Config, kubeClient, config.SharedInformerFactory); err != nil {
return err
} */
if err := o.RecommendedOptions.CoreAPI.ApplyTo(config); err != nil {
return err
}
_, err := o.RecommendedOptions.ExtraAdmissionInitializers(config)
if err != nil {
return err
}
if err := o.ServerRunOptions.ApplyTo(&config.Config); err != nil {
return err
}
return nil
}
func (o *Options) ApplyTo(serverConfig *genericapiserver.RecommendedConfig) error {
if o.LoggingOptions != nil {
if err := o.LoggingOptions.ApplyTo(serverConfig); err != nil {
return err
}
}
if o.ExtraOptions != nil {
if err := o.ExtraOptions.ApplyTo(serverConfig); err != nil {
return err
}
}
if o.RecommendedOptions.CoreAPI == nil {
if err := o.ModifiedApplyTo(serverConfig); err != nil {
return err
}
} else {
if err := o.RecommendedOptions.ApplyTo(serverConfig); err != nil {
return err
}
}
if o.TracingOptions != nil {
if err := o.TracingOptions.ApplyTo(serverConfig); err != nil {
return err
}
}
if o.MetricsOptions != nil {
if err := o.MetricsOptions.ApplyTo(serverConfig); err != nil {
return err
}
}
if o.ProfilingOptions != nil {
if err := o.ProfilingOptions.ApplyTo(serverConfig); err != nil {
return err
}
}
if o.ServerRunOptions != nil {
if err := o.ServerRunOptions.ApplyTo(&serverConfig.Config); err != nil {
return err
}
}
return nil
}
func (o *Options) internalFlags(fs *pflag.FlagSet) {
// We also want to be able to set the MaxRequestSize by using flags. This is
// usually not exposed by k8s. The value is already set to the upstream default
// at this stage, so we can use it as the default.
// Reference: https://github.com/kubernetes/kubernetes/blob/v1.31.1/staging/src/k8s.io/apiserver/pkg/server/config.go#L453
fs.Int64Var(&o.ServerRunOptions.MaxRequestBodyBytes, "max-request-body-bytes",
o.ServerRunOptions.MaxRequestBodyBytes, ""+
"Specifies the maximum allowable size for a request payload sent to the API server in bytes."+
"The default is 3MB.")
}
func (o *Options) internalFlagsValidate() []error {
if o.ServerRunOptions.MaxRequestBodyBytes < 0 {
return []error{fmt.Errorf("--max-request-body-bytes can not be a negative value")}
}
return nil
}

View File

@ -1,46 +0,0 @@
package options
import (
"runtime"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/spf13/pflag"
genericapiserver "k8s.io/apiserver/pkg/server"
)
type ProfilingOptions struct {
logger log.Logger
blockProfilingRate int
mutexProfilingRate int
}
func NewProfilingOptions(logger log.Logger) *ProfilingOptions {
return &ProfilingOptions{
logger: logger,
}
}
func (o *ProfilingOptions) AddFlags(fs *pflag.FlagSet) {
fs.IntVar(&o.blockProfilingRate, "grafana.profiling.block-rate", 0, "Controls the fraction of goroutine blocking events that are reported in the blocking profile. The profiler aims to sample an average of one blocking event per rate nanoseconds spent blocked. To turn off profiling entirely, use 0.")
fs.IntVar(&o.mutexProfilingRate, "grafana.profiling.mutex-rate", 0, "Controls the fraction of mutex contention events that are reported in the mutex profile. On average 1/rate events are reported. To turn off mutex profiling entirely, use 0.")
}
func (o *ProfilingOptions) Validate() []error {
return nil
}
func (o *ProfilingOptions) ApplyTo(config *genericapiserver.RecommendedConfig) error {
if !config.EnableProfiling {
return nil
}
// We bring our own block/mutex profiling configuration
config.EnableContentionProfiling = false
runtime.SetBlockProfileRate(o.blockProfilingRate)
runtime.SetMutexProfileFraction(o.mutexProfilingRate)
o.logger.Info("Profiling enabled", "blockProfileRate", o.blockProfilingRate, "mutexProfileRate", o.mutexProfilingRate)
return nil
}

View File

@ -1,137 +0,0 @@
package options
import (
"context"
"errors"
"fmt"
"net/url"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/tracing"
"github.com/grafana/grafana/pkg/setting"
"github.com/spf13/pflag"
"go.opentelemetry.io/otel/attribute"
genericfeatures "k8s.io/apiserver/pkg/features"
genericapiserver "k8s.io/apiserver/pkg/server"
utilfeature "k8s.io/apiserver/pkg/util/feature"
k8stracing "k8s.io/component-base/tracing"
)
type TracingOptions struct {
logger log.Logger
JaegerAddress string
JaegerPropagation string
OTLPAddress string
OTLPPropagation string
ServiceName string
Tags map[string]string
SamplerType string
SamplerParam float64
SamplingServiceURL string
TracingService *tracing.TracingService
}
func NewTracingOptions(logger log.Logger) *TracingOptions {
return &TracingOptions{
logger: logger,
Tags: map[string]string{},
}
}
func (o *TracingOptions) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.JaegerAddress, "grafana.tracing.jaeger.address", "", "Tracing Jaeger exporter destination, e.g. http://localhost:14268/api/traces. This enabled the Jaeger export and takes presedence over grafana.tracing.otlp.")
fs.StringVar(&o.JaegerPropagation, "grafana.tracing.jaeger.propagation", "jaeger", "Tracing Jaeger propagation specifies the text map propagation format, w3c or jaeger.")
fs.StringVar(&o.OTLPAddress, "grafana.tracing.otlp.address", "", "Tracing OTLP exporter destination, e.g. localhost:4317.")
fs.StringVar(&o.OTLPPropagation, "grafana.tracing.otlp.propagation", "w3c", "Tracing OTLP propagation specifies the text map propagation format, w3c or jaeger.")
fs.StringVar(&o.ServiceName, "grafana.tracing.service-name", "grafana-apiserver", "Override the default service name, grafana-apiserver.")
fs.StringToStringVar(&o.Tags, "grafana.tracing.tag", map[string]string{}, "Tracing server tag in 'key=value' format. Specify multiple times to add many.")
fs.StringVar(&o.SamplerType, "grafana.tracing.sampler-type", "const", "Tracing sampler type specifies the type of the sampler: const, probabilistic, rateLimiting, or remote.")
fs.Float64Var(&o.SamplerParam, "grafana.tracing.sampler-param", 0, "Tracing sampler configuration parameter. For 'const' sampler, 0 or 1 for always false/true respectively. For 'rateLimiting' sampler, the number of spans per second. For 'remote' sampler, param is the same as for 'probabilistic' and indicates the initial sampling rate before the actual one is received from the sampling service.")
fs.StringVar(&o.SamplingServiceURL, "grafana.tracing.sampling-service", "", "Tracing server sampling service URL (used for both Jaeger and OTLP) if grafana.tracing.sampler-type=remote.")
}
func (o *TracingOptions) Validate() []error {
errs := []error{}
if o.JaegerAddress != "" {
if _, err := url.Parse(o.JaegerAddress); err != nil {
errs = append(errs, fmt.Errorf("failed to parse tracing.jaeger.address: %w", err))
}
}
if o.SamplingServiceURL != "" {
if _, err := url.Parse(o.SamplingServiceURL); err != nil {
errs = append(errs, fmt.Errorf("failed to parse tracing.sampling-service: %w", err))
}
}
if o.ServiceName == "" {
errs = append(errs, errors.New("grafana.tracing.service-name cannot be empty"))
}
return errs
}
func (o *TracingOptions) ApplyTo(config *genericapiserver.RecommendedConfig) error {
if err := utilfeature.DefaultMutableFeatureGate.SetFromMap(map[string]bool{
string(genericfeatures.APIServerTracing): false,
}); err != nil {
return err
}
tracingCfg := tracing.NewEmptyTracingConfig()
var err error
if o.OTLPAddress != "" {
tracingCfg, err = tracing.NewOTLPTracingConfig(o.OTLPAddress, o.OTLPPropagation)
}
if o.JaegerAddress != "" {
tracingCfg, err = tracing.NewJaegerTracingConfig(o.JaegerAddress, o.JaegerPropagation)
}
if err != nil {
return err
}
tracingCfg.ServiceName = o.ServiceName
tracingCfg.ServiceVersion = setting.BuildVersion
for k, v := range o.Tags {
tracingCfg.CustomAttribs = append(tracingCfg.CustomAttribs, attribute.String(k, v))
}
tracingCfg.Sampler = o.SamplerType
tracingCfg.SamplerParam = o.SamplerParam
tracingCfg.SamplerRemoteURL = o.SamplingServiceURL
tracingCfg.ProfilingIntegration = true
ts, err := tracing.ProvideService(tracingCfg)
if err != nil {
return err
}
o.TracingService = ts
config.TracerProvider = ts.GetTracerProvider()
if config.LoopbackClientConfig != nil {
config.LoopbackClientConfig.Wrap(k8stracing.WrapperFor(config.TracerProvider))
}
config.AddPostStartHookOrDie("grafana-tracing-service", func(hookCtx genericapiserver.PostStartHookContext) error {
go func() {
if err := ts.Run(hookCtx.Context); err != nil && !errors.Is(err, context.Canceled) {
o.logger.Error("failed to shutdown tracing service", "error", err)
}
}()
return nil
})
return nil
}