mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-04 01:40:07 +00:00
Call parsing code from cloudcfg
This commit is contained in:
parent
881613e3f5
commit
853a4e26a8
@ -18,6 +18,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@ -50,6 +51,22 @@ func usage() {
|
|||||||
log.Fatal("Usage: cloudcfg -h <host> [-c config/file.json] [-p <hostPort>:<containerPort>,..., <hostPort-n>:<containerPort-n> <method> <path>")
|
log.Fatal("Usage: cloudcfg -h <host> [-c config/file.json] [-p <hostPort>:<containerPort>,..., <hostPort-n>:<containerPort-n> <method> <path>")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reads & parses config file. On error, calls log.Fatal().
|
||||||
|
func readConfig(storage string) []byte {
|
||||||
|
if len(*config) == 0 {
|
||||||
|
log.Fatal("Need config file (-c)")
|
||||||
|
}
|
||||||
|
data, err := ioutil.ReadFile(*config)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Unable to read %v: %#v\n", *config, err)
|
||||||
|
}
|
||||||
|
data, err = cloudcfg.ToWireFormat(data, storage)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error parsing %v as an object for %v: %#v\n", *config, storage, err)
|
||||||
|
}
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
// CloudCfg command line tool.
|
// CloudCfg command line tool.
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse() // Scan the arguments list
|
flag.Parse() // Scan the arguments list
|
||||||
@ -71,7 +88,8 @@ func main() {
|
|||||||
if parsedUrl.Scheme != "" && parsedUrl.Scheme != "https" {
|
if parsedUrl.Scheme != "" && parsedUrl.Scheme != "https" {
|
||||||
secure = false
|
secure = false
|
||||||
}
|
}
|
||||||
url := *httpServer + path.Join("/api/v1beta1", flag.Arg(1))
|
storage := flag.Arg(1)
|
||||||
|
url := *httpServer + path.Join("/api/v1beta1", storage)
|
||||||
var request *http.Request
|
var request *http.Request
|
||||||
|
|
||||||
var printer cloudcfg.ResourcePrinter
|
var printer cloudcfg.ResourcePrinter
|
||||||
@ -100,9 +118,9 @@ func main() {
|
|||||||
case "delete":
|
case "delete":
|
||||||
request, err = http.NewRequest("DELETE", url, nil)
|
request, err = http.NewRequest("DELETE", url, nil)
|
||||||
case "create":
|
case "create":
|
||||||
request, err = cloudcfg.RequestWithBody(*config, url, "POST")
|
request, err = cloudcfg.RequestWithBodyData(readConfig(storage), url, "POST")
|
||||||
case "update":
|
case "update":
|
||||||
request, err = cloudcfg.RequestWithBody(*config, url, "PUT")
|
request, err = cloudcfg.RequestWithBodyData(readConfig(storage), url, "PUT")
|
||||||
case "rollingupdate":
|
case "rollingupdate":
|
||||||
client := &kube_client.Client{
|
client := &kube_client.Client{
|
||||||
Host: *httpServer,
|
Host: *httpServer,
|
||||||
|
@ -102,12 +102,12 @@ func RequestWithBody(configFile, url, method string) (*http.Request, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return requestWithBodyData(data, url, method)
|
return RequestWithBodyData(data, url, method)
|
||||||
}
|
}
|
||||||
|
|
||||||
// requestWithBodyData is a helper method that creates an HTTP request with the specified url, method
|
// RequestWithBodyData is a helper method that creates an HTTP request with the specified url, method
|
||||||
// and body data
|
// and body data
|
||||||
func requestWithBodyData(data []byte, url, method string) (*http.Request, error) {
|
func RequestWithBodyData(data []byte, url, method string) (*http.Request, error) {
|
||||||
request, err := http.NewRequest(method, url, bytes.NewBuffer(data))
|
request, err := http.NewRequest(method, url, bytes.NewBuffer(data))
|
||||||
request.ContentLength = int64(len(data))
|
request.ContentLength = int64(len(data))
|
||||||
return request, err
|
return request, err
|
||||||
|
Loading…
Reference in New Issue
Block a user