Skip to content

Commit af4a686

Browse files
authored
Emit event when skipping mirroring of a service (linkerd#7655)
Follow up to linkerd#7597 Emit an event when service mirroring is skipped due to the namespace not existing in the source cluster. Signed-off-by: Alex Leong <[email protected]>
1 parent e3225d3 commit af4a686

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

multicluster/service-mirror/cluster_watcher.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ func (rcsw *RemoteClusterServiceWatcher) handleRemoteServiceCreated(ctx context.
474474
// Ensure the namespace exists, and skip mirroring if it doesn't
475475
if _, err := rcsw.localAPIClient.Client.CoreV1().Namespaces().Get(ctx, remoteService.Namespace, metav1.GetOptions{}); err != nil {
476476
if kerrors.IsNotFound(err) {
477+
rcsw.recorder.Event(remoteService, v1.EventTypeNormal, eventTypeSkipped, "Skipped mirroring service: namespace does not exist")
477478
rcsw.log.Warnf("Skipping mirroring of service %s: namespace %s does not exist", serviceInfo, remoteService.Namespace)
478479
return nil
479480
}

multicluster/service-mirror/cluster_watcher_mirroring_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import (
1111
"github.com/linkerd/linkerd2/pkg/multicluster"
1212
logging "github.com/sirupsen/logrus"
1313
corev1 "k8s.io/api/core/v1"
14+
v1 "k8s.io/api/core/v1"
1415
"k8s.io/apimachinery/pkg/api/errors"
1516
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17+
"k8s.io/client-go/tools/record"
1618
"k8s.io/client-go/util/workqueue"
1719
)
1820

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

223225
q := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
226+
eventRecorder := record.NewFakeRecorder(100)
224227

225228
watcher := RemoteClusterServiceWatcher{
226229
link: &multicluster.Link{
@@ -235,6 +238,7 @@ func TestLocalNamespaceCreatedAfterServiceExport(t *testing.T) {
235238
remoteAPIClient: remoteAPI,
236239
localAPIClient: localAPI,
237240
stopper: nil,
241+
recorder: eventRecorder,
238242
log: logging.WithFields(logging.Fields{"cluster": clusterName}),
239243
eventsQueue: q,
240244
requeueLimit: 0,
@@ -268,6 +272,11 @@ func TestLocalNamespaceCreatedAfterServiceExport(t *testing.T) {
268272
t.Fatalf("unexpected error: %v", err)
269273
}
270274

275+
skippedEvent := <-eventRecorder.Events
276+
if skippedEvent != fmt.Sprintf("%s %s %s", v1.EventTypeNormal, eventTypeSkipped, "Skipped mirroring service: namespace does not exist") {
277+
t.Error("Expected skipped event, got:", skippedEvent)
278+
}
279+
271280
ns, err := localAPI.Client.CoreV1().Namespaces().Create(context.Background(), &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "ns1"}}, metav1.CreateOptions{})
272281
if err != nil {
273282
t.Fatal(err)
@@ -277,9 +286,8 @@ func TestLocalNamespaceCreatedAfterServiceExport(t *testing.T) {
277286
for q.Len() > 0 {
278287
watcher.processNextEvent(context.Background())
279288
}
280-
localAPI.Sync(nil)
281289

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

0 commit comments

Comments
 (0)