Skip to content

Commit

Permalink
Streamline logging in provider package
Browse files Browse the repository at this point in the history
* Create utils.GetResponseBody() to get a ProblemDetails object
  from a failure response body
* Reduce the bulk of information returned in error logs to the
  response body

Signed-off-by: Dean Troyer <[email protected]>
  • Loading branch information
dtroyer-salad committed Oct 27, 2023
1 parent ee5f5a7 commit 2d559d0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 25 deletions.
73 changes: 48 additions & 25 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package provider

import (
"bytes"
"context"
"encoding/json"
"io"
Expand Down Expand Up @@ -119,23 +118,19 @@ func (p *SaladCloudProvider) CreatePod(ctx context.Context, pod *corev1.Pod) err
).Execute()
if err != nil {
// Get response body for error info
body, _ := io.ReadAll(r.Body)
r.Body.Close()
r.Body = io.NopCloser(bytes.NewBuffer(body))
pd := saladclient.NewNullableProblemDetails(nil)
err = pd.UnmarshalJSON(body)
pd, err := utils.GetResponseBody(r)
if err != nil {
p.logger.Errorf("Error decoding return body: %s", r.Body)
p.logger.Errorf("CreatePod: %s", err)
return err
}

// Also handle 403 and 429?
if r.StatusCode == 400 {
if *pd.Get().Type.Get() == "name_conflict" {
if *pd.Type.Get() == "name_conflict" {
// The exciting duplicate name condition!
p.logger.Errorf("Name %s has already been used in provider project %s/%s", pod.Name, p.inputVars.OrganizationName, p.inputVars.ProjectName)
} else {
p.logger.Errorf("Error type %s in `ContainerGroupsAPI.CreateContainerGroupModel`", *pd.Get().Type.Get())
p.logger.Errorf("Error type %s in `ContainerGroupsAPI.CreateContainerGroupModel`", *pd.Type.Get())
}
} else {
p.logger.Errorf("Error when calling `ContainerGroupsAPI.CreateContainerGroupModel`", r)
Expand All @@ -148,8 +143,15 @@ func (p *SaladCloudProvider) CreatePod(ctx context.Context, pod *corev1.Pod) err

startHttpResponse, err := p.apiClient.ContainerGroupsAPI.StartContainerGroup(p.contextWithAuth(), p.inputVars.OrganizationName, p.inputVars.ProjectName, utils.GetPodName(pod.Namespace, pod.Name, nil)).Execute()
if err != nil {
p.logger.Errorf("Error when calling `ContainerGroupsAPI.StartContainerGroup`", startHttpResponse)
err := p.DeletePod(ctx, pod)
// Get response body for error info
pd, err := utils.GetResponseBody(startHttpResponse)
if err != nil {
p.logger.Errorf("`ContainerGroupsAPI.StartContainerGroup`: %s", err)
return err
}

p.logger.Errorf("`ContainerGroupsAPI.StartContainerGroup`: Error: %+v", *pd)
err = p.DeletePod(ctx, pod)
if err != nil {
return err
}
Expand Down Expand Up @@ -185,7 +187,7 @@ func (p *SaladCloudProvider) CreatePod(ctx context.Context, pod *corev1.Pod) err
})
}

p.logger.Infof("Done creating the container and initiating the startup ", pod)
p.logger.Infof("Container %s created and initialized", pod.Name)
return nil
}

Expand All @@ -196,12 +198,19 @@ func (p *SaladCloudProvider) UpdatePod(ctx context.Context, pod *corev1.Pod) err
func (p *SaladCloudProvider) DeletePod(ctx context.Context, pod *corev1.Pod) error {
_, span := trace.StartSpan(ctx, "DeletePod")
defer span.End()
p.logger.Debug("deleting a pod")
p.logger.Debugf("Deleting pod %s", utils.GetPodName(pod.Namespace, pod.Name, pod))
response, err := p.apiClient.ContainerGroupsAPI.DeleteContainerGroup(p.contextWithAuth(), p.inputVars.OrganizationName, p.inputVars.ProjectName, utils.GetPodName(pod.Namespace, pod.Name, pod)).Execute()
pod.Status.Phase = corev1.PodSucceeded
pod.Status.Reason = "Pod Deleted"
if err != nil {
p.logger.Errorf("Error when deleting the container ", response)
// Get response body for error info
pd, err := utils.GetResponseBody(response)
if err != nil {
p.logger.Errorf("`ContainerGroupsAPI.DeletePod`: %s", err)
return err
}

p.logger.Errorf("`ContainerGroupsAPI.DeletePod`: Error: %+v", *pd)
return err
}
now := metav1.Now()
Expand All @@ -214,16 +223,27 @@ func (p *SaladCloudProvider) DeletePod(ctx context.Context, pod *corev1.Pod) err
Reason: "Salad Provider Pod Deleted",
},
}
p.logger.Infof("Container %s deleted", pod.Status.ContainerStatuses[idx].Name)
}
p.logger.Infof("Done deleting the container ", pod)
return nil
}

