2019-06-10 13:27:21 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-07-16 13:25:16 +03:00
|
|
|
"io/ioutil"
|
2019-06-11 12:43:13 +03:00
|
|
|
"github.com/ghodss/yaml"
|
2019-06-11 12:27:43 +03:00
|
|
|
|
2019-06-11 13:47:59 +03:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2019-06-11 12:27:43 +03:00
|
|
|
"k8s.io/api/core/v1"
|
2019-06-10 13:27:21 +03:00
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
)
|
|
|
|
|
2019-08-22 11:59:37 +03:00
|
|
|
func CreatePod(podname, agentId string) PodResponse {
|
2019-07-16 13:25:16 +03:00
|
|
|
cs, err := getInClusterClientSet()
|
2019-08-20 12:01:19 +03:00
|
|
|
var response PodResponse
|
2019-06-10 13:27:21 +03:00
|
|
|
if err != nil {
|
2019-08-20 12:30:51 +03:00
|
|
|
response.Status = "failure"
|
|
|
|
response.Message = err.Error()
|
2019-08-20 12:01:19 +03:00
|
|
|
return response
|
2019-06-10 13:27:21 +03:00
|
|
|
}
|
|
|
|
|
2019-07-16 13:57:30 +03:00
|
|
|
var podYaml = getAgentSpecification(podname)
|
2019-06-11 12:27:43 +03:00
|
|
|
var p1 v1.Pod
|
2019-06-11 12:43:13 +03:00
|
|
|
err1 := yaml.Unmarshal([]byte(podYaml), &p1)
|
2019-06-11 12:27:43 +03:00
|
|
|
if err1 != nil {
|
2019-08-20 12:30:51 +03:00
|
|
|
response.Status = "failure"
|
|
|
|
response.Message = "unmarshal error: " + err1.Error()
|
2019-08-20 12:01:19 +03:00
|
|
|
return response
|
2019-06-10 13:27:21 +03:00
|
|
|
}
|
|
|
|
|
2019-08-22 11:59:37 +03:00
|
|
|
// Set the agentId as label if specified
|
|
|
|
if(agentId != "") {
|
|
|
|
p1.Labels["AgentId"] = agentId
|
|
|
|
}
|
|
|
|
|
2019-07-16 13:25:16 +03:00
|
|
|
podClient := cs.CoreV1().Pods("azuredevops")
|
2019-06-11 12:27:43 +03:00
|
|
|
pod, err2 := podClient.Create(&p1)
|
|
|
|
if err2 != nil {
|
2019-08-20 12:30:51 +03:00
|
|
|
response.Status = "failure"
|
|
|
|
response.Message = "podclient create error: " + err2.Error()
|
2019-08-20 12:01:19 +03:00
|
|
|
return response
|
2019-06-11 12:27:43 +03:00
|
|
|
}
|
2019-06-10 13:27:21 +03:00
|
|
|
|
2019-08-20 12:30:51 +03:00
|
|
|
response.Status = "success"
|
|
|
|
response.Message = "Pod created: " + pod.GetName()
|
2019-08-20 12:01:19 +03:00
|
|
|
return response
|
2019-06-11 13:47:59 +03:00
|
|
|
}
|
|
|
|
|
2019-08-20 12:01:19 +03:00
|
|
|
func DeletePod(podname string) PodResponse {
|
2019-07-16 13:25:16 +03:00
|
|
|
cs, err := getInClusterClientSet()
|
2019-08-22 11:59:37 +03:00
|
|
|
response := PodResponse { "failure", "" }
|
2019-06-11 13:47:59 +03:00
|
|
|
if err != nil {
|
2019-08-20 12:30:51 +03:00
|
|
|
response.Message = err.Error()
|
2019-08-20 12:01:19 +03:00
|
|
|
return response
|
2019-06-11 13:47:59 +03:00
|
|
|
}
|
|
|
|
|
2019-07-16 13:25:16 +03:00
|
|
|
podClient := cs.CoreV1().Pods("azuredevops")
|
2019-06-11 13:47:59 +03:00
|
|
|
|
|
|
|
err2 := podClient.Delete(podname, &metav1.DeleteOptions{})
|
|
|
|
if err2 != nil {
|
2019-08-20 12:30:51 +03:00
|
|
|
response.Message = "podclient delete error: " + err2.Error()
|
2019-08-20 12:01:19 +03:00
|
|
|
return response
|
2019-06-11 13:47:59 +03:00
|
|
|
}
|
|
|
|
|
2019-08-20 12:30:51 +03:00
|
|
|
response.Status = "success"
|
|
|
|
response.Message = "Deleted " + podname
|
2019-08-20 12:01:19 +03:00
|
|
|
return response
|
2019-07-16 13:25:16 +03:00
|
|
|
}
|
|
|
|
|
2019-08-22 11:59:37 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-07-16 13:25:16 +03:00
|
|
|
func getInClusterClientSet() (*kubernetes.Clientset, error) {
|
|
|
|
config, err := rest.InClusterConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
clientset, err := kubernetes.NewForConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return clientset, nil
|
|
|
|
}
|
|
|
|
|
2019-07-16 13:57:30 +03:00
|
|
|
func getAgentSpecification(podname string) string {
|
2019-08-21 11:55:50 +03:00
|
|
|
if(podname == "") {
|
|
|
|
podname = "agent-dind"
|
|
|
|
}
|
2019-08-22 11:59:37 +03:00
|
|
|
|
2019-07-16 13:25:16 +03:00
|
|
|
// 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
|
2019-07-16 13:57:30 +03:00
|
|
|
// kubectl create secret generic vsts --from-literal=VSTS_TOKEN=<token> --from-literal=VSTS_ACCOUNT=<accountname>
|
|
|
|
dat, err := ioutil.ReadFile("agentpods/" + podname + ".yaml")
|
2019-07-16 13:25:16 +03:00
|
|
|
if err != nil {
|
|
|
|
return err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
var podYaml = string(dat)
|
|
|
|
return podYaml
|
2019-06-10 13:27:21 +03:00
|
|
|
}
|