Commit ea085e0a authored by Eric Chiang's avatar Eric Chiang

client-go: remove import of github.com/gregjones/httpcache

parent 8a9954d4
...@@ -40,6 +40,7 @@ go_library( ...@@ -40,6 +40,7 @@ go_library(
"//pkg/kubectl/plugins:go_default_library", "//pkg/kubectl/plugins:go_default_library",
"//pkg/kubectl/resource:go_default_library", "//pkg/kubectl/resource:go_default_library",
"//pkg/kubectl/scheme:go_default_library", "//pkg/kubectl/scheme:go_default_library",
"//pkg/kubectl/util/transport:go_default_library",
"//pkg/kubectl/validation:go_default_library", "//pkg/kubectl/validation:go_default_library",
"//pkg/printers:go_default_library", "//pkg/printers:go_default_library",
"//pkg/printers/internalversion:go_default_library", "//pkg/printers/internalversion:go_default_library",
......
...@@ -23,6 +23,7 @@ import ( ...@@ -23,6 +23,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
"net/http"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
...@@ -59,6 +60,7 @@ import ( ...@@ -59,6 +60,7 @@ import (
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset" "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
"k8s.io/kubernetes/pkg/kubectl" "k8s.io/kubernetes/pkg/kubectl"
"k8s.io/kubernetes/pkg/kubectl/resource" "k8s.io/kubernetes/pkg/kubectl/resource"
"k8s.io/kubernetes/pkg/kubectl/util/transport"
"k8s.io/kubernetes/pkg/printers" "k8s.io/kubernetes/pkg/printers"
printersinternal "k8s.io/kubernetes/pkg/printers/internalversion" printersinternal "k8s.io/kubernetes/pkg/printers/internalversion"
) )
...@@ -109,7 +111,15 @@ func (f *discoveryFactory) DiscoveryClient() (discovery.CachedDiscoveryInterface ...@@ -109,7 +111,15 @@ func (f *discoveryFactory) DiscoveryClient() (discovery.CachedDiscoveryInterface
return nil, err return nil, err
} }
cfg.CacheDir = f.cacheDir if f.cacheDir != "" {
wt := cfg.WrapTransport
cfg.WrapTransport = func(rt http.RoundTripper) http.RoundTripper {
if wt != nil {
rt = wt(rt)
}
return transport.NewCacheRoundTripper(f.cacheDir, rt)
}
}
discoveryClient, err := discovery.NewDiscoveryClientForConfig(cfg) discoveryClient, err := discovery.NewDiscoveryClientForConfig(cfg)
if err != nil { if err != nil {
......
...@@ -101,6 +101,7 @@ filegroup( ...@@ -101,6 +101,7 @@ filegroup(
"//pkg/kubectl/util/logs:all-srcs", "//pkg/kubectl/util/logs:all-srcs",
"//pkg/kubectl/util/slice:all-srcs", "//pkg/kubectl/util/slice:all-srcs",
"//pkg/kubectl/util/term:all-srcs", "//pkg/kubectl/util/term:all-srcs",
"//pkg/kubectl/util/transport:all-srcs",
], ],
tags = ["automanaged"], tags = ["automanaged"],
visibility = ["//build/visible_to:pkg_kubectl_util_CONSUMERS"], visibility = ["//build/visible_to:pkg_kubectl_util_CONSUMERS"],
......
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = ["round_tripper.go"],
importpath = "k8s.io/kubernetes/pkg/kubectl/util/transport",
visibility = ["//visibility:public"],
deps = [
"//vendor/github.com/gregjones/httpcache:go_default_library",
"//vendor/github.com/gregjones/httpcache/diskcache:go_default_library",
"//vendor/github.com/peterbourgon/diskv:go_default_library",
],
)
go_test(
name = "go_default_test",
srcs = ["round_tripper_test.go"],
embed = [":go_default_library"],
importpath = "k8s.io/kubernetes/pkg/kubectl/util/transport",
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package transport provides a round tripper capable of caching HTTP responses.
package transport
import (
"net/http"
"path/filepath"
"github.com/gregjones/httpcache"
"github.com/gregjones/httpcache/diskcache"
"github.com/peterbourgon/diskv"
)
type cacheRoundTripper struct {
rt *httpcache.Transport
}
// NewCacheRoundTripper creates a roundtripper that reads the ETag on
// response headers and send the If-None-Match header on subsequent
// corresponding requests.
func NewCacheRoundTripper(cacheDir string, rt http.RoundTripper) http.RoundTripper {
d := diskv.New(diskv.Options{
BasePath: cacheDir,
TempDir: filepath.Join(cacheDir, ".diskv-temp"),
})
t := httpcache.NewTransport(diskcache.NewWithDiskv(d))
t.Transport = rt
return &cacheRoundTripper{rt: t}
}
func (rt *cacheRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
return rt.rt.RoundTrip(req)
}
func (rt *cacheRoundTripper) WrappedRoundTripper() http.RoundTripper { return rt.rt.Transport }
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package transport
import (
"bytes"
"io/ioutil"
"net/http"
"net/url"
"os"
"testing"
)
// copied from k8s.io/client-go/transport/round_trippers_test.go
type testRoundTripper struct {
Request *http.Request
Response *http.Response
Err error
}
func (rt *testRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
rt.Request = req
return rt.Response, rt.Err
}
func TestCacheRoundTripper(t *testing.T) {
rt := &testRoundTripper{}
cacheDir, err := ioutil.TempDir("", "cache-rt")
defer os.RemoveAll(cacheDir)
if err != nil {
t.Fatal(err)
}
cache := NewCacheRoundTripper(cacheDir, rt)
// First call, caches the response
req := &http.Request{
Method: http.MethodGet,
URL: &url.URL{Host: "localhost"},
}
rt.Response = &http.Response{
Header: http.Header{"ETag": []string{`"123456"`}},
Body: ioutil.NopCloser(bytes.NewReader([]byte("Content"))),
StatusCode: http.StatusOK,
}
resp, err := cache.RoundTrip(req)
if err != nil {
t.Fatal(err)
}
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if string(content) != "Content" {
t.Errorf(`Expected Body to be "Content", got %q`, string(content))
}
// Second call, returns cached response
req = &http.Request{
Method: http.MethodGet,
URL: &url.URL{Host: "localhost"},
}
rt.Response = &http.Response{
StatusCode: http.StatusNotModified,
Body: ioutil.NopCloser(bytes.NewReader([]byte("Other Content"))),
}
resp, err = cache.RoundTrip(req)
if err != nil {
t.Fatal(err)
}
// Read body and make sure we have the initial content
content, err = ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
t.Fatal(err)
}
if string(content) != "Content" {
t.Errorf("Invalid content read from cache %q", string(content))
}
}
...@@ -71,10 +71,6 @@ type Config struct { ...@@ -71,10 +71,6 @@ type Config struct {
// TODO: demonstrate an OAuth2 compatible client. // TODO: demonstrate an OAuth2 compatible client.
BearerToken string BearerToken string
// CacheDir is the directory where we'll store HTTP cached responses.
// If set to empty string, no caching mechanism will be used.
CacheDir string
// Impersonate is the configuration that RESTClient will use for impersonation. // Impersonate is the configuration that RESTClient will use for impersonation.
Impersonate ImpersonationConfig Impersonate ImpersonationConfig
...@@ -434,7 +430,6 @@ func CopyConfig(config *Config) *Config { ...@@ -434,7 +430,6 @@ func CopyConfig(config *Config) *Config {
Username: config.Username, Username: config.Username,
Password: config.Password, Password: config.Password,
BearerToken: config.BearerToken, BearerToken: config.BearerToken,
CacheDir: config.CacheDir,
Impersonate: ImpersonationConfig{ Impersonate: ImpersonationConfig{
Groups: config.Impersonate.Groups, Groups: config.Impersonate.Groups,
Extra: config.Impersonate.Extra, Extra: config.Impersonate.Extra,
......
...@@ -267,7 +267,6 @@ func TestAnonymousConfig(t *testing.T) { ...@@ -267,7 +267,6 @@ func TestAnonymousConfig(t *testing.T) {
expected.BearerToken = "" expected.BearerToken = ""
expected.Username = "" expected.Username = ""
expected.Password = "" expected.Password = ""
expected.CacheDir = ""
expected.AuthProvider = nil expected.AuthProvider = nil
expected.AuthConfigPersister = nil expected.AuthConfigPersister = nil
expected.TLSClientConfig.CertData = nil expected.TLSClientConfig.CertData = nil
......
...@@ -89,7 +89,6 @@ func (c *Config) TransportConfig() (*transport.Config, error) { ...@@ -89,7 +89,6 @@ func (c *Config) TransportConfig() (*transport.Config, error) {
}, },
Username: c.Username, Username: c.Username,
Password: c.Password, Password: c.Password,
CacheDir: c.CacheDir,
BearerToken: c.BearerToken, BearerToken: c.BearerToken,
Impersonate: transport.ImpersonationConfig{ Impersonate: transport.ImpersonationConfig{
UserName: c.Impersonate.UserName, UserName: c.Impersonate.UserName,
......
...@@ -28,9 +28,6 @@ go_library( ...@@ -28,9 +28,6 @@ go_library(
importpath = "k8s.io/client-go/transport", importpath = "k8s.io/client-go/transport",
deps = [ deps = [
"//vendor/github.com/golang/glog:go_default_library", "//vendor/github.com/golang/glog:go_default_library",
"//vendor/github.com/gregjones/httpcache:go_default_library",
"//vendor/github.com/gregjones/httpcache/diskcache:go_default_library",
"//vendor/github.com/peterbourgon/diskv:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/net:go_default_library", "//vendor/k8s.io/apimachinery/pkg/util/net:go_default_library",
], ],
) )
......
...@@ -37,10 +37,6 @@ type Config struct { ...@@ -37,10 +37,6 @@ type Config struct {
// Bearer token for authentication // Bearer token for authentication
BearerToken string BearerToken string
// CacheDir is the directory where we'll store HTTP cached responses.
// If set to empty string, no caching mechanism will be used.
CacheDir string
// Impersonate is the config that this Config will impersonate using // Impersonate is the config that this Config will impersonate using
Impersonate ImpersonationConfig Impersonate ImpersonationConfig
......
...@@ -19,14 +19,10 @@ package transport ...@@ -19,14 +19,10 @@ package transport
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"path/filepath"
"strings" "strings"
"time" "time"
"github.com/golang/glog" "github.com/golang/glog"
"github.com/gregjones/httpcache"
"github.com/gregjones/httpcache/diskcache"
"github.com/peterbourgon/diskv"
utilnet "k8s.io/apimachinery/pkg/util/net" utilnet "k8s.io/apimachinery/pkg/util/net"
) )
...@@ -60,9 +56,6 @@ func HTTPWrappersForConfig(config *Config, rt http.RoundTripper) (http.RoundTrip ...@@ -60,9 +56,6 @@ func HTTPWrappersForConfig(config *Config, rt http.RoundTripper) (http.RoundTrip
len(config.Impersonate.Extra) > 0 { len(config.Impersonate.Extra) > 0 {
rt = NewImpersonatingRoundTripper(config.Impersonate, rt) rt = NewImpersonatingRoundTripper(config.Impersonate, rt)
} }
if len(config.CacheDir) > 0 {
rt = NewCacheRoundTripper(config.CacheDir, rt)
}
return rt, nil return rt, nil
} }
...@@ -86,30 +79,6 @@ type requestCanceler interface { ...@@ -86,30 +79,6 @@ type requestCanceler interface {
CancelRequest(*http.Request) CancelRequest(*http.Request)
} }
type cacheRoundTripper struct {
rt *httpcache.Transport
}
// NewCacheRoundTripper creates a roundtripper that reads the ETag on
// response headers and send the If-None-Match header on subsequent
// corresponding requests.
func NewCacheRoundTripper(cacheDir string, rt http.RoundTripper) http.RoundTripper {
d := diskv.New(diskv.Options{
BasePath: cacheDir,
TempDir: filepath.Join(cacheDir, ".diskv-temp"),
})
t := httpcache.NewTransport(diskcache.NewWithDiskv(d))
t.Transport = rt
return &cacheRoundTripper{rt: t}
}
func (rt *cacheRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
return rt.rt.RoundTrip(req)
}
func (rt *cacheRoundTripper) WrappedRoundTripper() http.RoundTripper { return rt.rt.Transport }
type authProxyRoundTripper struct { type authProxyRoundTripper struct {
username string username string
groups []string groups []string
......
...@@ -17,11 +17,7 @@ limitations under the License. ...@@ -17,11 +17,7 @@ limitations under the License.
package transport package transport
import ( import (
"bytes"
"io/ioutil"
"net/http" "net/http"
"net/url"
"os"
"reflect" "reflect"
"strings" "strings"
"testing" "testing"
...@@ -220,60 +216,3 @@ func TestAuthProxyRoundTripper(t *testing.T) { ...@@ -220,60 +216,3 @@ func TestAuthProxyRoundTripper(t *testing.T) {
} }
} }
} }
func TestCacheRoundTripper(t *testing.T) {
rt := &testRoundTripper{}
cacheDir, err := ioutil.TempDir("", "cache-rt")
defer os.RemoveAll(cacheDir)
if err != nil {
t.Fatal(err)
}
cache := NewCacheRoundTripper(cacheDir, rt)
// First call, caches the response
req := &http.Request{
Method: http.MethodGet,
URL: &url.URL{Host: "localhost"},
}
rt.Response = &http.Response{
Header: http.Header{"ETag": []string{`"123456"`}},
Body: ioutil.NopCloser(bytes.NewReader([]byte("Content"))),
StatusCode: http.StatusOK,
}
resp, err := cache.RoundTrip(req)
if err != nil {
t.Fatal(err)
}
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if string(content) != "Content" {
t.Errorf(`Expected Body to be "Content", got %q`, string(content))
}
// Second call, returns cached response
req = &http.Request{
Method: http.MethodGet,
URL: &url.URL{Host: "localhost"},
}
rt.Response = &http.Response{
StatusCode: http.StatusNotModified,
Body: ioutil.NopCloser(bytes.NewReader([]byte("Other Content"))),
}
resp, err = cache.RoundTrip(req)
if err != nil {
t.Fatal(err)
}
// Read body and make sure we have the initial content
content, err = ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
t.Fatal(err)
}
if string(content) != "Content" {
t.Errorf("Invalid content read from cache %q", string(content))
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment