Skip to content

Commit 57737ba

Browse files
authored
Merge pull request #1084 from luisdavim/client_reties
2 parents 5beaf80 + 26bdaec commit 57737ba

File tree

4 files changed

+102
-12
lines changed

4 files changed

+102
-12
lines changed

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ require (
3636
github.com/spf13/pflag v1.0.5
3737
github.com/wI2L/jsondiff v0.6.0
3838
golang.org/x/text v0.18.0
39-
helm.sh/helm/v3 v3.16.1
39+
helm.sh/helm/v3 v3.16.3
4040
k8s.io/api v0.31.1
4141
k8s.io/apiextensions-apiserver v0.31.1
4242
k8s.io/apimachinery v0.31.1
@@ -65,11 +65,11 @@ require (
6565
github.com/blang/semver/v4 v4.0.0 // indirect
6666
github.com/cespare/xxhash/v2 v2.3.0 // indirect
6767
github.com/chai2010/gettext-go v1.0.2 // indirect
68-
github.com/containerd/containerd v1.7.20 // indirect
69-
github.com/containerd/errdefs v0.1.0 // indirect
68+
github.com/containerd/containerd v1.7.23 // indirect
69+
github.com/containerd/errdefs v0.3.0 // indirect
7070
github.com/containerd/log v0.1.0 // indirect
7171
github.com/containerd/platforms v0.2.1 // indirect
72-
github.com/cyphar/filepath-securejoin v0.3.1 // indirect
72+
github.com/cyphar/filepath-securejoin v0.3.4 // indirect
7373
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
7474
github.com/distribution/reference v0.6.0 // indirect
7575
github.com/docker/cli v27.1.2+incompatible // indirect

go.sum

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,21 @@ github.com/chai2010/gettext-go v1.0.2 h1:1Lwwip6Q2QGsAdl/ZKPCwTe9fe0CjlUbqj5bFNS
5454
github.com/chai2010/gettext-go v1.0.2/go.mod h1:y+wnP2cHYaVj19NZhYKAwEMH2CI1gNHeQQ+5AjwawxA=
5555
github.com/containerd/cgroups v1.1.0 h1:v8rEWFl6EoqHB+swVNjVoCJE8o3jX7e8nqBGPLaDFBM=
5656
github.com/containerd/cgroups v1.1.0/go.mod h1:6ppBcbh/NOOUU+dMKrykgaBnK9lCIBxHqJDGwsa1mIw=
57-
github.com/containerd/containerd v1.7.20 h1:Sl6jQYk3TRavaU83h66QMbI2Nqg9Jm6qzwX57Vsn1SQ=
58-
github.com/containerd/containerd v1.7.20/go.mod h1:52GsS5CwquuqPuLncsXwG0t2CiUce+KsNHJZQJvAgR0=
57+
github.com/containerd/containerd v1.7.23 h1:H2CClyUkmpKAGlhQp95g2WXHfLYc7whAuvZGBNYOOwQ=
58+
github.com/containerd/containerd v1.7.23/go.mod h1:7QUzfURqZWCZV7RLNEn1XjUCQLEf0bkaK4GjUaZehxw=
5959
github.com/containerd/continuity v0.4.2 h1:v3y/4Yz5jwnvqPKJJ+7Wf93fyWoCB3F5EclWG023MDM=
6060
github.com/containerd/continuity v0.4.2/go.mod h1:F6PTNCKepoxEaXLQp3wDAjygEnImnZ/7o4JzpodfroQ=
61-
github.com/containerd/errdefs v0.1.0 h1:m0wCRBiu1WJT/Fr+iOoQHMQS/eP5myQ8lCv4Dz5ZURM=
62-
github.com/containerd/errdefs v0.1.0/go.mod h1:YgWiiHtLmSeBrvpw+UfPijzbLaB77mEG1WwJTDETIV0=
61+
github.com/containerd/errdefs v0.3.0 h1:FSZgGOeK4yuT/+DnF07/Olde/q4KBoMsaamhXxIMDp4=
62+
github.com/containerd/errdefs v0.3.0/go.mod h1:+YBYIdtsnF4Iw6nWZhJcqGSg/dwvV7tyJ/kCkyJ2k+M=
6363
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
6464
github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo=
6565
github.com/containerd/platforms v0.2.1 h1:zvwtM3rz2YHPQsF2CHYM8+KtB5dvhISiXh5ZpSBQv6A=
6666
github.com/containerd/platforms v0.2.1/go.mod h1:XHCb+2/hzowdiut9rkudds9bE5yJ7npe7dG/wG+uFPw=
6767
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
6868
github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY=
6969
github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
70-
github.com/cyphar/filepath-securejoin v0.3.1 h1:1V7cHiaW+C+39wEfpH6XlLBQo3j/PciWFrgfCLS8XrE=
71-
github.com/cyphar/filepath-securejoin v0.3.1/go.mod h1:F7i41x/9cBF7lzCrVsYs9fuzwRZm4NQsGTBdpp6mETc=
70+
github.com/cyphar/filepath-securejoin v0.3.4 h1:VBWugsJh2ZxJmLFSM06/0qzQyiQX2Qs0ViKrUAcqdZ8=
71+
github.com/cyphar/filepath-securejoin v0.3.4/go.mod h1:8s/MCNJREmFK0H02MF6Ihv1nakJe4L/w3WZLHNkvlYM=
7272
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7373
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7474
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
@@ -274,6 +274,8 @@ github.com/moby/spdystream v0.4.0 h1:Vy79D6mHeJJjiPdFEL2yku1kl0chZpJfZcPpb16BRl8
274274
github.com/moby/spdystream v0.4.0/go.mod h1:xBAYlnt/ay+11ShkdFKNAG7LsyK/tmNBVvVOwrfMgdI=
275275
github.com/moby/sys/mountinfo v0.6.2 h1:BzJjoreD5BMFNmD9Rus6gdd1pLuecOFPt8wC+Vygl78=
276276
github.com/moby/sys/mountinfo v0.6.2/go.mod h1:IJb6JQeOklcdMU9F5xQ8ZALD+CUr5VlGpwtX+VE0rpI=
277+
github.com/moby/sys/userns v0.1.0 h1:tVLXkFOxVu9A64/yh59slHVv9ahO9UIev4JZusOLG/g=
278+
github.com/moby/sys/userns v0.1.0/go.mod h1:IHUYgu/kao6N8YZlp9Cf444ySSvCmDlmzUcYfDHOl28=
277279
github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0=
278280
github.com/moby/term v0.5.0/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y=
279281
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
@@ -499,8 +501,8 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
499501
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
500502
gotest.tools/v3 v3.4.0 h1:ZazjZUfuVeZGLAmlKKuyv3IKP5orXcwtOwDQH6YVr6o=
501503
gotest.tools/v3 v3.4.0/go.mod h1:CtbdzLSsqVhDgMtKsx03ird5YTGB3ar27v0u/yKBW5g=
502-
helm.sh/helm/v3 v3.16.1 h1:cER6tI/8PgUAsaJaQCVBUg3VI9KN4oVaZJgY60RIc0c=
503-
helm.sh/helm/v3 v3.16.1/go.mod h1:r+xBHHP20qJeEqtvBXMf7W35QDJnzY/eiEBzt+TfHps=
504+
helm.sh/helm/v3 v3.16.3 h1:kb8bSxMeRJ+knsK/ovvlaVPfdis0X3/ZhYCSFRP+YmY=
505+
helm.sh/helm/v3 v3.16.3/go.mod h1:zeVWGDR4JJgiRbT3AnNsjYaX8OTJlIE9zC+Q7F7iUSU=
504506
k8s.io/api v0.31.1 h1:Xe1hX/fPW3PXYYv8BlozYqw63ytA92snr96zMW9gWTU=
505507
k8s.io/api v0.31.1/go.mod h1:sbN1g6eY6XVLeqNsZGLnI5FwVseTrZX7Fv3O26rhAaI=
506508
k8s.io/apiextensions-apiserver v0.31.1 h1:L+hwULvXx+nvTYX/MKM3kKMZyei+UiSXQWciX/N6E40=

internal/kube/client.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package kube
1818

1919
import (
2020
"fmt"
21+
"net/http"
2122
"sync"
2223

2324
"k8s.io/apimachinery/pkg/api/meta"
@@ -130,6 +131,10 @@ func (c *MemoryRESTClientGetter) ToRESTConfig() (*rest.Config, error) {
130131
if c.cfg == nil {
131132
return nil, fmt.Errorf("MemoryRESTClientGetter has no REST config")
132133
}
134+
// add retries to fix temporary "etcdserver: leader changed" errors from kube-apiserver
135+
c.cfg.Wrap(func(rt http.RoundTripper) http.RoundTripper {
136+
return &retryingRoundTripper{wrapped: rt}
137+
})
133138
return c.cfg, nil
134139
}
135140

internal/kube/roundtripper.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Copyright The Helm Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// TODO: this file was taken from https://github.com/helm/helm/blob/main/pkg/cli/roundtripper.go
18+
// We should be able to get rid of it once https://github.com/helm/helm/issues/13052 is addressed
19+
// A PR (https://github.com/helm/helm/pull/13383) was open and merged but not yet picked into any release
20+
package kube
21+
22+
import (
23+
"bytes"
24+
"encoding/json"
25+
"io"
26+
"net/http"
27+
"strings"
28+
)
29+
30+
type retryingRoundTripper struct {
31+
wrapped http.RoundTripper
32+
}
33+
34+
func (rt *retryingRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
35+
return rt.roundTrip(req, 1, nil)
36+
}
37+
38+
func (rt *retryingRoundTripper) roundTrip(req *http.Request, retry int, prevResp *http.Response) (*http.Response, error) {
39+
if retry < 0 {
40+
return prevResp, nil
41+
}
42+
resp, rtErr := rt.wrapped.RoundTrip(req)
43+
if rtErr != nil {
44+
return resp, rtErr
45+
}
46+
if resp.StatusCode < 500 {
47+
return resp, rtErr
48+
}
49+
if resp.Header.Get("content-type") != "application/json" {
50+
return resp, rtErr
51+
}
52+
b, err := io.ReadAll(resp.Body)
53+
resp.Body.Close()
54+
if err != nil {
55+
return resp, rtErr
56+
}
57+
58+
var ke kubernetesError
59+
r := bytes.NewReader(b)
60+
err = json.NewDecoder(r).Decode(&ke)
61+
r.Seek(0, io.SeekStart)
62+
resp.Body = io.NopCloser(r)
63+
if err != nil {
64+
return resp, rtErr
65+
}
66+
if ke.Code < 500 {
67+
return resp, rtErr
68+
}
69+
// Matches messages like "etcdserver: leader changed"
70+
if strings.HasSuffix(ke.Message, "etcdserver: leader changed") {
71+
return rt.roundTrip(req, retry-1, resp)
72+
}
73+
// Matches messages like "rpc error: code = Unknown desc = raft proposal dropped"
74+
if strings.HasSuffix(ke.Message, "raft proposal dropped") {
75+
return rt.roundTrip(req, retry-1, resp)
76+
}
77+
return resp, rtErr
78+
}
79+
80+
type kubernetesError struct {
81+
Message string `json:"message"`
82+
Code int `json:"code"`
83+
}

0 commit comments

Comments
 (0)