-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(kuma-cp) zone insights manager and limits (#976)
* fix(kuma-cp) zone insights manager and limits Add a ZoneInsight manager, so that we can enforce limits of the number of the subscriptions. Signed-off-by: Nikolay Nikolaev <[email protected]>
- Loading branch information
Nikolay Nikolaev
committed
Oct 1, 2020
1 parent
92f2e72
commit f8bd700
Showing
9 changed files
with
200 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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
64 changes: 64 additions & 0 deletions
64
pkg/core/managers/apis/zoneinsight/zone_insight_manager.go
This file contains 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,64 @@ | ||
package zoneinsight | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/kumahq/kuma/pkg/core/resources/apis/system" | ||
|
||
kuma_cp "github.com/kumahq/kuma/pkg/config/app/kuma-cp" | ||
|
||
"github.com/kumahq/kuma/pkg/core" | ||
|
||
core_manager "github.com/kumahq/kuma/pkg/core/resources/manager" | ||
core_model "github.com/kumahq/kuma/pkg/core/resources/model" | ||
core_store "github.com/kumahq/kuma/pkg/core/resources/store" | ||
) | ||
|
||
func NewZoneInsightManager(store core_store.ResourceStore, config *kuma_cp.ZoneMetrics) core_manager.ResourceManager { | ||
return &zoneInsightManager{ | ||
ResourceManager: core_manager.NewResourceManager(store), | ||
store: store, | ||
config: config, | ||
} | ||
} | ||
|
||
type zoneInsightManager struct { | ||
core_manager.ResourceManager | ||
store core_store.ResourceStore | ||
config *kuma_cp.ZoneMetrics | ||
} | ||
|
||
func (m *zoneInsightManager) Create(ctx context.Context, resource core_model.Resource, fs ...core_store.CreateOptionsFunc) error { | ||
if err := resource.Validate(); err != nil { | ||
return err | ||
} | ||
opts := core_store.NewCreateOptions(fs...) | ||
|
||
m.limitSubscription(resource.(*system.ZoneInsightResource)) | ||
|
||
zone := system.ZoneResource{} | ||
if err := m.store.Get(ctx, &zone, core_store.GetByKey(opts.Name, opts.Mesh)); err != nil { | ||
return err | ||
} | ||
return m.store.Create(ctx, resource, append(fs, core_store.CreatedAt(core.Now()), core_store.CreateWithOwner(&zone))...) | ||
} | ||
|
||
func (m *zoneInsightManager) Update(ctx context.Context, resource core_model.Resource, fs ...core_store.UpdateOptionsFunc) error { | ||
m.limitSubscription(resource.(*system.ZoneInsightResource)) | ||
return m.ResourceManager.Update(ctx, resource, fs...) | ||
} | ||
|
||
func (m *zoneInsightManager) limitSubscription(zoneInsight *system.ZoneInsightResource) { | ||
if !m.config.Enabled { | ||
zoneInsight.Spec.Subscriptions = nil | ||
return | ||
} | ||
if m.config.SubscriptionLimit == 0 { | ||
return | ||
} | ||
if len(zoneInsight.Spec.Subscriptions) <= m.config.SubscriptionLimit { | ||
return | ||
} | ||
s := zoneInsight.Spec.Subscriptions | ||
zoneInsight.Spec.Subscriptions = s[len(s)-m.config.SubscriptionLimit:] | ||
} |
13 changes: 13 additions & 0 deletions
13
pkg/core/managers/apis/zoneinsight/zone_insight_manager_suit_test.go
This file contains 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,13 @@ | ||
package zoneinsight_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestZoneInsightManager(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Zone Insights Manager Suite") | ||
} |
85 changes: 85 additions & 0 deletions
85
pkg/core/managers/apis/zoneinsight/zone_insight_manager_test.go
This file contains 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,85 @@ | ||
package zoneinsight_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/kumahq/kuma/api/system/v1alpha1" | ||
"github.com/kumahq/kuma/pkg/core/resources/apis/system" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
kuma_cp "github.com/kumahq/kuma/pkg/config/app/kuma-cp" | ||
"github.com/kumahq/kuma/pkg/core/managers/apis/zoneinsight" | ||
"github.com/kumahq/kuma/pkg/core/resources/store" | ||
"github.com/kumahq/kuma/pkg/plugins/resources/memory" | ||
) | ||
|
||
var _ = Describe("ZoneInsight Manager", func() { | ||
|
||
It("should limit the number of subscription", func() { | ||
// setup | ||
s := memory.NewStore() | ||
cfg := &kuma_cp.ZoneMetrics{ | ||
Enabled: true, | ||
SubscriptionLimit: 3, | ||
} | ||
manager := zoneinsight.NewZoneInsightManager(s, cfg) | ||
|
||
err := s.Create(context.Background(), &system.ZoneResource{}, store.CreateByKey("di1", "default")) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
input := system.ZoneInsightResource{} | ||
for i := 0; i < 10; i++ { | ||
input.Spec.Subscriptions = append(input.Spec.Subscriptions, &v1alpha1.KDSSubscription{ | ||
Id: fmt.Sprintf("%d", i), | ||
}) | ||
} | ||
|
||
// when | ||
err = manager.Create(context.Background(), &input, store.CreateByKey("di1", "default")) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
actual := system.ZoneInsightResource{} | ||
err = s.Get(context.Background(), &actual, store.GetByKey("di1", "default")) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
// then | ||
Expect(actual.Spec.Subscriptions).To(HaveLen(3)) | ||
Expect(actual.Spec.Subscriptions[0].Id).To(Equal("7")) | ||
Expect(actual.Spec.Subscriptions[1].Id).To(Equal("8")) | ||
Expect(actual.Spec.Subscriptions[2].Id).To(Equal("9")) | ||
}) | ||
|
||
It("should cleanup subscriptions if disabled", func() { | ||
// setup | ||
s := memory.NewStore() | ||
cfg := &kuma_cp.ZoneMetrics{ | ||
Enabled: false, | ||
} | ||
manager := zoneinsight.NewZoneInsightManager(s, cfg) | ||
|
||
err := s.Create(context.Background(), &system.ZoneResource{}, store.CreateByKey("di1", "default")) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
input := system.ZoneInsightResource{} | ||
for i := 0; i < 10; i++ { | ||
input.Spec.Subscriptions = append(input.Spec.Subscriptions, &v1alpha1.KDSSubscription{ | ||
Id: fmt.Sprintf("%d", i), | ||
}) | ||
} | ||
|
||
// when | ||
err = manager.Create(context.Background(), &input, store.CreateByKey("di1", "default")) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
actual := system.ZoneInsightResource{} | ||
err = s.Get(context.Background(), &actual, store.GetByKey("di1", "default")) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
// then | ||
Expect(actual.Spec.Subscriptions).To(HaveLen(0)) | ||
Expect(actual.Spec.Subscriptions).To(BeNil()) | ||
}) | ||
}) |