From dce3b8b2ec76dc6827f55b5c8450740620f4f50e Mon Sep 17 00:00:00 2001 From: Andy Pliszka Date: Thu, 21 Sep 2023 08:45:56 -0400 Subject: [PATCH] feat: sets node type label (#356) * feat(labels): adds node type label to resources * default type is FullNode * allowed values are FullNode|Sentry * fix(labels): test names * fix: uses constant for node type * refactor: adds HasTypeLabel assertion * style: goimports --- internal/fullnode/configmap_builder_test.go | 10 +++++ internal/fullnode/labels.go | 6 +++ internal/fullnode/node_key_builder_test.go | 12 ++++++ internal/fullnode/pod_builder_test.go | 7 ++++ internal/fullnode/pvc_builder_test.go | 10 +++++ internal/fullnode/service_builder_test.go | 11 ++++++ internal/test/assertions.go | 43 +++++++++++++++++++++ 7 files changed, 99 insertions(+) create mode 100644 internal/test/assertions.go diff --git a/internal/fullnode/configmap_builder_test.go b/internal/fullnode/configmap_builder_test.go index 57bd39eb..1d9f5069 100644 --- a/internal/fullnode/configmap_builder_test.go +++ b/internal/fullnode/configmap_builder_test.go @@ -60,6 +60,7 @@ func TestBuildConfigMaps(t *testing.T) { "app.kubernetes.io/instance": "agoric-0", "app.kubernetes.io/version": "v6.0.0", "cosmos.strange.love/network": "testnet", + "cosmos.strange.love/type": "FullNode", } require.Empty(t, cm.Annotations) @@ -387,4 +388,13 @@ func TestBuildConfigMaps(t *testing.T) { require.Contains(t, err.Error(), "invalid toml in app overrides") }) }) + + test.HasTypeLabel(t, func(crd cosmosv1.CosmosFullNode) []map[string]string { + cms, _ := BuildConfigMaps(&crd, nil) + labels := make([]map[string]string, 0) + for _, cm := range cms { + labels = append(labels, cm.Object().Labels) + } + return labels + }) } diff --git a/internal/fullnode/labels.go b/internal/fullnode/labels.go index e742dc3f..bb63aaea 100644 --- a/internal/fullnode/labels.go +++ b/internal/fullnode/labels.go @@ -10,6 +10,7 @@ import ( const ( networkLabel = "cosmos.strange.love/network" + typeLabel = "cosmos.strange.love/type" ) // kv is a list of extra kv pairs to add to the labels. Must be even. @@ -17,12 +18,17 @@ func defaultLabels(crd *cosmosv1.CosmosFullNode, kvPairs ...string) map[string]s if len(kvPairs)%2 != 0 { panic(errors.New("key/value pairs must be even")) } + nodeType := cosmosv1.FullNode + if crd.Spec.Type != "" { + nodeType = crd.Spec.Type + } labels := map[string]string{ kube.ControllerLabel: "cosmos-operator", kube.ComponentLabel: cosmosv1.CosmosFullNodeController, kube.NameLabel: appName(crd), kube.VersionLabel: kube.ParseImageVersion(crd.Spec.PodTemplate.Image), networkLabel: crd.Spec.ChainSpec.Network, + typeLabel: string(nodeType), } for i := 0; i < len(kvPairs); i += 2 { labels[kvPairs[i]] = kvPairs[i+1] diff --git a/internal/fullnode/node_key_builder_test.go b/internal/fullnode/node_key_builder_test.go index 4530ced6..6aad78f5 100644 --- a/internal/fullnode/node_key_builder_test.go +++ b/internal/fullnode/node_key_builder_test.go @@ -5,6 +5,8 @@ import ( "fmt" "testing" + "github.com/strangelove-ventures/cosmos-operator/internal/test" + cosmosv1 "github.com/strangelove-ventures/cosmos-operator/api/v1" "github.com/stretchr/testify/require" corev1 "k8s.io/api/core/v1" @@ -41,6 +43,7 @@ func TestBuildNodeKeySecrets(t *testing.T) { "app.kubernetes.io/instance": fmt.Sprintf("juno-%d", i), "app.kubernetes.io/version": "v1.2.3", "cosmos.strange.love/network": "mainnet", + "cosmos.strange.love/type": "FullNode", } require.Equal(t, wantLabels, got.Labels) @@ -89,4 +92,13 @@ func TestBuildNodeKeySecrets(t *testing.T) { require.NoError(t, err) require.Empty(t, secrets) }) + + test.HasTypeLabel(t, func(crd cosmosv1.CosmosFullNode) []map[string]string { + secrets, _ := BuildNodeKeySecrets(nil, &crd) + labels := make([]map[string]string, 0) + for _, secret := range secrets { + labels = append(labels, secret.Object().Labels) + } + return labels + }) } diff --git a/internal/fullnode/pod_builder_test.go b/internal/fullnode/pod_builder_test.go index 77f912dd..82c7041e 100644 --- a/internal/fullnode/pod_builder_test.go +++ b/internal/fullnode/pod_builder_test.go @@ -63,6 +63,7 @@ func TestPodBuilder(t *testing.T) { "app.kubernetes.io/name": "osmosis", "app.kubernetes.io/version": "v1.2.3", "cosmos.strange.love/network": "mainnet", + "cosmos.strange.love/type": "FullNode", } require.Equal(t, wantLabels, pod.Labels) require.NotNil(t, pod.Annotations) @@ -553,6 +554,12 @@ gaiad start --home /home/operator/cosmos` require.ElementsMatch(t, []string{"clean-init", "chain-init", "new-init", "genesis-init", "config-merge"}, lo.Keys(initConts)) require.Equal(t, "foo:latest", initConts["chain-init"].Image) }) + + test.HasTypeLabel(t, func(crd cosmosv1.CosmosFullNode) []map[string]string { + builder := NewPodBuilder(&crd) + pod, _ := builder.WithOrdinal(5).Build() + return []map[string]string{pod.Labels} + }) } func TestChainHomeDir(t *testing.T) { diff --git a/internal/fullnode/pvc_builder_test.go b/internal/fullnode/pvc_builder_test.go index 1a1e68f6..8cd9acf6 100644 --- a/internal/fullnode/pvc_builder_test.go +++ b/internal/fullnode/pvc_builder_test.go @@ -58,6 +58,7 @@ func TestBuildPVCs(t *testing.T) { "app.kubernetes.io/instance": fmt.Sprintf("juno-%d", i), "app.kubernetes.io/version": "v1.2.3", "cosmos.strange.love/network": "mainnet", + "cosmos.strange.love/type": "FullNode", } require.Equal(t, wantLabels, got.Labels) @@ -172,4 +173,13 @@ func TestBuildPVCs(t *testing.T) { require.Equal(t, want, pvcs[0].Object().Spec.Resources.Requests, tt) } }) + + test.HasTypeLabel(t, func(crd cosmosv1.CosmosFullNode) []map[string]string { + pvcs := BuildPVCs(&crd) + labels := make([]map[string]string, 0) + for _, pvc := range pvcs { + labels = append(labels, pvc.Object().Labels) + } + return labels + }) } diff --git a/internal/fullnode/service_builder_test.go b/internal/fullnode/service_builder_test.go index 81613979..246af0fc 100644 --- a/internal/fullnode/service_builder_test.go +++ b/internal/fullnode/service_builder_test.go @@ -42,6 +42,7 @@ func TestBuildServices(t *testing.T) { "app.kubernetes.io/version": "v6.0.0", "app.kubernetes.io/instance": fmt.Sprintf("terra-%d", i), "cosmos.strange.love/network": "testnet", + "cosmos.strange.love/type": "FullNode", } require.Equal(t, wantLabels, p2p.Labels) @@ -186,6 +187,7 @@ func TestBuildServices(t *testing.T) { "app.kubernetes.io/component": "rpc", "app.kubernetes.io/version": "v6.0.0", "cosmos.strange.love/network": "testnet", + "cosmos.strange.love/type": "FullNode", } require.Equal(t, wantLabels, rpc.Labels) @@ -265,4 +267,13 @@ func TestBuildServices(t *testing.T) { test.RequireValidMetadata(t, svc.Object()) } }) + + test.HasTypeLabel(t, func(crd cosmosv1.CosmosFullNode) []map[string]string { + svcs := BuildServices(&crd) + labels := make([]map[string]string, 0) + for _, svc := range svcs { + labels = append(labels, svc.Object().Labels) + } + return labels + }) } diff --git a/internal/test/assertions.go b/internal/test/assertions.go new file mode 100644 index 00000000..666a7b70 --- /dev/null +++ b/internal/test/assertions.go @@ -0,0 +1,43 @@ +package test + +import ( + "testing" + + cosmosv1 "github.com/strangelove-ventures/cosmos-operator/api/v1" + "github.com/stretchr/testify/require" +) + +func HasTypeLabel(t *testing.T, builder func(crd cosmosv1.CosmosFullNode) []map[string]string) { + t.Run("sets labels for", func(t *testing.T) { + var crd cosmosv1.CosmosFullNode + crd.Spec.Replicas = 3 + + t.Run("type", func(t *testing.T) { + t.Run("given unspecified type sets type to FullNode", func(t *testing.T) { + resources := builder(crd) + + for _, resource := range resources { + require.Equal(t, "FullNode", resource["cosmos.strange.love/type"]) + } + }) + + t.Run("given Sentry type", func(t *testing.T) { + crd.Spec.Type = "Sentry" + resources := builder(crd) + + for _, resource := range resources { + require.Equal(t, "Sentry", resource["cosmos.strange.love/type"]) + } + }) + + t.Run("given FullNode type", func(t *testing.T) { + crd.Spec.Type = "FullNode" + resources := builder(crd) + + for _, resource := range resources { + require.Equal(t, "FullNode", resource["cosmos.strange.love/type"]) + } + }) + }) + }) +}