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

feat: sets node type label #356

Merged
merged 5 commits into from
Sep 21, 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
10 changes: 10 additions & 0 deletions internal/fullnode/configmap_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
})
}
6 changes: 6 additions & 0 deletions internal/fullnode/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,25 @@ 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.
func defaultLabels(crd *cosmosv1.CosmosFullNode, kvPairs ...string) map[string]string {
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]
Expand Down
12 changes: 12 additions & 0 deletions internal/fullnode/node_key_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
})
}
7 changes: 7 additions & 0 deletions internal/fullnode/pod_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions internal/fullnode/pvc_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
})
}
11 changes: 11 additions & 0 deletions internal/fullnode/service_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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

Expand Down Expand Up @@ -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
})
}
43 changes: 43 additions & 0 deletions internal/test/assertions.go
Original file line number Diff line number Diff line change
@@ -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"])
}
})
})
})
}
Loading