Skip to content

Commit cfb1389

Browse files
authored
Merge pull request kubernetes#85828 from shihan9/master
ping kmsplugin gentely when in good state
2 parents 9587832 + c084d57 commit cfb1389

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

staging/src/k8s.io/apiserver/pkg/server/options/encryptionconfig/config.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ const (
4848
aesGCMTransformerPrefixV1 = "k8s:enc:aesgcm:v1:"
4949
secretboxTransformerPrefixV1 = "k8s:enc:secretbox:v1:"
5050
kmsTransformerPrefixV1 = "k8s:enc:kms:v1:"
51-
kmsPluginHealthzTTL = 3 * time.Second
51+
kmsPluginHealthzNegativeTTL = 3 * time.Second
52+
kmsPluginHealthzPositiveTTL = 20 * time.Second
5253
)
5354

5455
type kmsPluginHealthzResponse struct {
@@ -58,6 +59,7 @@ type kmsPluginHealthzResponse struct {
5859

5960
type kmsPluginProbe struct {
6061
name string
62+
ttl time.Duration
6163
envelope.Service
6264
lastResponse *kmsPluginHealthzResponse
6365
l *sync.Mutex
@@ -112,6 +114,7 @@ func getKMSPluginProbes(reader io.Reader) ([]*kmsPluginProbe, error) {
112114

113115
result = append(result, &kmsPluginProbe{
114116
name: p.KMS.Name,
117+
ttl: kmsPluginHealthzNegativeTTL,
115118
Service: s,
116119
l: &sync.Mutex{},
117120
lastResponse: &kmsPluginHealthzResponse{},
@@ -128,22 +131,25 @@ func (h *kmsPluginProbe) Check() error {
128131
h.l.Lock()
129132
defer h.l.Unlock()
130133

131-
if (time.Since(h.lastResponse.received)) < kmsPluginHealthzTTL {
134+
if (time.Since(h.lastResponse.received)) < h.ttl {
132135
return h.lastResponse.err
133136
}
134137

135138
p, err := h.Service.Encrypt([]byte("ping"))
136139
if err != nil {
137140
h.lastResponse = &kmsPluginHealthzResponse{err: err, received: time.Now()}
141+
h.ttl = kmsPluginHealthzNegativeTTL
138142
return fmt.Errorf("failed to perform encrypt section of the healthz check for KMS Provider %s, error: %v", h.name, err)
139143
}
140144

141145
if _, err := h.Service.Decrypt(p); err != nil {
142146
h.lastResponse = &kmsPluginHealthzResponse{err: err, received: time.Now()}
147+
h.ttl = kmsPluginHealthzNegativeTTL
143148
return fmt.Errorf("failed to perform decrypt section of the healthz check for KMS Provider %s, error: %v", h.name, err)
144149
}
145150

146151
h.lastResponse = &kmsPluginHealthzResponse{err: nil, received: time.Now()}
152+
h.ttl = kmsPluginHealthzPositiveTTL
147153
return nil
148154
}
149155

staging/src/k8s.io/apiserver/pkg/server/options/encryptionconfig/config_test.go

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ package encryptionconfig
1919
import (
2020
"bytes"
2121
"encoding/base64"
22+
"errors"
2223
"io"
2324
"io/ioutil"
2425
"os"
26+
"sync"
2527
"testing"
2628
"time"
2729

@@ -61,19 +63,31 @@ func mustConfigReader(t *testing.T, path string) io.Reader {
6163
// testEnvelopeService is a mock envelope service which can be used to simulate remote Envelope services
6264
// for testing of the envelope transformer with other transformers.
6365
type testEnvelopeService struct {
66+
err error
6467
}
6568

6669
func (t *testEnvelopeService) Decrypt(data []byte) ([]byte, error) {
70+
if t.err != nil {
71+
return nil, t.err
72+
}
6773
return base64.StdEncoding.DecodeString(string(data))
6874
}
6975

7076
func (t *testEnvelopeService) Encrypt(data []byte) ([]byte, error) {
77+
if t.err != nil {
78+
return nil, t.err
79+
}
7180
return []byte(base64.StdEncoding.EncodeToString(data)), nil
7281
}
7382

7483
// The factory method to create mock envelope service.
7584
func newMockEnvelopeService(endpoint string, timeout time.Duration) (envelope.Service, error) {
76-
return &testEnvelopeService{}, nil
85+
return &testEnvelopeService{nil}, nil
86+
}
87+
88+
// The factory method to create mock envelope service which always returns error.
89+
func newMockErrorEnvelopeService(endpoint string, timeout time.Duration) (envelope.Service, error) {
90+
return &testEnvelopeService{errors.New("test")}, nil
7791
}
7892

7993
func TestLegacyConfig(t *testing.T) {
@@ -261,6 +275,49 @@ func TestKMSPluginHealthz(t *testing.T) {
261275
}
262276
}
263277

278+
func TestKMSPluginHealthzTTL(t *testing.T) {
279+
service, _ := newMockEnvelopeService("unix:///tmp/testprovider.sock", 3*time.Second)
280+
errService, _ := newMockErrorEnvelopeService("unix:///tmp/testprovider.sock", 3*time.Second)
281+
282+
testCases := []struct {
283+
desc string
284+
probe *kmsPluginProbe
285+
wantTTL time.Duration
286+
}{
287+
{
288+
desc: "kms provider in good state",
289+
probe: &kmsPluginProbe{
290+
name: "test",
291+
ttl: kmsPluginHealthzNegativeTTL,
292+
Service: service,
293+
l: &sync.Mutex{},
294+
lastResponse: &kmsPluginHealthzResponse{},
295+
},
296+
wantTTL: kmsPluginHealthzPositiveTTL,
297+
},
298+
{
299+
desc: "kms provider in bad state",
300+
probe: &kmsPluginProbe{
301+
name: "test",
302+
ttl: kmsPluginHealthzPositiveTTL,
303+
Service: errService,
304+
l: &sync.Mutex{},
305+
lastResponse: &kmsPluginHealthzResponse{},
306+
},
307+
wantTTL: kmsPluginHealthzNegativeTTL,
308+
},
309+
}
310+
311+
for _, tt := range testCases {
312+
t.Run(tt.desc, func(t *testing.T) {
313+
tt.probe.Check()
314+
if tt.probe.ttl != tt.wantTTL {
315+
t.Fatalf("want ttl %v, got ttl %v", tt.wantTTL, tt.probe.ttl)
316+
}
317+
})
318+
}
319+
}
320+
264321
// As long as got and want contain envelope.Service we will return true.
265322
// If got has an envelope.Service and want does note (or vice versa) this will return false.
266323
func serviceComparer(_, _ envelope.Service) bool {

0 commit comments

Comments
 (0)