diff --git a/internal/fullnode/pod_builder.go b/internal/fullnode/pod_builder.go index 8ad423e3..d5b779f4 100644 --- a/internal/fullnode/pod_builder.go +++ b/internal/fullnode/pod_builder.go @@ -106,26 +106,29 @@ func NewPodBuilder(crd *cosmosv1.CosmosFullNode) PodBuilder { ReadinessProbe: probes[1], ImagePullPolicy: tpl.ImagePullPolicy, }, - // version check sidecar, runs on terminate in case the instance is halting for upgrade. - { - Name: "version-check-term", - Image: "ghcr.io/strangelove-ventures/cosmos-operator:" + version.DockerTag(), - Command: versionCheckCmd, - Resources: corev1.ResourceRequirements{ - Requests: corev1.ResourceList{ - corev1.ResourceCPU: resource.MustParse("5m"), - corev1.ResourceMemory: resource.MustParse("16Mi"), - }, - }, - Env: envVars(crd), - ImagePullPolicy: tpl.ImagePullPolicy, - WorkingDir: workDir, - SecurityContext: &corev1.SecurityContext{}, - }, }, }, } + if len(crd.Spec.ChainSpec.Versions) > 0 { + // version check sidecar, runs on inverval in case the instance is halting for upgrade. + pod.Spec.Containers = append(pod.Spec.Containers, corev1.Container{ + Name: "version-check-interval", + Image: "ghcr.io/strangelove-ventures/cosmos-operator:" + version.DockerTag(), + Command: versionCheckCmd, + Resources: corev1.ResourceRequirements{ + Requests: corev1.ResourceList{ + corev1.ResourceCPU: resource.MustParse("5m"), + corev1.ResourceMemory: resource.MustParse("16Mi"), + }, + }, + Env: envVars(crd), + ImagePullPolicy: tpl.ImagePullPolicy, + WorkingDir: workDir, + SecurityContext: &corev1.SecurityContext{}, + }) + } + preserveMergeInto(pod.Labels, tpl.Metadata.Labels) preserveMergeInto(pod.Annotations, tpl.Metadata.Annotations) @@ -270,7 +273,9 @@ func (b PodBuilder) WithOrdinal(ordinal int32) PodBuilder { // The healthcheck sidecar needs access to the home directory so it can read disk usage. {Name: volChainHome, MountPath: ChainHomeDir(b.crd), ReadOnly: true}, } - pod.Spec.Containers[2].VolumeMounts = mounts + if len(pod.Spec.Containers) > 2 { + pod.Spec.Containers[2].VolumeMounts = mounts + } b.pod = pod return b diff --git a/internal/fullnode/pod_builder_test.go b/internal/fullnode/pod_builder_test.go index 830827ac..27be4668 100644 --- a/internal/fullnode/pod_builder_test.go +++ b/internal/fullnode/pod_builder_test.go @@ -208,7 +208,7 @@ func TestPodBuilder(t *testing.T) { pod, err := builder.WithOrdinal(6).Build() require.NoError(t, err) - require.Len(t, pod.Spec.Containers, 3) + require.Len(t, pod.Spec.Containers, 2) startContainer := pod.Spec.Containers[0] require.Equal(t, "node", startContainer.Name) @@ -288,7 +288,7 @@ func TestPodBuilder(t *testing.T) { pod, err := builder.WithOrdinal(6).Build() require.NoError(t, err) - require.Len(t, pod.Spec.Containers, 3) + require.Len(t, pod.Spec.Containers, 2) container := pod.Spec.Containers[0] require.Equal(t, "node", container.Name) @@ -347,7 +347,7 @@ func TestPodBuilder(t *testing.T) { require.Equal(t, "osmosis-node-key-5", vols[4].Secret.SecretName) require.Equal(t, []corev1.KeyToPath{{Key: "node_key.json", Path: "node_key.json"}}, vols[4].Secret.Items) - require.Equal(t, len(pod.Spec.Containers), 3) + require.Equal(t, len(pod.Spec.Containers), 2) c := pod.Spec.Containers[0] require.Equal(t, "node", c.Name) // Sanity check @@ -523,7 +523,7 @@ gaiad start --home /home/operator/cosmos` require.Nilf(t, cont.ReadinessProbe, "container %d", i) } - require.Equal(t, 3, len(pod.Spec.Containers)) + require.Equal(t, 2, len(pod.Spec.Containers)) require.Equal(t, "node", pod.Spec.Containers[0].Name) sidecar := pod.Spec.Containers[1] @@ -558,7 +558,7 @@ gaiad start --home /home/operator/cosmos` require.Equal(t, &corev1.EmptyDirVolumeSource{}, vols["foo-vol"].VolumeSource.EmptyDir) containers := lo.SliceToMap(pod.Spec.Containers, func(c corev1.Container) (string, corev1.Container) { return c.Name, c }) - require.ElementsMatch(t, []string{"node", "new-sidecar", "healthcheck", "version-check-term"}, lo.Keys(containers)) + require.ElementsMatch(t, []string{"node", "new-sidecar", "healthcheck"}, lo.Keys(containers)) extraVol := lo.Filter(containers["node"].VolumeMounts, func(vm corev1.VolumeMount, _ int) bool { return vm.Name == "foo-vol" }) require.Equal(t, "/foo", extraVol[0].MountPath) @@ -568,6 +568,42 @@ gaiad start --home /home/operator/cosmos` require.Equal(t, "foo:latest", initConts["chain-init"].Image) }) + t.Run("containers with chain spec versions", func(t *testing.T) { + crd := defaultCRD() + crd.Spec.PodTemplate.Volumes = []corev1.Volume{ + {Name: "foo-vol", VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}}}, + } + crd.Spec.PodTemplate.InitContainers = []corev1.Container{ + {Name: "chain-init", Image: "foo:latest", VolumeMounts: []corev1.VolumeMount{ + {Name: "foo-vol", MountPath: "/foo"}, // Should be merged with existing. + }}, + {Name: "new-init", Image: "new-init:latest"}, // New container. + } + crd.Spec.PodTemplate.Containers = []corev1.Container{ + {Name: "node", VolumeMounts: []corev1.VolumeMount{ + {Name: "foo-vol", MountPath: "/foo"}, // Should be merged with existing. + }}, + {Name: "new-sidecar", Image: "new-sidecar:latest"}, // New container. + } + crd.Spec.ChainSpec.Versions = []cosmosv1.ChainVersion{ + { + UpgradeHeight: 1, + Image: "image:v1.0.0", + }, + { + UpgradeHeight: 100, + Image: "image:v2.0.0", + }, + } + + builder := NewPodBuilder(&crd) + pod, err := builder.WithOrdinal(0).Build() + require.NoError(t, err) + + containers := lo.SliceToMap(pod.Spec.Containers, func(c corev1.Container) (string, corev1.Container) { return c.Name, c }) + require.ElementsMatch(t, []string{"node", "new-sidecar", "healthcheck", "version-check-interval"}, lo.Keys(containers)) + }) + test.HasTypeLabel(t, func(crd cosmosv1.CosmosFullNode) []map[string]string { builder := NewPodBuilder(&crd) pod, _ := builder.WithOrdinal(5).Build()