Skip to content

Commit

Permalink
Add stableSvc field (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
dthomson25 committed Jan 22, 2020
1 parent 0c58875 commit c101107
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 8 deletions.
3 changes: 3 additions & 0 deletions pkg/apis/rollouts/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ type CanaryStrategy struct {
// CanaryService holds the name of a service which selects pods with canary version and don't select any pods with stable version.
// +optional
CanaryService string `json:"canaryService,omitempty"`
// StableService holds the name of a service which selects pods with stable version and don't select any pods with canary version.
// +optional
StableService string `json:"stableService,omitempty"`
// Steps define the order of phases to execute the canary deployment
// +optional
Steps []CanaryStep `json:"steps,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion rollout/canary.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (c *RolloutController) rolloutCanary(rollout *v1alpha1.Rollout, rsList []*a
return err
}

if err := c.reconcileCanaryService(roCtx); err != nil {
if err := c.reconcileStableAndCanaryService(roCtx); err != nil {
return err
}

Expand Down
51 changes: 51 additions & 0 deletions rollout/canary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,57 @@ func TestCanaryRolloutWithInvalidCanaryServiceName(t *testing.T) {
assert.Equal(t, condition["reason"], conditions.ServiceNotFoundReason)
}

func TestCanaryRolloutWithStableService(t *testing.T) {
f := newFixture(t)
defer f.Close()

stableSvc := newService("stable", 80, nil)
rollout := newCanaryRollout("foo", 0, nil, nil, nil, intstr.FromInt(1), intstr.FromInt(0))
rs := newReplicaSetWithStatus(rollout, 0, 0)
rollout.Spec.Strategy.Canary.StableService = stableSvc.Name
rollout.Status.Canary.StableRS = rs.Labels[v1alpha1.DefaultRolloutUniqueLabelKey]

f.rolloutLister = append(f.rolloutLister, rollout)
f.objects = append(f.objects, rollout)
f.kubeobjects = append(f.kubeobjects, stableSvc, rs)
f.serviceLister = append(f.serviceLister, stableSvc)

_ = f.expectPatchServiceAction(stableSvc, rollout.Status.CurrentPodHash)
_ = f.expectPatchRolloutAction(rollout)
f.run(getKey(rollout, t))
}

func TestCanaryRolloutWithInvalidStableServiceName(t *testing.T) {
f := newFixture(t)
defer f.Close()

rollout := newCanaryRollout("foo", 0, nil, nil, nil, intstr.FromInt(1), intstr.FromInt(0))
rs := newReplicaSetWithStatus(rollout, 0, 0)
rollout.Spec.Strategy.Canary.StableService = "invalid-stable"
rollout.Status.Canary.StableRS = rs.Labels[v1alpha1.DefaultRolloutUniqueLabelKey]

f.rolloutLister = append(f.rolloutLister, rollout)
f.objects = append(f.objects, rollout)
f.kubeobjects = append(f.kubeobjects, rs)

patchIndex := f.expectPatchRolloutAction(rollout)
f.runExpectError(getKey(rollout, t), true)

patch := make(map[string]interface{})
patchData := f.getPatchedRollout(patchIndex)
err := json.Unmarshal([]byte(patchData), &patch)
assert.NoError(t, err)

c, ok, err := unstructured.NestedSlice(patch, "status", "conditions")
assert.NoError(t, err)
assert.True(t, ok)
assert.Len(t, c, 1)

condition, ok := c[0].(map[string]interface{})
assert.True(t, ok)
assert.Equal(t, condition["reason"], conditions.ServiceNotFoundReason)
}

func TestCanaryRolloutScaleWhilePaused(t *testing.T) {
f := newFixture(t)
defer f.Close()
Expand Down
2 changes: 1 addition & 1 deletion rollout/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,7 @@ func TestComputeHashChangeTolerationCanary(t *testing.T) {
// this should only update observedGeneration and nothing else
// NOTE: This test will fail on every k8s library upgrade.
// To fix it, update expectedPatch to match the new hash.
expectedPatch := `{"status":{"observedGeneration":"866857855d"}}`
expectedPatch := `{"status":{"observedGeneration":"66c6f797f8"}}`
patch := f.getPatchedRollout(patchIndex)
assert.Equal(t, expectedPatch, patch)
}
Expand Down
29 changes: 23 additions & 6 deletions rollout/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,34 @@ func (c *RolloutController) getPreviewAndActiveServices(r *v1alpha1.Rollout) (*c
return previewSvc, activeSvc, nil
}

func (c *RolloutController) reconcileCanaryService(roCtx *canaryContext) error {
func (c *RolloutController) reconcileStableAndCanaryService(roCtx *canaryContext) error {
r := roCtx.Rollout()
newRS := roCtx.NewRS()
if r.Spec.Strategy.Canary == nil || r.Spec.Strategy.Canary.CanaryService == "" {
stableRS := roCtx.StableRS()
if r.Spec.Strategy.Canary == nil {
return nil
}
if r.Spec.Strategy.Canary.StableService != "" && stableRS != nil {
svc, err := c.getReferencedService(r, r.Spec.Strategy.Canary.StableService)
if err != nil {
return err
}

svc, err := c.getReferencedService(r, r.Spec.Strategy.Canary.CanaryService)
if err != nil {
return err
err = c.switchServiceSelector(svc, stableRS.Labels[v1alpha1.DefaultRolloutUniqueLabelKey], r)
if err != nil {
return err
}
}
if r.Spec.Strategy.Canary.CanaryService != "" && newRS != nil {
svc, err := c.getReferencedService(r, r.Spec.Strategy.Canary.CanaryService)
if err != nil {
return err
}

return c.switchServiceSelector(svc, newRS.Labels[v1alpha1.DefaultRolloutUniqueLabelKey], r)
err = c.switchServiceSelector(svc, newRS.Labels[v1alpha1.DefaultRolloutUniqueLabelKey], r)
if err != nil {
return err
}
}
return nil
}

0 comments on commit c101107

Please sign in to comment.