Skip to content

Commit

Permalink
feat(labels): adds node type label to resources
Browse files Browse the repository at this point in the history
* default type is FullNode
* allowed values are FullNode|Sentry
  • Loading branch information
AntiTyping committed Sep 19, 2023
1 parent 18f98d4 commit 1f6d37c
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 0 deletions.
38 changes: 38 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,41 @@ func TestBuildConfigMaps(t *testing.T) {
require.Contains(t, err.Error(), "invalid toml in app overrides")
})
})

t.Run("sets labels for", func(t *testing.T) {
t.Run("type", func(t *testing.T) {
crd := defaultCRD()
crd.Spec.Replicas = 3

t.Run("given unspecified type sets type to FullNode", func(t *testing.T) {
cms, err := BuildConfigMaps(&crd, nil)
require.NoError(t, err)

require.Equal(t, "FullNode", cms[0].Object().Labels["cosmos.strange.love/type"])
require.Equal(t, "FullNode", cms[1].Object().Labels["cosmos.strange.love/type"])
require.Equal(t, "FullNode", cms[2].Object().Labels["cosmos.strange.love/type"])
})

t.Run("given Sentry type", func(t *testing.T) {
crd.Spec.Type = "Sentry"
cms, err := BuildConfigMaps(&crd, nil)
require.NoError(t, err)

require.Equal(t, "Sentry", cms[0].Object().Labels["cosmos.strange.love/type"])
require.Equal(t, "Sentry", cms[1].Object().Labels["cosmos.strange.love/type"])
require.Equal(t, "Sentry", cms[2].Object().Labels["cosmos.strange.love/type"])
})

t.Run("given FullNode type", func(t *testing.T) {
crd.Spec.Type = "FullNode"
cms, err := BuildConfigMaps(&crd, nil)
require.NoError(t, err)

require.Equal(t, "FullNode", cms[0].Object().Labels["cosmos.strange.love/type"])
require.Equal(t, "FullNode", cms[1].Object().Labels["cosmos.strange.love/type"])
require.Equal(t, "FullNode", cms[2].Object().Labels["cosmos.strange.love/type"])
})
})
})

}
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 := "FullNode"
if crd.Spec.Type != "" {
nodeType = string(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: nodeType,
}
for i := 0; i < len(kvPairs); i += 2 {
labels[kvPairs[i]] = kvPairs[i+1]
Expand Down
37 changes: 37 additions & 0 deletions internal/fullnode/node_key_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,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 +90,40 @@ func TestBuildNodeKeySecrets(t *testing.T) {
require.NoError(t, err)
require.Empty(t, secrets)
})

t.Run("sets label 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) {
secrets, err := BuildNodeKeySecrets(nil, &crd)
require.NoError(t, err)

require.Equal(t, "FullNode", secrets[0].Object().Labels["cosmos.strange.love/type"])
require.Equal(t, "FullNode", secrets[1].Object().Labels["cosmos.strange.love/type"])
require.Equal(t, "FullNode", secrets[2].Object().Labels["cosmos.strange.love/type"])
})

t.Run("given Sentry type", func(t *testing.T) {
crd.Spec.Type = "Sentry"
secrets, err := BuildNodeKeySecrets(nil, &crd)
require.NoError(t, err)

require.Equal(t, "Sentry", secrets[0].Object().Labels["cosmos.strange.love/type"])
require.Equal(t, "Sentry", secrets[1].Object().Labels["cosmos.strange.love/type"])
require.Equal(t, "Sentry", secrets[2].Object().Labels["cosmos.strange.love/type"])
})

t.Run("given FullNode type", func(t *testing.T) {
crd.Spec.Type = "FullNode"
secrets, err := BuildNodeKeySecrets(nil, &crd)
require.NoError(t, err)

require.Equal(t, "FullNode", secrets[0].Object().Labels["cosmos.strange.love/type"])
require.Equal(t, "FullNode", secrets[1].Object().Labels["cosmos.strange.love/type"])
require.Equal(t, "FullNode", secrets[2].Object().Labels["cosmos.strange.love/type"])
})
})
})
}
33 changes: 33 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,38 @@ 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)
})

