Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backwards compatibility when spec.chain.versions is not populated #375

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions internal/fullnode/pod_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
46 changes: 41 additions & 5 deletions internal/fullnode/pod_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down
Loading