Skip to content

Commit 5cebdff

Browse files
fix: report event on application metadata changes (v2 reporter) (#314)
* reporter v2: event reporting on application metadata changes * reporter v2 / ShouldSendApplicationEvent: moved some logic to dedicated func applicationMetadataChanged
1 parent a34128a commit 5cebdff

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

changelog/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
### Fix
2+
- fix(event-reporter v2): report event on application metadata changes

event_reporter/reporter/application_event_reporter.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,38 @@ func (s *applicationEventReporter) ShouldSendApplicationEvent(ae *appv1.Applicat
467467
return true, false
468468
}
469469

470+
metadataChanged := applicationMetadataChanged(ae, cachedApp)
471+
472+
if metadataChanged {
473+
logCtx.Info("application metadata changed")
474+
return true, false
475+
}
476+
470477
return false, false
471478
}
472479

480+
func applicationMetadataChanged(ae *appv1.ApplicationWatchEvent, cachedApp *appv1.Application) (changed bool) {
481+
if ae.Type != watch.Modified {
482+
return false
483+
}
484+
485+
cachedAppMeta := cachedApp.ObjectMeta.DeepCopy()
486+
newEventAppMeta := ae.Application.ObjectMeta.DeepCopy()
487+
488+
if newEventAppMeta.Annotations != nil {
489+
delete(newEventAppMeta.Annotations, "kubectl.kubernetes.io/last-applied-configuration")
490+
delete(cachedAppMeta.Annotations, "kubectl.kubernetes.io/last-applied-configuration")
491+
}
492+
493+
cachedAppMeta.ResourceVersion = newEventAppMeta.ResourceVersion // ignore those in the diff
494+
cachedAppMeta.Generation = newEventAppMeta.Generation // ignore those in the diff
495+
cachedAppMeta.GenerateName = newEventAppMeta.GenerateName // ignore those in the diff
496+
newEventAppMeta.ManagedFields = nil // ignore those in the diff
497+
cachedAppMeta.ManagedFields = nil // ignore those in the diff
498+
499+
return !reflect.DeepEqual(newEventAppMeta, cachedAppMeta)
500+
}
501+
473502
func isApp(rs appv1.ResourceStatus) bool {
474503
return rs.GroupVersionKind().String() == appv1.ApplicationSchemaGroupVersionKind.String()
475504
}

event_reporter/reporter/application_event_reporter_test.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package reporter
33
import (
44
"context"
55
"encoding/json"
6+
"k8s.io/apimachinery/pkg/watch"
67
"net/http"
78
"testing"
89
"time"
@@ -536,3 +537,112 @@ func TestGetParentAppIdentityWithinControllerNs(t *testing.T) {
536537
assert.Equal(t, expectedName, res.name)
537538
assert.Equal(t, expectedNamespace, res.namespace)
538539
}
540+
541+
func TestShouldSendApplicationEvent(t *testing.T) {
542+
eventReporter := fakeReporter()
543+
544+
t.Run("should send because cache is missing", func(t *testing.T) {
545+
app := v1alpha1.Application{
546+
ObjectMeta: metav1.ObjectMeta{
547+
Labels: map[string]string{
548+
"sdsds": "sdsd",
549+
},
550+
},
551+
}
552+
553+
shouldSend, _ := eventReporter.ShouldSendApplicationEvent(&v1alpha1.ApplicationWatchEvent{
554+
Type: watch.Modified,
555+
Application: app,
556+
})
557+
assert.True(t, shouldSend)
558+
})
559+
560+
t.Run("should send because labels changed", func(t *testing.T) {
561+
appCache := v1alpha1.Application{
562+
ObjectMeta: metav1.ObjectMeta{
563+
Labels: map[string]string{
564+
"data": "old value",
565+
},
566+
},
567+
}
568+
569+
err := eventReporter.cache.SetLastApplicationEvent(&appCache, time.Second*5)
570+
assert.NoError(t, err)
571+
572+
app := v1alpha1.Application{
573+
ObjectMeta: metav1.ObjectMeta{
574+
Labels: map[string]string{
575+
"data": "new value",
576+
},
577+
},
578+
}
579+
580+
shouldSend, _ := eventReporter.ShouldSendApplicationEvent(&v1alpha1.ApplicationWatchEvent{
581+
Type: watch.Modified,
582+
Application: app,
583+
})
584+
assert.True(t, shouldSend)
585+
})
586+
587+
t.Run("should send because annotations changed", func(t *testing.T) {
588+
appCache := v1alpha1.Application{
589+
ObjectMeta: metav1.ObjectMeta{
590+
Annotations: map[string]string{
591+
"data": "old value",
592+
},
593+
},
594+
}
595+
596+
err := eventReporter.cache.SetLastApplicationEvent(&appCache, time.Second*5)
597+
assert.NoError(t, err)
598+
599+
app := v1alpha1.Application{
600+
ObjectMeta: metav1.ObjectMeta{
601+
Annotations: map[string]string{
602+
"data": "new value",
603+
},
604+
},
605+
}
606+
607+
shouldSend, _ := eventReporter.ShouldSendApplicationEvent(&v1alpha1.ApplicationWatchEvent{
608+
Type: watch.Modified,
609+
Application: app,
610+
})
611+
assert.True(t, shouldSend)
612+
})
613+
614+
t.Run("should ignore some changed metadata fields", func(t *testing.T) {
615+
appCache := v1alpha1.Application{
616+
ObjectMeta: metav1.ObjectMeta{
617+
ResourceVersion: "1",
618+
Generation: 1,
619+
GenerateName: "first",
620+
ManagedFields: []metav1.ManagedFieldsEntry{},
621+
Annotations: map[string]string{
622+
"kubectl.kubernetes.io/last-applied-configuration": "first",
623+
},
624+
},
625+
}
626+
627+
err := eventReporter.cache.SetLastApplicationEvent(&appCache, time.Second*5)
628+
assert.NoError(t, err)
629+
630+
app := v1alpha1.Application{
631+
ObjectMeta: metav1.ObjectMeta{
632+
ResourceVersion: "2",
633+
Generation: 2,
634+
GenerateName: "changed",
635+
ManagedFields: []metav1.ManagedFieldsEntry{{Manager: "changed"}},
636+
Annotations: map[string]string{
637+
"kubectl.kubernetes.io/last-applied-configuration": "changed",
638+
},
639+
},
640+
}
641+
642+
shouldSend, _ := eventReporter.ShouldSendApplicationEvent(&v1alpha1.ApplicationWatchEvent{
643+
Type: watch.Modified,
644+
Application: app,
645+
})
646+
assert.False(t, shouldSend)
647+
})
648+
}

0 commit comments

Comments
 (0)