Skip to content

Commit

Permalink
feat(CosmosFullNode): Add image override (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
agouin authored Sep 20, 2023
1 parent fae58d6 commit 48f30b6
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 7 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/manifests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Kube Manifests

on:
push:
branches:
- main
pull_request:

jobs:
verify:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '>=1.20.2'
- run: make generate manifests

- uses: CatChen/check-git-status-action@v1
with:
fail-if-not-clean: true
4 changes: 4 additions & 0 deletions api/v1/cosmosfullnode_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,10 @@ type InstanceOverridesSpec struct {
// Overrides an individual instance's PVC.
// +optional
VolumeClaimTemplate *PersistentVolumeClaimSpec `json:"volumeClaimTemplate"`

// Overrides an individual instance's Image.
// +optional
Image string `json:"image"`
}

type DisableStrategy string
Expand Down
3 changes: 3 additions & 0 deletions config/crd/bases/cosmos.strange.love_cosmosfullnodes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ spec:
- Pod
- All
type: string
image:
description: Overrides an individual instance's Image.
type: string
volumeClaimTemplate:
description: Overrides an individual instance's PVC.
properties:
Expand Down
20 changes: 17 additions & 3 deletions internal/fullnode/build_pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,32 @@ func BuildPods(crd *cosmosv1.CosmosFullNode, cksums ConfigChecksums) ([]diff.Res
if err != nil {
return nil, err
}
if disable := overrides[pod.Name].DisableStrategy; disable != nil {
continue
}
if _, shouldSnapshot := candidates[pod.Name]; shouldSnapshot {
continue
}
if o, ok := overrides[pod.Name]; ok {
if o.DisableStrategy != nil {
continue
}
if o.Image != "" {
setMainContainerImage(pod, o.Image)
}
}
pod.Annotations[configChecksumAnnotation] = cksums[client.ObjectKeyFromObject(pod)]
pods = append(pods, diff.Adapt(pod, i))
}
return pods, nil
}

func setMainContainerImage(pod *corev1.Pod, image string) {
for i := range pod.Spec.Containers {
if pod.Spec.Containers[i].Name == mainContainer {
pod.Spec.Containers[i].Image = image
return
}
}
}

func podCandidates(crd *cosmosv1.CosmosFullNode) map[string]struct{} {
candidates := make(map[string]struct{})
for _, v := range crd.Status.ScheduledSnapshotStatus {
Expand Down
21 changes: 19 additions & 2 deletions internal/fullnode/build_pods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,24 @@ func TestBuildPods(t *testing.T) {
})

t.Run("instance overrides", func(t *testing.T) {
const (
image = "agoric:latest"
overrideImage = "some_image:custom"
overridePod = "agoric-5"
)
crd := &cosmosv1.CosmosFullNode{
ObjectMeta: metav1.ObjectMeta{
Name: "agoric",
},
Spec: cosmosv1.FullNodeSpec{
Replicas: 6,
PodTemplate: cosmosv1.PodSpec{
Image: image,
},
InstanceOverrides: map[string]cosmosv1.InstanceOverridesSpec{
"agoric-2": {DisableStrategy: ptr(cosmosv1.DisablePod)},
"agoric-4": {DisableStrategy: ptr(cosmosv1.DisableAll)},
"agoric-2": {DisableStrategy: ptr(cosmosv1.DisablePod)},
"agoric-4": {DisableStrategy: ptr(cosmosv1.DisableAll)},
overridePod: {Image: overrideImage},
},
},
}
Expand All @@ -82,6 +91,14 @@ func TestBuildPods(t *testing.T) {
})
got := lo.Map(pods, func(pod diff.Resource[*corev1.Pod], _ int) string { return pod.Object().Name })
require.Equal(t, want, got)
for _, pod := range pods {
image := pod.Object().Spec.Containers[0].Image
if pod.Object().Name == overridePod {
require.Equal(t, overrideImage, image)
} else {
require.Equal(t, image, image)
}
}
})

t.Run("scheduled volume snapshot pod candidate", func(t *testing.T) {
Expand Down
7 changes: 5 additions & 2 deletions internal/fullnode/pod_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import (

var bufPool = sync.Pool{New: func() any { return new(bytes.Buffer) }}

const healthCheckPort = healthcheck.Port
const (
healthCheckPort = healthcheck.Port
mainContainer = "node"
)

// PodBuilder builds corev1.Pods
type PodBuilder struct {
Expand Down Expand Up @@ -65,7 +68,7 @@ func NewPodBuilder(crd *cosmosv1.CosmosFullNode) PodBuilder {
Containers: []corev1.Container{
// Main start container.
{
Name: "node",
Name: mainContainer,
Image: tpl.Image,
// The following is a useful hack if you need to inspect the PV.
//Command: []string{"/bin/sh"},
Expand Down

0 comments on commit 48f30b6

Please sign in to comment.