Skip to content

Commit

Permalink
Merge pull request #34 from shopware/fix-container-name-matching
Browse files Browse the repository at this point in the history
fix: container name matching
  • Loading branch information
TrayserCassa committed Aug 21, 2024
2 parents 47a05b2 + 005af60 commit 9e28a9b
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 21 deletions.
8 changes: 7 additions & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ before:
- go mod tidy
- make generate
- make manifests
- make licenses path=.

builds:
- id: manager
Expand All @@ -30,7 +31,9 @@ dockers:
- id: operator-amd64
goarch: amd64
use: buildx

extra_files:
- "LICENSE"
- "third-party-licenses.md"
goamd64: v3
dockerfile: build/Dockerfile
image_templates:
Expand All @@ -43,6 +46,9 @@ dockers:
- id: operator-arm64
goarch: arm64
use: buildx
extra_files:
- "LICENSE"
- "third-party-licenses.md"
goamd64: v3
dockerfile: build/Dockerfile
image_templates:
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ help: ## Display this help.
licenses: path go-licenses
mkdir -p $(path)
@cd cmd; \
$(GOLICENSES) report . --template ../build/licenses.tpl > ../third-party-licenses.md
mv third-party-licenses.md $(path)
$(GOLICENSES) report . --template ../build/licenses.tpl > ../tpl.md
mv tpl.md $(path)/third-party-licenses.md

##@ Development

Expand Down
5 changes: 3 additions & 2 deletions internal/controller/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion internal/deployment/storefront.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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{
Expand All @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion internal/deployment/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
4 changes: 3 additions & 1 deletion internal/job/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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"},
Expand Down
31 changes: 18 additions & 13 deletions internal/job/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ func IsJobContainerDone(
ctx context.Context,
c client.Client,
job *batchv1.Job,
containerName string,
) (bool, error) {

if job == nil {
return false, fmt.Errorf("job to check is nil")
}

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)
Expand All @@ -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(
Expand Down

0 comments on commit 9e28a9b

Please sign in to comment.