Repository navigation
feat(planstep): resolve bound groups and re-check arming when they widen #1834
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1acdd72
feat(planstep): resolve a bound group from its selected recommendations
cristim 737c9b7
feat(api): re-check the arming grant when a bound plan's groups widen
cristim fea2b01
fix(api): avoid copying service configs when comparing bound groups
cristim 56c54f8
fix(api): ignore disabled bound groups when checking arming escalation
cristim 2134f22
Merge remote-tracking branch 'origin/main' into feat/1746-bound-groups-a
cristim aaaff73
fix(config): require a bound key to name its own provider and service
cristim 0042441
Merge remote-tracking branch 'origin/main' into feat/1746-bound-groups-a
cristim de3ea4b
fix(config): derive provider and service scopes from group-free keys …
cristim d39d9e8
Merge remote-tracking branch 'origin/main' into feat/1746-bound-groups-a
cristim f5cefdd
fix(api): refuse a stored bound plan whose key names another entry be…
cristim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "slices" | ||
|
|
||
| "github.com/LeanerCloud/cloud-commitments-platform/internal/config" | ||
| ) | ||
|
|
||
| // boundGroupsEscalate reports whether a write widens what an armed bound plan | ||
| // (issue #1746) buys in a way the provider and service sets cannot show, so | ||
| // the arming grant's constraints must be checked again. It applies when the | ||
| // stored or the new plan carries the bound_groups marker; unmarked plans keep | ||
| // today's rules. | ||
| func boundGroupsEscalate(before, after *config.PurchasePlan) bool { | ||
| beforeBound := isBoundPlan(before) | ||
| afterBound := isBoundPlan(after) | ||
| if !beforeBound && !afterBound { | ||
| return false | ||
| } | ||
| if beforeBound && !afterBound { | ||
| return true // the marker was removed | ||
| } | ||
| old := make(map[string]config.ServiceConfig, len(before.Services)) | ||
| for k := range before.Services { | ||
| svc := before.Services[k] | ||
| old[svc.EffectiveGroupID(k)] = svc | ||
| } | ||
| for k := range after.Services { | ||
| svc := after.Services[k] | ||
| // A disabled group buys nothing; enabling it later is checked then. | ||
| if !svc.Enabled { | ||
| continue | ||
| } | ||
| prev, ok := old[svc.EffectiveGroupID(k)] | ||
| if !ok || groupWidens(&prev, &svc) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func isBoundPlan(p *config.PurchasePlan) bool { | ||
| return p != nil && p.RampSchedule.AllocationMode == config.RampAllocationBoundGroups | ||
| } | ||
|
|
||
| // groupWidens compares one group before and after a write. | ||
| func groupWidens(prev, next *config.ServiceConfig) bool { | ||
| if next.Enabled && !prev.Enabled { | ||
| return true | ||
| } | ||
| // An empty set means filter re-match, the widest binding there is. | ||
| if len(prev.BoundIdentities) > 0 && len(next.BoundIdentities) == 0 { | ||
| return true | ||
| } | ||
| for _, id := range next.BoundIdentities { | ||
| if !slices.Contains(prev.BoundIdentities, id) { | ||
| return true | ||
| } | ||
| } | ||
| if groupCapRaised(prev.MaxStepCommitment, next.MaxStepCommitment) { | ||
| return true | ||
| } | ||
| return next.Term != prev.Term || canonicalPayment(next) != canonicalPayment(prev) | ||
| } | ||
|
|
||
| // groupCapRaised treats a nil group cap as unbounded, unlike capCents(nil). | ||
| func groupCapRaised(prev, next *float64) bool { | ||
| if next == nil { | ||
| return prev != nil | ||
| } | ||
| if prev == nil { | ||
| return false | ||
| } | ||
| return capCents(next) > capCents(prev) | ||
| } | ||
|
|
||
| func canonicalPayment(svc *config.ServiceConfig) string { | ||
| if canon, ok := config.NormalizePaymentOption(svc.Provider, svc.Payment); ok { | ||
| return canon | ||
| } | ||
| return svc.Payment | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/LeanerCloud/cloud-commitments-platform/internal/config" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| // Fixtures are hand-built plans, not real configuration (issue #1746). | ||
|
|
||
| // armedBoundPlan is an armed bound plan with one group g1. | ||
| func armedBoundPlan() *config.PurchasePlan { | ||
| p := apgStoredPlan(true, apgCap(1000)) | ||
| p.RampSchedule.AllocationMode = config.RampAllocationBoundGroups | ||
| p.Services = map[string]config.ServiceConfig{"aws/ec2#g1": { | ||
| Provider: "aws", Service: "ec2", Enabled: true, Term: 1, Payment: "no-upfront", Coverage: 80, | ||
| MinCount: 1, GroupID: "g1", BoundIdentities: []string{"k1", "k2"}, MaxStepCommitment: apgCap(100), | ||
| }} | ||
| return p | ||
| } | ||
|
|
||
| // edited returns a copy of the armed bound plan with f applied to group g1. | ||
| func edited(f func(p *config.PurchasePlan, g *config.ServiceConfig)) *config.PurchasePlan { | ||
| p := armedBoundPlan() | ||
| g := p.Services["aws/ec2#g1"] | ||
| f(p, &g) | ||
| p.Services["aws/ec2#g1"] = g | ||
| return p | ||
| } | ||
|
|
||
| func TestPlanArmEscalates_BoundGroups(t *testing.T) { | ||
| for _, tc := range []struct { | ||
| name string | ||
| after *config.PurchasePlan | ||
| want bool | ||
| }{ | ||
| {"unchanged", armedBoundPlan(), false}, | ||
| {"narrowed bound set", edited(func(_ *config.PurchasePlan, g *config.ServiceConfig) { g.BoundIdentities = []string{"k1"} }), false}, | ||
| {"lowered group cap", edited(func(_ *config.PurchasePlan, g *config.ServiceConfig) { g.MaxStepCommitment = apgCap(50) }), false}, | ||
| {"coverage edit", edited(func(_ *config.PurchasePlan, g *config.ServiceConfig) { g.Coverage = 60 }), false}, | ||
| {"new group", edited(func(p *config.PurchasePlan, _ *config.ServiceConfig) { | ||
| p.Services["aws/ec2#g2"] = config.ServiceConfig{Provider: "aws", Service: "ec2", Enabled: true, Term: 3, Payment: "all-upfront", GroupID: "g2", BoundIdentities: []string{"k9"}} | ||
| }), true}, | ||
| {"added bound identity", edited(func(_ *config.PurchasePlan, g *config.ServiceConfig) { g.BoundIdentities = []string{"k1", "k2", "k3"} }), true}, | ||
| {"bound to unbound", edited(func(_ *config.PurchasePlan, g *config.ServiceConfig) { g.BoundIdentities = nil }), true}, | ||
| {"raised group cap", edited(func(_ *config.PurchasePlan, g *config.ServiceConfig) { g.MaxStepCommitment = apgCap(200) }), true}, | ||
| {"removed group cap (nil is unbounded)", edited(func(_ *config.PurchasePlan, g *config.ServiceConfig) { g.MaxStepCommitment = nil }), true}, | ||
| {"term changed", edited(func(_ *config.PurchasePlan, g *config.ServiceConfig) { g.Term = 3 }), true}, | ||
| {"payment changed", edited(func(_ *config.PurchasePlan, g *config.ServiceConfig) { g.Payment = "all-upfront" }), true}, | ||
| {"marker removed", edited(func(p *config.PurchasePlan, _ *config.ServiceConfig) { p.RampSchedule.AllocationMode = "" }), true}, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| assert.Equal(t, tc.want, planArmEscalates(armedBoundPlan(), tc.after)) | ||
| }) | ||
| } | ||
| t.Run("re-enabling a disabled group", func(t *testing.T) { | ||
| before := edited(func(_ *config.PurchasePlan, g *config.ServiceConfig) { g.Enabled = false }) | ||
| assert.True(t, planArmEscalates(before, armedBoundPlan())) | ||
| assert.False(t, planArmEscalates(armedBoundPlan(), before), "disabling a group narrows") | ||
| }) | ||
| t.Run("arming a disarmed bound plan escalates even when it narrows", func(t *testing.T) { | ||
| disarmed := armedBoundPlan() | ||
| disarmed.AutoPurchase = false | ||
| assert.True(t, planArmEscalates(disarmed, armedBoundPlan()), "unchanged") | ||
| narrowed := edited(func(_ *config.PurchasePlan, g *config.ServiceConfig) { g.BoundIdentities = []string{"k1"} }) | ||
| assert.True(t, planArmEscalates(disarmed, narrowed), "narrowed") | ||
| }) | ||
| t.Run("a disabled group buys nothing, so its edits wait for re-enabling", func(t *testing.T) { | ||
| withG2 := func(enabled bool, ids ...string) *config.PurchasePlan { | ||
| return edited(func(p *config.PurchasePlan, _ *config.ServiceConfig) { | ||
| p.Services["aws/ec2#g2"] = config.ServiceConfig{Provider: "aws", Service: "ec2", Enabled: enabled, Term: 1, Payment: "no-upfront", GroupID: "g2", BoundIdentities: ids} | ||
| }) | ||
| } | ||
| assert.False(t, planArmEscalates(armedBoundPlan(), withG2(false, "k9")), "new disabled group") | ||
| assert.False(t, planArmEscalates(withG2(false, "k9"), withG2(false, "k9", "k8")), "disabled group widened") | ||
| assert.True(t, planArmEscalates(withG2(false, "k9"), withG2(true, "k9")), "enabling it") | ||
| assert.True(t, planArmEscalates(armedBoundPlan(), withG2(true, "k9")), "new enabled group") | ||
| }) | ||
| t.Run("a group cap added where none was set narrows", func(t *testing.T) { | ||
| before := edited(func(_ *config.PurchasePlan, g *config.ServiceConfig) { g.MaxStepCommitment = nil }) | ||
| assert.False(t, planArmEscalates(before, armedBoundPlan())) | ||
| }) | ||
| } | ||
|
|
||
| // (guard) Unmarked armed plans keep today's rules: a term- or payment-only | ||
| // edit is not an escalation, so admin-key callers are unaffected. | ||
| func TestPlanArmEscalates_UnmarkedTermAndPaymentEditsDoNotEscalate(t *testing.T) { | ||
| plan := func(term int, payment string) *config.PurchasePlan { | ||
| p := apgStoredPlan(true, apgCap(1000)) | ||
| p.Services = map[string]config.ServiceConfig{"aws/rds": {Provider: "aws", Service: "rds", Enabled: true, Term: term, Payment: payment}} | ||
| return p | ||
| } | ||
| assert.False(t, planArmEscalates(plan(1, "no-upfront"), plan(3, "no-upfront"))) | ||
| assert.False(t, planArmEscalates(plan(1, "no-upfront"), plan(1, "all-upfront"))) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| // Fixtures are hand-built plans, not real configuration (issue #1746). | ||
|
|
||
| func TestDerivePlanServices_CutsBoundGroupSuffix(t *testing.T) { | ||
| p := &PurchasePlan{Services: map[string]ServiceConfig{ | ||
| "aws/ec2#g1": {}, "aws/ec2#g2": {}, "aws/rds#g3": {}, | ||
| }} | ||
| p.RampSchedule.AllocationMode = RampAllocationBoundGroups | ||
| assert.Equal(t, []string{"ec2", "rds"}, DerivePlanServices(p)) | ||
| providers, err := DerivePlanProviders(p) | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, []string{"aws"}, providers) | ||
| // (guard) legacy keys are unchanged. | ||
| legacy := &PurchasePlan{Services: map[string]ServiceConfig{"aws/ec2": {}, "aws:savings-plans-compute": {}}} | ||
| assert.Equal(t, []string{"ec2", "savings-plans-compute"}, DerivePlanServices(legacy)) | ||
| } | ||
|
|
||
| // An unmarked plan keeps today's service parsing: a "#" in its key is not a | ||
| // group suffix, so the entry still names its service (#1746 changes bound plans only). | ||
| func TestDerivePlanServices_UnmarkedKeyWithHashKeepsItsService(t *testing.T) { | ||
| p := &PurchasePlan{Services: map[string]ServiceConfig{"aws/rds": {}, "team#west/ec2": {}}} | ||
| assert.Equal(t, []string{"ec2", "rds"}, DerivePlanServices(p)) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.