diff --git a/pkg/kubectl/cmd/util/helpers.go b/pkg/kubectl/cmd/util/helpers.go index bda52ad503b..aac691e8ef0 100644 --- a/pkg/kubectl/cmd/util/helpers.go +++ b/pkg/kubectl/cmd/util/helpers.go @@ -22,7 +22,6 @@ import ( "fmt" "io" "io/ioutil" - "net/http" "net/url" "os" "strings" @@ -361,45 +360,6 @@ func ReadConfigDataFromReader(reader io.Reader, source string) ([]byte, error) { return data, nil } -// ReadConfigData reads the bytes from the specified filesystem or network -// location or from stdin if location == "-". -// TODO: replace with resource.Builder -func ReadConfigData(location string) ([]byte, error) { - if len(location) == 0 { - return nil, fmt.Errorf("location given but empty") - } - - if location == "-" { - // Read from stdin. - return ReadConfigDataFromReader(os.Stdin, "stdin ('-')") - } - - // Use the location as a file path or URL. - return ReadConfigDataFromLocation(location) -} - -// TODO: replace with resource.Builder -func ReadConfigDataFromLocation(location string) ([]byte, error) { - // we look for http:// or https:// to determine if valid URL, otherwise do normal file IO - if strings.Index(location, "http://") == 0 || strings.Index(location, "https://") == 0 { - resp, err := http.Get(location) - if err != nil { - return nil, fmt.Errorf("unable to access URL %s: %v\n", location, err) - } - defer resp.Body.Close() - if resp.StatusCode != 200 { - return nil, fmt.Errorf("unable to read URL, server reported %d %s", resp.StatusCode, resp.Status) - } - return ReadConfigDataFromReader(resp.Body, location) - } else { - file, err := os.Open(location) - if err != nil { - return nil, fmt.Errorf("unable to read %s: %v\n", location, err) - } - return ReadConfigDataFromReader(file, location) - } -} - // Merge requires JSON serialization // TODO: merge assumes JSON serialization, and does not properly abstract API retrieval func Merge(codec runtime.Codec, dst runtime.Object, fragment, kind string) (runtime.Object, error) { diff --git a/pkg/kubectl/cmd/util/helpers_test.go b/pkg/kubectl/cmd/util/helpers_test.go index 10422e676b7..95c68070d9a 100644 --- a/pkg/kubectl/cmd/util/helpers_test.go +++ b/pkg/kubectl/cmd/util/helpers_test.go @@ -20,7 +20,6 @@ import ( "fmt" "io/ioutil" "net/http" - "net/http/httptest" "reflect" "strings" "syscall" @@ -212,65 +211,6 @@ func (f *fileHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) { res.Write(f.data) } -func TestReadConfigData(t *testing.T) { - httpData := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} - // TODO: Close() this server when fix #19254 - server := httptest.NewServer(&fileHandler{data: httpData}) - - fileData := []byte{11, 12, 13, 14, 15, 16, 17, 18, 19} - f, err := ioutil.TempFile("", "config") - if err != nil { - t.Errorf("unexpected error setting up config file") - t.Fail() - } - defer syscall.Unlink(f.Name()) - ioutil.WriteFile(f.Name(), fileData, 0644) - // TODO: test TLS here, requires making it possible to inject the HTTP client. - - tests := []struct { - config string - data []byte - expectErr bool - }{ - { - config: server.URL, - data: httpData, - }, - { - config: server.URL + "/error", - expectErr: true, - }, - { - config: "http://some.non.existent.foobar", - expectErr: true, - }, - { - config: f.Name(), - data: fileData, - }, - { - config: "some-non-existent-file", - expectErr: true, - }, - { - config: "", - expectErr: true, - }, - } - for _, test := range tests { - dataOut, err := ReadConfigData(test.config) - if err != nil && !test.expectErr { - t.Errorf("unexpected err: %v for %s", err, test.config) - } - if err == nil && test.expectErr { - t.Errorf("unexpected non-error for %s", test.config) - } - if !test.expectErr && !reflect.DeepEqual(test.data, dataOut) { - t.Errorf("unexpected data: %v, expected %v", dataOut, test.data) - } - } -} - func TestCheckInvalidErr(t *testing.T) { tests := []struct { err error