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.
🌱 Persistent the agent event state to the configmap (stolostron#899)
* Add time filter Signed-off-by: myan <[email protected]> * add more tests Signed-off-by: myan <[email protected]> * format Signed-off-by: Meng Yan <[email protected]> * refactor test Signed-off-by: myan <[email protected]> * fix sonar Signed-off-by: myan <[email protected]> * reply review Signed-off-by: myan <[email protected]> * use creattionTimestamp Signed-off-by: myan <[email protected]> --------- Signed-off-by: myan <[email protected]> Signed-off-by: Meng Yan <[email protected]>
- Loading branch information
Showing
11 changed files
with
551 additions
and
166 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
135 changes: 3 additions & 132 deletions
135
agent/pkg/status/controller/event/event_integration_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 |
---|---|---|
@@ -1,139 +1,10 @@ | ||
package event | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
corev1 "k8s.io/api/core/v1" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
clusterv1 "open-cluster-management.io/api/cluster/v1" | ||
policyv1 "open-cluster-management.io/governance-policy-propagator/api/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/stolostron/multicluster-global-hub/pkg/bundle/event" | ||
"github.com/stolostron/multicluster-global-hub/pkg/constants" | ||
"github.com/stolostron/multicluster-global-hub/pkg/enum" | ||
) | ||
|
||
var _ = Describe("test the policy emitter", Ordered, func() { | ||
It("should pass the root policy event", func() { | ||
By("Creating a root policy") | ||
rootPolicy := &policyv1.Policy{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "policy1", | ||
Namespace: "default", | ||
Finalizers: []string{constants.GlobalHubCleanupFinalizer}, | ||
}, | ||
Spec: policyv1.PolicySpec{ | ||
Disabled: true, | ||
PolicyTemplates: []*policyv1.PolicyTemplate{}, | ||
}, | ||
Status: policyv1.PolicyStatus{ | ||
ComplianceState: policyv1.Compliant, | ||
}, | ||
} | ||
Expect(kubeClient.Create(ctx, rootPolicy)).NotTo(HaveOccurred()) | ||
|
||
evt := &corev1.Event{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "policy1.123r543243242", | ||
Namespace: "default", | ||
}, | ||
InvolvedObject: v1.ObjectReference{ | ||
Kind: string(policyv1.Kind), | ||
Namespace: "default", | ||
Name: "policy1", | ||
}, | ||
Reason: "PolicyPropagation", | ||
Message: "Policy default/policy1 was propagated to cluster1", | ||
Source: corev1.EventSource{ | ||
Component: "policy-propagator", | ||
}, | ||
} | ||
Expect(kubeClient.Create(ctx, evt)).NotTo(HaveOccurred()) | ||
|
||
receivedEvent := <-consumer.EventChan() | ||
fmt.Sprintln(receivedEvent) | ||
Expect(string(enum.LocalRootPolicyEventType)).To(Equal(receivedEvent.Type())) | ||
|
||
rootPolicyEvents := []event.RootPolicyEvent{} | ||
err := json.Unmarshal(receivedEvent.Data(), &rootPolicyEvents) | ||
Expect(err).Should(Succeed()) | ||
Expect(rootPolicyEvents[0].EventName).To(Equal(evt.Name)) | ||
}) | ||
|
||
It("should pass the replicated policy event", func() { | ||
By("Create namespace and cluster for the replicated policy") | ||
err := kubeClient.Create(ctx, &corev1.Namespace{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "cluster1", | ||
}, | ||
}, &client.CreateOptions{}) | ||
Expect(err).Should(Succeed()) | ||
|
||
By("Create the cluster") | ||
cluster := &clusterv1.ManagedCluster{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "cluster1", | ||
}, | ||
} | ||
Expect(kubeClient.Create(ctx, cluster, &client.CreateOptions{})).Should(Succeed()) | ||
cluster.Status = clusterv1.ManagedClusterStatus{ | ||
ClusterClaims: []clusterv1.ManagedClusterClaim{ | ||
{ | ||
Name: "id.k8s.io", | ||
Value: "3f406177-34b2-4852-88dd-ff2809680336", | ||
}, | ||
}, | ||
} | ||
Expect(kubeClient.Status().Update(ctx, cluster)).Should(Succeed()) | ||
|
||
By("Create the replicated policy") | ||
replicatedPolicy := &policyv1.Policy{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "default.policy1", | ||
Namespace: "cluster1", | ||
Labels: map[string]string{ | ||
constants.PolicyEventRootPolicyNameLabelKey: fmt.Sprintf("%s.%s", "default", "policy1"), | ||
constants.PolicyEventClusterNameLabelKey: "cluster1", | ||
}, | ||
}, | ||
Spec: policyv1.PolicySpec{ | ||
Disabled: false, | ||
PolicyTemplates: []*policyv1.PolicyTemplate{}, | ||
}, | ||
} | ||
Expect(kubeClient.Create(ctx, replicatedPolicy)).ToNot(HaveOccurred()) | ||
|
||
By("Create the replicated policy event") | ||
evt := &corev1.Event{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "default.policy1.17af98f19c06811e", | ||
Namespace: "cluster1", | ||
}, | ||
InvolvedObject: v1.ObjectReference{ | ||
Kind: string(policyv1.Kind), | ||
Namespace: "cluster1", | ||
Name: "default.policy1", | ||
}, | ||
Reason: "PolicyStatusSync", | ||
Message: "Policy default.policy1 status was updated in cluster", | ||
Source: corev1.EventSource{ | ||
Component: "policy-status-sync", | ||
}, | ||
} | ||
Expect(kubeClient.Create(ctx, evt)).NotTo(HaveOccurred()) | ||
|
||
receivedEvent := <-consumer.EventChan() | ||
fmt.Sprintln(receivedEvent) | ||
Expect(string(enum.LocalReplicatedPolicyEventType)).To(Equal(receivedEvent.Type())) | ||
|
||
replicatedPolicyEvents := []event.ReplicatedPolicyEvent{} | ||
err = json.Unmarshal(receivedEvent.Data(), &replicatedPolicyEvents) | ||
Expect(err).Should(Succeed()) | ||
Expect(replicatedPolicyEvents[0].EventName).To(Equal(evt.Name)) | ||
}) | ||
var _ = Describe("Events emitters", Ordered, func() { | ||
Context("with root policy events", Ordered, localRootPolicyEventTestSpecs) | ||
Context("with replicated policy events", Ordered, localReplicatedPolicyEventTestSpecs) | ||
}) |
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
95 changes: 95 additions & 0 deletions
95
agent/pkg/status/controller/event/local_replicated_policy_emitter_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,95 @@ | ||
package event | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
corev1 "k8s.io/api/core/v1" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
clusterv1 "open-cluster-management.io/api/cluster/v1" | ||
policyv1 "open-cluster-management.io/governance-policy-propagator/api/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/stolostron/multicluster-global-hub/pkg/bundle/event" | ||
"github.com/stolostron/multicluster-global-hub/pkg/constants" | ||
"github.com/stolostron/multicluster-global-hub/pkg/enum" | ||
) | ||
|
||
func localReplicatedPolicyEventTestSpecs() { | ||
It("should pass the replicated policy event", func() { | ||
By("Create namespace and cluster for the replicated policy") | ||
err := kubeClient.Create(ctx, &corev1.Namespace{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "cluster1", | ||
}, | ||
}, &client.CreateOptions{}) | ||
Expect(err).Should(Succeed()) | ||
|
||
By("Create the cluster") | ||
cluster := &clusterv1.ManagedCluster{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "cluster1", | ||
}, | ||
} | ||
Expect(kubeClient.Create(ctx, cluster, &client.CreateOptions{})).Should(Succeed()) | ||
cluster.Status = clusterv1.ManagedClusterStatus{ | ||
ClusterClaims: []clusterv1.ManagedClusterClaim{ | ||
{ | ||
Name: "id.k8s.io", | ||
Value: "3f406177-34b2-4852-88dd-ff2809680336", | ||
}, | ||
}, | ||
} | ||
Expect(kubeClient.Status().Update(ctx, cluster)).Should(Succeed()) | ||
|
||
By("Create the replicated policy") | ||
replicatedPolicy := &policyv1.Policy{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "default.policy1", | ||
Namespace: "cluster1", | ||
Labels: map[string]string{ | ||
constants.PolicyEventRootPolicyNameLabelKey: fmt.Sprintf("%s.%s", "default", "policy1"), | ||
constants.PolicyEventClusterNameLabelKey: "cluster1", | ||
}, | ||
}, | ||
Spec: policyv1.PolicySpec{ | ||
Disabled: false, | ||
PolicyTemplates: []*policyv1.PolicyTemplate{}, | ||
}, | ||
} | ||
Expect(kubeClient.Create(ctx, replicatedPolicy)).ToNot(HaveOccurred()) | ||
|
||
By("Create the replicated policy event") | ||
evt := &corev1.Event{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "default.policy1.17af98f19c06811e", | ||
Namespace: "cluster1", | ||
}, | ||
InvolvedObject: v1.ObjectReference{ | ||
Kind: string(policyv1.Kind), | ||
Namespace: "cluster1", | ||
Name: "default.policy1", | ||
}, | ||
Reason: "PolicyStatusSync", | ||
Message: "Policy default.policy1 status was updated in cluster", | ||
Source: corev1.EventSource{ | ||
Component: "policy-status-sync", | ||
}, | ||
LastTimestamp: metav1.Time{Time: time.Now()}, | ||
} | ||
Expect(kubeClient.Create(ctx, evt)).NotTo(HaveOccurred()) | ||
|
||
receivedEvent := <-consumer.EventChan() | ||
fmt.Println(">>>>>>>>>>>>>>>>>>> replicated policy event", receivedEvent) | ||
Expect(string(enum.LocalReplicatedPolicyEventType)).To(Equal(receivedEvent.Type())) | ||
|
||
replicatedPolicyEvents := []event.ReplicatedPolicyEvent{} | ||
err = json.Unmarshal(receivedEvent.Data(), &replicatedPolicyEvents) | ||
Expect(err).Should(Succeed()) | ||
Expect(replicatedPolicyEvents[0].EventName).To(Equal(evt.Name)) | ||
}) | ||
} |
Oops, something went wrong.