[v2.23] correct acquire agent response
This commit is contained in:
Родитель
067050d2e9
Коммит
f6a39a73d1
33
contracts.go
33
contracts.go
|
@ -1,32 +1,33 @@
|
|||
package main
|
||||
|
||||
type AgentConfigurationData struct {
|
||||
AgentSettings string
|
||||
AgentCredentials string
|
||||
AgentVersion string
|
||||
AgentSettings string
|
||||
AgentCredentials string
|
||||
AgentVersion string
|
||||
AgentDownloadUrls string
|
||||
}
|
||||
|
||||
type AgentRequest struct {
|
||||
AgentId string
|
||||
AgentPool string
|
||||
AccountId string
|
||||
FailRequestUrl string
|
||||
AgentId string
|
||||
AgentPool string
|
||||
AccountId string
|
||||
FailRequestUrl string
|
||||
AppendRequestMessageUrl string
|
||||
IsScheduled bool
|
||||
IsPublic bool
|
||||
AgentConfiguration AgentConfigurationData
|
||||
AgentSpec string
|
||||
IsScheduled bool
|
||||
IsPublic bool
|
||||
AgentConfiguration AgentConfigurationData
|
||||
AgentSpec string
|
||||
}
|
||||
|
||||
type AcquireAgentResponse struct {
|
||||
AgentData string
|
||||
Success bool
|
||||
type AgentProvisionResponse struct {
|
||||
AgentData string
|
||||
ResponseType string
|
||||
ErrorMessage string
|
||||
}
|
||||
|
||||
type ReleaseAgentRequest struct {
|
||||
AgentId string
|
||||
AgentId string
|
||||
AccountId string
|
||||
AgentPool string
|
||||
AgentData string
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ rbac:
|
|||
|
||||
image:
|
||||
repository: divyanshm/k8s-poolprovider
|
||||
tag: v2.21
|
||||
tag: v2.23
|
||||
pullPolicy: IfNotPresent
|
||||
|
||||
vsts:
|
||||
|
|
|
@ -3,24 +3,25 @@ package main
|
|||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
|
||||
v1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/api/core/v1"
|
||||
)
|
||||
|
||||
// External callers calling into Kubernetes APIs via this package will get a PodResponse
|
||||
type PodResponse struct {
|
||||
Status string
|
||||
Message string
|
||||
Status string
|
||||
Message string
|
||||
}
|
||||
|
||||
const agentIdLabel = "AgentId"
|
||||
|
||||
// Creates a Pod with the default image specification. The pod is labelled with the agentId passed to it.
|
||||
func CreatePod(agentId string) PodResponse {
|
||||
func CreatePod(agentId string) AgentProvisionResponse {
|
||||
cs, err := GetClientSet()
|
||||
var response PodResponse
|
||||
var response AgentProvisionResponse
|
||||
if err != nil {
|
||||
return getFailureResponse(response, err)
|
||||
}
|
||||
|
@ -33,26 +34,25 @@ func CreatePod(agentId string) PodResponse {
|
|||
}
|
||||
|
||||
// Set the agentId as label if specified
|
||||
if(agentId != "") {
|
||||
if agentId != "" {
|
||||
p1.SetLabels(map[string]string{
|
||||
agentIdLabel: agentId,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
podClient := cs.CoreV1().Pods("azuredevops")
|
||||
pod, err2 := podClient.Create(&p1)
|
||||
_, err2 := podClient.Create(&p1)
|
||||
if err2 != nil {
|
||||
return getFailureResponse(response, err)
|
||||
}
|
||||
|
||||
response.Status = "success"
|
||||
response.Message = "Pod created: " + pod.GetName()
|
||||
response.ResponseType = "Success"
|
||||
return response
|
||||
}
|
||||
|
||||
func DeletePod(podname string) PodResponse {
|
||||
cs, err := GetClientSet()
|
||||
response := PodResponse { "failure", "" }
|
||||
response := PodResponse{"failure", ""}
|
||||
if err != nil {
|
||||
response.Message = err.Error()
|
||||
return response
|
||||
|
@ -75,20 +75,20 @@ func DeletePodWithAgentId(agentId string) PodResponse {
|
|||
cs, err := GetClientSet()
|
||||
var response PodResponse
|
||||
if err != nil {
|
||||
return getFailureResponse(response, err)
|
||||
return getFailure(response, err)
|
||||
}
|
||||
|
||||
podClient := cs.CoreV1().Pods("azuredevops")
|
||||
|
||||
// Get the pod with this agentId
|
||||
pods, _ := podClient.List(metav1.ListOptions{LabelSelector: agentIdLabel + "=" + agentId})
|
||||
if(pods == nil || len(pods.Items) == 0) {
|
||||
return getFailureResponse(response, errors.New("Could not find running pod with AgentId" + agentId))
|
||||
if pods == nil || len(pods.Items) == 0 {
|
||||
return getFailure(response, errors.New("Could not find running pod with AgentId"+agentId))
|
||||
}
|
||||
|
||||
err1 := podClient.Delete(pods.Items[0].GetName(), &metav1.DeleteOptions{})
|
||||
if err1 != nil {
|
||||
return getFailureResponse(response, err1)
|
||||
return getFailure(response, err1)
|
||||
}
|
||||
|
||||
response.Status = "success"
|
||||
|
@ -113,8 +113,14 @@ func getAgentSpecification() string {
|
|||
return podYaml
|
||||
}
|
||||
|
||||
func getFailureResponse(response PodResponse, err error) PodResponse {
|
||||
response.Status = "failure"
|
||||
func getFailure(response PodResponse, err error) PodResponse {
|
||||
response.Status = "fail"
|
||||
response.Message = err.Error()
|
||||
return response
|
||||
}
|
||||
}
|
||||
|
||||
func getFailureResponse(response AgentProvisionResponse, err error) AgentProvisionResponse {
|
||||
response.ResponseType = "fail"
|
||||
response.ErrorMessage = err.Error()
|
||||
return response
|
||||
}
|
||||
|
|
52
main.go
52
main.go
|
@ -23,19 +23,19 @@ func main() {
|
|||
|
||||
func AcquireAgentHandler(resp http.ResponseWriter, req *http.Request) {
|
||||
// HTTP method should be POST and the HMAC header should be valid
|
||||
if (req.Method == "POST") {
|
||||
if (isRequestHmacValid(req)) {
|
||||
var agentRequest AgentRequest
|
||||
requestBody, _ := ioutil.ReadAll(req.Body)
|
||||
json.Unmarshal(requestBody, &agentRequest)
|
||||
if req.Method == "POST" {
|
||||
if isRequestHmacValid(req) {
|
||||
var agentRequest AgentRequest
|
||||
requestBody, _ := ioutil.ReadAll(req.Body)
|
||||
json.Unmarshal(requestBody, &agentRequest)
|
||||
|
||||
if(agentRequest.AgentId == "") {
|
||||
http.Error(resp, "No AgentId sent in request body.", http.StatusCreated)
|
||||
}
|
||||
if agentRequest.AgentId == "" {
|
||||
http.Error(resp, "No AgentId sent in request body.", http.StatusCreated)
|
||||
}
|
||||
|
||||
var pods = CreatePod(agentRequest.AgentId)
|
||||
writeJsonResponse(resp, pods)
|
||||
} else{
|
||||
var pods = CreatePod(agentRequest.AgentId)
|
||||
writeJsonResponse(resp, pods)
|
||||
} else {
|
||||
http.Error(resp, "Endpoint can only be invoked with AzureDevOps with the correct Shared Signature.", http.StatusForbidden)
|
||||
}
|
||||
} else {
|
||||
|
@ -45,21 +45,21 @@ func AcquireAgentHandler(resp http.ResponseWriter, req *http.Request) {
|
|||
|
||||
func ReleaseAgentHandler(resp http.ResponseWriter, req *http.Request) {
|
||||
// HTTP method should be POST and the HMAC header should be valid
|
||||
if (req.Method == "POST") {
|
||||
if (isRequestHmacValid(req)) {
|
||||
if req.Method == "POST" {
|
||||
if isRequestHmacValid(req) {
|
||||
var agentRequest ReleaseAgentRequest
|
||||
requestBody, _ := ioutil.ReadAll(req.Body)
|
||||
json.Unmarshal(requestBody, &agentRequest)
|
||||
requestBody, _ := ioutil.ReadAll(req.Body)
|
||||
json.Unmarshal(requestBody, &agentRequest)
|
||||
|
||||
if(agentRequest.AgentId == "") {
|
||||
http.Error(resp, "No AgentId sent in request body.", http.StatusCreated)
|
||||
}
|
||||
if agentRequest.AgentId == "" {
|
||||
http.Error(resp, "No AgentId sent in request body.", http.StatusCreated)
|
||||
}
|
||||
|
||||
var pods = DeletePodWithAgentId(agentRequest.AgentId)
|
||||
writeJsonResponse(resp, pods)
|
||||
var pods = DeletePodWithAgentId(agentRequest.AgentId)
|
||||
writeJsonResponse(resp, pods)
|
||||
} else {
|
||||
http.Error(resp, "Endpoint can only be invoked with AzureDevOps with the correct Shared Signature.", http.StatusForbidden)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
http.Error(resp, "Invalid request Method.", http.StatusMethodNotAllowed)
|
||||
}
|
||||
|
@ -70,23 +70,23 @@ func EmptyResponeHandler(resp http.ResponseWriter, req *http.Request) {
|
|||
writeJsonResponse(resp, emptyResponse)
|
||||
}
|
||||
|
||||
func writeJsonResponse(resp http.ResponseWriter, podResponse PodResponse) {
|
||||
func writeJsonResponse(resp http.ResponseWriter, podResponse interface{}) {
|
||||
jsonData, _ := json.Marshal(podResponse)
|
||||
resp.Header().Set("Content-Type", "application/json")
|
||||
resp.WriteHeader(http.StatusCreated)
|
||||
resp.Write(jsonData)
|
||||
resp.Write(jsonData)
|
||||
}
|
||||
|
||||
func isRequestHmacValid(req *http.Request) bool {
|
||||
azureDevOpsHeader := "X-Azure-Signature"
|
||||
headerVal := req.Header.Get(azureDevOpsHeader)
|
||||
requestBody, _ := ioutil.ReadAll(req.Body)
|
||||
|
||||
|
||||
// No header is specified
|
||||
if (headerVal == "") {
|
||||
if headerVal == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
// Compute HMAC for body and compare against the one sent by azure dev ops
|
||||
return ValidateHash(string(requestBody), headerVal)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,7 +51,7 @@ spec:
|
|||
tier: frontend
|
||||
spec:
|
||||
containers:
|
||||
- image: divyanshm/k8s-poolprovider:v2.21
|
||||
- image: divyanshm/k8s-poolprovider:v2.23
|
||||
name: k8s-poolprovider
|
||||
command: ["/app/main"]
|
||||
ports:
|
||||
|
|
Загрузка…
Ссылка в новой задаче