Skip to content

Commit

Permalink
feat: Add staringOrdinal
Browse files Browse the repository at this point in the history
  • Loading branch information
vimystic committed Nov 11, 2024
1 parent 20b4d27 commit 3d50513
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 23 deletions.
4 changes: 4 additions & 0 deletions api/v1/cosmosfullnode_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ type FullNodeSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

// StartingOrdinal specifies the initial ordinal number for pod naming
// +kubebuilder:validation:Minimum:=0
StartingOrdinal *int32 `json:"startingOrdinal,omitempty"`

// Number of replicas to create.
// Individual replicas have a consistent identity.
// +kubebuilder:validation:Minimum:=0
Expand Down
5 changes: 5 additions & 0 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions config/crd/bases/cosmos.strange.love_cosmosfullnodes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5783,6 +5783,12 @@ spec:
type: string
type: object
type: object
startingOrdinal:
description: StartingOrdinal specifies the initial ordinal number
for pod naming
format: int32
minimum: 0
type: integer
strategy:
description: How to scale pods when performing an update.
properties:
Expand Down
7 changes: 6 additions & 1 deletion controllers/cosmosfullnode_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ func (r *CosmosFullNodeReconciler) Reconcile(ctx context.Context, req ctrl.Reque
return stopResult, client.IgnoreNotFound(err)
}

startingOrdinal := int32(0)
if crd.Spec.StartingOrdinal != nil {
startingOrdinal = *crd.Spec.StartingOrdinal
}

reporter := kube.NewEventReporter(logger, r.recorder, crd)

fullnode.ResetStatus(crd)
Expand Down Expand Up @@ -174,7 +179,7 @@ func (r *CosmosFullNodeReconciler) Reconcile(ctx context.Context, req ctrl.Reque
}

