Skip to content

Commit

Permalink
Emit event when skipping mirroring of a service (#7655)
Browse files Browse the repository at this point in the history
Follow up to #7597

Emit an event when service mirroring is skipped due to the namespace not existing in the source cluster.

Signed-off-by: Alex Leong <alex@buoyant.io>
  • Loading branch information
adleong authored Jan 21, 2022
1 parent e3225d3 commit af4a686
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions multicluster/service-mirror/cluster_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ func (rcsw *RemoteClusterServiceWatcher) handleRemoteServiceCreated(ctx context.
// Ensure the namespace exists, and skip mirroring if it doesn't
if _, err := rcsw.localAPIClient.Client.CoreV1().Namespaces().Get(ctx, remoteService.Namespace, metav1.GetOptions{}); err != nil {
if kerrors.IsNotFound(err) {
rcsw.recorder.Event(remoteService, v1.EventTypeNormal, eventTypeSkipped, "Skipped mirroring service: namespace does not exist")
rcsw.log.Warnf("Skipping mirroring of service %s: namespace %s does not exist", serviceInfo, remoteService.Namespace)
return nil
}
Expand Down
12 changes: 10 additions & 2 deletions multicluster/service-mirror/cluster_watcher_mirroring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"github.com/linkerd/linkerd2/pkg/multicluster"
logging "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"
)

Expand Down Expand Up @@ -221,6 +223,7 @@ func TestLocalNamespaceCreatedAfterServiceExport(t *testing.T) {
localAPI.Sync(nil)

q := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
eventRecorder := record.NewFakeRecorder(100)

watcher := RemoteClusterServiceWatcher{
link: &multicluster.Link{
Expand All @@ -235,6 +238,7 @@ func TestLocalNamespaceCreatedAfterServiceExport(t *testing.T) {
remoteAPIClient: remoteAPI,
localAPIClient: localAPI,
stopper: nil,
recorder: eventRecorder,
log: logging.WithFields(logging.Fields{"cluster": clusterName}),
eventsQueue: q,
requeueLimit: 0,
Expand Down Expand Up @@ -268,6 +272,11 @@ func TestLocalNamespaceCreatedAfterServiceExport(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}

skippedEvent := <-eventRecorder.Events
if skippedEvent != fmt.Sprintf("%s %s %s", v1.EventTypeNormal, eventTypeSkipped, "Skipped mirroring service: namespace does not exist") {
t.Error("Expected skipped event, got:", skippedEvent)
}

ns, err := localAPI.Client.CoreV1().Namespaces().Create(context.Background(), &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "ns1"}}, metav1.CreateOptions{})
if err != nil {
t.Fatal(err)
Expand All @@ -277,9 +286,8 @@ func TestLocalNamespaceCreatedAfterServiceExport(t *testing.T) {
for q.Len() > 0 {
watcher.processNextEvent(context.Background())
}
localAPI.Sync(nil)

_, err = localAPI.Svc().Lister().Services("ns1").Get("service-one-remote")
_, err = localAPI.Client.CoreV1().Services("ns1").Get(context.Background(), "service-one-remote", metav1.GetOptions{})
if err != nil {
t.Fatalf("error getting service-one locally: %v", err)
}
Expand Down

0 comments on commit af4a686

Please sign in to comment.