@@ -7,10 +7,13 @@ import (
7
7
"github.com/spf13/cobra"
8
8
"k8s.io/apiserver/pkg/server"
9
9
"k8s.io/client-go/rest"
10
+ "k8s.io/klog"
10
11
11
12
"github.com/jetstack/kube-oidc-proxy/cmd/app/options"
13
+ "github.com/jetstack/kube-oidc-proxy/pkg/metrics"
12
14
"github.com/jetstack/kube-oidc-proxy/pkg/probe"
13
15
"github.com/jetstack/kube-oidc-proxy/pkg/proxy"
16
+ "github.com/jetstack/kube-oidc-proxy/pkg/proxy/hooks"
14
17
"github.com/jetstack/kube-oidc-proxy/pkg/proxy/tokenreview"
15
18
"github.com/jetstack/kube-oidc-proxy/pkg/util"
16
19
)
@@ -38,6 +41,14 @@ func buildRunCommand(stopCh <-chan struct{}, opts *options.Options) *cobra.Comma
38
41
return err
39
42
}
40
43
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
+
41
52
// Here we determine to either use custom or 'in-cluster' client configuration
42
53
var err error
43
54
var restConfig * rest.Config
@@ -57,10 +68,14 @@ func buildRunCommand(stopCh <-chan struct{}, opts *options.Options) *cobra.Comma
57
68
}
58
69
}
59
70
71
+ // Initialise metrics handler
72
+ metrics := metrics .New ()
73
+ hooks .AddPreShutdownHook ("Metrics" , metrics .Shutdown )
74
+
60
75
// Initialise token reviewer if enabled
61
76
var tokenReviewer * tokenreview.TokenReview
62
77
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 )
64
79
if err != nil {
65
80
return err
66
81
}
@@ -85,7 +100,7 @@ func buildRunCommand(stopCh <-chan struct{}, opts *options.Options) *cobra.Comma
85
100
86
101
// Initialise proxy with OIDC token authenticator
87
102
p , err := proxy .New (restConfig , opts .OIDCAuthentication , opts .Audit ,
88
- tokenReviewer , secureServingInfo , proxyConfig )
103
+ tokenReviewer , secureServingInfo , hooks , metrics , proxyConfig )
89
104
if err != nil {
90
105
return err
91
106
}
@@ -97,10 +112,20 @@ func buildRunCommand(stopCh <-chan struct{}, opts *options.Options) *cobra.Comma
97
112
}
98
113
99
114
// 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 {
102
118
return err
103
119
}
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
+ }
104
129
105
130
// Run proxy
106
131
waitCh , err := p .Run (stopCh )
@@ -110,10 +135,6 @@ func buildRunCommand(stopCh <-chan struct{}, opts *options.Options) *cobra.Comma
110
135
111
136
<- waitCh
112
137
113
- if err := p .RunPreShutdownHooks (); err != nil {
114
- return err
115
- }
116
-
117
138
return nil
118
139
},
119
140
}
0 commit comments