Skip to content
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
5 changes: 5 additions & 0 deletions api/v1alpha/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ const (
// location only, so a restart there needs only permission to update the
// Workload. Deployments are owned by the workload controller, and a direct
// edit to their template is overwritten on its next reconcile.
//
// The annotation is left in place after the restart, since a value that
// does not change does not roll again. Removing it changes the template of
// that location's deployments, which rolls the location once more. An
// annotation naming a location the workload does not place in is ignored.
LocationRestartAnnotationPrefix = "location-restart." + AnnotationNamespace + "/"

// LocationRestartedAtAnnotation is set by the workload controller on the
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/compute/restart/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,13 @@ func restartLocation(ctx context.Context, c client.Client, workload *computev1al
return fmt.Errorf("no deployment found for workload %q in location %q", workload.Name, location)
}

base := workload.DeepCopy()
if workload.Annotations == nil {
workload.Annotations = make(map[string]string)
}
workload.Annotations[annotation] = restartedAt

if err := c.Update(ctx, workload); err != nil {
if err := c.Patch(ctx, workload, client.MergeFrom(base)); err != nil {
return fmt.Errorf("updating workload: %w", err)
}

Expand Down
18 changes: 18 additions & 0 deletions internal/cmd/compute/restart/restart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,21 @@ func TestRestartLocation_UnknownLocation(t *testing.T) {
require.NoError(t, c.Get(ctx, client.ObjectKeyFromObject(workload), &got))
assert.Empty(t, got.Annotations)
}

func TestRestartLocation_SurvivesConcurrentWorkloadEdit(t *testing.T) {
t.Parallel()
ctx := context.Background()
c, workload := newFixture(t)

var concurrent computev1alpha.Workload
require.NoError(t, c.Get(ctx, client.ObjectKeyFromObject(workload), &concurrent))
concurrent.Labels = map[string]string{"edited": "true"}
require.NoError(t, c.Update(ctx, &concurrent))

require.NoError(t, restartLocation(ctx, c, workload, testLocation, testRestartedAt))

var got computev1alpha.Workload
require.NoError(t, c.Get(ctx, client.ObjectKeyFromObject(workload), &got))
assert.Equal(t, testRestartedAt, got.Annotations[computev1alpha.LocationRestartAnnotationPrefix+testLocation])
assert.Equal(t, "true", got.Labels["edited"])
}
44 changes: 44 additions & 0 deletions internal/controller/workload_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,3 +865,47 @@ func TestGetDeploymentsForWorkload_LocationRestartReachesOnlyThatLocation(t *tes
assert.NotContains(t, byLocation[otherLocation].Spec.Template.Annotations, computev1alpha.LocationRestartedAtAnnotation)
assert.Empty(t, workload.Spec.Template.Annotations, "the workload's own template must not change")
}

func TestGetDeploymentsForWorkload_LocationRestartForUnplacedLocationIgnored(t *testing.T) {
t.Parallel()

workload := &computev1alpha.Workload{
ObjectMeta: metav1.ObjectMeta{
Name: rdTestWorkloadName,
Namespace: testDefaultNamespace,
UID: types.UID("workload-uid"),
Annotations: map[string]string{
computev1alpha.LocationRestartAnnotationPrefix + "ord": "2026-01-02T15:04:05Z",
},
},
Spec: computev1alpha.WorkloadSpec{
Placements: []computev1alpha.WorkloadPlacement{
{
Name: testDefaultPlacement,
Locations: []locationsv1alpha1.LocationReference{{Name: testLocationName}},
ScaleSettings: computev1alpha.HorizontalScaleSettings{MinReplicas: 1},
},
},
},
}

s := newNetworkingScheme()
require.NoError(t, locationsv1alpha1.AddToScheme(s))
cl := fake.NewClientBuilder().
WithScheme(s).
WithObjects(
newLocationsServiceLocation(testLocationName, "DFW"),
newLocationsServiceLocation("ord", "ORD"),
newTestComputeAvailability(testLocationName),
newTestComputeAvailability("ord"),
).
WithIndex(&computev1alpha.WorkloadDeployment{}, deploymentWorkloadUIDIndex, deploymentWorkloadUIDIndexFunc).
Build()
r := &WorkloadReconciler{}

desired, _, err := r.getDeploymentsForWorkload(context.Background(), cl, workload)
require.NoError(t, err)
require.Len(t, desired, 1)
assert.Equal(t, testLocationName, desired[0].Spec.LocationRef.Name)
assert.NotContains(t, desired[0].Spec.Template.Annotations, computev1alpha.LocationRestartedAtAnnotation)
}
Loading