From 187f7d25344b35d11dba4028c3da8bbe79eeb264 Mon Sep 17 00:00:00 2001 From: Daniel Smith Date: Wed, 11 Jun 2014 18:13:25 -0700 Subject: [PATCH] cloudcfg working locally now --- cmd/cloudcfg/cloudcfg.go | 27 +++++++++++++++++++++------ hack/local-up.sh | 3 --- hack/localcfg.sh | 2 +- pkg/cloudcfg/cloudcfg.go | 18 ++++++++++++++---- 4 files changed, 36 insertions(+), 14 deletions(-) diff --git a/cmd/cloudcfg/cloudcfg.go b/cmd/cloudcfg/cloudcfg.go index 5f87ba1f76..ed8942bcde 100644 --- a/cmd/cloudcfg/cloudcfg.go +++ b/cmd/cloudcfg/cloudcfg.go @@ -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 :,...") 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) } diff --git a/hack/local-up.sh b/hack/local-up.sh index ef3d962419..a279b68c2d 100755 --- a/hack/local-up.sh +++ b/hack/local-up.sh @@ -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) diff --git a/hack/localcfg.sh b/hack/localcfg.sh index 3ed16afbb3..620106fd1b 100755 --- a/hack/localcfg.sh +++ b/hack/localcfg.sh @@ -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 $@ diff --git a/pkg/cloudcfg/cloudcfg.go b/pkg/cloudcfg/cloudcfg.go index a65612476c..7ef7b9927a 100644 --- a/pkg/cloudcfg/cloudcfg.go +++ b/pkg/cloudcfg/cloudcfg.go @@ -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)