Skip to content

Commit

Permalink
Merge branch 'main' into fix-db-check
Browse files Browse the repository at this point in the history
  • Loading branch information
TrayserCassa authored Feb 18, 2025
2 parents 2fb7449 + 9a7ac88 commit edb0c56
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
3 changes: 2 additions & 1 deletion helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ tolerations: []
labels: {}
podAnnotations: {}

logStructured: false
# If set to true this will create json logs instead of user logs
logStructured: true
debug: false
26 changes: 17 additions & 9 deletions internal/job/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func IsJobContainerDone(
return JobState{}, fmt.Errorf("job to check is nil")
}

logger := log.FromContext(ctx).WithValues("job", job.Name)

var errorStates []JobState
for _, container := range job.Spec.Template.Spec.Containers {
if container.Name == containerName {
selector, err := labels.ValidatedSelectorFromSet(job.Labels)
Expand All @@ -56,24 +59,24 @@ func IsJobContainerDone(
for _, pod := range pods.Items {
for _, c := range pod.Status.ContainerStatuses {
if c.Name == containerName {
log.FromContext(ctx).Info(fmt.Sprintf("Found container for job `%s`", c.Name))
logger.Info(fmt.Sprintf("Found container for job `%s`", c.Name))
if c.State.Terminated == nil {
log.FromContext(ctx).Info("Job not terminated still running")
logger.Info("Job not terminated still running")
return JobState{
ExitCode: -1,
Running: true,
}, nil
}
if c.State.Terminated.ExitCode != 0 {
log.FromContext(ctx).
logger.
Info("Job has not 0 as exit code, check job", "exitcode", c.State.Terminated.ExitCode)
return JobState{
errorStates = append(errorStates, JobState{
ExitCode: int(c.State.Terminated.ExitCode),
Running: false,
}, nil
})
}
if c.State.Terminated.Reason == "Completed" {
log.FromContext(ctx).Info("Job completed")
logger.Info("Job completed")
return JobState{
ExitCode: 0,
Running: false,
Expand All @@ -86,23 +89,28 @@ func IsJobContainerDone(
}

if job.Status.Succeeded > 0 {
log.FromContext(ctx).Info(fmt.Sprintf("job not found in container: %s. But job has succeeded continue with job done.", containerName))
logger.Info(fmt.Sprintf("job not found in container: %s. But job has succeeded continue with job done.", containerName))
return JobState{
ExitCode: 0,
Running: false,
}, nil
}

if len(errorStates) > 0 {
// Return the latest error state
return errorStates[len(errorStates)-1], nil
}

if job.Status.Failed > 0 {
log.FromContext(ctx).Info(fmt.Sprintf("job not found in container: %s. But job has failed.", containerName))
logger.Info(fmt.Sprintf("job not found in container: %s. But job has failed.", containerName))
return JobState{
ExitCode: -404,
Running: false,
}, nil
}

err := fmt.Errorf("job not found in container: %s", containerName)
log.FromContext(ctx).Info(err.Error())
logger.Info(err.Error())
return JobState{}, err
}

Expand Down
38 changes: 38 additions & 0 deletions internal/util/labels_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package util_test

import (
"fmt"
"testing"

v1 "github.com/shopware/shopware-operator/api/v1"
"github.com/shopware/shopware-operator/internal/util"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestLabelMerge(t *testing.T) {
store := v1.Store{
ObjectMeta: metav1.ObjectMeta{
Name: "name",
},
Spec: v1.StoreSpec{
Container: v1.ContainerSpec{
Labels: map[string]string{
"test": "test",
"app": "selector",
},
},
},
}

overwrite := make(map[string]string)
overwrite["test"] = "overwrite"
overwrite["test2"] = "test2"

labels := util.GetDefaultContainerStoreLabels(store, overwrite)
fmt.Println(labels)
require.Equal(t, "selector", labels["app"])
require.Equal(t, "overwrite", labels["test"])
require.Equal(t, "test2", labels["test2"])
require.Equal(t, "name", labels["shop.shopware.com/store.name"])
}

0 comments on commit edb0c56

Please sign in to comment.