Skip to content

Commit

Permalink
Merge pull request #85 from shopware/fix-envs
Browse files Browse the repository at this point in the history
fix: env overwrite
  • Loading branch information
TrayserCassa authored Feb 11, 2025
2 parents 654635e + eea9125 commit 56fba67
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
11 changes: 10 additions & 1 deletion api/v1/store_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v1

import (
"fmt"
"slices"
"strconv"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -327,5 +328,13 @@ func (s *Store) GetEnv() []corev1.EnvVar {
c = append(c, s.getWorker()...)
c = append(c, s.Spec.FPM.getFPMConfiguration()...)

return append(c, s.Spec.Container.ExtraEnvs...)
for _, obj2 := range s.Spec.Container.ExtraEnvs {
if i := slices.IndexFunc(c, func(c corev1.EnvVar) bool { return c.Name == obj2.Name }); i > -1 {
c[i] = obj2
} else {
c = append(c, obj2)
}
}

return c
}
43 changes: 43 additions & 0 deletions api/v1/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
v1 "github.com/shopware/shopware-operator/api/v1"
"github.com/shopware/shopware-operator/internal/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)
Expand Down Expand Up @@ -294,3 +295,45 @@ func TestAffinity(t *testing.T) {
baseContainer.Spec.Container.Merge(mergeSpec)
assert.Equal(t, "merged", baseContainer.Spec.Container.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms[0].MatchExpressions[0].Key)
}

func TestEnvMerge(t *testing.T) {
baseContainer := &v1.Store{
Spec: v1.StoreSpec{
Container: v1.ContainerSpec{
Image: "originalImage",
ImagePullPolicy: corev1.PullAlways,
Replicas: 5,
ProgressDeadlineSeconds: 30,
RestartPolicy: corev1.RestartPolicyAlways,
ExtraEnvs: []corev1.EnvVar{
{Name: "APP_URL", Value: "overwritten"},
{Name: "NEW", Value: "exists"},
},
VolumeMounts: []corev1.VolumeMount{
{Name: "original-volume", MountPath: "/original"},
},
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("1"),
},
},
Labels: map[string]string{
"original": "value",
},
Annotations: map[string]string{
"original": "value",
},
TerminationGracePeriodSeconds: 10,
},
},
}
env := baseContainer.GetEnv()
for _, envVar := range env {
if envVar.Name == "APP_URL" {
require.Equal(t, "overwritten", envVar.Value)
}
if envVar.Name == "NEW" {
require.Equal(t, "exists", envVar.Value)
}
}
}
15 changes: 15 additions & 0 deletions internal/controller/store_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controller
import (
"context"
"fmt"
"slices"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
Expand All @@ -11,6 +12,7 @@ import (
"github.com/shopware/shopware-operator/internal/job"
"github.com/shopware/shopware-operator/internal/k8s"
"github.com/shopware/shopware-operator/internal/util"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -41,6 +43,8 @@ func (r *StoreReconciler) reconcileCRStatus(
)
}

printWarningForEnvs(ctx, store)

// First creation
if store.IsState(v1.StateEmpty) || store.IsState(v1.StateWait) {
// We disable the checks for local development, so you don't need to run
Expand Down Expand Up @@ -104,6 +108,17 @@ func (r *StoreReconciler) reconcileCRStatus(
}, store.Status)
}

func printWarningForEnvs(ctx context.Context, store *v1.Store) {
l := log.FromContext(ctx)

envs := store.GetEnv()
for _, obj2 := range store.Spec.Container.ExtraEnvs {
if slices.ContainsFunc(envs, func(c corev1.EnvVar) bool { return c.Name == obj2.Name }) {
l.Info("Overwriting env var. If you can, please use the crd to define it", "name", obj2.Name)
}
}
}

func (r *StoreReconciler) checkDatabaseServices(
ctx context.Context,
store *v1.Store,
Expand Down
16 changes: 16 additions & 0 deletions internal/job/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@ 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))
return JobState{
ExitCode: -404,
Running: false,
}, nil
}

if job.Status.Failed > 0 {
log.FromContext(ctx).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())
return JobState{}, err
Expand Down

0 comments on commit 56fba67

Please sign in to comment.