From d870949b6245303b0226a07121165b17fe7b0b7f Mon Sep 17 00:00:00 2001 From: David Nix Date: Wed, 29 Mar 2023 07:38:54 -0600 Subject: [PATCH] fix: Don't rely on volume order when finding PVC name --- internal/fullnode/pod_builder.go | 26 +++++++++++++++----------- internal/fullnode/pod_builder_test.go | 5 +++++ 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/internal/fullnode/pod_builder.go b/internal/fullnode/pod_builder.go index 392b1072..d6909bef 100644 --- a/internal/fullnode/pod_builder.go +++ b/internal/fullnode/pod_builder.go @@ -10,6 +10,7 @@ import ( "strings" "sync" + "github.com/samber/lo" cosmosv1 "github.com/strangelove-ventures/cosmos-operator/api/v1" "github.com/strangelove-ventures/cosmos-operator/internal/healthcheck" "github.com/strangelove-ventures/cosmos-operator/internal/kube" @@ -172,6 +173,13 @@ func (b PodBuilder) Build() (*corev1.Pod, error) { return pod, nil } +const ( + volChainHome = "vol-chain-home" // Stores live chain data and config files. + volTmp = "vol-tmp" // Stores temporary config files for manipulation later. + volConfig = "vol-config" // Items from ConfigMap. + volSystemTmp = "vol-system-tmp" // Necessary for statesync or else you may see the error: ERR State sync failed err="failed to create chunk queue: unable to create temp dir for state sync chunks: stat /tmp: no such file or directory" module=statesync +) + // WithOrdinal updates adds name and other metadata to the pod using "ordinal" which is the pod's // ordered sequence. Pods have deterministic, consistent names similar to a StatefulSet instead of generated names. func (b PodBuilder) WithOrdinal(ordinal int32) PodBuilder { @@ -184,12 +192,6 @@ func (b PodBuilder) WithOrdinal(ordinal int32) PodBuilder { pod.Name = name pod.Spec.InitContainers = initContainers(b.crd, name) - const ( - volChainHome = "vol-chain-home" // Stores live chain data and config files. - volTmp = "vol-tmp" // Stores temporary config files for manipulation later. - volConfig = "vol-config" // Items from ConfigMap. - volSystemTmp = "vol-system-tmp" // Necessary for statesync or else you may see the error: ERR State sync failed err="failed to create chunk queue: unable to create temp dir for state sync chunks: stat /tmp: no such file or directory" module=statesync - ) pod.Spec.Volumes = []corev1.Volume{ { Name: volChainHome, @@ -401,10 +403,12 @@ func podPatch(crd *cosmosv1.CosmosFullNode) *corev1.Pod { // PVCName returns the primary PVC holding the chain data associated with the pod. func PVCName(pod *corev1.Pod) string { - if vols := pod.Spec.Volumes; len(vols) > 0 { - if claim := vols[0].PersistentVolumeClaim; claim != nil { - return claim.ClaimName - } + found, ok := lo.Find(pod.Spec.Volumes, func(v corev1.Volume) bool { return v.Name == volChainHome }) + if !ok { + return "" + } + if found.PersistentVolumeClaim == nil { + return "" } - return "" + return found.PersistentVolumeClaim.ClaimName } diff --git a/internal/fullnode/pod_builder_test.go b/internal/fullnode/pod_builder_test.go index 92d3b7bc..a08f07cc 100644 --- a/internal/fullnode/pod_builder_test.go +++ b/internal/fullnode/pod_builder_test.go @@ -542,5 +542,10 @@ func TestPVCName(t *testing.T) { builder := NewPodBuilder(&crd) pod, err := builder.WithOrdinal(5).Build() require.NoError(t, err) + + require.Equal(t, "pvc-osmosis-5", PVCName(pod)) + + pod.Spec.Volumes = append([]corev1.Volume{{Name: "foo"}}, pod.Spec.Volumes...) + require.Equal(t, "pvc-osmosis-5", PVCName(pod)) }