Skip to content

Commit 8759669

Browse files
committed
unit test and fix
1 parent a428e54 commit 8759669

File tree

2 files changed

+54
-13
lines changed

2 files changed

+54
-13
lines changed

internal/planner/planner.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,13 @@ func GeneratePlan(
7575
ScaleDeployments: make(map[*corev1.ObjectReference]uint32),
7676
}
7777

78+
// If Deployment was not found in temporal, which always happens on the first worker deployment version
79+
// and sometimes happens transiently thereafter, the versions list will be empty. If the deployment
80+
// exists and was found, there will always be at least one version in the list.
81+
foundDeploymentInTemporal := temporalState != nil && len(temporalState.Versions) > 0
82+
7883
// Add delete/scale operations based on version status
79-
plan.DeleteDeployments = getDeleteDeployments(k8sState, status, spec, temporalState)
84+
plan.DeleteDeployments = getDeleteDeployments(k8sState, status, spec, foundDeploymentInTemporal)
8085
plan.ScaleDeployments = getScaleDeployments(k8sState, status, spec)
8186
plan.ShouldCreateDeployment = shouldCreateDeployment(status, maxVersionsIneligibleForDeletion)
8287
plan.UpdateDeployments = getUpdateDeployments(k8sState, status, connection)
@@ -191,7 +196,7 @@ func getDeleteDeployments(
191196
k8sState *k8s.DeploymentState,
192197
status *temporaliov1alpha1.TemporalWorkerDeploymentStatus,
193198
spec *temporaliov1alpha1.TemporalWorkerDeploymentSpec,
194-
temporalState *temporal.TemporalWorkerState,
199+
foundDeploymentInTemporal bool,
195200
) []*appsv1.Deployment {
196201
var deleteDeployments []*appsv1.Deployment
197202

@@ -217,7 +222,7 @@ func getDeleteDeployments(
217222
}
218223
case temporaliov1alpha1.VersionStatusNotRegistered:
219224
// Only delete Deployments of NotRegistered versions if temporalState was not empty
220-
if len(temporalState.Versions) > 0 &&
225+
if foundDeploymentInTemporal &&
221226
// NotRegistered versions are versions that the server doesn't know about.
222227
// Only delete if it's not the target version.
223228
status.TargetVersion.BuildID != version.BuildID {

internal/planner/planner_test.go

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -426,12 +426,13 @@ func TestGeneratePlan(t *testing.T) {
426426

427427
func TestGetDeleteDeployments(t *testing.T) {
428428
testCases := []struct {
429-
name string
430-
k8sState *k8s.DeploymentState
431-
status *temporaliov1alpha1.TemporalWorkerDeploymentStatus
432-
spec *temporaliov1alpha1.TemporalWorkerDeploymentSpec
433-
config *Config
434-
expectDeletes int
429+
name string
430+
k8sState *k8s.DeploymentState
431+
status *temporaliov1alpha1.TemporalWorkerDeploymentStatus
432+
spec *temporaliov1alpha1.TemporalWorkerDeploymentSpec
433+
config *Config
434+
expectDeletes int
435+
foundDeploymentInTemporal bool
435436
}{
436437
{
437438
name: "drained version should be deleted",
@@ -463,7 +464,8 @@ func TestGetDeleteDeployments(t *testing.T) {
463464
config: &Config{
464465
RolloutStrategy: temporaliov1alpha1.RolloutStrategy{},
465466
},
466-
expectDeletes: 1,
467+
expectDeletes: 1,
468+
foundDeploymentInTemporal: true,
467469
},
468470
{
469471
name: "not yet drained long enough",
@@ -492,7 +494,8 @@ func TestGetDeleteDeployments(t *testing.T) {
492494
},
493495
Replicas: func() *int32 { r := int32(1); return &r }(),
494496
},
495-
expectDeletes: 0,
497+
expectDeletes: 0,
498+
foundDeploymentInTemporal: true,
496499
},
497500
{
498501
name: "not registered version should be deleted",
@@ -523,15 +526,48 @@ func TestGetDeleteDeployments(t *testing.T) {
523526
spec: &temporaliov1alpha1.TemporalWorkerDeploymentSpec{
524527
Replicas: func() *int32 { r := int32(1); return &r }(),
525528
},
526-
expectDeletes: 1,
529+
expectDeletes: 1,
530+
foundDeploymentInTemporal: true,
531+
},
532+
{
533+
name: "not registered version should NOT be deleted, deployment not found in temporal",
534+
k8sState: &k8s.DeploymentState{
535+
Deployments: map[string]*appsv1.Deployment{
536+
"123": createDeploymentWithDefaultConnectionSpecHash(1),
537+
"456": createDeploymentWithDefaultConnectionSpecHash(1),
538+
},
539+
},
540+
status: &temporaliov1alpha1.TemporalWorkerDeploymentStatus{
541+
TargetVersion: temporaliov1alpha1.TargetWorkerDeploymentVersion{
542+
BaseWorkerDeploymentVersion: temporaliov1alpha1.BaseWorkerDeploymentVersion{
543+
BuildID: "123",
544+
Status: temporaliov1alpha1.VersionStatusCurrent,
545+
Deployment: &corev1.ObjectReference{Name: "test-123"},
546+
},
547+
},
548+
DeprecatedVersions: []*temporaliov1alpha1.DeprecatedWorkerDeploymentVersion{
549+
{
550+
BaseWorkerDeploymentVersion: temporaliov1alpha1.BaseWorkerDeploymentVersion{
551+
BuildID: "456",
552+
Status: temporaliov1alpha1.VersionStatusNotRegistered,
553+
Deployment: &corev1.ObjectReference{Name: "test-456"},
554+
},
555+
},
556+
},
557+
},
558+
spec: &temporaliov1alpha1.TemporalWorkerDeploymentSpec{
559+
Replicas: func() *int32 { r := int32(1); return &r }(),
560+
},
561+
expectDeletes: 0,
562+
foundDeploymentInTemporal: false,
527563
},
528564
}
529565

530566
for _, tc := range testCases {
531567
t.Run(tc.name, func(t *testing.T) {
532568
err := tc.spec.Default(context.Background())
533569
require.NoError(t, err)
534-
deletes := getDeleteDeployments(tc.k8sState, tc.status, tc.spec)
570+
deletes := getDeleteDeployments(tc.k8sState, tc.status, tc.spec, tc.foundDeploymentInTemporal)
535571
assert.Equal(t, tc.expectDeletes, len(deletes), "unexpected number of deletes")
536572
})
537573
}

0 commit comments

Comments
 (0)