This commit is contained in:
Divyansh Manchanda 2019-08-20 14:31:19 +05:30
Родитель 3854986c60
Коммит 1ab9aa0ea8
4 изменённых файлов: 41 добавлений и 13 удалений

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

@ -14,7 +14,7 @@ rbac:
image: image:
repository: divyanshm/k8s-poolprovider repository: divyanshm/k8s-poolprovider
tag: v2.16 tag: v2.17
pullPolicy: IfNotPresent pullPolicy: IfNotPresent
vsts: vsts:

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

@ -10,42 +10,61 @@ import (
"k8s.io/client-go/rest" "k8s.io/client-go/rest"
) )
func CreatePod(podname string) string { type PodResponse struct {
status string
message string
}
func CreatePod(podname string) PodResponse {
cs, err := getInClusterClientSet() cs, err := getInClusterClientSet()
var response PodResponse
if err != nil { if err != nil {
return err.Error() response.status = "failure"
response.message = err.Error()
return response
} }
var podYaml = getAgentSpecification(podname) var podYaml = getAgentSpecification(podname)
var p1 v1.Pod var p1 v1.Pod
err1 := yaml.Unmarshal([]byte(podYaml), &p1) err1 := yaml.Unmarshal([]byte(podYaml), &p1)
if err1 != nil { if err1 != nil {
return "unmarshal error: " + err1.Error() response.status = "failure"
response.message = "unmarshal error: " + err1.Error()
return response
} }
podClient := cs.CoreV1().Pods("azuredevops") podClient := cs.CoreV1().Pods("azuredevops")
pod, err2 := podClient.Create(&p1) pod, err2 := podClient.Create(&p1)
if err2 != nil { if err2 != nil {
return "podclient create error: " + err2.Error() response.status = "failure"
response.message = "podclient create error: " + err2.Error()
return response
} }
return pod.GetName() response.status = "success"
response.message = "Pod created: " + pod.GetName()
return response
} }
func DeletePod(podname string) string { func DeletePod(podname string) PodResponse {
cs, err := getInClusterClientSet() cs, err := getInClusterClientSet()
response := PodResponse { "failure", "" }
if err != nil { if err != nil {
return err.Error() response.message = err.Error()
return response
} }
podClient := cs.CoreV1().Pods("azuredevops") podClient := cs.CoreV1().Pods("azuredevops")
err2 := podClient.Delete(podname, &metav1.DeleteOptions{}) err2 := podClient.Delete(podname, &metav1.DeleteOptions{})
if err2 != nil { if err2 != nil {
return "podclient delete error: " + err2.Error() response.message = "podclient delete error: " + err2.Error()
return response
} }
return "Deleted " + podname response.status = "success"
response.message = "Deleted " + podname
return response
} }
func getInClusterClientSet() (*kubernetes.Clientset, error) { func getInClusterClientSet() (*kubernetes.Clientset, error) {

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

@ -51,7 +51,7 @@ spec:
tier: frontend tier: frontend
spec: spec:
containers: containers:
- image: divyanshm/k8s-poolprovider:v2.16 - image: divyanshm/k8s-poolprovider:v2.17
name: k8s-poolprovider name: k8s-poolprovider
command: ["/app/main"] command: ["/app/main"]
ports: ports:

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

@ -1,6 +1,7 @@
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
@ -41,7 +42,11 @@ func KubernetesCreateHandler(resp http.ResponseWriter, req *http.Request) {
} }
var pods = CreatePod(agentSpec) var pods = CreatePod(agentSpec)
fmt.Fprintf(resp, "Pods: %s", pods)
jsonData, _ := json.Marshal(pods)
resp.Header().Set("Content-Type", "application/json")
resp.WriteHeader(http.StatusCreated)
resp.Write(jsonData)
} }
func KubernetesDeleteHandler(resp http.ResponseWriter, req *http.Request) { func KubernetesDeleteHandler(resp http.ResponseWriter, req *http.Request) {
@ -52,7 +57,11 @@ func KubernetesDeleteHandler(resp http.ResponseWriter, req *http.Request) {
} }
var pods = DeletePod(podname[0]) var pods = DeletePod(podname[0])
fmt.Fprintf(resp, "Response: %s", pods)
jsonData, _ := json.Marshal(pods)
resp.Header().Set("Content-Type", "application/json")
resp.WriteHeader(http.StatusCreated)
resp.Write(jsonData)
} }
func StorageSetHandler(s Storage) http.HandlerFunc { func StorageSetHandler(s Storage) http.HandlerFunc {