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