pod labels and annotations

This commit is contained in:
Stefan Prodan
2018-01-06 12:45:32 +02:00
parent 0a88d68159
commit fb384bfa49
2 changed files with 53 additions and 3 deletions

View File

@@ -1,7 +1,8 @@
package server
type Response struct {
Environment []string `json:"environment"`
Runtime map[string]string `json:"runtime"`
Environment []string `json:"environment"`
Runtime map[string]string `json:"runtime"`
Labels []string `json:"labels"`
Annotations []string `json:"annotations"`
}

View File

@@ -1,11 +1,14 @@
package server
import (
"io/ioutil"
"net/http"
"os"
"path/filepath"
"runtime"
"strconv"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
@@ -37,9 +40,23 @@ func (s *Server) index(w http.ResponseWriter, r *http.Request) {
}
runtime["hostname"], _ = os.Hostname()
labels, err := readFiles("/etc/podinfod/metadata/labels")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
}
annotations, err := readFiles("/etc/podinfod/metadata/annotations")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
}
resp := &Response{
Environment: os.Environ(),
Runtime: runtime,
Labels: labels,
Annotations: annotations,
}
d, err := yaml.Marshal(resp)
@@ -62,3 +79,35 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.mux.ServeHTTP(w, r)
}
func readFiles(dir string) ([]string, error) {
files := []string{}
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([]string, 0)
for _, path := range files {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, errors.Wrapf(err, "Reading %v failed", path)
}
content := string(data)
duplicate := false
for _, p := range list {
if p == content {
duplicate = true
break
}
}
if duplicate {
continue
}
list = append(list, content)
}
return list, nil
}