This commit is contained in:
Divyansh Manchanda 2019-06-11 14:57:43 +05:30
Родитель d985502682
Коммит a7847f53f9
4 изменённых файлов: 56 добавлений и 32 удалений

Просмотреть файл

@ -7,7 +7,7 @@ RUN apk add --no-cache git mercurial \
&& 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/api/core/v1 \
&& apk del git mercurial
RUN apk --no-cache add curl

Просмотреть файл

@ -1,34 +1,72 @@
package main
import (
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"encoding/json"
"k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
func GetPodNames() string {
// creates the in-cluster config
func CreatePod() string {
config, err := rest.InClusterConfig()
if err != nil {
return err.Error()
}
// creates the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return err.Error()
}
pods, err := clientset.CoreV1().Pods("").List(metav1.ListOptions{})
if err != nil {
return err.Error()
var podYaml = `
apiVersion: v1
kind: Pod
metadata:
name: vsts-agent-dind
spec:
containers:
- name: vsts-agent
image: microsoft/vsts-agent:ubuntu-16.04-docker-18.06.1-ce-standard
env:
- name: VSTS_ACCOUNT
valueFrom:
secretKeyRef:
name: vsts
key: VSTS_ACCOUNT
- name: VSTS_TOKEN
valueFrom:
secretKeyRef:
name: vsts
key: VSTS_TOKEN
- name: VSTS_POOL
value: divman
- name: DOCKER_HOST
value: tcp://localhost:2375
- name: dind-daemon
image: docker:18.09.6-dind
securityContext:
privileged: true
volumeMounts:
- name: daemon-storage
mountPath: /var/lib/docker
volumes:
- name: daemon-storage
emptyDir: {}
`
var p1 v1.Pod
err1 := json.Unmarshal([]byte(podYaml), &p1)
if err1 != nil {
return err1.Error()
}
var ret strings.Builder
ret.WriteString("Name of one pod is: ")
ret.WriteString(pods.Items[0].GetName())
// running the app in the default namespace. Pass namespace to pods method.
podClient := clientset.CoreV1().Pods("")
pod, err2 := podClient.Create(&p1)
if err2 != nil {
return err2.Error()
}
return ret.String();
return pod.GetName()
}

Просмотреть файл

@ -27,7 +27,7 @@ spec:
tier: frontend
spec:
containers:
- image: divyanshm/goserver:v1.5
- image: divyanshm/goserver:v1.6
name: goserver
command: ["/app/main", "-redis", "goserver-redis:6379"]
ports:

20
main.go
Просмотреть файл

@ -34,7 +34,7 @@ func main() {
s.HandleFunc("/ping", PingHandler(r))
s.HandleFunc("/version", VersionHandler)
s.HandleFunc("/payload", PayloadHandler)
s.HandleFunc("/kube", KubernetesHandler)
s.HandleFunc("/kubecreate", KubernetesCreateHandler)
// Bootstrap logger
logger := log.New(os.Stdout, "", log.LstdFlags)
@ -103,28 +103,14 @@ func PayloadHandler(resp http.ResponseWriter, req *http.Request) {
fmt.Fprintf(resp, "Payload: %s", string(body))
}
func KubernetesHandler(resp http.ResponseWriter, req *http.Request) {
func KubernetesCreateHandler(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)
}
}
}
var pods = CreatePod()
fmt.Fprintf(resp, "Pods: %s", pods)
}