From 005af60d112571c9e17232f8fc27448779344394 Mon Sep 17 00:00:00 2001 From: Patrick Derks Date: Wed, 21 Aug 2024 18:59:26 +0200 Subject: [PATCH] fix: container name matching This was an issue if the helm release name is not shopware.. --- internal/controller/status.go | 5 +++-- internal/deployment/storefront.go | 4 +++- internal/deployment/util.go | 2 +- internal/job/setup.go | 4 +++- internal/job/util.go | 31 ++++++++++++++++++------------- 5 files changed, 28 insertions(+), 18 deletions(-) diff --git a/internal/controller/status.go b/internal/controller/status.go index df4d113..cdcfc99 100644 --- a/internal/controller/status.go +++ b/internal/controller/status.go @@ -229,7 +229,7 @@ func (r *StoreReconciler) stateSetup(ctx context.Context, store *v1.Store) v1.St return v1.StateSetup } - done, err := job.IsJobContainerDone(ctx, r.Client, setup) + done, err := job.IsJobContainerDone(ctx, r.Client, setup, job.CONTAINER_NAME_SETUP_JOB) if err != nil { con.Reason = err.Error() con.Status = Error @@ -281,7 +281,7 @@ func (r *StoreReconciler) stateMigration(ctx context.Context, store *v1.Store) v return v1.StateMigration } - done, err := job.IsJobContainerDone(ctx, r.Client, migration) + done, err := job.IsJobContainerDone(ctx, r.Client, migration, job.MigrateJobName(store)) if err != nil { con.Reason = err.Error() con.Status = Error @@ -369,6 +369,7 @@ func (r *StoreReconciler) stateReady(ctx context.Context, store *v1.Store) v1.St } con.Status = Error con.Reason = fmt.Sprintf("get deployment: %s", err.Error()) + return v1.StateReady } if currentImage == store.Spec.Container.Image { diff --git a/internal/deployment/storefront.go b/internal/deployment/storefront.go index 5f2244c..e650e8e 100644 --- a/internal/deployment/storefront.go +++ b/internal/deployment/storefront.go @@ -15,6 +15,8 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" ) +const DEPLOYMENT_STOREFRONT_CONTAINER_NAME = "shopware-storefront" + func GetStorefrontDeployment( ctx context.Context, store *v1.Store, @@ -39,6 +41,7 @@ func StorefrontDeployment(store *v1.Store) *appsv1.Deployment { maps.Copy(labels, util.GetDefaultLabels(store)) containers := append(store.Spec.Container.ExtraContainers, corev1.Container{ + Name: DEPLOYMENT_STOREFRONT_CONTAINER_NAME, LivenessProbe: &corev1.Probe{ ProbeHandler: corev1.ProbeHandler{ HTTPGet: &corev1.HTTPGetAction{ @@ -65,7 +68,6 @@ func StorefrontDeployment(store *v1.Store) *appsv1.Deployment { TimeoutSeconds: 5, InitialDelaySeconds: 5, }, - Name: appName, Image: store.Spec.Container.Image, ImagePullPolicy: store.Spec.Container.ImagePullPolicy, Env: store.GetEnv(), diff --git a/internal/deployment/util.go b/internal/deployment/util.go index c8f37e4..13fc0a8 100644 --- a/internal/deployment/util.go +++ b/internal/deployment/util.go @@ -28,7 +28,7 @@ func GetStoreDeploymentImage( } for _, container := range search.Spec.Template.Spec.Containers { - if container.Name == GetStorefrontDeploymentName(store) { + if container.Name == DEPLOYMENT_STOREFRONT_CONTAINER_NAME { return container.Image, nil } } diff --git a/internal/job/setup.go b/internal/job/setup.go index c7edcc3..f8e4b37 100644 --- a/internal/job/setup.go +++ b/internal/job/setup.go @@ -15,6 +15,8 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" ) +const CONTAINER_NAME_SETUP_JOB = "shopware-setup" + func GetSetupJob(ctx context.Context, client client.Client, store *v1.Store) (*batchv1.Job, error) { setup := SetupJob(store) search := &batchv1.Job{ @@ -64,7 +66,7 @@ func SetupJob(store *v1.Store) *batchv1.Job { ) containers := append(store.Spec.Container.ExtraContainers, corev1.Container{ - Name: "shopware-setup", + Name: CONTAINER_NAME_SETUP_JOB, ImagePullPolicy: store.Spec.Container.ImagePullPolicy, Image: store.Spec.Container.Image, Command: []string{"sh", "-c"}, diff --git a/internal/job/util.go b/internal/job/util.go index 6bb25ca..1032dca 100644 --- a/internal/job/util.go +++ b/internal/job/util.go @@ -16,6 +16,7 @@ func IsJobContainerDone( ctx context.Context, c client.Client, job *batchv1.Job, + containerName string, ) (bool, error) { if job == nil { @@ -23,7 +24,7 @@ func IsJobContainerDone( } for _, container := range job.Spec.Template.Spec.Containers { - if container.Name == job.Name { + if container.Name == containerName { selector, err := labels.ValidatedSelectorFromSet(job.Labels) if err != nil { return false, fmt.Errorf("get selector: %w", err) @@ -40,33 +41,37 @@ func IsJobContainerDone( return false, fmt.Errorf("get pods: %w", err) } + var isOneFinished bool for _, pod := range pods.Items { for _, c := range pod.Status.ContainerStatuses { - if c.Name == job.Name { + if c.Name == containerName { if c.State.Terminated == nil { - log.FromContext(ctx).Info("Setup not terminated still running") - return false, nil + log.FromContext(ctx).Info("Job not terminated still running") + continue } if c.State.Terminated.ExitCode != 0 { log.FromContext(ctx). - Info("Setup job has not 0 as exit code, check setup") - return false, fmt.Errorf( - "Errors in setup: %s", - c.State.Terminated.Reason, - ) + Info("Job has not 0 as exit code, check job") + continue } - if c.State.Terminated.Reason == "Completed" { - log.FromContext(ctx).Info("Setup job completed") - return true, nil + log.FromContext(ctx).Info("Job completed") + isOneFinished = true } } } } + if isOneFinished { + return true, nil + } else { + return false, nil + } } } - return false, nil + err := fmt.Errorf("job not found in container") + log.FromContext(ctx).Error(err, "job not found in container") + return false, err } func deleteJobsByLabel(