forked from stolostron/multicluster-global-hub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Managed cluster events inventory for global hub manager (stolostron…
…#911) * Managed cluster events inventory Signed-off-by: myan <[email protected]> * replacing controler Signed-off-by: myan <[email protected]> * add manager Signed-off-by: myan <[email protected]> --------- Signed-off-by: myan <[email protected]>
- Loading branch information
Showing
11 changed files
with
185 additions
and
0 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
77 changes: 77 additions & 0 deletions
77
manager/pkg/statussyncer/syncers/managedcluster_event_handler.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,77 @@ | ||
package dbsyncer | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
cloudevents "github.com/cloudevents/sdk-go/v2" | ||
"github.com/go-logr/logr" | ||
"gorm.io/gorm/clause" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
|
||
"github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator" | ||
"github.com/stolostron/multicluster-global-hub/pkg/bundle/event" | ||
eventversion "github.com/stolostron/multicluster-global-hub/pkg/bundle/version" | ||
"github.com/stolostron/multicluster-global-hub/pkg/database" | ||
"github.com/stolostron/multicluster-global-hub/pkg/enum" | ||
) | ||
|
||
type managedClusterEventHandler struct { | ||
log logr.Logger | ||
eventType string | ||
eventSyncMode enum.EventSyncMode | ||
eventPriority conflator.ConflationPriority | ||
} | ||
|
||
func NewManagedClusterEventHandler() conflator.Handler { | ||
eventType := string(enum.ManagedClusterEventType) | ||
logName := strings.Replace(eventType, enum.EventTypePrefix, "", -1) | ||
return &managedClusterEventHandler{ | ||
log: ctrl.Log.WithName(logName), | ||
eventType: eventType, | ||
eventSyncMode: enum.DeltaStateMode, | ||
eventPriority: conflator.ManagedClusterEventPriority, | ||
} | ||
} | ||
|
||
func (h *managedClusterEventHandler) RegisterHandler(conflationManager *conflator.ConflationManager) { | ||
conflationManager.Register(conflator.NewConflationRegistration( | ||
h.eventPriority, | ||
h.eventSyncMode, | ||
h.eventType, | ||
h.handleEvent, | ||
)) | ||
} | ||
|
||
func (h *managedClusterEventHandler) handleEvent(ctx context.Context, evt *cloudevents.Event) error { | ||
version := evt.Extensions()[eventversion.ExtVersion] | ||
leafHubName := evt.Source() | ||
h.log.V(2).Info(startMessage, "type", evt.Type(), "LH", evt.Source(), "version", version) | ||
|
||
managedClusterEvents := event.ManagedClusterEventBundle{} | ||
if err := evt.DataAs(&managedClusterEvents); err != nil { | ||
return err | ||
} | ||
|
||
for _, managedClusterEvent := range managedClusterEvents { | ||
managedClusterEvent.LeafHubName = leafHubName | ||
} | ||
|
||
if len(managedClusterEvents) <= 0 { | ||
h.log.Info("empty managed cluster event payload", "event", evt) | ||
return nil | ||
} | ||
|
||
db := database.GetGorm() | ||
err := db.Clauses(clause.OnConflict{ | ||
Columns: []clause.Column{{Name: "leaf_hub_name"}, {Name: "event_name"}, {Name: "created_at"}}, | ||
DoNothing: true, | ||
}).CreateInBatches(managedClusterEvents, 100).Error | ||
if err != nil { | ||
return fmt.Errorf("failed handling leaf hub LocalPolicyStatusEvent event - %w", err) | ||
} | ||
|
||
h.log.V(2).Info(finishMessage, "type", evt.Type(), "LH", evt.Source(), "version", version) | ||
return nil | ||
} |
65 changes: 65 additions & 0 deletions
65
manager/pkg/statussyncer/syncers/managedcluster_event_handler_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,65 @@ | ||
package dbsyncer_test | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/stolostron/multicluster-global-hub/pkg/bundle/event" | ||
eventversion "github.com/stolostron/multicluster-global-hub/pkg/bundle/version" | ||
"github.com/stolostron/multicluster-global-hub/pkg/database" | ||
"github.com/stolostron/multicluster-global-hub/pkg/database/models" | ||
"github.com/stolostron/multicluster-global-hub/pkg/enum" | ||
) | ||
|
||
// go test ./manager/pkg/statussyncer/syncers -v -ginkgo.focus "ManagedClusterEventHandler" | ||
var _ = Describe("ManagedClusterEventHandler", Ordered, func() { | ||
It("should be able to sync replicate policy event", func() { | ||
By("Create hubClusterInfo event") | ||
|
||
leafHubName := "hub1" | ||
version := eventversion.NewVersion() | ||
version.Incr() | ||
|
||
data := event.ManagedClusterEventBundle{} | ||
data = append(data, models.ManagedClusterEvent{ | ||
EventNamespace: "managed-cluster1", | ||
EventName: "managed-cluster1.17cd5c3642c43a8a", | ||
ClusterID: "13b2e003-2bdf-4c82-9bdf-f1aa7ccf608d", | ||
LeafHubName: "hub1", | ||
Message: "The managed cluster (managed-cluster1) cannot connect to the hub cluster.", | ||
Reason: "AvailableUnknown", | ||
ReportingController: "registration-controller", | ||
ReportingInstance: "registration-controller-cluster-manager-registration-controller-6794cf54d9-j7lgm", | ||
EventType: "Warning", | ||
CreatedAt: time.Now(), | ||
}) | ||
|
||
evt := ToCloudEvent(leafHubName, string(enum.ManagedClusterEventType), version, data) | ||
|
||
By("Sync event with transport") | ||
err := producer.SendEvent(ctx, *evt) | ||
Expect(err).Should(Succeed()) | ||
|
||
By("Check the leaf hubs table") | ||
Eventually(func() error { | ||
db := database.GetGorm() | ||
items := []models.ManagedClusterEvent{} | ||
if err := db.Find(&items).Error; err != nil { | ||
return err | ||
} | ||
|
||
count := 0 | ||
for _, item := range items { | ||
fmt.Println(item.LeafHubName, item.EventName, item.Message, item.CreatedAt) | ||
count++ | ||
} | ||
if count > 0 { | ||
return nil | ||
} | ||
return fmt.Errorf("not found expected resource on the table") | ||
}, 30*time.Second, 100*time.Millisecond).ShouldNot(HaveOccurred()) | ||
}) | ||
}) |
File renamed without changes.
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package event | ||
|
||
import "github.com/stolostron/multicluster-global-hub/pkg/database/models" | ||
|
||
type ManagedClusterEventBundle []models.ManagedClusterEvent |
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