Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log specific error messages in the provider #25

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 49 additions & 9 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package provider

import (
"bytes"
"context"
"encoding/json"
"io"
"strconv"
"strings"
"time"

"github.com/SaladTechnologies/virtual-kubelet-saladcloud/internal/models"
"github.com/SaladTechnologies/virtual-kubelet-saladcloud/internal/utils"
"github.com/google/uuid"
Expand All @@ -14,14 +20,10 @@ import (
"github.com/virtual-kubelet/virtual-kubelet/node/api/statsv1alpha1"
"github.com/virtual-kubelet/virtual-kubelet/node/nodeutil"
"github.com/virtual-kubelet/virtual-kubelet/trace"
"io"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1listers "k8s.io/client-go/listers/core/v1"
"strconv"
"strings"
"time"
)

type SaladCloudProvider struct {
Expand Down Expand Up @@ -116,7 +118,28 @@ func (p *SaladCloudProvider) CreatePod(ctx context.Context, pod *corev1.Pod) err
createContainerGroup[0],
).Execute()
if err != nil {
p.logger.Errorf("Error when calling `ContainerGroupsAPI.CreateContainerGroupModel`", r)
// 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)
if err != nil {
p.logger.Errorf("Error decoding return body: %s", r.Body)
return err
}

// Also handle 403 and 429?
if r.StatusCode == 400 {
if *pd.Get().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())
}
} else {
p.logger.Errorf("Error when calling `ContainerGroupsAPI.CreateContainerGroupModel`", r)
}
return err
}

Expand All @@ -125,7 +148,7 @@ 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.CreateContainerGroupModel`", startHttpResponse)
p.logger.Errorf("Error when calling `ContainerGroupsAPI.StartContainerGroup`", startHttpResponse)
err := p.DeletePod(ctx, pod)
if err != nil {
return err
Expand Down Expand Up @@ -246,9 +269,26 @@ 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 {
p.logger.WithField("namespace", namespace).
WithField("name", name).
Errorf("ContainerGroupsAPI.GetPodStatus ", response)
// 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)
if err != nil {
p.logger.Errorf("Error decoding return body: %s", response.Body)
return nil, models.NewSaladCloudError(err, response)
}

if response.StatusCode == 404 {
p.logger.WithField("namespace", namespace).
WithField("name", name).
Warnf("Not Found")
} else {
p.logger.WithField("namespace", namespace).
WithField("name", name).
Errorf("ContainerGroupsAPI.GetPodStatus ", body)
}
return nil, models.NewSaladCloudError(err, response)
}

Expand Down