-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Send available condition events for managed cluster (#450)
* Send available condition events for managed cluster Signed-off-by: zhujian <jiazhu@redhat.com> * Send available condition events for managed cluster Signed-off-by: zhujian <jiazhu@redhat.com> * Rename event reporting component Signed-off-by: zhujian <jiazhu@redhat.com> --------- Signed-off-by: zhujian <jiazhu@redhat.com>
- Loading branch information
Showing
12 changed files
with
295 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package helpers | ||
|
||
import ( | ||
"context" | ||
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/client-go/kubernetes" | ||
kevents "k8s.io/client-go/tools/events" | ||
) | ||
|
||
// NewEventRecorder creates a new event recorder for the given controller, it will also log the events | ||
func NewEventRecorder(ctx context.Context, scheme *runtime.Scheme, | ||
kubeClient kubernetes.Interface, controllerName string) (kevents.EventRecorder, error) { | ||
broadcaster := kevents.NewBroadcaster(&kevents.EventSinkImpl{Interface: kubeClient.EventsV1()}) | ||
err := broadcaster.StartRecordingToSinkWithContext(ctx) | ||
if err != nil { | ||
return nil, nil | ||
} | ||
broadcaster.StartStructuredLogging(0) | ||
recorder := broadcaster.NewRecorder(scheme, controllerName) | ||
return recorder, nil | ||
} |
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,72 @@ | ||
package helpers | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
fakekube "k8s.io/client-go/kubernetes/fake" | ||
clienttesting "k8s.io/client-go/testing" | ||
|
||
clusterscheme "open-cluster-management.io/api/client/cluster/clientset/versioned/scheme" | ||
workscheme "open-cluster-management.io/api/client/work/clientset/versioned/scheme" | ||
clusterv1 "open-cluster-management.io/api/cluster/v1" | ||
|
||
testingcommon "open-cluster-management.io/ocm/pkg/common/testing" | ||
) | ||
|
||
func TestNewEventRecorder(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
scheme *runtime.Scheme | ||
wait time.Duration | ||
validateActions func(t *testing.T, actions []clienttesting.Action) | ||
}{ | ||
{ | ||
name: "test new event recorder, scheme not match ", | ||
scheme: workscheme.Scheme, | ||
wait: 100 * time.Millisecond, | ||
validateActions: func(t *testing.T, actions []clienttesting.Action) { | ||
testingcommon.AssertNoActions(t, actions) | ||
}, | ||
}, | ||
{ | ||
name: "test new event recorder, scheme match", | ||
scheme: clusterscheme.Scheme, | ||
wait: 100 * time.Millisecond, | ||
validateActions: func(t *testing.T, actions []clienttesting.Action) { | ||
testingcommon.AssertActions(t, actions, "create") | ||
}, | ||
}, | ||
{ | ||
name: "test new event recorder, scheme match, no wait", | ||
scheme: clusterscheme.Scheme, | ||
validateActions: func(t *testing.T, actions []clienttesting.Action) { | ||
testingcommon.AssertNoActions(t, actions) | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ctx := context.TODO() | ||
kubeClient := fakekube.NewSimpleClientset() | ||
recorder, err := NewEventRecorder(ctx, tt.scheme, kubeClient, "test") | ||
if err != nil { | ||
t.Errorf("NewEventRecorder() error = %v", err) | ||
return | ||
} | ||
|
||
object := &clusterv1.ManagedCluster{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test", | ||
}, | ||
} | ||
recorder.Eventf(object, nil, corev1.EventTypeNormal, "test", "test", "") | ||
time.Sleep(tt.wait) | ||
tt.validateActions(t, kubeClient.Actions()) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.