t.Run("sets labels for", func(t *testing.T) {
t.Run("type", func(t *testing.T) {
crd := defaultCRD()

t.Run("given unspecified type sets type to FullNode", func(t *testing.T) {
builder := NewPodBuilder(&crd)
pod, err := builder.WithOrdinal(5).Build()
require.NoError(t, err)

require.Equal(t, "FullNode", pod.Labels["cosmos.strange.love/type"])
})

t.Run("given Sentry type", func(t *testing.T) {
crd.Spec.Type = "Sentry"
builder := NewPodBuilder(&crd)
pod, err := builder.WithOrdinal(5).Build()
require.NoError(t, err)

require.Equal(t, "Sentry", pod.Labels["cosmos.strange.love/type"])
})

t.Run("given FullNode type", func(t *testing.T) {
crd.Spec.Type = "FullNode"
builder := NewPodBuilder(&crd)
pod, err := builder.WithOrdinal(5).Build()
require.NoError(t, err)

require.Equal(t, "FullNode", pod.Labels["cosmos.strange.love/type"])
})
})
})
}

func TestChainHomeDir(t *testing.T) {
Expand Down
37 changes: 37 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,40 @@ func TestBuildPVCs(t *testing.T) {
require.Equal(t, want, pvcs[0].Object().Spec.Resources.Requests, tt)
}
})

t.Run("sets label for", func(t *testing.T) {
crd := defaultCRD()
crd.Spec.Replicas = 3

t.Run("type", func(t *testing.T) {
t.Run("given unspecified type sets type to FullNode", func(t *testing.T) {
pvcs := BuildPVCs(&crd)
require.NotEmpty(t, pvcs)

require.Equal(t, "FullNode", pvcs[0].Object().Labels["cosmos.strange.love/type"])
require.Equal(t, "FullNode", pvcs[1].Object().Labels["cosmos.strange.love/type"])
require.Equal(t, "FullNode", pvcs[2].Object().Labels["cosmos.strange.love/type"])
})

t.Run("given Sentry type", func(t *testing.T) {
crd.Spec.Type = "Sentry"
pvcs := BuildPVCs(&crd)
require.NotEmpty(t, pvcs)

require.Equal(t, "Sentry", pvcs[0].Object().Labels["cosmos.strange.love/type"])
require.Equal(t, "Sentry", pvcs[1].Object().Labels["cosmos.strange.love/type"])
require.Equal(t, "Sentry", pvcs[2].Object().Labels["cosmos.strange.love/type"])
})

t.Run("given FullNode type", func(t *testing.T) {
crd.Spec.Type = "FullNode"
pvcs := BuildPVCs(&crd)
require.NotEmpty(t, pvcs)

require.Equal(t, "FullNode", pvcs[0].Object().Labels["cosmos.strange.love/type"])
require.Equal(t, "FullNode", pvcs[1].Object().Labels["cosmos.strange.love/type"])
require.Equal(t, "FullNode", pvcs[2].Object().Labels["cosmos.strange.love/type"])
})
})
})
}
37 changes: 37 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,39 @@ func TestBuildServices(t *testing.T) {
test.RequireValidMetadata(t, svc.Object())
}
})

t.Run("sets labels for", func(t *testing.T) {
t.Run("type", func(t *testing.T) {
crd := defaultCRD()
crd.Spec.Replicas = 3

t.Run("given unspecified type sets type to FullNode", func(t *testing.T) {
svcs := BuildServices(&crd)

require.Equal(t, "FullNode", svcs[0].Object().Labels["cosmos.strange.love/type"])
require.Equal(t, "FullNode", svcs[1].Object().Labels["cosmos.strange.love/type"])
require.Equal(t, "FullNode", svcs[2].Object().Labels["cosmos.strange.love/type"])
})

t.Run("given Sentry type", func(t *testing.T) {
crd.Spec.Type = "Sentry"

svcs := BuildServices(&crd)

require.Equal(t, "Sentry", svcs[0].Object().Labels["cosmos.strange.love/type"])
require.Equal(t, "Sentry", svcs[1].Object().Labels["cosmos.strange.love/type"])
require.Equal(t, "Sentry", svcs[2].Object().Labels["cosmos.strange.love/type"])
})

t.Run("given FullNode type", func(t *testing.T) {
crd.Spec.Type = "FullNode"

svcs := BuildServices(&crd)

require.Equal(t, "FullNode", svcs[0].Object().Labels["cosmos.strange.love/type"])
require.Equal(t, "FullNode", svcs[1].Object().Labels["cosmos.strange.love/type"])
require.Equal(t, "FullNode", svcs[2].Object().Labels["cosmos.strange.love/type"])
})
})
})
}

0 comments on commit 1f6d37c

Please sign in to comment.