Skip to content

Commit

Permalink
metrics: add component-base/metrics.Options
Browse files Browse the repository at this point in the history
Signed-off-by: scydas <scyda@outlook.com>
  • Loading branch information
scydas committed Aug 29, 2024
1 parent 5afaf71 commit 358a263
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 6 additions & 0 deletions cmd/apiserver/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"k8s.io/component-base/featuregate"
"k8s.io/component-base/logs"
logsapi "k8s.io/component-base/logs/api/v1"
"k8s.io/component-base/metrics"

"github.com/clusterpedia-io/clusterpedia/pkg/apiserver"
generatedopenapi "github.com/clusterpedia-io/clusterpedia/pkg/generated/openapi"
Expand All @@ -37,6 +38,7 @@ type ClusterPediaServerOptions struct {
CoreAPI *genericoptions.CoreAPIOptions
FeatureGate featuregate.FeatureGate
Traces *genericoptions.TracingOptions
Metrics *metrics.Options

Storage *storageoptions.StorageOptions
}
Expand Down Expand Up @@ -66,6 +68,7 @@ func NewServerOptions() *ClusterPediaServerOptions {
CoreAPI: genericoptions.NewCoreAPIOptions(),
FeatureGate: feature.DefaultFeatureGate,
Traces: genericoptions.NewTracingOptions(),
Metrics: metrics.NewOptions(),

Storage: storageoptions.NewStorageOptions(),
}
Expand All @@ -75,6 +78,7 @@ func (o *ClusterPediaServerOptions) Validate() error {
errors := []error{}
errors = append(errors, o.validateGenericOptions()...)
errors = append(errors, o.Storage.Validate()...)
errors = append(errors, o.Metrics.Validate()...)

return utilerrors.NewAggregate(errors)
}
Expand All @@ -83,6 +87,7 @@ func (o *ClusterPediaServerOptions) Config() (*apiserver.Config, error) {
if err := o.Validate(); err != nil {
return nil, err
}
o.Metrics.Apply()

storage, err := storage.NewStorageFactory(o.Storage.Name, o.Storage.ConfigPath)
if err != nil {
Expand Down Expand Up @@ -170,6 +175,7 @@ func (o *ClusterPediaServerOptions) Flags() cliflag.NamedFlagSets {
o.Features.AddFlags(fss.FlagSet("features"))
logsapi.AddFlags(o.Logs, fss.FlagSet("logs"))
o.Traces.AddFlags(fss.FlagSet("traces"))
o.Metrics.AddFlags(fss.FlagSet("metrics"))

o.Storage.AddFlags(fss.FlagSet("storage"))
return fss
Expand Down
10 changes: 9 additions & 1 deletion pkg/metrics/server/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strconv"

"github.com/spf13/pflag"
"k8s.io/component-base/metrics"
)

type Options struct {
Expand All @@ -13,18 +14,22 @@ type Options struct {

TLSConfig string
DisableGZIPEncoding bool

Metrics *metrics.Options
}

func NewOptions() *Options {
return &Options{
Host: "::",
Port: 8081,
DisableGZIPEncoding: false,

Metrics: metrics.NewOptions(),
}
}

func (o *Options) Validate() []error {
return nil
return o.Metrics.Validate()
}

func (o *Options) AddFlags(fs *pflag.FlagSet) {
Expand All @@ -33,9 +38,12 @@ func (o *Options) AddFlags(fs *pflag.FlagSet) {

fs.BoolVar(&o.DisableGZIPEncoding, "metrics-disable-gzip-encoding", o.DisableGZIPEncoding, "Gzip responses when requested by clients via 'Accept-Encoding: gzip' header.")
fs.StringVar(&o.TLSConfig, "metrics-tls-config", o.TLSConfig, "Path to the TLS configuration file of metrics")
o.Metrics.AddFlags(fs)
}

func (o *Options) Config() (config Config) {
o.Metrics.Apply()

return Config{
Endpoint: net.JoinHostPort(o.Host, strconv.Itoa(o.Port)),
TLSConfig: o.TLSConfig,
Expand Down

0 comments on commit 358a263

Please sign in to comment.