...

Source file src/k8s.io/kubectl/pkg/rawhttp/raw.go

Documentation: k8s.io/kubectl/pkg/rawhttp

     1  /*
     2  Copyright 2019 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 rawhttp
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"fmt"
    23  	"io"
    24  	"os"
    25  
    26  	"k8s.io/cli-runtime/pkg/genericiooptions"
    27  	"k8s.io/client-go/rest"
    28  )
    29  
    30  // RawPost uses the REST client to POST content
    31  func RawPost(restClient *rest.RESTClient, streams genericiooptions.IOStreams, url, filename string) error {
    32  	return raw(restClient, streams, url, filename, "POST")
    33  }
    34  
    35  // RawPut uses the REST client to PUT content
    36  func RawPut(restClient *rest.RESTClient, streams genericiooptions.IOStreams, url, filename string) error {
    37  	return raw(restClient, streams, url, filename, "PUT")
    38  }
    39  
    40  // RawGet uses the REST client to GET content
    41  func RawGet(restClient *rest.RESTClient, streams genericiooptions.IOStreams, url string) error {
    42  	return raw(restClient, streams, url, "", "GET")
    43  }
    44  
    45  // RawDelete uses the REST client to DELETE content
    46  func RawDelete(restClient *rest.RESTClient, streams genericiooptions.IOStreams, url, filename string) error {
    47  	return raw(restClient, streams, url, filename, "DELETE")
    48  }
    49  
    50  // raw makes a simple HTTP request to the provided path on the server using the default credentials.
    51  func raw(restClient *rest.RESTClient, streams genericiooptions.IOStreams, url, filename, requestType string) error {
    52  	var data io.Reader
    53  	switch {
    54  	case len(filename) == 0:
    55  		data = bytes.NewBuffer([]byte{})
    56  
    57  	case filename == "-":
    58  		data = streams.In
    59  
    60  	default:
    61  		f, err := os.Open(filename)
    62  		if err != nil {
    63  			return err
    64  		}
    65  		defer f.Close()
    66  		data = f
    67  	}
    68  
    69  	var request *rest.Request
    70  	switch requestType {
    71  	case "GET":
    72  		request = restClient.Get().RequestURI(url)
    73  	case "PUT":
    74  		request = restClient.Put().RequestURI(url).Body(data)
    75  	case "POST":
    76  		request = restClient.Post().RequestURI(url).Body(data)
    77  	case "DELETE":
    78  		request = restClient.Delete().RequestURI(url).Body(data)
    79  
    80  	default:
    81  		return fmt.Errorf("unknown requestType: %q", requestType)
    82  	}
    83  
    84  	stream, err := request.Stream(context.TODO())
    85  	if err != nil {
    86  		return err
    87  	}
    88  	defer stream.Close()
    89  
    90  	_, err = io.Copy(streams.Out, stream)
    91  	if err != nil && err != io.EOF {
    92  		return err
    93  	}
    94  	return nil
    95  }
    96  

View as plain text