Skip to content

Commit

Permalink
cloudcfg working locally now
Browse files Browse the repository at this point in the history
  • Loading branch information
lavalamp committed Jun 12, 2014
1 parent 912ec01 commit 187f7d2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
27 changes: 21 additions & 6 deletions cmd/cloudcfg/cloudcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"log"
"net/http"
"net/url"
"os"
"strconv"
"time"
Expand All @@ -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")
)
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 0 additions & 3 deletions hack/local-up.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion hack/localcfg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 $@
18 changes: 14 additions & 4 deletions pkg/cloudcfg/cloudcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,21 @@ 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
}

// 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},
Expand All @@ -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)
Expand Down

0 comments on commit 187f7d2

Please sign in to comment.