mirror of
https://github.com/stefanprodan/podinfo.git
synced 2026-03-04 02:50:20 +00:00
pod labels and annotations
This commit is contained in:
@@ -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"`
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user