Skip to content

Commit 46e9cde

Browse files
Merge pull request #29 from segmentio/yolken-update-kube-deps
Update kube dependencies
2 parents 8c96541 + 1e045fe commit 46e9cde

File tree

12 files changed

+419
-222
lines changed

12 files changed

+419
-222
lines changed

cmd/kubeapply-server/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/segmentio/stats/v4/datadog"
1818
log "github.com/sirupsen/logrus"
1919
prefixed "github.com/x-cray/logrus-prefixed-formatter"
20-
"k8s.io/klog"
20+
"k8s.io/klog/v2"
2121
)
2222

2323
func init() {

cmd/kubeapply/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/segmentio/kubeapply/cmd/kubeapply/subcmd"
77
log "github.com/sirupsen/logrus"
88
prefixed "github.com/x-cray/logrus-prefixed-formatter"
9-
"k8s.io/klog"
9+
"k8s.io/klog/v2"
1010
)
1111

1212
var (

cmd/kubestar/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/segmentio/kubeapply/cmd/kubestar/subcmd"
77
log "github.com/sirupsen/logrus"
88
prefixed "github.com/x-cray/logrus-prefixed-formatter"
9-
"k8s.io/klog"
9+
"k8s.io/klog/v2"
1010
)
1111

1212
var (

data/data.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module github.com/segmentio/kubeapply
22

3-
go 1.14
3+
go 1.15
44

55
require (
6-
github.com/Masterminds/semver/v3 v3.1.0
7-
github.com/Masterminds/sprig/v3 v3.1.0
6+
github.com/Masterminds/semver/v3 v3.1.1
7+
github.com/Masterminds/sprig/v3 v3.2.1
88
github.com/aws/aws-lambda-go v1.15.0
99
github.com/aws/aws-sdk-go v1.29.16
1010
github.com/briandowns/spinner v1.11.1
@@ -13,11 +13,8 @@ require (
1313
github.com/fatih/color v1.7.0
1414
github.com/ghodss/yaml v1.0.0
1515
github.com/gogo/protobuf v1.3.1
16-
github.com/google/go-cmp v0.4.1 // indirect
1716
github.com/google/go-github/v30 v30.0.0
1817
github.com/gorilla/mux v1.7.4
19-
github.com/imdario/mergo v0.3.11 // indirect
20-
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
2118
github.com/kr/text v0.2.0 // indirect
2219
github.com/mattn/go-colorable v0.1.4 // indirect
2320
github.com/mattn/go-isatty v0.0.11 // indirect
@@ -29,27 +26,26 @@ require (
2926
github.com/segmentio/encoding v0.2.7
3027
github.com/segmentio/stats v3.0.0+incompatible
3128
github.com/segmentio/stats/v4 v4.5.3
32-
github.com/sirupsen/logrus v1.4.2
33-
github.com/spf13/cobra v0.0.6
34-
github.com/stretchr/testify v1.6.0
29+
github.com/sirupsen/logrus v1.6.0
30+
github.com/spf13/cobra v1.1.1
31+
github.com/stretchr/testify v1.6.1
3532
github.com/stripe/skycfg v0.0.0-20200303020846-4f599970a3e6
3633
github.com/x-cray/logrus-prefixed-formatter v0.5.2
3734
github.com/zorkian/go-datadog-api v2.28.0+incompatible // indirect
38-
go.starlark.net v0.0.0-20200330013621-be5394c419b6
39-
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 // indirect
40-
golang.org/x/net v0.0.0-20200528225125-3c3fba18258b // indirect
41-
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
42-
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect
43-
google.golang.org/appengine v1.6.1 // indirect
35+
go.starlark.net v0.0.0-20201204201740-42d4f566359b
36+
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
4437
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
4538
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
4639
gopkg.in/src-d/go-git.v4 v4.13.1
4740
gopkg.in/yaml.v2 v2.3.0
4841
gopkg.in/yaml.v3 v3.0.0-20200601152816-913338de1bd2 // indirect
4942
gopkg.in/zorkian/go-datadog-api.v2 v2.28.0
50-
k8s.io/api v0.17.2
51-
k8s.io/apimachinery v0.17.2
52-
k8s.io/client-go v0.17.2
53-
k8s.io/klog v1.0.0
54-
k8s.io/kubectl v0.17.2
43+
k8s.io/api v0.20.2
44+
k8s.io/apimachinery v0.20.2
45+
k8s.io/client-go v0.20.2
46+
k8s.io/klog/v2 v2.4.0
47+
k8s.io/kubectl v0.20.2
5548
)
49+
50+
// Need to pin to older version to get around https://github.com/stripe/skycfg/issues/86.
51+
replace github.com/golang/protobuf v1.4.3 => github.com/golang/protobuf v1.3.2

go.sum

Lines changed: 331 additions & 129 deletions
Large diffs are not rendered by default.

pkg/cluster/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ type ClusterClient interface {
3535
Summary(ctx context.Context) (string, error)
3636

3737
// GetStoreValue gets the value of the given key.
38-
GetStoreValue(key string) (string, error)
38+
GetStoreValue(ctx context.Context, key string) (string, error)
3939

4040
// SetStoreValue sets the given key/value pair in the cluster.
41-
SetStoreValue(key string, value string) error
41+
SetStoreValue(ctx context.Context, key string, value string) error
4242

4343
// Config returns the config for this cluster.
4444
Config() *config.ClusterConfig

pkg/cluster/fake_client.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,12 @@ func (cc *FakeClusterClient) Summary(ctx context.Context) (string, error) {
123123
}
124124

125125
// GetStoreValue gets the value of the argument key.
126-
func (cc *FakeClusterClient) GetStoreValue(key string) (string, error) {
126+
func (cc *FakeClusterClient) GetStoreValue(ctx context.Context, key string) (string, error) {
127127
return cc.store[key], nil
128128
}
129129

130130
// SetStoreValue sets the argument key to the argument value.
131-
func (cc *FakeClusterClient) SetStoreValue(key string, value string) error {
131+
func (cc *FakeClusterClient) SetStoreValue(ctx context.Context, key string, value string) error {
132132
cc.store[key] = value
133133
return nil
134134
}
@@ -139,7 +139,10 @@ func (cc *FakeClusterClient) Config() *config.ClusterConfig {
139139
}
140140

141141
// GetNamespaceUID returns the kubernetes identifier for a given namespace in this cluster.
142-
func (cc *FakeClusterClient) GetNamespaceUID(ctx context.Context, namespace string) (string, error) {
142+
func (cc *FakeClusterClient) GetNamespaceUID(
143+
ctx context.Context,
144+
namespace string,
145+
) (string, error) {
143146
return fmt.Sprintf("ns-%s", namespace), cc.kubectlErr
144147
}
145148

pkg/cluster/kube_client.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,13 @@ func (cc *KubeClusterClient) Summary(ctx context.Context) (string, error) {
251251
}
252252

253253
// GetStoreValue gets the value of the argument key.
254-
func (cc *KubeClusterClient) GetStoreValue(key string) (string, error) {
255-
return cc.kubeStore.Get(key)
254+
func (cc *KubeClusterClient) GetStoreValue(ctx context.Context, key string) (string, error) {
255+
return cc.kubeStore.Get(ctx, key)
256256
}
257257

258258
// SetStoreValue sets the value of the argument key to the argument value.
259-
func (cc *KubeClusterClient) SetStoreValue(key string, value string) error {
260-
return cc.kubeStore.Set(key, value)
259+
func (cc *KubeClusterClient) SetStoreValue(ctx context.Context, key string, value string) error {
260+
return cc.kubeStore.Set(ctx, key, value)
261261
}
262262

263263
// Config returns this client's cluster config.
@@ -311,7 +311,7 @@ func (cc *KubeClusterClient) execApply(
311311

312312
if cc.checkApplyConsistency {
313313
log.Infof("Fetching diff event for key %s", cc.clusterKey)
314-
storeValue, err := cc.GetStoreValue(cc.clusterKey)
314+
storeValue, err := cc.GetStoreValue(ctx, cc.clusterKey)
315315
if err != nil {
316316
return nil, err
317317
}
@@ -388,5 +388,5 @@ func (cc *KubeClusterClient) execDiff(
388388
diffEventStr := string(diffEventBytes)
389389

390390
log.Infof("Setting store key value: %s, %s", cc.clusterKey, diffEventStr)
391-
return diffResult, cc.kubeStore.Set(cc.clusterKey, diffEventStr)
391+
return diffResult, cc.kubeStore.Set(ctx, cc.clusterKey, diffEventStr)
392392
}

pkg/store/leaderelection/leaderelection.go

Lines changed: 37 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
/*
22
Copyright 2015 The Kubernetes Authors.
3-
43
Licensed under the Apache License, Version 2.0 (the "License");
54
you may not use this file except in compliance with the License.
65
You may obtain a copy of the License at
7-
86
http://www.apache.org/licenses/LICENSE-2.0
9-
107
Unless required by applicable law or agreed to in writing, software
118
distributed under the License is distributed on an "AS IS" BASIS,
129
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -58,18 +55,16 @@ import (
5855
"fmt"
5956
"time"
6057

58+
log "github.com/sirupsen/logrus"
6159
"k8s.io/apimachinery/pkg/api/errors"
6260
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
6361
"k8s.io/apimachinery/pkg/util/clock"
6462
"k8s.io/apimachinery/pkg/util/runtime"
6563
"k8s.io/apimachinery/pkg/util/wait"
6664
rl "k8s.io/client-go/tools/leaderelection/resourcelock"
67-
68-
"k8s.io/klog"
6965
)
7066

7167
const (
72-
// JitterFactor is a multiplier used to add jitter to leader renewal times.
7368
JitterFactor = 1.2
7469
)
7570

@@ -107,8 +102,6 @@ func NewLeaderElector(lec LeaderElectionConfig) (*LeaderElector, error) {
107102
return &le, nil
108103
}
109104

110-
// LeaderElectionConfig contains the settings associated with a leader
111-
// election process.
112105
type LeaderElectionConfig struct {
113106
// Lock is the resource that will be used for locking
114107
Lock rl.Interface
@@ -183,17 +176,17 @@ type LeaderElector struct {
183176

184177
// clock is wrapper around time to allow for less flaky testing
185178
clock clock.Clock
186-
187-
// name is the name of the resource lock for debugging
188-
name string
189179
}
190180

191-
// Run starts the leader election loop
181+
// Run starts the leader election loop. Run will not return
182+
// before leader election loop is stopped by ctx or it has
183+
// stopped holding the leader lease
192184
func (le *LeaderElector) Run(ctx context.Context) {
185+
defer runtime.HandleCrash()
193186
defer func() {
194-
runtime.HandleCrash()
195187
le.config.Callbacks.OnStoppedLeading()
196188
}()
189+
197190
if !le.acquire(ctx) {
198191
return // ctx signalled done
199192
}
@@ -204,7 +197,8 @@ func (le *LeaderElector) Run(ctx context.Context) {
204197
}
205198

206199
// RunOrDie starts a client with the provided config or panics if the config
207-
// fails to validate.
200+
// fails to validate. RunOrDie blocks until leader election loop is
201+
// stopped by ctx or it has stopped holding the leader lease
208202
func RunOrDie(ctx context.Context, lec LeaderElectionConfig) {
209203
le, err := NewLeaderElector(lec)
210204
if err != nil {
@@ -231,16 +225,16 @@ func (le *LeaderElector) acquire(ctx context.Context) bool {
231225
defer cancel()
232226
succeeded := false
233227
desc := le.config.Lock.Describe()
234-
klog.Infof("attempting to acquire leader lease %v...", desc)
228+
log.Infof("Attempting to acquire leader lease %v...", desc)
235229
wait.JitterUntil(func() {
236-
succeeded = le.tryAcquireOrRenew()
230+
succeeded = le.tryAcquireOrRenew(ctx)
237231
le.maybeReportTransition()
238232
if !succeeded {
239-
klog.V(4).Infof("failed to acquire lease %v", desc)
233+
log.Infof("Failed to acquire lease %v", desc)
240234
return
241235
}
242236
le.config.Lock.RecordEvent("became leader")
243-
klog.Infof("successfully acquired lease %v", desc)
237+
log.Infof("Successfully acquired lease %v", desc)
244238
cancel()
245239
}, le.config.RetryPeriod, JitterFactor, true, ctx.Done())
246240
return succeeded
@@ -254,48 +248,40 @@ func (le *LeaderElector) renew(ctx context.Context) {
254248
timeoutCtx, timeoutCancel := context.WithTimeout(ctx, le.config.RenewDeadline)
255249
defer timeoutCancel()
256250
err := wait.PollImmediateUntil(le.config.RetryPeriod, func() (bool, error) {
257-
done := make(chan bool, 1)
258-
go func() {
259-
defer close(done)
260-
done <- le.tryAcquireOrRenew()
261-
}()
262-
263-
select {
264-
case <-timeoutCtx.Done():
265-
return false, fmt.Errorf("failed to tryAcquireOrRenew %s", timeoutCtx.Err())
266-
case result := <-done:
267-
return result, nil
268-
}
251+
return le.tryAcquireOrRenew(timeoutCtx), nil
269252
}, timeoutCtx.Done())
270253

271254
le.maybeReportTransition()
272255
desc := le.config.Lock.Describe()
273256
if err == nil {
274-
klog.V(5).Infof("successfully renewed lease %v", desc)
257+
log.Debugf("Successfully renewed lease %v", desc)
275258
return
276259
}
277260
le.config.Lock.RecordEvent("stopped leading")
278-
klog.Infof("failed to renew lease %v: %v", desc, err)
261+
log.Infof("Failed to renew lease %v: %v", desc, err)
279262
cancel()
280263
}, le.config.RetryPeriod, ctx.Done())
281264

282265
// if we hold the lease, give it up
283266
if le.config.ReleaseOnCancel {
284-
le.release()
267+
le.release(ctx)
285268
}
286269
}
287270

288271
// release attempts to release the leader lease if we have acquired it.
289-
func (le *LeaderElector) release() bool {
272+
func (le *LeaderElector) release(ctx context.Context) bool {
290273
if !le.IsLeader() {
291274
return true
292275
}
276+
now := metav1.Now()
293277
leaderElectionRecord := rl.LeaderElectionRecord{
294278
LeaderTransitions: le.observedRecord.LeaderTransitions,
295-
LeaseDurationSeconds: int(le.config.LeaseDuration.Seconds()),
279+
LeaseDurationSeconds: 1,
280+
RenewTime: now,
281+
AcquireTime: now,
296282
}
297-
if err := le.config.Lock.Update(leaderElectionRecord); err != nil {
298-
klog.Errorf("Failed to release lock: %v", err)
283+
if err := le.config.Lock.Update(ctx, leaderElectionRecord); err != nil {
284+
log.Errorf("Failed to release lock: %v", err)
299285
return false
300286
}
301287
le.observedRecord = leaderElectionRecord
@@ -306,7 +292,7 @@ func (le *LeaderElector) release() bool {
306292
// tryAcquireOrRenew tries to acquire a leader lease if it is not already acquired,
307293
// else it tries to renew the lease if it has already been acquired. Returns true
308294
// on success else returns false.
309-
func (le *LeaderElector) tryAcquireOrRenew() bool {
295+
func (le *LeaderElector) tryAcquireOrRenew(ctx context.Context) bool {
310296
now := metav1.Now()
311297
leaderElectionRecord := rl.LeaderElectionRecord{
312298
HolderIdentity: le.config.Lock.Identity(),
@@ -316,14 +302,14 @@ func (le *LeaderElector) tryAcquireOrRenew() bool {
316302
}
317303

318304
// 1. obtain or create the ElectionRecord
319-
oldLeaderElectionRecord, oldLeaderElectionRawRecord, err := le.config.Lock.Get()
305+
oldLeaderElectionRecord, oldLeaderElectionRawRecord, err := le.config.Lock.Get(ctx)
320306
if err != nil {
321307
if !errors.IsNotFound(err) {
322-
klog.Errorf("error retrieving resource lock %v: %v", le.config.Lock.Describe(), err)
308+
log.Errorf("Error retrieving resource lock %v: %v", le.config.Lock.Describe(), err)
323309
return false
324310
}
325-
if err = le.config.Lock.Create(leaderElectionRecord); err != nil {
326-
klog.Errorf("error initially creating leader election record: %v", err)
311+
if err = le.config.Lock.Create(ctx, leaderElectionRecord); err != nil {
312+
log.Errorf("Error initially creating leader election record: %v", err)
327313
return false
328314
}
329315
le.observedRecord = leaderElectionRecord
@@ -337,10 +323,16 @@ func (le *LeaderElector) tryAcquireOrRenew() bool {
337323
le.observedRawRecord = oldLeaderElectionRawRecord
338324
le.observedTime = le.clock.Now()
339325
}
326+
327+
// If the renew time is more than 2x the lease duration in the past, don't worry
328+
// about clock skew and just take the lock.
329+
thresholdTime := now.Time.Add(-2 * le.config.LeaseDuration)
330+
340331
if len(oldLeaderElectionRecord.HolderIdentity) > 0 &&
341332
le.observedTime.Add(le.config.LeaseDuration).After(now.Time) &&
333+
oldLeaderElectionRecord.RenewTime.Time.After(thresholdTime) &&
342334
!le.IsLeader() {
343-
klog.V(4).Infof("lock is held by %v and has not yet expired", oldLeaderElectionRecord.HolderIdentity)
335+
log.Infof("Lock is held by %v and has not yet expired", oldLeaderElectionRecord.HolderIdentity)
344336
return false
345337
}
346338

@@ -354,8 +346,8 @@ func (le *LeaderElector) tryAcquireOrRenew() bool {
354346
}
355347

356348
// update the lock itself
357-
if err = le.config.Lock.Update(leaderElectionRecord); err != nil {
358-
klog.Errorf("Failed to update lock: %v", err)
349+
if err = le.config.Lock.Update(ctx, leaderElectionRecord); err != nil {
350+
log.Errorf("Failed to update lock: %v", err)
359351
return false
360352
}
361353

0 commit comments

Comments
 (0)