|
1 | 1 | package kubernetes |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "encoding/json" |
5 | 6 | "fmt" |
| 7 | + "io" |
6 | 8 | "io/ioutil" |
7 | 9 | "net/http" |
8 | 10 | "net/url" |
9 | 11 |
|
10 | 12 | "github.com/docker/cli/cli/command" |
11 | 13 | "github.com/docker/cli/cli/command/formatter" |
12 | 14 | "github.com/docker/cli/cli/command/stack/options" |
13 | | - "github.com/docker/go-connections/tlsconfig" |
14 | 15 | "github.com/pkg/errors" |
15 | 16 | "github.com/sirupsen/logrus" |
16 | 17 | core_v1 "k8s.io/api/core/v1" |
@@ -86,44 +87,22 @@ func getUserVisibleNamespaces(dockerCli command.Cli) (*core_v1.NamespaceList, er |
86 | 87 | if err != nil { |
87 | 88 | return nil, err |
88 | 89 | } |
89 | | - endpoint.Scheme = "https" |
90 | | - endpoint.Path = "/kubernetesNamespaces" |
| 90 | + url := "https://" + endpoint.Host + "/kubernetesNamespaces" |
91 | 91 | res := core_v1.NamespaceList{} |
92 | | - return &res, makeRequest(dockerCli, *endpoint, func(resp http.Response) error { |
93 | | - body, err := ioutil.ReadAll(resp.Body) |
| 92 | + err = dockerCli.Client().CustomRequest(context.Background(), "GET", url, nil, nil, nil, func(statusCode int, body io.Reader) error { |
| 93 | + bytes, err := ioutil.ReadAll(body) |
94 | 94 | if err != nil { |
95 | | - return errors.Wrapf(err, "received %d status and unable to read response", resp.StatusCode) |
| 95 | + return errors.Wrapf(err, "received %d status and unable to read response", statusCode) |
96 | 96 | } |
97 | | - if resp.StatusCode != http.StatusOK { |
98 | | - return fmt.Errorf(string(body)) |
| 97 | + if statusCode != http.StatusOK { |
| 98 | + return fmt.Errorf(string(bytes)) |
99 | 99 | } |
100 | | - if err := json.Unmarshal(body, &res); err != nil { |
101 | | - return errors.Wrapf(err, "unmarshal failed: %s", string(body)) |
| 100 | + if err := json.Unmarshal(bytes, &res); err != nil { |
| 101 | + return errors.Wrapf(err, "unmarshal failed: %s", string(bytes)) |
102 | 102 | } |
103 | 103 | return nil |
104 | 104 | }) |
105 | | -} |
106 | | - |
107 | | -func makeRequest(dockerCli command.Cli, endpoint url.URL, handler func(resp http.Response) error) error { |
108 | | - tlsOptions := dockerCli.ClientInfo().TLSOptions |
109 | | - if tlsOptions == nil { |
110 | | - return fmt.Errorf("missing TLS options for https") |
111 | | - } |
112 | | - tlsConfig, err := tlsconfig.Client(*tlsOptions) |
113 | | - if err != nil { |
114 | | - return err |
115 | | - } |
116 | | - httpClient := http.Client{ |
117 | | - Transport: &http.Transport{ |
118 | | - TLSClientConfig: tlsConfig, |
119 | | - }, |
120 | | - } |
121 | | - resp, err := httpClient.Get(endpoint.String()) |
122 | | - if err != nil { |
123 | | - return err |
124 | | - } |
125 | | - defer resp.Body.Close() |
126 | | - return handler(*resp) |
| 105 | + return &res, err |
127 | 106 | } |
128 | 107 |
|
129 | 108 | func getStacksWithNamespaces(kubeCli *KubeCli, opts options.List) ([]*formatter.Stack, error) { |
|
0 commit comments