Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor adding logging flags #546

Merged
merged 1 commit into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions cmd/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main
import (
"crypto/tls"
"crypto/x509"
"flag"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -151,10 +150,13 @@ func (cmd *PrometheusAdapter) addFlags() {
cmd.Flags().StringVar(&cmd.AdapterConfigFile, "config", cmd.AdapterConfigFile,
"Configuration file containing details of how to transform between Prometheus metrics "+
"and custom metrics API resources")
cmd.Flags().DurationVar(&cmd.MetricsRelistInterval, "metrics-relist-interval", cmd.MetricsRelistInterval, ""+
cmd.Flags().DurationVar(&cmd.MetricsRelistInterval, "metrics-relist-interval", cmd.MetricsRelistInterval,
"interval at which to re-list the set of all available metrics from Prometheus")
cmd.Flags().DurationVar(&cmd.MetricsMaxAge, "metrics-max-age", cmd.MetricsMaxAge, ""+
cmd.Flags().DurationVar(&cmd.MetricsMaxAge, "metrics-max-age", cmd.MetricsMaxAge,
"period for which to query the set of available metrics from Prometheus")

// Add logging flags
logs.AddFlags(cmd.Flags())
}

func (cmd *PrometheusAdapter) loadConfig() error {
Expand Down Expand Up @@ -295,10 +297,6 @@ func main() {
cmd.OpenAPIConfig.Info.Version = "1.0.0"

cmd.addFlags()
// make sure we get klog flags
local := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
logs.AddGoFlags(local)
cmd.Flags().AddGoFlagSet(local)
if err := cmd.Flags().Parse(os.Args); err != nil {
klog.Fatalf("unable to parse flags: %v", err)
}
Expand Down
31 changes: 31 additions & 0 deletions cmd/adapter/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,34 @@ func TestParseHeaderArgs(t *testing.T) {
}
}
}

func TestFlags(t *testing.T) {
cmd := &PrometheusAdapter{
PrometheusURL: "https://localhost",
}
cmd.addFlags()

flags := cmd.FlagSet
if flags == nil {
t.Fatalf("FlagSet should not be nil")
}

expectedFlags := []struct {
flag string
defaultValue string
}{
{flag: "v", defaultValue: "0"}, // logging flag (klog)
{flag: "prometheus-url", defaultValue: "https://localhost"}, // default is set in cmd
}

for _, e := range expectedFlags {
flag := flags.Lookup(e.flag)
if flag == nil {
t.Errorf("Flag %q expected to be present, was absent", e.flag)
continue
}
if flag.DefValue != e.defaultValue {
t.Errorf("Expected default value %q for flag %q, got %q", e.defaultValue, e.flag, flag.DefValue)
}
}
}