func (p *SaladCloudProvider) GetPod(ctx context.Context, namespace string, name string) (*corev1.Pod, error) {

resp, r, err := saladclient.NewAPIClient(saladclient.NewConfiguration()).ContainerGroupsAPI.GetContainerGroup(p.contextWithAuth(), p.inputVars.OrganizationName, p.inputVars.ProjectName, utils.GetPodName(namespace, name, nil)).Execute()
if err != nil {
p.logger.Errorf("Error when calling `ContainerGroupsAPI.GetPod`", r)
// Get response body for error info
pd, err := utils.GetResponseBody(r)
if err != nil {
p.logger.Errorf("`ContainerGroupsAPI.GetPod`: %s", err)
return nil, err
}

if r.StatusCode == 404 {
p.logger.Warnf("`ContainerGroupsAPI.GetPod`: %s not found", name)
} else {
p.logger.Errorf("`ContainerGroupsAPI.GetPod`: Error: %+v", *pd)
}
return nil, err
}
startTime := metav1.NewTime(resp.CreateTime)
Expand Down Expand Up @@ -270,14 +290,10 @@ func (p *SaladCloudProvider) GetPodStatus(ctx context.Context, namespace string,
containerGroup, response, err := p.apiClient.ContainerGroupsAPI.GetContainerGroup(p.contextWithAuth(), p.inputVars.OrganizationName, p.inputVars.ProjectName, utils.GetPodName(namespace, name, nil)).Execute()
if err != nil {
// Get response body for error info
body, _ := io.ReadAll(response.Body)
response.Body.Close()
response.Body = io.NopCloser(bytes.NewBuffer(body))
pd := saladclient.NewNullableProblemDetails(nil)
err = pd.UnmarshalJSON(body)
pd, err := utils.GetResponseBody(response)
if err != nil {
p.logger.Errorf("Error decoding return body: %s", response.Body)
return nil, models.NewSaladCloudError(err, response)
p.logger.Errorf("GetPodStatus: %s", err)
return nil, err
}

if response.StatusCode == 404 {
Expand All @@ -287,7 +303,7 @@ func (p *SaladCloudProvider) GetPodStatus(ctx context.Context, namespace string,
} else {
p.logger.WithField("namespace", namespace).
WithField("name", name).
Errorf("ContainerGroupsAPI.GetPodStatus ", body)
Errorf("ContainerGroupsAPI.GetPodStatus: %+v ", *pd)
}
return nil, models.NewSaladCloudError(err, response)
}
Expand All @@ -310,7 +326,14 @@ func (p *SaladCloudProvider) GetPodStatus(ctx context.Context, namespace string,
func (p *SaladCloudProvider) GetPods(ctx context.Context) ([]*corev1.Pod, error) {
resp, r, err := p.apiClient.ContainerGroupsAPI.ListContainerGroups(p.contextWithAuth(), p.inputVars.OrganizationName, p.inputVars.ProjectName).Execute()
if err != nil {
p.logger.Errorf("Error when list ContainerGroupsAPI.ListContainerGroups ", r)
// Get response body for error info
pd, err := utils.GetResponseBody(r)
if err != nil {
p.logger.Errorf("GetPods: %s", err)
return nil, err
}

p.logger.Errorf("`ContainerGroupsAPI.GetPods`: Error: %+v", *pd)
return nil, err
}
pods := make([]*corev1.Pod, 0)
Expand Down
20 changes: 20 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package utils

import (
"bytes"
"fmt"
"io"
"net/http"

saladclient "github.com/lucklypriyansh-2/salad-client"
corev1 "k8s.io/api/core/v1"
)
Expand Down Expand Up @@ -73,3 +78,18 @@ func GetPodPhaseFromContainerGroupState(containerGroupState saladclient.Containe
return ""

}

func GetResponseBody(response *http.Response) (*saladclient.ProblemDetails, error) {
// Get response body for error info
body, _ := io.ReadAll(response.Body)
response.Body.Close()
response.Body = io.NopCloser(bytes.NewBuffer(body))

// Get a ProblemDetails struct from the body
pd := saladclient.NewNullableProblemDetails(nil)
err := pd.UnmarshalJSON(body)
if err != nil {
return nil, fmt.Errorf("Error decoding response body: %s", response.Body)
}
return pd.Get(), nil
}

0 comments on commit 2d559d0

Please sign in to comment.