@@ -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