Return error to kubectl logs; use http statuscode
This commit is contained in:
@@ -7,8 +7,8 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/virtual-kubelet/virtual-kubelet/manager"
|
||||
@@ -181,10 +181,9 @@ func (p *ACIProvider) DeletePod(pod *v1.Pod) error {
|
||||
// GetPod returns a pod by name that is running inside ACI
|
||||
// returns nil if a pod by that name is not found.
|
||||
func (p *ACIProvider) GetPod(namespace, name string) (*v1.Pod, error) {
|
||||
cg, err := p.aciClient.GetContainerGroup(p.resourceGroup, fmt.Sprintf("%s-%s", namespace, name))
|
||||
cg, err, status := p.aciClient.GetContainerGroup(p.resourceGroup, fmt.Sprintf("%s-%s", namespace, name))
|
||||
if err != nil {
|
||||
// Trap error for 404 and return gracefully
|
||||
if strings.Contains(err.Error(), "ResourceNotFound") {
|
||||
if *status == http.StatusNotFound {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
@@ -200,12 +199,8 @@ func (p *ACIProvider) GetPod(namespace, name string) (*v1.Pod, error) {
|
||||
// GetPodLogs returns the logs of a pod by name that is running inside ACI.
|
||||
func (p *ACIProvider) GetContainerLogs(namespace, podName, containerName string, tail int) (string, error) {
|
||||
logContent := ""
|
||||
cg, err := p.aciClient.GetContainerGroup(p.resourceGroup, fmt.Sprintf("%s-%s", namespace, podName))
|
||||
cg, err, _ := p.aciClient.GetContainerGroup(p.resourceGroup, fmt.Sprintf("%s-%s", namespace, podName))
|
||||
if err != nil {
|
||||
// Trap error for 404 and return gracefully
|
||||
if strings.Contains(err.Error(), "ResourceNotFound") {
|
||||
return logContent, nil
|
||||
}
|
||||
return logContent, err
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
// GetContainerGroup gets an Azure Container Instance in the provided
|
||||
// resource group with the given container group name.
|
||||
// From: https://docs.microsoft.com/en-us/rest/api/container-instances/containergroups/get
|
||||
func (c *Client) GetContainerGroup(resourceGroup, containerGroupName string) (*ContainerGroup, error) {
|
||||
func (c *Client) GetContainerGroup(resourceGroup, containerGroupName string) (*ContainerGroup, error, *int) {
|
||||
urlParams := url.Values{
|
||||
"api-version": []string{apiVersion},
|
||||
}
|
||||
@@ -25,7 +25,7 @@ func (c *Client) GetContainerGroup(resourceGroup, containerGroupName string) (*C
|
||||
// Create the request.
|
||||
req, err := http.NewRequest("GET", uri, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Creating get container group uri request failed: %v", err)
|
||||
return nil, fmt.Errorf("Creating get container group uri request failed: %v", err), nil
|
||||
}
|
||||
|
||||
// Add the parameters to the url.
|
||||
@@ -34,29 +34,29 @@ func (c *Client) GetContainerGroup(resourceGroup, containerGroupName string) (*C
|
||||
"resourceGroup": resourceGroup,
|
||||
"containerGroupName": containerGroupName,
|
||||
}); err != nil {
|
||||
return nil, fmt.Errorf("Expanding URL with parameters failed: %v", err)
|
||||
return nil, fmt.Errorf("Expanding URL with parameters failed: %v", err), nil
|
||||
}
|
||||
|
||||
// Send the request.
|
||||
resp, err := c.hc.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Sending get container group request failed: %v", err)
|
||||
return nil, fmt.Errorf("Sending get container group request failed: %v", err), &resp.StatusCode
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// 200 (OK) is a success response.
|
||||
if err := api.CheckResponse(resp); err != nil {
|
||||
return nil, err
|
||||
return nil, err, &resp.StatusCode
|
||||
}
|
||||
|
||||
// Decode the body from the response.
|
||||
if resp.Body == nil {
|
||||
return nil, errors.New("Create container group returned an empty body in the response")
|
||||
return nil, errors.New("Create container group returned an empty body in the response"), &resp.StatusCode
|
||||
}
|
||||
var cg ContainerGroup
|
||||
if err := json.NewDecoder(resp.Body).Decode(&cg); err != nil {
|
||||
return nil, fmt.Errorf("Decoding get container group response body failed: %v", err)
|
||||
return nil, fmt.Errorf("Decoding get container group response body failed: %v", err), &resp.StatusCode
|
||||
}
|
||||
|
||||
return &cg, nil
|
||||
return &cg, nil, &resp.StatusCode
|
||||
}
|
||||
|
||||
@@ -37,7 +37,6 @@ func ApiserverStart(provider Provider) {
|
||||
|
||||
func ApiServerHandler(w http.ResponseWriter, req *http.Request) {
|
||||
vars := mux.Vars(req)
|
||||
|
||||
if len(vars) == 3 {
|
||||
namespace := vars["namespace"]
|
||||
pod := vars["pod"]
|
||||
@@ -54,16 +53,14 @@ func ApiServerHandler(w http.ResponseWriter, req *http.Request) {
|
||||
tail = t
|
||||
}
|
||||
}
|
||||
log.Println(tail)
|
||||
podsLogs, err := p.GetContainerLogs(namespace, pod, container, tail)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
io.WriteString(w, err.Error())
|
||||
io.WriteString(w, err.Error())
|
||||
} else {
|
||||
io.WriteString(w, podsLogs)
|
||||
}
|
||||
} else {
|
||||
log.Println("404 request pattern not found")
|
||||
NotFound(w, req)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user