Skip to content

Commit

Permalink
Merge pull request #70 from digitalocean/http-transport-option
Browse files Browse the repository at this point in the history
kube: allow wrapping the underlying http.RoundTripper
  • Loading branch information
bouk authored Nov 25, 2019
2 parents 73f0904 + 7ef7080 commit d658b20
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
4 changes: 4 additions & 0 deletions kube/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ func NewClient(opts ...Option) (*Client, error) {
return nil, err
}
config.Timeout = defOpts.timeout
if defOpts.transportWrapper != nil {
config.Wrap(defOpts.transportWrapper)
}

client, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
Expand Down
35 changes: 35 additions & 0 deletions kube/objects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package kube
import (
"context"
"errors"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -70,3 +71,37 @@ func TestNewClientErrors(t *testing.T) {
assert.Equal(t, errors.New("cannot specify yaml and kubeconfig file paths"), err)
})
}

type failTransport struct{}

func (failTransport) RoundTrip(_ *http.Request) (*http.Response, error) {
return nil, errors.New("fail")
}

func TestNewClientRoundTripper(t *testing.T) {
client, err := NewClient(WithTransportWrapper(func(_ http.RoundTripper) http.RoundTripper {
return failTransport{}
}), WithYaml([]byte(`apiVersion: v1
kind: Config
clusters:
- cluster:
server: http://localhost
name: cool
contexts:
- context:
cluster: cool
user: admin
name: cool
current-context: cool
users:
- name: admin
`)))
assert.NoError(t, err)
_, err = client.KubeClient.CoreV1().Namespaces().Create(&corev1.Namespace{
TypeMeta: metav1.TypeMeta{Kind: "Namespace", APIVersion: "v1"},
ObjectMeta: metav1.ObjectMeta{
Name: "kube-system",
},
})
assert.Contains(t, err.Error(), "fail")
}
21 changes: 17 additions & 4 deletions kube/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ package kube

import (
"errors"
"net/http"
"time"
)

const delimiter = ":"

type options struct {
paths []string
kubeContext string
yaml []byte
timeout time.Duration
paths []string
kubeContext string
yaml []byte
transportWrapper TransportWrapper
timeout time.Duration
}

// Option function that allows injecting options while building kube.Client.
Expand Down Expand Up @@ -73,6 +75,17 @@ func WithTimeout(t time.Duration) Option {
}
}

// TransportWrapper wraps an http.RoundTripper
type TransportWrapper = func(http.RoundTripper) http.RoundTripper

// WithTransportWrapper allows wrapping the underlying http.RoundTripper
func WithTransportWrapper(f TransportWrapper) Option {
return func(o *options) error {
o.transportWrapper = f
return nil
}
}

func (o *options) validate() error {
if o.yaml != nil && len(o.paths) != 0 {
return errors.New("cannot specify yaml and kubeconfig file paths")
Expand Down

0 comments on commit d658b20

Please sign in to comment.