@@ -19,13 +19,11 @@ import (
1919 "context"
2020 "flag"
2121 "fmt"
22- "net"
2322 "net/http"
2423 "net/url"
2524 "os"
2625 "os/signal"
2726 "path/filepath"
28- "strconv"
2927 "syscall"
3028 "time"
3129
5755)
5856
5957func main () {
58+ ctx , cancel := signal .NotifyContext (context .Background (),
59+ syscall .SIGINT , syscall .SIGTERM )
60+ defer cancel ()
61+
6062 flag .Parse ()
6163 mustValidateFlags ()
6264
@@ -69,13 +71,12 @@ func main() {
6971 // httpClient should be constructed with context.Background. Sending a context with
7072 // timeout or deadline will cause subsequent calls via the client to fail once the timeout or
7173 // deadline is triggered. Instead, the plugin supplies a context per individual calls.
72- httpClient , err = plugin .NewHTTPClient (context . Background () , * gceConf )
74+ httpClient , err = plugin .NewHTTPClient (ctx , * gceConf )
7375 if err != nil {
7476 glog .Exitf ("failed to instantiate http httpClient: %v" , err )
7577 }
7678 }
7779
78- ctx := context .Background ()
7980 kms , err := cloudkms .NewService (ctx , option .WithHTTPClient (httpClient ))
8081 if err != nil {
8182 glog .Exitf ("failed to instantiate cloud kms httpClient: %v" , err )
@@ -87,57 +88,60 @@ func main() {
8788
8889 metrics := & plugin.Metrics {
8990 ServingURL : & url.URL {
90- Host : net . JoinHostPort ("localhost" , strconv . FormatUint ( uint64 ( * metricsPort ), 10 ) ),
91+ Host : fmt . Sprintf ("localhost:%d " , * metricsPort ),
9192 Path : * metricsPath ,
9293 },
9394 }
9495
9596 var p plugin.Plugin
96- var hc plugin.HealthChecker
97-
97+ var healthChecker plugin.HealthChecker
9898 switch * kmsVersion {
9999 case "v1" :
100- p = v1 .NewPlugin (kms .Projects .Locations .KeyRings .CryptoKeys , * keyURI , * pathToUnixSocket )
101- hc = plugin .NewHealthChecker (* keyURI , kms .Projects .Locations .KeyRings .CryptoKeys , * pathToUnixSocket , * healthzTimeout , & url.URL {
102- Host : net .JoinHostPort ("localhost" , strconv .FormatUint (uint64 (* healthzPort ), 10 )),
103- Path : * healthzPath ,
104- })
100+ p = v1 .NewPlugin (kms .Projects .Locations .KeyRings .CryptoKeys , * keyURI )
101+ healthChecker = v1 .NewHealthChecker ()
105102 glog .Info ("Kubernetes KMS API v1beta1" )
106- default :
107- p = v2 .NewPlugin (kms .Projects .Locations .KeyRings .CryptoKeys , * keyURI , * keySuffix , * pathToUnixSocket )
108- hc = plugin .NewHealthChecker (* keyURI , kms .Projects .Locations .KeyRings .CryptoKeys , * pathToUnixSocket , * healthzTimeout , & url.URL {
109- Host : net .JoinHostPort ("localhost" , strconv .FormatUint (uint64 (* healthzPort ), 10 )),
110- Path : * healthzPath ,
111- })
103+ case "v2" :
104+ p = v2 .NewPlugin (kms .Projects .Locations .KeyRings .CryptoKeys , * keyURI , * keySuffix )
105+ healthChecker = v2 .NewHealthChecker ()
112106 glog .Info ("Kubernetes KMS API v2" )
107+ default :
108+ glog .Exitf ("invalid value %q for --kms" , * kmsVersion )
113109 }
114- glog .Exit (run (p , hc , metrics ))
110+
111+ hc := plugin .NewHealthChecker (healthChecker , * keyURI , kms .Projects .Locations .KeyRings .CryptoKeys , * pathToUnixSocket , * healthzTimeout , & url.URL {
112+ Host : fmt .Sprintf ("localhost:%d" , * healthzPort ),
113+ Path : * healthzPath ,
114+ })
115+
116+ pluginManager := plugin .NewManager (p , * pathToUnixSocket )
117+
118+ glog .Exit (run (pluginManager , hc , metrics ))
115119}
116120
117- func run (p plugin.Plugin , h plugin.HealthChecker , m * plugin.Metrics ) error {
121+ func run (pluginManager * plugin.PluginManager , h * plugin.HealthCheckerManager , m * plugin.Metrics ) error {
118122 signalsChan := make (chan os.Signal , 1 )
119123 signal .Notify (signalsChan , syscall .SIGINT , syscall .SIGTERM )
120124
121- metricsErrChan := m .Serve ()
122- healthzErrChan := h .Serve ()
125+ metricsErrCh := m .Serve ()
126+ healthzErrCh := h .Serve ()
123127
124- gRPCSrv , kmsErrorChan := p . ServeKMSRequests ()
128+ gRPCSrv , kmsErrorCh := pluginManager . Start ()
125129 defer gRPCSrv .GracefulStop ()
126130
127131 for {
128132 select {
129133 case sig := <- signalsChan :
130134 return fmt .Errorf ("captured %v, shutting down kms-plugin" , sig )
131- case kmsError := <- kmsErrorChan :
135+ case kmsError := <- kmsErrorCh :
132136 return kmsError
133- case metricsErr := <- metricsErrChan :
137+ case metricsErr := <- metricsErrCh :
134138 // Limiting this to warning only - will run without metrics.
135139 glog .Warning (metricsErr )
136- metricsErrChan = nil
137- case healthzErr := <- healthzErrChan :
140+ metricsErrCh = nil
141+ case healthzErr := <- healthzErrCh :
138142 // Limiting this to warning only - will run without healthz.
139143 glog .Warning (healthzErr )
140- healthzErrChan = nil
144+ healthzErrCh = nil
141145 }
142146 }
143147}
0 commit comments