kubernetes endpoint
This commit is contained in:
Родитель
235b37b4ab
Коммит
52faae2e5b
|
@ -5,6 +5,10 @@ MAINTAINER Divyansh Manchanda <divyanshm@gmail.com>
|
|||
RUN apk add --no-cache git mercurial \
|
||||
&& go get github.com/garyburd/redigo/redis \
|
||||
&& go get github.com/gorilla/handlers \
|
||||
&& go get k8s.io/client-go/kubernetes \
|
||||
&& go get k8s.io/client-go/rest \
|
||||
&& go get k8s.io/apimachinery/pkg/apis/meta/v1 \
|
||||
&& go get k8s.io/apimachinery/pkg/api/errors \
|
||||
&& apk del git mercurial
|
||||
|
||||
RUN apk --no-cache add curl
|
||||
|
|
|
@ -4,7 +4,7 @@ services:
|
|||
redis:
|
||||
image: redis:3-alpine
|
||||
simple-webserver:
|
||||
image: divyanshm/goserver:v1.1
|
||||
image: divyanshm/goserver:v1.2
|
||||
command: -redis "redis:6379"
|
||||
depends_on:
|
||||
- redis
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/rest"
|
||||
)
|
||||
|
||||
func GetPodNames() string {
|
||||
// creates the in-cluster config
|
||||
config, err := rest.InClusterConfig()
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
|
||||
// creates the clientset
|
||||
clientset, err := kubernetes.NewForConfig(config)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
|
||||
pods, err := clientset.CoreV1().Pods("").List(metav1.ListOptions{})
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
|
||||
var ret string
|
||||
for _, pod := range pods.Items {
|
||||
ret = ret + pod.GetName() + ", "
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
|
@ -27,7 +27,7 @@ spec:
|
|||
tier: frontend
|
||||
spec:
|
||||
containers:
|
||||
- image: divyanshm/goserver:v1.1
|
||||
- image: divyanshm/goserver:v1.2
|
||||
name: goserver
|
||||
command: ["/app/main", "-redis", "goserver-redis:6379"]
|
||||
ports:
|
||||
|
|
27
main.go
27
main.go
|
@ -34,6 +34,7 @@ func main() {
|
|||
s.HandleFunc("/ping", PingHandler(r))
|
||||
s.HandleFunc("/version", VersionHandler)
|
||||
s.HandleFunc("/payload", PayloadHandler)
|
||||
s.HandleFunc("/kube", KubernetesHandler)
|
||||
|
||||
// Bootstrap logger
|
||||
logger := log.New(os.Stdout, "", log.LstdFlags)
|
||||
|
@ -102,6 +103,32 @@ func PayloadHandler(resp http.ResponseWriter, req *http.Request) {
|
|||
fmt.Fprintf(resp, "Payload: %s", string(body))
|
||||
}
|
||||
|
||||
func KubernetesHandler(resp http.ResponseWriter, req *http.Request) {
|
||||
_, err := ioutil.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
resp.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var pods = GetPodNames()
|
||||
|
||||
resp.WriteHeader(http.StatusOK)
|
||||
fmt.Fprintf(resp, "Method: %s\n", req.Method)
|
||||
|
||||
if len(req.Header) > 0 {
|
||||
log.Println("Headers:")
|
||||
fmt.Fprint(resp, "Headers:\n")
|
||||
for key, values := range req.Header {
|
||||
for _, val := range values {
|
||||
log.Printf("%s: %s\n", key, val)
|
||||
fmt.Fprintf(resp, "%s: %s\n", key, val)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Fprintf(resp, "Pods: %s", pods)
|
||||
}
|
||||
|
||||
// EnvOrDefault will read env from the environment.
|
||||
// If the environment variable is not set in the environment
|
||||
// fallback will be returned.
|
||||
|
|
Загрузка…
Ссылка в новой задаче