Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit ea2e81d

Browse files
committed
Expose metrics listen address
Signed-off-by: JoshVanL <[email protected]>
1 parent 2a93892 commit ea2e81d

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

cmd/app/options/app.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
type KubeOIDCProxyOptions struct {
1414
DisableImpersonation bool
1515
ReadinessProbePort int
16+
MetricsListenAddress string
1617

1718
FlushInterval time.Duration
1819

@@ -43,6 +44,10 @@ func (k *KubeOIDCProxyOptions) AddFlags(fs *pflag.FlagSet) *KubeOIDCProxyOptions
4344
fs.IntVarP(&k.ReadinessProbePort, "readiness-probe-port", "P", 8080,
4445
"Port to expose readiness probe.")
4546

47+
fs.StringVar(&k.MetricsListenAddress, "metrics-serving-address", "0.0.0.0:80",
48+
"Adress to serving metrics on at the /metrics path. An empty address will "+
49+
"disable serving metrics.")
50+
4651
fs.DurationVar(&k.FlushInterval, "flush-interval", time.Millisecond*50,
4752
"Specifies the interval to flush request bodies. If 0ms, "+
4853
"no periodic flushing is done. A negative value means to flush "+

cmd/app/run.go

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ import (
77
"github.com/spf13/cobra"
88
"k8s.io/apiserver/pkg/server"
99
"k8s.io/client-go/rest"
10+
"k8s.io/klog"
1011

1112
"github.com/jetstack/kube-oidc-proxy/cmd/app/options"
13+
"github.com/jetstack/kube-oidc-proxy/pkg/metrics"
1214
"github.com/jetstack/kube-oidc-proxy/pkg/probe"
1315
"github.com/jetstack/kube-oidc-proxy/pkg/proxy"
16+
"github.com/jetstack/kube-oidc-proxy/pkg/proxy/hooks"
1417
"github.com/jetstack/kube-oidc-proxy/pkg/proxy/tokenreview"
1518
"github.com/jetstack/kube-oidc-proxy/pkg/util"
1619
)
@@ -38,6 +41,14 @@ func buildRunCommand(stopCh <-chan struct{}, opts *options.Options) *cobra.Comma
3841
return err
3942
}
4043

44+
// Initialise hooks handler
45+
hooks := hooks.New()
46+
defer func() {
47+
if err := hooks.RunPreShutdownHooks(); err != nil {
48+
klog.Errorf("failed to run shut down hooks: %s", err)
49+
}
50+
}()
51+
4152
// Here we determine to either use custom or 'in-cluster' client configuration
4253
var err error
4354
var restConfig *rest.Config
@@ -57,10 +68,14 @@ func buildRunCommand(stopCh <-chan struct{}, opts *options.Options) *cobra.Comma
5768
}
5869
}
5970

71+
// Initialise metrics handler
72+
metrics := metrics.New()
73+
hooks.AddPreShutdownHook("Metrics", metrics.Shutdown)
74+
6075
// Initialise token reviewer if enabled
6176
var tokenReviewer *tokenreview.TokenReview
6277
if opts.App.TokenPassthrough.Enabled {
63-
tokenReviewer, err = tokenreview.New(restConfig, opts.App.TokenPassthrough.Audiences)
78+
tokenReviewer, err = tokenreview.New(restConfig, metrics, opts.App.TokenPassthrough.Audiences)
6479
if err != nil {
6580
return err
6681
}
@@ -85,7 +100,7 @@ func buildRunCommand(stopCh <-chan struct{}, opts *options.Options) *cobra.Comma
85100

86101
// Initialise proxy with OIDC token authenticator
87102
p, err := proxy.New(restConfig, opts.OIDCAuthentication, opts.Audit,
88-
tokenReviewer, secureServingInfo, proxyConfig)
103+
tokenReviewer, secureServingInfo, hooks, metrics, proxyConfig)
89104
if err != nil {
90105
return err
91106
}
@@ -97,10 +112,20 @@ func buildRunCommand(stopCh <-chan struct{}, opts *options.Options) *cobra.Comma
97112
}
98113

99114
// Start readiness probe
100-
if err := probe.Run(strconv.Itoa(opts.App.ReadinessProbePort),
101-
fakeJWT, p.OIDCTokenAuthenticator()); err != nil {
115+
readinessHandler, err := probe.Run(
116+
strconv.Itoa(opts.App.ReadinessProbePort), fakeJWT, p.OIDCTokenAuthenticator())
117+
if err != nil {
102118
return err
103119
}
120+
hooks.AddPreShutdownHook("Readiness Probe", readinessHandler.Shutdown)
121+
122+
if len(opts.App.MetricsListenAddress) > 0 {
123+
if err := metrics.Start(opts.App.MetricsListenAddress); err != nil {
124+
return err
125+
}
126+
} else {
127+
klog.Info("metrics listen address empty, disabling serving")
128+
}
104129

105130
// Run proxy
106131
waitCh, err := p.Run(stopCh)
@@ -110,10 +135,6 @@ func buildRunCommand(stopCh <-chan struct{}, opts *options.Options) *cobra.Comma
110135

111136
<-waitCh
112137

113-
if err := p.RunPreShutdownHooks(); err != nil {
114-
return err
115-
}
116-
117138
return nil
118139
},
119140
}

0 commit comments

Comments
 (0)