[v2.17] API response as JSON
This commit is contained in:
Родитель
3854986c60
Коммит
1ab9aa0ea8
|
@ -14,7 +14,7 @@ rbac:
|
|||
|
||||
image:
|
||||
repository: divyanshm/k8s-poolprovider
|
||||
tag: v2.16
|
||||
tag: v2.17
|
||||
pullPolicy: IfNotPresent
|
||||
|
||||
vsts:
|
||||
|
|
|
@ -10,42 +10,61 @@ import (
|
|||
"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()
|
||||
var response PodResponse
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
response.status = "failure"
|
||||
response.message = err.Error()
|
||||
return response
|
||||
}
|
||||
|
||||
var podYaml = getAgentSpecification(podname)
|
||||
var p1 v1.Pod
|
||||
err1 := yaml.Unmarshal([]byte(podYaml), &p1)
|
||||
if err1 != nil {
|
||||
return "unmarshal error: " + err1.Error()
|
||||
response.status = "failure"
|
||||
response.message = "unmarshal error: " + err1.Error()
|
||||
return response
|
||||
}
|
||||
|
||||
podClient := cs.CoreV1().Pods("azuredevops")
|
||||
pod, err2 := podClient.Create(&p1)
|
||||
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()
|
||||
response := PodResponse { "failure", "" }
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
response.message = err.Error()
|
||||
return response
|
||||
}
|
||||
|
||||
podClient := cs.CoreV1().Pods("azuredevops")
|
||||
|
||||
err2 := podClient.Delete(podname, &metav1.DeleteOptions{})
|
||||
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) {
|
||||
|
|
|
@ -51,7 +51,7 @@ spec:
|
|||
tier: frontend
|
||||
spec:
|
||||
containers:
|
||||
- image: divyanshm/k8s-poolprovider:v2.16
|
||||
- image: divyanshm/k8s-poolprovider:v2.17
|
||||
name: k8s-poolprovider
|
||||
command: ["/app/main"]
|
||||
ports:
|
||||
|
|
13
main.go
13
main.go
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
@ -41,7 +42,11 @@ func KubernetesCreateHandler(resp http.ResponseWriter, req *http.Request) {
|
|||
}
|
||||
|
||||
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) {
|
||||
|
@ -52,7 +57,11 @@ func KubernetesDeleteHandler(resp http.ResponseWriter, req *http.Request) {
|
|||
}
|
||||
|
||||
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 {
|
||||
|
|
Загрузка…
Ссылка в новой задаче