cloudcfg working locally now

pull/6/head
Daniel Smith 2014-06-11 18:13:25 -07:00
parent 912ec01856
commit 187f7d2534
4 changed files with 36 additions and 14 deletions

View File

@ -20,6 +20,7 @@ import (
"fmt"
"log"
"net/http"
"net/url"
"os"
"strconv"
"time"
@ -39,7 +40,7 @@ var (
updatePeriod = flag.Duration("u", 60*time.Second, "Update interarrival period")
portSpec = flag.String("p", "", "The port spec, comma-separated list of <external>:<internal>,...")
servicePort = flag.Int("s", -1, "If positive, create and run a corresponding service on this port, only used with 'run'")
authConfig = flag.String("auth", os.Getenv("HOME")+"/.kubernetes_auth", "Path to the auth info file. If missing, prompt the user")
authConfig = flag.String("auth", os.Getenv("HOME")+"/.kubernetes_auth", "Path to the auth info file. If missing, prompt the user. Only used if doing https.")
json = flag.Bool("json", false, "If true, print raw JSON for responses")
yaml = flag.Bool("yaml", false, "If true, print raw YAML for responses")
)
@ -61,9 +62,16 @@ func main() {
usage()
}
method := flag.Arg(0)
secure := true
parsedUrl, err := url.Parse(*httpServer)
if err != nil {
log.Fatalf("Unable to parse %v as a URL\n", err)
}
if parsedUrl.Scheme != "" && parsedUrl.Scheme != "https" {
secure = false
}
url := *httpServer + "/api/v1beta1" + flag.Arg(1)
var request *http.Request
var err error
var printer cloudcfg.ResourcePrinter
if *json {
@ -74,9 +82,12 @@ func main() {
printer = &cloudcfg.HumanReadablePrinter{}
}
auth, err := cloudcfg.LoadAuthInfo(*authConfig)
if err != nil {
log.Fatalf("Error loading auth: %#v", err)
var auth kube_client.AuthInfo
if secure {
auth, err = cloudcfg.LoadAuthInfo(*authConfig)
if err != nil {
log.Fatalf("Error loading auth: %#v", err)
}
}
switch method {
@ -132,7 +143,11 @@ func main() {
log.Fatalf("Error: %#v", err)
}
var body string
body, err = cloudcfg.DoRequest(request, auth.User, auth.Password)
if secure {
body, err = cloudcfg.DoSecureRequest(request, auth.User, auth.Password)
} else {
body, err = cloudcfg.DoInsecureRequest(request)
}
if err != nil {
log.Fatalf("Error: %#v", err)
}

View File

@ -35,9 +35,6 @@ set -e
done
)
source "$(dirname 0)/../cluster/util.sh"
get-password
echo "Starting etcd"
ETCD_DIR=$(mktemp -d -t kube-integration.XXXXXX)

View File

@ -24,4 +24,4 @@ if [ ! -x $CLOUDCFG ]; then
fi
# 8080 is the default port for the master
$CLOUDCFG -h https://localhost:8080 $@
$CLOUDCFG -h http://localhost:8080 $@

View File

@ -99,13 +99,13 @@ func RequestWithBody(configFile, url, method string) (*http.Request, error) {
if err != nil {
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
// and body data
// FIXME: need to be public API?
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.ContentLength = int64(len(data))
return request, err
@ -113,8 +113,7 @@ func RequestWithBodyData(data []byte, url, method string) (*http.Request, error)
// Execute a request, adds authentication, and HTTPS cert ignoring.
// TODO: Make this stuff optional
// FIXME: need to be public API?
func DoRequest(request *http.Request, user, password string) (string, error) {
func DoSecureRequest(request *http.Request, user, password string) (string, error) {
request.SetBasicAuth(user, password)
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
@ -129,6 +128,17 @@ func DoRequest(request *http.Request, user, password string) (string, error) {
return string(body), err
}
// Execute a request.
func DoInsecureRequest(request *http.Request) (string, error) {
response, err := http.DefaultClient.Do(request)
if err != nil {
return "", err
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
return string(body), err
}
// StopController stops a controller named 'name' by setting replicas to zero
func StopController(name string, client client.ClientInterface) error {
controller, err := client.GetReplicationController(name)