Skip to content

Commit acc65db

Browse files
committed
Demonstrate plugin and health checker interfaces
1 parent 1ab84c5 commit acc65db

File tree

17 files changed

+452
-392
lines changed

17 files changed

+452
-392
lines changed

cmd/k8s-cloudkms-plugin/main.go

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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

@@ -57,6 +55,10 @@ var (
5755
)
5856

5957
func 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
}

go.mod

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ toolchain go1.22.0
77
require (
88
github.com/gogo/protobuf v1.3.2
99
github.com/golang/glog v1.2.0
10-
github.com/golang/protobuf v1.5.3
1110
github.com/google/go-cmp v0.6.0
1211
github.com/google/go-tpm v0.9.0
1312
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5
14-
github.com/prometheus/client_golang v1.18.0
13+
github.com/prometheus/client_golang v1.19.0
1514
github.com/prometheus/client_model v0.6.0
16-
github.com/stretchr/testify v1.8.4
15+
github.com/stretchr/testify v1.9.0
1716
golang.org/x/net v0.21.0
1817
golang.org/x/oauth2 v0.17.0
1918
google.golang.org/api v0.167.0
@@ -36,23 +35,23 @@ require (
3635
github.com/google/s2a-go v0.1.7 // indirect
3736
github.com/google/uuid v1.6.0
3837
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
39-
github.com/googleapis/gax-go/v2 v2.12.1 // indirect
38+
github.com/googleapis/gax-go/v2 v2.12.2 // indirect
4039
github.com/json-iterator/go v1.1.12 // indirect
4140
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
4241
github.com/modern-go/reflect2 v1.0.2 // indirect
43-
github.com/prometheus/common v0.48.0 // indirect
42+
github.com/prometheus/common v0.49.0 // indirect
4443
github.com/prometheus/procfs v0.12.0 // indirect
4544
go.opencensus.io v0.24.0 // indirect
4645
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
4746
go.opentelemetry.io/otel v1.24.0 // indirect
4847
go.opentelemetry.io/otel/metric v1.24.0 // indirect
4948
go.opentelemetry.io/otel/trace v1.24.0 // indirect
50-
golang.org/x/crypto v0.19.0 // indirect
49+
golang.org/x/crypto v0.20.0 // indirect
5150
golang.org/x/sys v0.17.0 // indirect
5251
golang.org/x/text v0.14.0 // indirect
5352
google.golang.org/appengine v1.6.8 // indirect
54-
google.golang.org/genproto/googleapis/rpc v0.0.0-20240221002015-b0ce06bbee7c // indirect
55-
google.golang.org/protobuf v1.32.0 // indirect
53+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240228224816-df926f6c8641 // indirect
54+
google.golang.org/protobuf v1.32.0
5655
gopkg.in/inf.v0 v0.9.1 // indirect
5756
gopkg.in/warnings.v0 v0.1.2 // indirect
5857
gopkg.in/yaml.v2 v2.4.0 // indirect
@@ -64,6 +63,7 @@ require (
6463

6564
require (
6665
github.com/davecgh/go-spew v1.1.1 // indirect
66+
github.com/golang/protobuf v1.5.3 // indirect
6767
github.com/pmezard/go-difflib v1.0.0 // indirect
6868
gopkg.in/yaml.v3 v3.0.1 // indirect
6969
)

go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ github.com/googleapis/enterprise-certificate-proxy v0.3.2 h1:Vie5ybvEvT75RniqhfF
7171
github.com/googleapis/enterprise-certificate-proxy v0.3.2/go.mod h1:VLSiSSBs/ksPL8kq3OBOQ6WRI2QnaFynd1DCjZ62+V0=
7272
github.com/googleapis/gax-go/v2 v2.12.1 h1:9F8GV9r9ztXyAi00gsMQHNoF51xPZm8uj1dpYt2ZETM=
7373
github.com/googleapis/gax-go/v2 v2.12.1/go.mod h1:61M8vcyyXR2kqKFxKrfA22jaA8JGF7Dc8App1U3H6jc=
74+
github.com/googleapis/gax-go/v2 v2.12.2 h1:mhN09QQW1jEWeMF74zGR81R30z4VJzjZsfkUhuHF+DA=
75+
github.com/googleapis/gax-go/v2 v2.12.2/go.mod h1:61M8vcyyXR2kqKFxKrfA22jaA8JGF7Dc8App1U3H6jc=
7476
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
7577
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
7678
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
@@ -90,11 +92,15 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
9092
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
9193
github.com/prometheus/client_golang v1.18.0 h1:HzFfmkOzH5Q8L8G+kSJKUx5dtG87sewO+FoDDqP5Tbk=
9294
github.com/prometheus/client_golang v1.18.0/go.mod h1:T+GXkCk5wSJyOqMIzVgvvjFDlkOQntgjkJWKrN5txjA=
95+
github.com/prometheus/client_golang v1.19.0 h1:ygXvpU1AoN1MhdzckN+PyD9QJOSD4x7kmXYlnfbA6JU=
96+
github.com/prometheus/client_golang v1.19.0/go.mod h1:ZRM9uEAypZakd+q/x7+gmsvXdURP+DABIEIjnmDdp+k=
9397
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
9498
github.com/prometheus/client_model v0.6.0 h1:k1v3CzpSRUTrKMppY35TLwPvxHqBu0bYgxZzqGIgaos=
9599
github.com/prometheus/client_model v0.6.0/go.mod h1:NTQHnmxFpouOD0DpvP4XujX3CdOAGQPoaGhyTchlyt8=
96100
github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSzKKE=
97101
github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc=
102+
github.com/prometheus/common v0.49.0 h1:ToNTdK4zSnPVJmh698mGFkDor9wBI/iGaJy5dbH1EgI=
103+
github.com/prometheus/common v0.49.0/go.mod h1:Kxm+EULxRbUkjGU6WFsQqo3ORzB4tyKvlWFOE9mB2sE=
98104
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
99105
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
100106
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
@@ -110,6 +116,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
110116
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
111117
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
112118
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
119+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
120+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
113121
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
114122
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
115123
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
@@ -129,6 +137,8 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
129137
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
130138
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
131139
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
140+
golang.org/x/crypto v0.20.0 h1:jmAMJJZXr5KiCw05dfYK9QnqaqKLYXijU23lsEdcQqg=
141+
golang.org/x/crypto v0.20.0/go.mod h1:Xwo95rrVNIoSMx9wa1JroENMToLWn3RNVrTBpLHgZPQ=
132142
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
133143
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
134144
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
@@ -206,6 +216,8 @@ google.golang.org/genproto/googleapis/api v0.0.0-20240205150955-31a09d347014 h1:
206216
google.golang.org/genproto/googleapis/api v0.0.0-20240205150955-31a09d347014/go.mod h1:rbHMSEDyoYX62nRVLOCc4Qt1HbsdytAYoVwgjiOhF3I=
207217
google.golang.org/genproto/googleapis/rpc v0.0.0-20240221002015-b0ce06bbee7c h1:NUsgEN92SQQqzfA+YtqYNqYmB3DMMYLlIwUZAQFVFbo=
208218
google.golang.org/genproto/googleapis/rpc v0.0.0-20240221002015-b0ce06bbee7c/go.mod h1:H4O17MA/PE9BsGx3w+a+W2VOLLD1Qf7oJneAoU6WktY=
219+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240228224816-df926f6c8641 h1:DKU1r6Tj5s1vlU/moGhuGz7E3xRfwjdAfDzbsaQJtEY=
220+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240228224816-df926f6c8641/go.mod h1:UCOku4NytXMJuLQE5VuqA5lX3PcHCBo8pxNyvkf4xBs=
209221
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
210222
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
211223
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=

0 commit comments

Comments
 (0)