Skip to content

Commit

Permalink
Add handling for non latest revisions with mutation bool
Browse files Browse the repository at this point in the history
  • Loading branch information
vyasgun committed Nov 29, 2021
1 parent 83b6290 commit da88cf5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
29 changes: 16 additions & 13 deletions pkg/kn/traffic/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,24 +285,27 @@ func verifyInput(trafficFlags *flags.Traffic, svc *servingv1.Service, revisions
return errorTrafficDistribution(sum, errorDistributionRevisionCount)
}

if specRevPercentCount == existingRevisionCount-1 {
if latestRefTraffic {
// if mutation is set, @latest tag is referring to the revision which
// will be created after service update. specRevPercentCount should have been
// equal to existingRevisionCount
if mutation {
return errorTrafficDistribution(sum, errorDistributionRevisionCount)
}
revisionRefMap[svc.Status.LatestReadyRevisionName] = 0
// if mutation is set, a new revision will be created after service update.
// specRevPercentCount should be equal to existingRevisionCount
if mutation {
if specRevPercentCount != existingRevisionCount {
return errorTrafficDistribution(sum, errorDistributionRevisionCount)
}
if _, ok := revisionRefMap[latestRevisionRef]; !ok {
// remaining % can only go to the @latest tag
trafficFlags.RevisionsPercentages = append(trafficFlags.RevisionsPercentages, fmt.Sprintf("%s=%d", latestRevisionRef, 100-sum))
return nil
}
}

if specRevPercentCount == existingRevisionCount && latestRefTraffic {
// if mutation is not set, @latest tag is referring to the revision which
// already exists. specRevPercentCount should have been equal to existingRevisionCount
if !mutation {
// if mutation is not set, specRevPercentCount should be equal to existingRevisionCount
if !mutation {
if specRevPercentCount != existingRevisionCount-1 {
return errorTrafficDistribution(sum, errorDistributionRevisionCount)
}
if latestRefTraffic {
revisionRefMap[svc.Status.LatestReadyRevisionName] = 0
}
}

// remaining % to 100
Expand Down
17 changes: 17 additions & 0 deletions test/e2e/traffic_split_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,23 @@ func TestTrafficSplit(t *testing.T) {
verifyTargets(r, serviceName, expectedTargets, false)
test.ServiceDelete(r, serviceName)
})
t.Run("45:55 automatic traffic split to @latest after mutation", func(t *testing.T) {
t.Log("direct 45% traffic explicitly to previous revision and remaining 55 will automatically be directed to @latest")
r := test.NewKnRunResultCollector(t, it)
defer r.DumpIfFailed()

serviceName := test.GetNextServiceName(serviceBase)

test.ServiceCreate(r, serviceName)

rev1 := fmt.Sprintf("%s-00001", serviceName)
rev2 := fmt.Sprintf("%s-00002", serviceName)
test.ServiceUpdate(r, serviceName, "--env", "TARGET=v1", "--traffic", fmt.Sprintf("%s=%d", rev1, 45))

expectedTargets := []TargetFields{newTargetFields("", rev2, 55, true), newTargetFields("", rev1, 45, false)}
verifyTargets(r, serviceName, expectedTargets, false)
test.ServiceDelete(r, serviceName)
})
t.Run("automatic traffic split failure", func(t *testing.T) {
t.Log("direct 50% traffic to one of the three revisions. Remaining will not be automatically redirected as only one revision should be missing from spec")
r := test.NewKnRunResultCollector(t, it)
Expand Down

0 comments on commit da88cf5

Please sign in to comment.