Skip to content
This repository was archived by the owner on May 3, 2022. It is now read-only.

Commit c11dff7

Browse files
committed
release controller: move cluster choosing to scheduling
This may or may not make conceptual sense (it does to me, but I could not find out why choosing clusters and scheduling a release was two separate steps in the first place, although after #166[1] I'm fairly convinced this was just a technical artifact), but it sure is convenient: we move all the error handling during the scheduling step to a single chunk of code. This fixes an issue where errors in ChooseClusters were not reflected in any condition, making the Release object not change during the sync, and therefore not triggering any events, being essentially invisible to users. As a bonus, I restored the actual testing part of this in the unit tests. We were previously just checking that ChooseClusters didn't trigger any updates, without actually checking if it was doing the right thing (choosing clusters). [1] https://github.com/bookingcom/shipper/pull/166/files#diff-caffe52421149f1f8d77a0e7c749867dR327-R341
1 parent 01d854e commit c11dff7

File tree

3 files changed

+31
-37
lines changed

3 files changed

+31
-37
lines changed

pkg/controller/release/release_controller.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -386,24 +386,6 @@ func (c *Controller) scheduleRelease(rel *shipper.Release) (*shipper.Release, er
386386
)
387387
releaseutil.SetReleaseCondition(&rel.Status, *condition)
388388

389-
if !releaseHasClusters(rel) {
390-
shouldForce := false
391-
rel, err = scheduler.ChooseClusters(rel, shouldForce)
392-
393-
if err != nil {
394-
return initialRel, err
395-
}
396-
397-
c.recorder.Eventf(
398-
rel,
399-
corev1.EventTypeNormal,
400-
"ClustersSelected",
401-
"Set clusters for %q to %v",
402-
controller.MetaKey(rel),
403-
rel.Annotations[shipper.ReleaseClustersAnnotation],
404-
)
405-
}
406-
407389
rel, err = scheduler.ScheduleRelease(rel.DeepCopy())
408390
if err != nil {
409391
reason := reasonForReleaseCondition(err)

pkg/controller/release/scheduler.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ func NewScheduler(
6363
}
6464
}
6565

66-
func (s *Scheduler) ChooseClusters(rel *shipper.Release, force bool) (*shipper.Release, error) {
66+
func (s *Scheduler) ChooseClusters(rel *shipper.Release) (*shipper.Release, error) {
6767
metaKey := controller.MetaKey(rel)
68-
if !force && releaseHasClusters(rel) {
68+
if releaseHasClusters(rel) {
6969
return nil, shippererrors.NewUnrecoverableError(fmt.Errorf("release %q has already been assigned to clusters", metaKey))
7070
}
7171
klog.Infof("Choosing clusters for release %q", metaKey)
@@ -93,7 +93,19 @@ func (s *Scheduler) ScheduleRelease(rel *shipper.Release) (*shipper.Release, err
9393
defer klog.Infof("Finished processing %q", metaKey)
9494

9595
if !releaseHasClusters(rel) {
96-
return nil, shippererrors.NewUnrecoverableError(fmt.Errorf("release %q clusters have not been chosen yet", metaKey))
96+
rel, err := s.ChooseClusters(rel)
97+
if err != nil {
98+
return nil, err
99+
}
100+
101+
s.recorder.Eventf(
102+
rel,
103+
corev1.EventTypeNormal,
104+
"ClustersSelected",
105+
"Set clusters for %q to %v",
106+
controller.MetaKey(rel),
107+
rel.Annotations[shipper.ReleaseClustersAnnotation],
108+
)
97109
}
98110

99111
replicaCount, err := s.fetchChartAndExtractReplicaCount(rel)

pkg/controller/release/scheduler_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -352,21 +352,18 @@ func TestSchedule(t *testing.T) {
352352
expected := release.DeepCopy()
353353
expected.Annotations[shipper.ReleaseClustersAnnotation] = clusterA.GetName() + "," + clusterB.GetName()
354354

355-
relWithConditions := expected.DeepCopy()
356-
357-
// release should be marked as Scheduled
358-
condition := releaseutil.NewReleaseCondition(shipper.ReleaseConditionTypeScheduled, corev1.ConditionTrue, "", "")
359-
releaseutil.SetReleaseCondition(&relWithConditions.Status, *condition)
360-
361-
expectedActions := []kubetesting.Action{}
355+
c, _ := newScheduler(fixtures)
362356

363-
c, clientset := newScheduler(fixtures)
364-
if _, err := c.ChooseClusters(release.DeepCopy(), false); err != nil {
357+
got, err := c.ChooseClusters(release.DeepCopy())
358+
if err != nil {
365359
t.Fatal(err)
366360
}
367361

368-
filteredActions := filterActions(clientset.Actions(), []string{"update"}, []string{"releases"})
369-
shippertesting.CheckActions(expectedActions, filteredActions, t)
362+
if expected.Annotations[shipper.ReleaseClustersAnnotation] != got.Annotations[shipper.ReleaseClustersAnnotation] {
363+
t.Errorf("expected release to have clusters %q, got %q",
364+
expected.Annotations[shipper.ReleaseClustersAnnotation],
365+
got.Annotations[shipper.ReleaseClustersAnnotation])
366+
}
370367
}
371368

372369
// TestScheduleSkipsUnschedulable tests the first part of the cluster
@@ -391,15 +388,18 @@ func TestScheduleSkipsUnschedulable(t *testing.T) {
391388
condition := releaseutil.NewReleaseCondition(shipper.ReleaseConditionTypeScheduled, corev1.ConditionTrue, "", "")
392389
releaseutil.SetReleaseCondition(&relWithConditions.Status, *condition)
393390

394-
expectedActions := []kubetesting.Action{}
391+
c, _ := newScheduler(fixtures)
395392

396-
c, clientset := newScheduler(fixtures)
397-
if _, err := c.ChooseClusters(release.DeepCopy(), false); err != nil {
393+
got, err := c.ChooseClusters(release.DeepCopy())
394+
if err != nil {
398395
t.Fatal(err)
399396
}
400397

401-
filteredActions := filterActions(clientset.Actions(), []string{"update"}, []string{"releases"})
402-
shippertesting.CheckActions(expectedActions, filteredActions, t)
398+
if expected.Annotations[shipper.ReleaseClustersAnnotation] != got.Annotations[shipper.ReleaseClustersAnnotation] {
399+
t.Errorf("expected release to have clusters %q, got %q",
400+
expected.Annotations[shipper.ReleaseClustersAnnotation],
401+
got.Annotations[shipper.ReleaseClustersAnnotation])
402+
}
403403
}
404404

405405
// TestCreateAssociatedObjects checks whether the associated object set is being

0 commit comments

Comments
 (0)