Skip to content

Commit ea085e0

Browse files
committed
client-go: remove import of github.com/gregjones/httpcache
1 parent 8a9954d commit ea085e0

File tree

13 files changed

+193
-107
lines changed

13 files changed

+193
-107
lines changed

pkg/kubectl/cmd/util/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ go_library(
4040
"//pkg/kubectl/plugins:go_default_library",
4141
"//pkg/kubectl/resource:go_default_library",
4242
"//pkg/kubectl/scheme:go_default_library",
43+
"//pkg/kubectl/util/transport:go_default_library",
4344
"//pkg/kubectl/validation:go_default_library",
4445
"//pkg/printers:go_default_library",
4546
"//pkg/printers/internalversion:go_default_library",

pkg/kubectl/cmd/util/factory_client_access.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"flag"
2424
"fmt"
2525
"io"
26+
"net/http"
2627
"os"
2728
"path/filepath"
2829
"regexp"
@@ -59,6 +60,7 @@ import (
5960
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
6061
"k8s.io/kubernetes/pkg/kubectl"
6162
"k8s.io/kubernetes/pkg/kubectl/resource"
63+
"k8s.io/kubernetes/pkg/kubectl/util/transport"
6264
"k8s.io/kubernetes/pkg/printers"
6365
printersinternal "k8s.io/kubernetes/pkg/printers/internalversion"
6466
)
@@ -109,7 +111,15 @@ func (f *discoveryFactory) DiscoveryClient() (discovery.CachedDiscoveryInterface
109111
return nil, err
110112
}
111113

112-
cfg.CacheDir = f.cacheDir
114+
if f.cacheDir != "" {
115+
wt := cfg.WrapTransport
116+
cfg.WrapTransport = func(rt http.RoundTripper) http.RoundTripper {
117+
if wt != nil {
118+
rt = wt(rt)
119+
}
120+
return transport.NewCacheRoundTripper(f.cacheDir, rt)
121+
}
122+
}
113123

114124
discoveryClient, err := discovery.NewDiscoveryClientForConfig(cfg)
115125
if err != nil {

pkg/kubectl/util/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ filegroup(
101101
"//pkg/kubectl/util/logs:all-srcs",
102102
"//pkg/kubectl/util/slice:all-srcs",
103103
"//pkg/kubectl/util/term:all-srcs",
104+
"//pkg/kubectl/util/transport:all-srcs",
104105
],
105106
tags = ["automanaged"],
106107
visibility = ["//build/visible_to:pkg_kubectl_util_CONSUMERS"],

pkg/kubectl/util/transport/BUILD

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
2+
3+
go_library(
4+
name = "go_default_library",
5+
srcs = ["round_tripper.go"],
6+
importpath = "k8s.io/kubernetes/pkg/kubectl/util/transport",
7+
visibility = ["//visibility:public"],
8+
deps = [
9+
"//vendor/github.com/gregjones/httpcache:go_default_library",
10+
"//vendor/github.com/gregjones/httpcache/diskcache:go_default_library",
11+
"//vendor/github.com/peterbourgon/diskv:go_default_library",
12+
],
13+
)
14+
15+
go_test(
16+
name = "go_default_test",
17+
srcs = ["round_tripper_test.go"],
18+
embed = [":go_default_library"],
19+
importpath = "k8s.io/kubernetes/pkg/kubectl/util/transport",
20+
)
21+
22+
filegroup(
23+
name = "package-srcs",
24+
srcs = glob(["**"]),
25+
tags = ["automanaged"],
26+
visibility = ["//visibility:private"],
27+
)
28+
29+
filegroup(
30+
name = "all-srcs",
31+
srcs = [":package-srcs"],
32+
tags = ["automanaged"],
33+
visibility = ["//visibility:public"],
34+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Copyright 2017 The Kubernetes 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+
// Package transport provides a round tripper capable of caching HTTP responses.
18+
package transport
19+
20+
import (
21+
"net/http"
22+
"path/filepath"
23+
24+
"github.com/gregjones/httpcache"
25+
"github.com/gregjones/httpcache/diskcache"
26+
"github.com/peterbourgon/diskv"
27+
)
28+
29+
type cacheRoundTripper struct {
30+
rt *httpcache.Transport
31+
}
32+
33+
// NewCacheRoundTripper creates a roundtripper that reads the ETag on
34+
// response headers and send the If-None-Match header on subsequent
35+
// corresponding requests.
36+
func NewCacheRoundTripper(cacheDir string, rt http.RoundTripper) http.RoundTripper {
37+
d := diskv.New(diskv.Options{
38+
BasePath: cacheDir,
39+
TempDir: filepath.Join(cacheDir, ".diskv-temp"),
40+
})
41+
t := httpcache.NewTransport(diskcache.NewWithDiskv(d))
42+
t.Transport = rt
43+
44+
return &cacheRoundTripper{rt: t}
45+
}
46+
47+
func (rt *cacheRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
48+
return rt.rt.RoundTrip(req)
49+
}
50+
51+
func (rt *cacheRoundTripper) WrappedRoundTripper() http.RoundTripper { return rt.rt.Transport }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
Copyright 2017 The Kubernetes 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+
package transport
18+
19+
import (
20+
"bytes"
21+
"io/ioutil"
22+
"net/http"
23+
"net/url"
24+
"os"
25+
"testing"
26+
)
27+
28+
// copied from k8s.io/client-go/transport/round_trippers_test.go
29+
type testRoundTripper struct {
30+
Request *http.Request
31+
Response *http.Response
32+
Err error
33+
}
34+
35+
func (rt *testRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
36+
rt.Request = req
37+
return rt.Response, rt.Err
38+
}
39+
40+
func TestCacheRoundTripper(t *testing.T) {
41+
rt := &testRoundTripper{}
42+
cacheDir, err := ioutil.TempDir("", "cache-rt")
43+
defer os.RemoveAll(cacheDir)
44+
if err != nil {
45+
t.Fatal(err)
46+
}
47+
cache := NewCacheRoundTripper(cacheDir, rt)
48+
49+
// First call, caches the response
50+
req := &http.Request{
51+
Method: http.MethodGet,
52+
URL: &url.URL{Host: "localhost"},
53+
}
54+
rt.Response = &http.Response{
55+
Header: http.Header{"ETag": []string{`"123456"`}},
56+
Body: ioutil.NopCloser(bytes.NewReader([]byte("Content"))),
57+
StatusCode: http.StatusOK,
58+
}
59+
resp, err := cache.RoundTrip(req)
60+
if err != nil {
61+
t.Fatal(err)
62+
}
63+
content, err := ioutil.ReadAll(resp.Body)
64+
if err != nil {
65+
t.Fatal(err)
66+
}
67+
if string(content) != "Content" {
68+
t.Errorf(`Expected Body to be "Content", got %q`, string(content))
69+
}
70+
71+
// Second call, returns cached response
72+
req = &http.Request{
73+
Method: http.MethodGet,
74+
URL: &url.URL{Host: "localhost"},
75+
}
76+
rt.Response = &http.Response{
77+
StatusCode: http.StatusNotModified,
78+
Body: ioutil.NopCloser(bytes.NewReader([]byte("Other Content"))),
79+
}
80+
81+
resp, err = cache.RoundTrip(req)
82+
if err != nil {
83+
t.Fatal(err)
84+
}
85+
86+
// Read body and make sure we have the initial content
87+
content, err = ioutil.ReadAll(resp.Body)
88+
resp.Body.Close()
89+
if err != nil {
90+
t.Fatal(err)
91+
}
92+
if string(content) != "Content" {
93+
t.Errorf("Invalid content read from cache %q", string(content))
94+
}
95+
}

staging/src/k8s.io/client-go/rest/config.go

-5
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@ type Config struct {
7171
// TODO: demonstrate an OAuth2 compatible client.
7272
BearerToken string
7373

74-
// CacheDir is the directory where we'll store HTTP cached responses.
75-
// If set to empty string, no caching mechanism will be used.
76-
CacheDir string
77-
7874
// Impersonate is the configuration that RESTClient will use for impersonation.
7975
Impersonate ImpersonationConfig
8076

@@ -434,7 +430,6 @@ func CopyConfig(config *Config) *Config {
434430
Username: config.Username,
435431
Password: config.Password,
436432
BearerToken: config.BearerToken,
437-
CacheDir: config.CacheDir,
438433
Impersonate: ImpersonationConfig{
439434
Groups: config.Impersonate.Groups,
440435
Extra: config.Impersonate.Extra,

staging/src/k8s.io/client-go/rest/config_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,6 @@ func TestAnonymousConfig(t *testing.T) {
267267
expected.BearerToken = ""
268268
expected.Username = ""
269269
expected.Password = ""
270-
expected.CacheDir = ""
271270
expected.AuthProvider = nil
272271
expected.AuthConfigPersister = nil
273272
expected.TLSClientConfig.CertData = nil

staging/src/k8s.io/client-go/rest/transport.go

-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ func (c *Config) TransportConfig() (*transport.Config, error) {
8989
},
9090
Username: c.Username,
9191
Password: c.Password,
92-
CacheDir: c.CacheDir,
9392
BearerToken: c.BearerToken,
9493
Impersonate: transport.ImpersonationConfig{
9594
UserName: c.Impersonate.UserName,

staging/src/k8s.io/client-go/transport/BUILD

-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ go_library(
2828
importpath = "k8s.io/client-go/transport",
2929
deps = [
3030
"//vendor/github.com/golang/glog:go_default_library",
31-
"//vendor/github.com/gregjones/httpcache:go_default_library",
32-
"//vendor/github.com/gregjones/httpcache/diskcache:go_default_library",
33-
"//vendor/github.com/peterbourgon/diskv:go_default_library",
3431
"//vendor/k8s.io/apimachinery/pkg/util/net:go_default_library",
3532
],
3633
)

staging/src/k8s.io/client-go/transport/config.go

-4
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ type Config struct {
3737
// Bearer token for authentication
3838
BearerToken string
3939

40-
// CacheDir is the directory where we'll store HTTP cached responses.
41-
// If set to empty string, no caching mechanism will be used.
42-
CacheDir string
43-
4440
// Impersonate is the config that this Config will impersonate using
4541
Impersonate ImpersonationConfig
4642

staging/src/k8s.io/client-go/transport/round_trippers.go

-31
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,10 @@ package transport
1919
import (
2020
"fmt"
2121
"net/http"
22-
"path/filepath"
2322
"strings"
2423
"time"
2524

2625
"github.com/golang/glog"
27-
"github.com/gregjones/httpcache"
28-
"github.com/gregjones/httpcache/diskcache"
29-
"github.com/peterbourgon/diskv"
3026

3127
utilnet "k8s.io/apimachinery/pkg/util/net"
3228
)
@@ -60,9 +56,6 @@ func HTTPWrappersForConfig(config *Config, rt http.RoundTripper) (http.RoundTrip
6056
len(config.Impersonate.Extra) > 0 {
6157
rt = NewImpersonatingRoundTripper(config.Impersonate, rt)
6258
}
63-
if len(config.CacheDir) > 0 {
64-
rt = NewCacheRoundTripper(config.CacheDir, rt)
65-
}
6659
return rt, nil
6760
}
6861

@@ -86,30 +79,6 @@ type requestCanceler interface {
8679
CancelRequest(*http.Request)
8780
}
8881

89-
type cacheRoundTripper struct {
90-
rt *httpcache.Transport
91-
}
92-
93-
// NewCacheRoundTripper creates a roundtripper that reads the ETag on
94-
// response headers and send the If-None-Match header on subsequent
95-
// corresponding requests.
96-
func NewCacheRoundTripper(cacheDir string, rt http.RoundTripper) http.RoundTripper {
97-
d := diskv.New(diskv.Options{
98-
BasePath: cacheDir,
99-
TempDir: filepath.Join(cacheDir, ".diskv-temp"),
100-
})
101-
t := httpcache.NewTransport(diskcache.NewWithDiskv(d))
102-
t.Transport = rt
103-
104-
return &cacheRoundTripper{rt: t}
105-
}
106-
107-
func (rt *cacheRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
108-
return rt.rt.RoundTrip(req)
109-
}
110-
111-
func (rt *cacheRoundTripper) WrappedRoundTripper() http.RoundTripper { return rt.rt.Transport }
112-
11382
type authProxyRoundTripper struct {
11483
username string
11584
groups []string

0 commit comments

Comments
 (0)