mirror of https://github.com/k3s-io/k3s
Add new api usage mechanism.
parent
49c25a4e28
commit
b1d8a41049
|
@ -139,27 +139,34 @@ func executeAPIRequest(method string, auth *kube_client.AuthInfo) bool {
|
||||||
return *httpServer + path.Join("/api/v1beta1", storage)
|
return *httpServer + path.Join("/api/v1beta1", storage)
|
||||||
}
|
}
|
||||||
|
|
||||||
var request *http.Request
|
verb := ""
|
||||||
var err error
|
|
||||||
switch method {
|
switch method {
|
||||||
case "get", "list":
|
case "get", "list":
|
||||||
url := readUrl(parseStorage())
|
verb = "GET"
|
||||||
if len(*selector) > 0 && method == "list" {
|
|
||||||
url = url + "?labels=" + *selector
|
|
||||||
}
|
|
||||||
request, err = http.NewRequest("GET", url, nil)
|
|
||||||
case "delete":
|
case "delete":
|
||||||
request, err = http.NewRequest("DELETE", readUrl(parseStorage()), nil)
|
verb = "DELETE"
|
||||||
case "create":
|
case "create":
|
||||||
storage := parseStorage()
|
verb = "POST"
|
||||||
request, err = cloudcfg.RequestWithBodyData(readConfig(storage), readUrl(storage), "POST")
|
|
||||||
case "update":
|
case "update":
|
||||||
storage := parseStorage()
|
verb = "PUT"
|
||||||
request, err = cloudcfg.RequestWithBodyData(readConfig(storage), readUrl(storage), "PUT")
|
|
||||||
default:
|
default:
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s := cloudcfg.New(*httpServer, auth)
|
||||||
|
r := s.Verb(verb).
|
||||||
|
Path("api/v1beta1").
|
||||||
|
Path(parseStorage()).
|
||||||
|
Selector(*selector)
|
||||||
|
if method == "create" || method == "update" {
|
||||||
|
r.Body(readConfig(parseStorage()))
|
||||||
|
}
|
||||||
|
obj, err := r.Do()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Got request error: %v\n", err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
var printer cloudcfg.ResourcePrinter
|
var printer cloudcfg.ResourcePrinter
|
||||||
if *json {
|
if *json {
|
||||||
printer = &cloudcfg.IdentityPrinter{}
|
printer = &cloudcfg.IdentityPrinter{}
|
||||||
|
@ -169,15 +176,10 @@ func executeAPIRequest(method string, auth *kube_client.AuthInfo) bool {
|
||||||
printer = &cloudcfg.HumanReadablePrinter{}
|
printer = &cloudcfg.HumanReadablePrinter{}
|
||||||
}
|
}
|
||||||
|
|
||||||
var body []byte
|
if err = printer.PrintObj(obj, os.Stdout); err != nil {
|
||||||
if body, err = cloudcfg.DoRequest(request, auth); err == nil {
|
log.Fatalf("Failed to print: %#v\nRaw received text:\n%v\n", err, string(body))
|
||||||
if err = printer.Print(body, os.Stdout); err != nil {
|
|
||||||
log.Fatalf("Failed to print: %#v\nRaw received text:\n%v\n", err, string(body))
|
|
||||||
}
|
|
||||||
fmt.Print("\n")
|
|
||||||
} else {
|
|
||||||
log.Fatalf("Error: %#v %s", err, body)
|
|
||||||
}
|
}
|
||||||
|
fmt.Print("\n")
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,16 +21,20 @@ import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
||||||
"gopkg.in/v1/yaml"
|
"gopkg.in/v1/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -90,6 +94,132 @@ func Update(name string, client client.ClientInterface, updatePeriod time.Durati
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Server contains info locating a kubernetes api server.
|
||||||
|
// Example usage:
|
||||||
|
// auth, err := LoadAuth(filename)
|
||||||
|
// s := New(url, auth)
|
||||||
|
// resp, err := s.Verb("GET").
|
||||||
|
// Path("api/v1beta1").
|
||||||
|
// Path("pods").
|
||||||
|
// Selector("area=staging").
|
||||||
|
// Timeout(10*time.Second).
|
||||||
|
// Do()
|
||||||
|
// list, ok := resp.(api.PodList)
|
||||||
|
type Server struct {
|
||||||
|
auth *client.AuthInfo
|
||||||
|
rawUrl string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new server object.
|
||||||
|
func New(serverUrl string, auth *client.AuthInfo) *Server {
|
||||||
|
return &Server{
|
||||||
|
auth: auth,
|
||||||
|
rawUrl: serverUrl,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Begin a request with a verb (GET, POST, PUT, DELETE)
|
||||||
|
func (s *Server) Verb(verb string) *Request {
|
||||||
|
return &Request{
|
||||||
|
verb: verb,
|
||||||
|
s: s,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Request allows for building up a request to a server in a chained fashion.
|
||||||
|
type Request struct {
|
||||||
|
s *Server
|
||||||
|
err error
|
||||||
|
verb string
|
||||||
|
path string
|
||||||
|
body interface{}
|
||||||
|
selector labels.Selector
|
||||||
|
timeout time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
// Append an item to the request path. You must call Path at least once.
|
||||||
|
func (r *Request) Path(item string) *Request {
|
||||||
|
if r.err != nil {
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
r.path = path.Join(r.path, item)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use the given item as a resource label selector. Optional.
|
||||||
|
func (r *Request) Selector(item string) *Request {
|
||||||
|
if r.err != nil {
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
r.selector, r.err = labels.ParseSelector(item)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use the given duration as a timeout. Optional.
|
||||||
|
func (r *Request) Timeout(d time.Duration) *Request {
|
||||||
|
if r.err != nil {
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
r.timeout = d
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use obj as the body of the request. Optional.
|
||||||
|
// If obj is a string, try to read a file of that name.
|
||||||
|
// If obj is a []byte, send it directly.
|
||||||
|
// Otherwise, assume obj is an api type and marshall it correctly.
|
||||||
|
func (r *Request) Body(obj interface{}) *Request {
|
||||||
|
if r.err != nil {
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
r.body = obj
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format and xecute the request. Returns the API object received, or an error.
|
||||||
|
func (r *Request) Do() (interface{}, error) {
|
||||||
|
if r.err != nil {
|
||||||
|
return nil, r.err
|
||||||
|
}
|
||||||
|
finalUrl := path.Join(r.s.rawUrl, r.path)
|
||||||
|
query := url.Values{}
|
||||||
|
if r.selector != nil {
|
||||||
|
query.Add("labels", r.selector.String())
|
||||||
|
}
|
||||||
|
if r.timeout != 0 {
|
||||||
|
query.Add("timeout", r.timeout.String())
|
||||||
|
}
|
||||||
|
finalUrl += "?" + query.Encode()
|
||||||
|
var body io.Reader
|
||||||
|
if r.body != nil {
|
||||||
|
switch t := r.body.(type) {
|
||||||
|
case string:
|
||||||
|
data, err := ioutil.ReadFile(t)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
body = bytes.NewBuffer(data)
|
||||||
|
case []byte:
|
||||||
|
body = bytes.NewBuffer(t)
|
||||||
|
default:
|
||||||
|
data, err := api.Encode(r.body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
body = bytes.NewBuffer(data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
req, err := http.NewRequest(r.verb, finalUrl, body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
str, err := DoRequest(req, r.s.auth)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return api.Decode([]byte(str))
|
||||||
|
}
|
||||||
|
|
||||||
// RequestWithBody is a helper method that creates an HTTP request with the specified url, method
|
// RequestWithBody is a helper method that creates an HTTP request with the specified url, method
|
||||||
// and a body read from 'configFile'
|
// and a body read from 'configFile'
|
||||||
// FIXME: need to be public API?
|
// FIXME: need to be public API?
|
||||||
|
|
|
@ -32,6 +32,7 @@ import (
|
||||||
type ResourcePrinter interface {
|
type ResourcePrinter interface {
|
||||||
// Print receives an arbitrary JSON body, formats it and prints it to a writer
|
// Print receives an arbitrary JSON body, formats it and prints it to a writer
|
||||||
Print([]byte, io.Writer) error
|
Print([]byte, io.Writer) error
|
||||||
|
PrintObj(interface{}, io.Writer)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Identity printer simply copies the body out to the output stream
|
// Identity printer simply copies the body out to the output stream
|
||||||
|
@ -42,6 +43,14 @@ func (i *IdentityPrinter) Print(data []byte, w io.Writer) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *IdentityPrinter) PrintObj(obj interface{}, output io.Writer) error {
|
||||||
|
data, err := api.EncodeIndent(obj)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return i.Print(data, output)
|
||||||
|
}
|
||||||
|
|
||||||
// YAMLPrinter parses JSON, and re-formats as YAML
|
// YAMLPrinter parses JSON, and re-formats as YAML
|
||||||
type YAMLPrinter struct{}
|
type YAMLPrinter struct{}
|
||||||
|
|
||||||
|
@ -58,6 +67,15 @@ func (y *YAMLPrinter) Print(data []byte, w io.Writer) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (y *YAMLPrinter) PrintObj(obj interface{}, w io.Writer) error {
|
||||||
|
output, err := yaml.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = fmt.Fprint(w, string(output))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// HumanReadablePrinter attempts to provide more elegant output
|
// HumanReadablePrinter attempts to provide more elegant output
|
||||||
type HumanReadablePrinter struct{}
|
type HumanReadablePrinter struct{}
|
||||||
|
|
||||||
|
@ -168,6 +186,10 @@ func (h *HumanReadablePrinter) Print(data []byte, output io.Writer) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
return h.PrintObj(obj, output)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *HumanReadablePrinter) PrintObj(obj interface{}, output io.Writer) {
|
||||||
switch o := obj.(type) {
|
switch o := obj.(type) {
|
||||||
case *api.Pod:
|
case *api.Pod:
|
||||||
h.printHeader(podColumns, w)
|
h.printHeader(podColumns, w)
|
||||||
|
|
Loading…
Reference in New Issue