// Reconcile pods.
podRequeue, err := r.podControl.Reconcile(ctx, reporter, crd, configCksums, syncInfo)
podRequeue, err := r.podControl.Reconcile(ctx, reporter, crd, configCksums, syncInfo, startingOrdinal)
if err != nil {
errs.Append(err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/fullnode/build_pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ const (
)

// BuildPods creates the final state of pods given the crd.
func BuildPods(crd *cosmosv1.CosmosFullNode, cksums ConfigChecksums) ([]diff.Resource[*corev1.Pod], error) {
func BuildPods(crd *cosmosv1.CosmosFullNode, cksums ConfigChecksums, startingOrdinal int32) ([]diff.Resource[*corev1.Pod], error) {
var (
builder = NewPodBuilder(crd)
pods []diff.Resource[*corev1.Pod]
)
candidates := podCandidates(crd)
for i := int32(0); i < crd.Spec.Replicas; i++ {
for i := int32(startingOrdinal); i < crd.Spec.Replicas; i++ {

Check failure on line 21 in internal/fullnode/build_pods.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary conversion (unconvert)
pod, err := builder.WithOrdinal(i).Build()
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions internal/fullnode/build_pods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestBuildPods(t *testing.T) {
cksums[client.ObjectKey{Namespace: crd.Namespace, Name: fmt.Sprintf("agoric-%d", i)}] = strconv.Itoa(i)
}

pods, err := BuildPods(crd, cksums)
pods, err := BuildPods(crd, cksums, 0)
require.NoError(t, err)
require.Equal(t, 5, len(pods))

Expand Down Expand Up @@ -82,7 +82,7 @@ func TestBuildPods(t *testing.T) {
},
}

pods, err := BuildPods(crd, nil)
pods, err := BuildPods(crd, nil, 0)
require.NoError(t, err)
require.Equal(t, 4, len(pods))

Expand Down Expand Up @@ -118,7 +118,7 @@ func TestBuildPods(t *testing.T) {
},
}

pods, err := BuildPods(crd, nil)
pods, err := BuildPods(crd, nil, 0)
require.NoError(t, err)
require.Equal(t, 4, len(pods))

Expand Down
3 changes: 2 additions & 1 deletion internal/fullnode/pod_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func (pc PodControl) Reconcile(
crd *cosmosv1.CosmosFullNode,
cksums ConfigChecksums,
syncInfo map[string]*cosmosv1.SyncInfoPodStatus,
startingOrdinal int32,
) (bool, kube.ReconcileError) {
var pods corev1.PodList
if err := pc.client.List(ctx, &pods,
Expand All @@ -60,7 +61,7 @@ func (pc PodControl) Reconcile(
return false, kube.TransientError(fmt.Errorf("list existing pods: %w", err))
}

wantPods, err := BuildPods(crd, cksums)
wantPods, err := BuildPods(crd, cksums, startingOrdinal)
if err != nil {
return false, kube.UnrecoverableError(fmt.Errorf("build pods: %w", err))
}
Expand Down
32 changes: 16 additions & 16 deletions internal/fullnode/pod_control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestPodControl_Reconcile(t *testing.T) {
crd.Namespace = namespace
crd.Spec.Replicas = 1

pods, err := BuildPods(&crd, nil)
pods, err := BuildPods(&crd, nil, 0)
require.NoError(t, err)
existing := diff.New(nil, pods).Creates()

Expand All @@ -82,7 +82,7 @@ func TestPodControl_Reconcile(t *testing.T) {
}

control := NewPodControl(mClient, nil)
requeue, err := control.Reconcile(ctx, nopReporter, &crd, nil, syncInfo)
requeue, err := control.Reconcile(ctx, nopReporter, &crd, nil, syncInfo, 0)
require.NoError(t, err)
require.False(t, requeue)

Expand All @@ -108,7 +108,7 @@ func TestPodControl_Reconcile(t *testing.T) {
})

control := NewPodControl(mClient, nil)
requeue, err := control.Reconcile(ctx, nopReporter, &crd, nil, nil)
requeue, err := control.Reconcile(ctx, nopReporter, &crd, nil, nil, 0)
require.NoError(t, err)
require.True(t, requeue)

Expand All @@ -130,7 +130,7 @@ func TestPodControl_Reconcile(t *testing.T) {
MaxUnavailable: ptr(intstr.FromInt(2)),
}

pods, err := BuildPods(&crd, nil)
pods, err := BuildPods(&crd, nil, 0)
require.NoError(t, err)

mClient := newMockPodClient(diff.New(nil, pods).Creates())
Expand All @@ -153,7 +153,7 @@ func TestPodControl_Reconcile(t *testing.T) {

// Trigger updates
crd.Spec.PodTemplate.Image = "new-image"
requeue, err := control.Reconcile(ctx, nopReporter, &crd, nil, syncInfo)
requeue, err := control.Reconcile(ctx, nopReporter, &crd, nil, syncInfo, 0)
require.NoError(t, err)
require.True(t, requeue)

Expand All @@ -167,7 +167,7 @@ func TestPodControl_Reconcile(t *testing.T) {
return kube.ComputeRollout(maxUnavail, desired, ready)
}

requeue, err = control.Reconcile(ctx, nopReporter, &crd, nil, syncInfo)
requeue, err = control.Reconcile(ctx, nopReporter, &crd, nil, syncInfo, 0)
require.NoError(t, err)

require.True(t, requeue)
Expand All @@ -191,7 +191,7 @@ func TestPodControl_Reconcile(t *testing.T) {
return kube.ComputeRollout(maxUnavail, desired, ready)
}

requeue, err = control.Reconcile(ctx, nopReporter, &crd, nil, syncInfo)
requeue, err = control.Reconcile(ctx, nopReporter, &crd, nil, syncInfo, 0)
require.NoError(t, err)
require.True(t, requeue)

Expand Down Expand Up @@ -222,7 +222,7 @@ func TestPodControl_Reconcile(t *testing.T) {
}
crd.Status.Height = make(map[string]uint64)

pods, err := BuildPods(&crd, nil)
pods, err := BuildPods(&crd, nil, 0)
require.NoError(t, err)
existing := diff.New(nil, pods).Creates()

Expand Down Expand Up @@ -267,7 +267,7 @@ func TestPodControl_Reconcile(t *testing.T) {

// Reconcile 1, should update 0 and 1

requeue, err := control.Reconcile(ctx, nopReporter, &crd, nil, syncInfo)
requeue, err := control.Reconcile(ctx, nopReporter, &crd, nil, syncInfo, 0)
require.NoError(t, err)

// only handled 2 updates, so should requeue.
Expand All @@ -284,7 +284,7 @@ func TestPodControl_Reconcile(t *testing.T) {
return kube.ComputeRollout(maxUnavail, desired, ready)
}

requeue, err = control.Reconcile(ctx, nopReporter, &crd, nil, syncInfo)
requeue, err = control.Reconcile(ctx, nopReporter, &crd, nil, syncInfo, 0)
require.NoError(t, err)

require.True(t, requeue)
Expand All @@ -310,7 +310,7 @@ func TestPodControl_Reconcile(t *testing.T) {
return kube.ComputeRollout(maxUnavail, desired, ready)
}

requeue, err = control.Reconcile(ctx, nopReporter, &crd, nil, syncInfo)
requeue, err = control.Reconcile(ctx, nopReporter, &crd, nil, syncInfo, 0)
require.NoError(t, err)

// no further updates yet, should requeue.
Expand All @@ -334,7 +334,7 @@ func TestPodControl_Reconcile(t *testing.T) {
return kube.ComputeRollout(maxUnavail, desired, ready)
}

requeue, err = control.Reconcile(ctx, nopReporter, &crd, nil, syncInfo)
requeue, err = control.Reconcile(ctx, nopReporter, &crd, nil, syncInfo, 0)
require.NoError(t, err)

// only handled 1 updates, so should requeue.
Expand All @@ -353,7 +353,7 @@ func TestPodControl_Reconcile(t *testing.T) {
return kube.ComputeRollout(maxUnavail, desired, ready)
}

requeue, err = control.Reconcile(ctx, nopReporter, &crd, nil, syncInfo)
requeue, err = control.Reconcile(ctx, nopReporter, &crd, nil, syncInfo, 0)
require.NoError(t, err)

require.True(t, requeue)
Expand Down Expand Up @@ -381,7 +381,7 @@ func TestPodControl_Reconcile(t *testing.T) {
return kube.ComputeRollout(maxUnavail, desired, ready)
}

requeue, err = control.Reconcile(ctx, nopReporter, &crd, nil, syncInfo)
requeue, err = control.Reconcile(ctx, nopReporter, &crd, nil, syncInfo, 0)
require.NoError(t, err)

// all updates are now handled, no longer need requeue.
Expand Down Expand Up @@ -415,7 +415,7 @@ func TestPodControl_Reconcile(t *testing.T) {
}
crd.Status.Height = make(map[string]uint64)

pods, err := BuildPods(&crd, nil)
pods, err := BuildPods(&crd, nil, 0)
require.NoError(t, err)
existing := diff.New(nil, pods).Creates()

Expand Down Expand Up @@ -458,7 +458,7 @@ func TestPodControl_Reconcile(t *testing.T) {
crd.Status.Height[pod.Name] = 100
}

requeue, err := control.Reconcile(ctx, nopReporter, &crd, nil, syncInfo)
requeue, err := control.Reconcile(ctx, nopReporter, &crd, nil, syncInfo, 0)
require.NoError(t, err)

// all updates are handled, so should not requeue
Expand Down

0 comments on commit 3d50513

Please sign in to comment.