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

Remove hard-coded prefix on container group name, create dummy ProblemDetails from unknown errors #36

Merged
merged 6 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
7 changes: 5 additions & 2 deletions demo/qr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ metadata:
name: qr-code
namespace: saladcloud-demo
spec:
replicas: 2
replicas: 1
selector:
matchLabels:
app: qr-code-demo
Expand All @@ -26,9 +26,12 @@ spec:
- image: saladtechnologies/stable-fast-qr-code:latest-baked
name: qr-code
resources:
requests:
cpu: 2
seniorquico marked this conversation as resolved.
Show resolved Hide resolved
memory: 8Gi
limits:
cpu: 2
dtroyer-salad marked this conversation as resolved.
Show resolved Hide resolved
memory: 8192
memory: 8Gi
env:
- name: HOST
value: "*"
Expand Down
16 changes: 10 additions & 6 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@ func (p *SaladCloudProvider) NotifyPods(ctx context.Context, notifierCallback fu
func (p *SaladCloudProvider) CreatePod(ctx context.Context, pod *corev1.Pod) error {
ctx, span := trace.StartSpan(ctx, "CreatePod")
defer span.End()
p.logger.Debug("creating a CreatePod", pod.Name)
p.logger.Infof("CreatePod: %s", pod.Name)
createContainerObject := p.createContainersObject(pod)
p.logger.Debugf(" createContainerObject: %+v", createContainerObject)
createContainerGroup := p.createContainerGroup(createContainerObject, pod)
p.logger.Debugf(" createContainerGroup: %+v", createContainerGroup[0])

_, r, err := p.apiClient.
ContainerGroupsAPI.CreateContainerGroup(
Expand Down Expand Up @@ -192,6 +194,7 @@ func (p *SaladCloudProvider) CreatePod(ctx context.Context, pod *corev1.Pod) err
}

func (p *SaladCloudProvider) UpdatePod(ctx context.Context, pod *corev1.Pod) error {
p.logger.Debugf("UpdatePod: %s: %+v", utils.GetPodName(pod.Namespace, pod.Name, pod), pod)
return nil
}

Expand Down Expand Up @@ -229,8 +232,8 @@ func (p *SaladCloudProvider) DeletePod(ctx context.Context, pod *corev1.Pod) err
}

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()
podname := utils.GetPodName(namespace, name, nil)
resp, r, err := saladclient.NewAPIClient(saladclient.NewConfiguration()).ContainerGroupsAPI.GetContainerGroup(p.contextWithAuth(), p.inputVars.OrganizationName, p.inputVars.ProjectName, podname).Execute()
if err != nil {
// Get response body for error info
pd, err := utils.GetResponseBody(r)
Expand All @@ -240,7 +243,7 @@ func (p *SaladCloudProvider) GetPod(ctx context.Context, namespace string, name
}

if r.StatusCode == 404 {
p.logger.Warnf("`ContainerGroupsAPI.GetPod`: %s not found", name)
p.logger.Warnf("`ContainerGroupsAPI.GetPod`: %s not found", podname)
} else {
p.logger.Errorf("`ContainerGroupsAPI.GetPod`: Error: %+v", *pd)
}
Expand Down Expand Up @@ -287,7 +290,8 @@ func (p *SaladCloudProvider) GetPodStatus(ctx context.Context, namespace string,
_, span := trace.StartSpan(ctx, "GetPodStatus")
defer span.End()

containerGroup, response, err := p.apiClient.ContainerGroupsAPI.GetContainerGroup(p.contextWithAuth(), p.inputVars.OrganizationName, p.inputVars.ProjectName, utils.GetPodName(namespace, name, nil)).Execute()
podname := utils.GetPodName(namespace, name, nil)
containerGroup, response, err := p.apiClient.ContainerGroupsAPI.GetContainerGroup(p.contextWithAuth(), p.inputVars.OrganizationName, p.inputVars.ProjectName, podname).Execute()
if err != nil {
// Get response body for error info
pd, err := utils.GetResponseBody(response)
Expand All @@ -298,7 +302,7 @@ func (p *SaladCloudProvider) GetPodStatus(ctx context.Context, namespace string,

if response.StatusCode == 404 {
p.logger.WithField("namespace", namespace).
WithField("name", name).
WithField("name", podname).
Warnf("Not Found")
} else {
p.logger.WithField("namespace", namespace).
Expand Down
10 changes: 7 additions & 3 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package utils

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

Expand Down Expand Up @@ -49,7 +48,7 @@ func GetPodName(nameSpace, containerGroup string, pod *corev1.Pod) string {
if nameSpace == "" && containerGroup == "" && pod.Spec.Containers[0].Name != "" {
return pod.Spec.Containers[0].Name
}
return "salad-cloud-" + nameSpace + "-" + containerGroup
return nameSpace + "-" + containerGroup
}

func GetPodPhaseFromContainerGroupState(containerGroupState saladclient.ContainerGroupState) corev1.PodPhase {
Expand Down Expand Up @@ -89,7 +88,12 @@ func GetResponseBody(response *http.Response) (*saladclient.ProblemDetails, erro
pd := saladclient.NewNullableProblemDetails(nil)
err := pd.UnmarshalJSON(body)
if err != nil {
return nil, fmt.Errorf("Error decoding response body: %s", response.Body)
// Can't decode ProblemDetails, just make one and return the string
npd := saladclient.NewProblemDetails()
npd.SetType("unknown_error")
npd.SetTitle("Error decoding response body")
npd.SetDetail(string(body))
pd.Set(npd)
}
return pd.Get(), nil
}