Skip to content

Commit

Permalink
refactor: add new methods to Insights
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Beaumont <[email protected]>
  • Loading branch information
michaelbeaumont committed Oct 26, 2023
1 parent e29729f commit 9928dbb
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 48 deletions.
3 changes: 3 additions & 0 deletions api/generic/insights.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import (
type Insight interface {
proto.Message
IsOnline() bool
// TODO Deprecated: bad
GetLastSubscription() Subscription
GetSubscription(id string) Subscription
GetOnlineSubscriptions() []Subscription
UpdateSubscription(Subscription) error
}

Expand Down
31 changes: 21 additions & 10 deletions api/mesh/v1alpha1/dataplane_insight_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,23 @@ func (x *DataplaneInsight) IsOnline() bool {
return false
}

func (x *DataplaneInsight) GetSubscription(id string) (int, *DiscoverySubscription) {
for i, s := range x.GetSubscriptions() {
func (x *DataplaneInsight) GetOnlineSubscriptions() []generic.Subscription {
var subs []generic.Subscription
for _, s := range x.GetSubscriptions() {
if s.ConnectTime != nil && s.DisconnectTime == nil {
subs = append(subs, s)
}
}
return subs
}

func (x *DataplaneInsight) GetSubscription(id string) generic.Subscription {
for _, s := range x.GetSubscriptions() {
if s.Id == id {
return i, s
return s
}
}
return -1, nil
return nil
}

func (x *DataplaneInsight) UpdateCert(generation time.Time, expiration time.Time, issuedBackend string, supportedBackends []string) error {
Expand Down Expand Up @@ -93,13 +103,14 @@ func (x *DataplaneInsight) UpdateSubscription(s generic.Subscription) error {
if !ok {
return errors.Errorf("invalid type %T for DataplaneInsight", s)
}
i, old := x.GetSubscription(discoverySubscription.Id)
if old != nil {
x.Subscriptions[i] = discoverySubscription
} else {
x.finalizeSubscriptions()
x.Subscriptions = append(x.Subscriptions, discoverySubscription)
for i, sub := range x.GetSubscriptions() {
if sub.GetId() == discoverySubscription.Id {
x.Subscriptions[i] = discoverySubscription
return nil
}
}
x.finalizeSubscriptions()
x.Subscriptions = append(x.Subscriptions, discoverySubscription)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions api/mesh/v1alpha1/dataplane_insight_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ var _ = Describe("DataplaneHelpers", func() {
})).To(Succeed())

// then
_, subscription := dataplaneInsight.GetSubscription("2")
Expect(subscription.DisconnectTime).ToNot(BeNil())
subscription := dataplaneInsight.GetSubscription("2")
Expect(subscription.(*DiscoverySubscription).DisconnectTime).ToNot(BeNil())
})

It("should return error for wrong subscription type", func() {
Expand Down
31 changes: 21 additions & 10 deletions api/mesh/v1alpha1/zone_ingress_insight_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (

var _ generic.Insight = &ZoneIngressInsight{}

func (x *ZoneIngressInsight) GetSubscription(id string) (int, *DiscoverySubscription) {
for i, s := range x.GetSubscriptions() {
func (x *ZoneIngressInsight) GetSubscription(id string) generic.Subscription {
for _, s := range x.GetSubscriptions() {
if s.Id == id {
return i, s
return s
}
}
return -1, nil
return nil
}

func (x *ZoneIngressInsight) UpdateSubscription(s generic.Subscription) error {
Expand All @@ -26,13 +26,14 @@ func (x *ZoneIngressInsight) UpdateSubscription(s generic.Subscription) error {
if !ok {
return errors.Errorf("invalid type %T for ZoneIngressInsight", s)
}
i, old := x.GetSubscription(discoverySubscription.Id)
if old != nil {
x.Subscriptions[i] = discoverySubscription
} else {
x.finalizeSubscriptions()
x.Subscriptions = append(x.Subscriptions, discoverySubscription)
for i, sub := range x.GetSubscriptions() {
if sub.GetId() == discoverySubscription.Id {
x.Subscriptions[i] = discoverySubscription
return nil
}
}
x.finalizeSubscriptions()
x.Subscriptions = append(x.Subscriptions, discoverySubscription)
return nil
}

Expand All @@ -57,6 +58,16 @@ func (x *ZoneIngressInsight) IsOnline() bool {
return false
}

func (x *ZoneIngressInsight) GetOnlineSubscriptions() []generic.Subscription {
var subs []generic.Subscription
for _, s := range x.GetSubscriptions() {
if s.ConnectTime != nil && s.DisconnectTime == nil {
subs = append(subs, s)
}
}
return subs
}

func (x *ZoneIngressInsight) GetLastSubscription() generic.Subscription {
if len(x.GetSubscriptions()) == 0 {
return (*DiscoverySubscription)(nil)
Expand Down
4 changes: 2 additions & 2 deletions api/mesh/v1alpha1/zone_ingress_insight_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ var _ = Describe("Zone Ingress Insights", func() {
})).To(Succeed())

// then
_, subscription := zoneInsight.GetSubscription("2")
Expect(subscription.DisconnectTime).ToNot(BeNil())
subscription := zoneInsight.GetSubscription("2")
Expect(subscription.(*mesh_proto.DiscoverySubscription).DisconnectTime).ToNot(BeNil())
})

It("should return error for wrong subscription type", func() {
Expand Down
31 changes: 21 additions & 10 deletions api/mesh/v1alpha1/zoneegressinsight_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (

var _ generic.Insight = &ZoneEgressInsight{}

func (x *ZoneEgressInsight) GetSubscription(id string) (int, *DiscoverySubscription) {
for i, s := range x.GetSubscriptions() {
func (x *ZoneEgressInsight) GetSubscription(id string) generic.Subscription {
for _, s := range x.GetSubscriptions() {
if s.Id == id {
return i, s
return s
}
}
return -1, nil
return nil
}

func (x *ZoneEgressInsight) UpdateSubscription(s generic.Subscription) error {
Expand All @@ -26,13 +26,14 @@ func (x *ZoneEgressInsight) UpdateSubscription(s generic.Subscription) error {
if !ok {
return errors.Errorf("invalid type %T for ZoneEgressInsight", s)
}
i, old := x.GetSubscription(discoverySubscription.Id)
if old != nil {
x.Subscriptions[i] = discoverySubscription
} else {
x.finalizeSubscriptions()
x.Subscriptions = append(x.Subscriptions, discoverySubscription)
for i, sub := range x.GetSubscriptions() {
if sub.GetId() == discoverySubscription.Id {
x.Subscriptions[i] = discoverySubscription
return nil
}
}
x.finalizeSubscriptions()
x.Subscriptions = append(x.Subscriptions, discoverySubscription)
return nil
}

Expand All @@ -57,6 +58,16 @@ func (x *ZoneEgressInsight) IsOnline() bool {
return false
}

func (x *ZoneEgressInsight) GetOnlineSubscriptions() []generic.Subscription {
var subs []generic.Subscription
for _, s := range x.GetSubscriptions() {
if s.ConnectTime != nil && s.DisconnectTime == nil {
subs = append(subs, s)
}
}
return subs
}

func (x *ZoneEgressInsight) GetLastSubscription() generic.Subscription {
if len(x.GetSubscriptions()) == 0 {
return (*DiscoverySubscription)(nil)
Expand Down
4 changes: 2 additions & 2 deletions api/mesh/v1alpha1/zoneegressinsight_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ var _ = Describe("Zone Egress Insights", func() {
})).To(Succeed())

// then
_, subscription := zoneInsight.GetSubscription("2")
Expect(subscription.DisconnectTime).ToNot(BeNil())
subscription := zoneInsight.GetSubscription("2")
Expect(subscription.(*mesh_proto.DiscoverySubscription)).ToNot(BeNil())
})

It("should return error for wrong subscription type", func() {
Expand Down
31 changes: 21 additions & 10 deletions api/system/v1alpha1/zone_insight_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ func NewSubscriptionStatus() *KDSSubscriptionStatus {
}
}

func (x *ZoneInsight) GetSubscription(id string) (int, *KDSSubscription) {
for i, s := range x.GetSubscriptions() {
func (x *ZoneInsight) GetSubscription(id string) generic.Subscription {
for _, s := range x.GetSubscriptions() {
if s.Id == id {
return i, s
return s
}
}
return -1, nil
return nil
}

func (x *ZoneInsight) GetLastSubscription() generic.Subscription {
Expand All @@ -44,6 +44,16 @@ func (x *ZoneInsight) IsOnline() bool {
return false
}

func (x *ZoneInsight) GetOnlineSubscriptions() []generic.Subscription {
var subs []generic.Subscription
for _, s := range x.GetSubscriptions() {
if s.ConnectTime != nil && s.DisconnectTime == nil {
subs = append(subs, s)
}
}
return subs
}

func (x *KDSSubscription) SetDisconnectTime(time time.Time) {
x.DisconnectTime = timestamppb.New(time)
}
Expand All @@ -64,13 +74,14 @@ func (x *ZoneInsight) UpdateSubscription(s generic.Subscription) error {
if !ok {
return errors.Errorf("invalid type %T for ZoneInsight", s)
}
i, old := x.GetSubscription(kdsSubscription.Id)
if old != nil {
x.Subscriptions[i] = kdsSubscription
} else {
x.finalizeSubscriptions()
x.Subscriptions = append(x.Subscriptions, kdsSubscription)
for i, sub := range x.GetSubscriptions() {
if sub.GetId() == kdsSubscription.Id {
x.Subscriptions[i] = kdsSubscription
return nil
}
}
x.finalizeSubscriptions()
x.Subscriptions = append(x.Subscriptions, kdsSubscription)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions api/system/v1alpha1/zone_insight_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ var _ = Describe("Zone Insights", func() {
})).To(Succeed())

// then
_, subscription := zoneInsight.GetSubscription("2")
Expect(subscription.DisconnectTime).ToNot(BeNil())
subscription := zoneInsight.GetSubscription("2")
Expect(subscription.(*system_proto.KDSSubscription).DisconnectTime).ToNot(BeNil())
})

It("should return error for wrong subscription type", func() {
Expand Down

0 comments on commit 9928dbb

Please sign in to comment.