From 3466c3c1982f2aed1697f5b48a40fc9f24cd0596 Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Sat, 6 Jan 2018 13:51:44 +0200 Subject: [PATCH] response refactoring --- pkg/server/response.go | 88 ++++++++++++++++++++++++++++++++++++++++++ pkg/server/server.go | 77 +----------------------------------- 2 files changed, 89 insertions(+), 76 deletions(-) diff --git a/pkg/server/response.go b/pkg/server/response.go index aadb549..283e5be 100644 --- a/pkg/server/response.go +++ b/pkg/server/response.go @@ -1,8 +1,96 @@ package server +import ( + "bufio" + "os" + "path/filepath" + "runtime" + "strconv" + "strings" + + "github.com/pkg/errors" +) + type Response struct { Runtime map[string]string `json:"runtime"` Labels map[string]string `json:"labels"` Annotations map[string]string `json:"annotations"` Environment map[string]string `json:"environment"` } + +func makeResponse() (*Response, error) { + labels, err := filesToMap("/etc/podinfod/metadata/labels") + if err != nil { + return nil, err + } + + annotations, err := filesToMap("/etc/podinfod/metadata/annotations") + if err != nil { + return nil, err + } + + resp := &Response{ + Environment: envToMap(), + Runtime: runtimeToMap(), + Labels: labels, + Annotations: annotations, + } + + return resp, nil +} + +func filesToMap(dir string) (map[string]string, error) { + files := make([]string, 0) + err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error { + files = append(files, path) + + return nil + }) + if err != nil { + return nil, errors.Wrapf(err, "Reading from %v failed", dir) + } + list := make(map[string]string, 0) + for _, path := range files { + file, err := os.Open(path) + if err != nil { + continue + } + s := bufio.NewScanner(file) + for s.Scan() { + kv := strings.Split(s.Text(), "=") + if len(kv) > 1 { + list[kv[0]] = strings.Replace(kv[1], "\"", "", -1) + } else { + list[kv[0]] = "" + } + } + file.Close() + } + return list, nil +} + +func envToMap() map[string]string { + list := make(map[string]string, 0) + for _, env := range os.Environ() { + kv := strings.Split(env, "=") + if len(kv) > 1 { + list[kv[0]] = strings.Replace(kv[1], "\"", "", -1) + } else { + list[kv[0]] = "" + } + } + return list +} + +func runtimeToMap() map[string]string { + runtime := map[string]string{ + "os": runtime.GOOS, + "arch": runtime.GOARCH, + "version": runtime.Version(), + "max_procs": strconv.FormatInt(int64(runtime.GOMAXPROCS(0)), 10), + "num_goroutine": strconv.FormatInt(int64(runtime.NumGoroutine()), 10), + "num_cpu": strconv.FormatInt(int64(runtime.NumCPU()), 10), + } + runtime["hostname"], _ = os.Hostname() + return runtime +} diff --git a/pkg/server/server.go b/pkg/server/server.go index b3c9b8e..06c4b26 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -1,15 +1,9 @@ package server import ( - "bufio" "net/http" - "os" - "path/filepath" "runtime" - "strconv" - "strings" - "github.com/pkg/errors" "gopkg.in/yaml.v2" ) @@ -31,25 +25,12 @@ func New(options ...func(*Server)) *Server { } func (s *Server) index(w http.ResponseWriter, r *http.Request) { - labels, err := filesToMap("/etc/podinfod/metadata/labels") + resp, err := makeResponse() if err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte(err.Error())) } - annotations, err := filesToMap("/etc/podinfod/metadata/annotations") - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(err.Error())) - } - - resp := &Response{ - Environment: envToMap(), - Runtime: runtimeToMap(), - Labels: labels, - Annotations: annotations, - } - d, err := yaml.Marshal(resp) if err != nil { w.WriteHeader(http.StatusInternalServerError) @@ -70,59 +51,3 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { s.mux.ServeHTTP(w, r) } - -func filesToMap(dir string) (map[string]string, error) { - files := make([]string, 0) - err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error { - files = append(files, path) - - return nil - }) - if err != nil { - return nil, errors.Wrapf(err, "Reading from %v failed", dir) - } - list := make(map[string]string, 0) - for _, path := range files { - file, err := os.Open(path) - if err != nil { - continue - } - s := bufio.NewScanner(file) - for s.Scan() { - kv := strings.Split(s.Text(), "=") - if len(kv) > 1 { - list[kv[0]] = strings.Replace(kv[1], "\"", "", -1) - } else { - list[kv[0]] = "" - } - } - file.Close() - } - return list, nil -} - -func envToMap() map[string]string { - list := make(map[string]string, 0) - for _, env := range os.Environ() { - kv := strings.Split(env, "=") - if len(kv) > 1 { - list[kv[0]] = strings.Replace(kv[1], "\"", "", -1) - } else { - list[kv[0]] = "" - } - } - return list -} - -func runtimeToMap() map[string]string { - runtime := map[string]string{ - "os": runtime.GOOS, - "arch": runtime.GOARCH, - "version": runtime.Version(), - "max_procs": strconv.FormatInt(int64(runtime.GOMAXPROCS(0)), 10), - "num_goroutine": strconv.FormatInt(int64(runtime.NumGoroutine()), 10), - "num_cpu": strconv.FormatInt(int64(runtime.NumCPU()), 10), - } - runtime["hostname"], _ = os.Hostname() - return runtime -}