Address comments

pull/6/head
Daniel Smith 2014-06-11 22:25:50 -07:00
parent 69acbf5a74
commit ac65cc7094
4 changed files with 24 additions and 37 deletions

View File

@ -22,6 +22,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"path"
"strconv" "strconv"
"time" "time"
@ -70,7 +71,7 @@ func main() {
if parsedUrl.Scheme != "" && parsedUrl.Scheme != "https" { if parsedUrl.Scheme != "" && parsedUrl.Scheme != "https" {
secure = false secure = false
} }
url := *httpServer + "/api/v1beta1" + flag.Arg(1) url := *httpServer + path.Join("/api/v1beta1", flag.Arg(1))
var request *http.Request var request *http.Request
var printer cloudcfg.ResourcePrinter var printer cloudcfg.ResourcePrinter
@ -82,7 +83,7 @@ func main() {
printer = &cloudcfg.HumanReadablePrinter{} printer = &cloudcfg.HumanReadablePrinter{}
} }
var auth kube_client.AuthInfo var auth *kube_client.AuthInfo
if secure { if secure {
auth, err = cloudcfg.LoadAuthInfo(*authConfig) auth, err = cloudcfg.LoadAuthInfo(*authConfig)
if err != nil { if err != nil {
@ -105,7 +106,7 @@ func main() {
case "rollingupdate": case "rollingupdate":
client := &kube_client.Client{ client := &kube_client.Client{
Host: *httpServer, Host: *httpServer,
Auth: &auth, Auth: auth,
} }
cloudcfg.Update(flag.Arg(1), client, *updatePeriod) cloudcfg.Update(flag.Arg(1), client, *updatePeriod)
case "run": case "run":
@ -119,19 +120,19 @@ func main() {
if err != nil { if err != nil {
log.Fatalf("Error parsing replicas: %#v", err) log.Fatalf("Error parsing replicas: %#v", err)
} }
err = cloudcfg.RunController(image, name, replicas, kube_client.Client{Host: *httpServer, Auth: &auth}, *portSpec, *servicePort) err = cloudcfg.RunController(image, name, replicas, kube_client.Client{Host: *httpServer, Auth: auth}, *portSpec, *servicePort)
if err != nil { if err != nil {
log.Fatalf("Error: %#v", err) log.Fatalf("Error: %#v", err)
} }
return return
case "stop": case "stop":
err = cloudcfg.StopController(flag.Arg(1), kube_client.Client{Host: *httpServer, Auth: &auth}) err = cloudcfg.StopController(flag.Arg(1), kube_client.Client{Host: *httpServer, Auth: auth})
if err != nil { if err != nil {
log.Fatalf("Error: %#v", err) log.Fatalf("Error: %#v", err)
} }
return return
case "rm": case "rm":
err = cloudcfg.DeleteController(flag.Arg(1), kube_client.Client{Host: *httpServer, Auth: &auth}) err = cloudcfg.DeleteController(flag.Arg(1), kube_client.Client{Host: *httpServer, Auth: auth})
if err != nil { if err != nil {
log.Fatalf("Error: %#v", err) log.Fatalf("Error: %#v", err)
} }
@ -142,12 +143,7 @@ func main() {
if err != nil { if err != nil {
log.Fatalf("Error: %#v", err) log.Fatalf("Error: %#v", err)
} }
var body string body, err := cloudcfg.DoRequest(request, auth)
if secure {
body, err = cloudcfg.DoSecureRequest(request, auth.User, auth.Password)
} else {
body, err = cloudcfg.DoInsecureRequest(request)
}
if err != nil { if err != nil {
log.Fatalf("Error: %#v", err) log.Fatalf("Error: %#v", err)
} }

View File

@ -79,7 +79,6 @@ func fake_kubelet() {
// Starts api services (the master). Never returns. // Starts api services (the master). Never returns.
func api_server() { func api_server() {
//machineList := util.StringList{fmt.Sprintf("%s:%v", *kubelet_address, *kubelet_port)}
machineList := util.StringList{*kubelet_address} machineList := util.StringList{*kubelet_address}
etcdClient := etcd.NewClient([]string{*etcd_server}) etcdClient := etcd.NewClient([]string{*etcd_server})

View File

@ -42,25 +42,28 @@ func promptForString(field string) string {
return result return result
} }
// Parse an AuthInfo object from a file path // Parse an AuthInfo object from a file path. Prompt user and create file if it doesn't exist.
func LoadAuthInfo(path string) (client.AuthInfo, error) { func LoadAuthInfo(path string) (*client.AuthInfo, error) {
var auth client.AuthInfo var auth client.AuthInfo
if _, err := os.Stat(path); os.IsNotExist(err) { if _, err := os.Stat(path); os.IsNotExist(err) {
auth.User = promptForString("Username") auth.User = promptForString("Username")
auth.Password = promptForString("Password") auth.Password = promptForString("Password")
data, err := json.Marshal(auth) data, err := json.Marshal(auth)
if err != nil { if err != nil {
return auth, err return &auth, err
} }
err = ioutil.WriteFile(path, data, 0600) err = ioutil.WriteFile(path, data, 0600)
return auth, err return &auth, err
} }
data, err := ioutil.ReadFile(path) data, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
return auth, err return nil, err
} }
err = json.Unmarshal(data, &auth) err = json.Unmarshal(data, &auth)
return auth, err if err != nil {
return nil, err
}
return &auth, err
} }
// Perform a rolling update of a collection of pods. // Perform a rolling update of a collection of pods.
@ -102,19 +105,19 @@ func RequestWithBody(configFile, url, method string) (*http.Request, error) {
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
// 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, err := http.NewRequest(method, url, bytes.NewBuffer(data))
request.ContentLength = int64(len(data)) request.ContentLength = int64(len(data))
return request, err return request, err
} }
// Execute a request, adds authentication, and HTTPS cert ignoring. // Execute a request, adds authentication (if auth != nil), and HTTPS cert ignoring.
// TODO: Make this stuff optional func DoRequest(request *http.Request, auth *client.AuthInfo) (string, error) {
func DoSecureRequest(request *http.Request, user, password string) (string, error) { if auth != nil {
request.SetBasicAuth(user, password) request.SetBasicAuth(auth.User, auth.Password)
}
tr := &http.Transport{ tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
} }
@ -128,17 +131,6 @@ func DoSecureRequest(request *http.Request, user, password string) (string, erro
return string(body), err 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 // StopController stops a controller named 'name' by setting replicas to zero
func StopController(name string, client client.ClientInterface) error { func StopController(name string, client client.ClientInterface) error {
controller, err := client.GetReplicationController(name) controller, err := client.GetReplicationController(name)

View File

@ -26,7 +26,7 @@ import (
func HandleCrash() { func HandleCrash() {
r := recover() r := recover()
if r != nil { if r != nil {
log.Printf("Recovered from panic: %v", r) log.Printf("Recovered from panic: %#v", r)
} }
} }