add remove agent API
This commit is contained in:
Родитель
ddb3840dbf
Коммит
602184e4bc
|
@ -10,7 +10,7 @@ import (
|
||||||
"k8s.io/client-go/rest"
|
"k8s.io/client-go/rest"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreatePod(podname string) PodResponse {
|
func CreatePod(podname, agentId string) PodResponse {
|
||||||
cs, err := getInClusterClientSet()
|
cs, err := getInClusterClientSet()
|
||||||
var response PodResponse
|
var response PodResponse
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -28,6 +28,11 @@ func CreatePod(podname string) PodResponse {
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set the agentId as label if specified
|
||||||
|
if(agentId != "") {
|
||||||
|
p1.Labels["AgentId"] = agentId
|
||||||
|
}
|
||||||
|
|
||||||
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 {
|
||||||
|
@ -43,7 +48,7 @@ func CreatePod(podname string) PodResponse {
|
||||||
|
|
||||||
func DeletePod(podname string) PodResponse {
|
func DeletePod(podname string) PodResponse {
|
||||||
cs, err := getInClusterClientSet()
|
cs, err := getInClusterClientSet()
|
||||||
response := PodResponse { "failure", "" }
|
response := PodResponse { "failure", "" }
|
||||||
if err != nil {
|
if err != nil {
|
||||||
response.Message = err.Error()
|
response.Message = err.Error()
|
||||||
return response
|
return response
|
||||||
|
@ -62,6 +67,34 @@ func DeletePod(podname string) PodResponse {
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DeletePodWithAgentId(agentId string) PodResponse {
|
||||||
|
cs, err := getInClusterClientSet()
|
||||||
|
response := PodResponse { "failure", "" }
|
||||||
|
if err != nil {
|
||||||
|
response.Message = err.Error()
|
||||||
|
return response
|
||||||
|
}
|
||||||
|
|
||||||
|
podClient := cs.CoreV1().Pods("azuredevops")
|
||||||
|
|
||||||
|
// Get the pod with this agentId
|
||||||
|
pods, _ := podClient.List(metav1.ListOptions{LabelSelector: agentId})
|
||||||
|
if(pods == nil || len(pods.Items) == 0) {
|
||||||
|
response.Message = "Could not find running pod with AgentId" + agentId
|
||||||
|
return response
|
||||||
|
}
|
||||||
|
|
||||||
|
err2 := podClient.Delete(pods.Items[0].GetName(), &metav1.DeleteOptions{})
|
||||||
|
if err2 != nil {
|
||||||
|
response.Message = "podclient delete error: " + err2.Error()
|
||||||
|
return response
|
||||||
|
}
|
||||||
|
|
||||||
|
response.Status = "success"
|
||||||
|
response.Message = "Deleted " + pods.Items[0].GetName()
|
||||||
|
return response
|
||||||
|
}
|
||||||
|
|
||||||
func getInClusterClientSet() (*kubernetes.Clientset, error) {
|
func getInClusterClientSet() (*kubernetes.Clientset, error) {
|
||||||
config, err := rest.InClusterConfig()
|
config, err := rest.InClusterConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -80,7 +113,7 @@ func getAgentSpecification(podname string) string {
|
||||||
if(podname == "") {
|
if(podname == "") {
|
||||||
podname = "agent-dind"
|
podname = "agent-dind"
|
||||||
}
|
}
|
||||||
|
|
||||||
// If pod is to be created in a different namespace
|
// If pod is to be created in a different namespace
|
||||||
// then secrets need to be created in the same namespace, i.e. VSTS_TOKEN and VSTS_ACCOUNT
|
// then secrets need to be created in the same namespace, i.e. VSTS_TOKEN and VSTS_ACCOUNT
|
||||||
// kubectl create secret generic vsts --from-literal=VSTS_TOKEN=<token> --from-literal=VSTS_ACCOUNT=<accountname>
|
// kubectl create secret generic vsts --from-literal=VSTS_TOKEN=<token> --from-literal=VSTS_ACCOUNT=<accountname>
|
||||||
|
|
30
main.go
30
main.go
|
@ -26,6 +26,7 @@ func main() {
|
||||||
s.HandleFunc("/delete", func(w http.ResponseWriter, r *http.Request) { KubernetesDeleteHandler(w, r) })
|
s.HandleFunc("/delete", func(w http.ResponseWriter, r *http.Request) { KubernetesDeleteHandler(w, r) })
|
||||||
s.HandleFunc("/definitions", func(w http.ResponseWriter, r *http.Request) { EmptyResponeHandler(w, r) })
|
s.HandleFunc("/definitions", func(w http.ResponseWriter, r *http.Request) { EmptyResponeHandler(w, r) })
|
||||||
s.HandleFunc("/acquire", func(w http.ResponseWriter, r *http.Request) { AcquireAgentHandler(w, r) })
|
s.HandleFunc("/acquire", func(w http.ResponseWriter, r *http.Request) { AcquireAgentHandler(w, r) })
|
||||||
|
s.HandleFunc("/release", func(w http.ResponseWriter, r *http.Request) { ReleaseAgentHandler(w, r) })
|
||||||
|
|
||||||
// Test redis
|
// Test redis
|
||||||
s.HandleFunc("/testredisdata", StorageSetHandler(storage))
|
s.HandleFunc("/testredisdata", StorageSetHandler(storage))
|
||||||
|
@ -44,7 +45,9 @@ func KubernetesCreateHandler(resp http.ResponseWriter, req *http.Request) {
|
||||||
agentSpec = userSpec[0]
|
agentSpec = userSpec[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
var pods = CreatePod(agentSpec)
|
// Create a new pod without an AgentId label
|
||||||
|
var pods = CreatePod(agentSpec, "")
|
||||||
|
|
||||||
WriteJsonResponse(resp, pods)
|
WriteJsonResponse(resp, pods)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,8 +57,29 @@ func AcquireAgentHandler(resp http.ResponseWriter, req *http.Request) {
|
||||||
requestBody, _ := ioutil.ReadAll(req.Body)
|
requestBody, _ := ioutil.ReadAll(req.Body)
|
||||||
json.Unmarshal(requestBody, &agentRequest)
|
json.Unmarshal(requestBody, &agentRequest)
|
||||||
|
|
||||||
var pods = CreatePod("")
|
if(agentRequest.AgentId == "") {
|
||||||
WriteJsonResponse(resp, pods)
|
http.Error(resp, "No AgentId sent in request body.", http.StatusCreated)
|
||||||
|
}
|
||||||
|
|
||||||
|
var pods = CreatePod("", "")
|
||||||
|
WriteJsonResponse(resp, pods)
|
||||||
|
} else {
|
||||||
|
http.Error(resp, "Invalid request Method.", http.StatusMethodNotAllowed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReleaseAgentHandler(resp http.ResponseWriter, req *http.Request) {
|
||||||
|
if(req.Method == "POST") {
|
||||||
|
var agentRequest ReleaseAgentRequest
|
||||||
|
requestBody, _ := ioutil.ReadAll(req.Body)
|
||||||
|
json.Unmarshal(requestBody, &agentRequest)
|
||||||
|
|
||||||
|
if(agentRequest.AgentId == "") {
|
||||||
|
http.Error(resp, "No AgentId sent in request body.", http.StatusCreated)
|
||||||
|
}
|
||||||
|
|
||||||
|
var pods = DeletePod(agentRequest.AgentId)
|
||||||
|
WriteJsonResponse(resp, pods)
|
||||||
} else {
|
} else {
|
||||||
http.Error(resp, "Invalid request Method.", http.StatusMethodNotAllowed)
|
http.Error(resp, "Invalid request Method.", http.StatusMethodNotAllowed)
|
||||||
}
|
}
|
||||||
|
|
Загрузка…
Ссылка в новой задаче