Skip to content

Commit

Permalink
add analysisRun to the rollout notification
Browse files Browse the repository at this point in the history
Signed-off-by: asingh51 <[email protected]>
  • Loading branch information
asingh51 committed Jan 9, 2024
1 parent 03480d5 commit 7199f6c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 19 deletions.
2 changes: 1 addition & 1 deletion controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (f *fixture) newManager(t *testing.T) *Manager {
Recorder: record.NewFakeEventRecorder(),
})

apiFactory := notificationapi.NewFactory(record.NewAPIFactorySettings(), "default", k8sI.Core().V1().Secrets().Informer(), k8sI.Core().V1().ConfigMaps().Informer())
apiFactory := notificationapi.NewFactory(record.NewAPIFactorySettings(i.Argoproj().V1alpha1().AnalysisRuns()), "default", k8sI.Core().V1().Secrets().Informer(), k8sI.Core().V1().ConfigMaps().Informer())
// rolloutsInformer := rolloutinformers.NewRolloutInformer(f.client, "", time.Minute, cache.Indexers{})
cm.notificationsController = notificationcontroller.NewController(dynamicClient.Resource(v1alpha1.RolloutGVR), i.Argoproj().V1alpha1().Rollouts().Informer(), apiFactory,
notificationcontroller.WithToUnstructured(func(obj metav1.Object) (*unstructured.Unstructured, error) {
Expand Down
44 changes: 29 additions & 15 deletions utils/record/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ import (
"encoding/json"
"fmt"

Check failure on line 8 in utils/record/record.go

View workflow job for this annotation

GitHub Actions / Lint Go code

File is not `goimports`-ed (goimports)

Check failure on line 8 in utils/record/record.go

View workflow job for this annotation

GitHub Actions / Lint Go code

File is not `goimports`-ed (goimports)
timeutil "github.com/argoproj/argo-rollouts/utils/time"
"k8s.io/apimachinery/pkg/labels"
"regexp"
"sort"
"strings"
"time"

"github.com/argoproj/notifications-engine/pkg/api"
"github.com/argoproj/notifications-engine/pkg/services"
"github.com/argoproj/notifications-engine/pkg/subscriptions"
Expand All @@ -21,6 +15,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
k8sinformers "k8s.io/client-go/informers"
Expand All @@ -30,6 +25,10 @@ import (
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/kubectl/pkg/scheme"
"regexp"
"sort"
"strings"
"time"

"github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
rolloutscheme "github.com/argoproj/argo-rollouts/pkg/client/clientset/versioned/scheme"
Expand Down Expand Up @@ -236,16 +235,31 @@ func NewAPIFactorySettings(analysisRunInformer informers.AnalysisRunInformer) ap
InitGetVars: func(cfg *api.Config, configMap *corev1.ConfigMap, secret *corev1.Secret) (api.GetVars, error) {
return func(obj map[string]interface{}, dest services.Destination) map[string]any {

ars, _ := analysisRunInformer.Lister().List(labels.Everything())
sort.Slice(ars[:], func(i, j int) bool {
ts1 := ars[i].ObjectMeta.CreationTimestamp.Time
ts2 := ars[j].ObjectMeta.CreationTimestamp.Time
return ts1.After(ts2)
})
ars[0].ObjectMeta.ManagedFields = nil
un := unstructured.Unstructured{Object: obj}
var ro v1alpha1.Rollout
err := runtime.DefaultUnstructuredConverter.FromUnstructured(un.Object, &ro)
if err != nil {
log.Errorf("failed to convert obj to rollout: %v", err)
return map[string]any{"rollout": nil, "time": timeExprs}

obj["metadata"].(map[string]interface{})["managedFields"] = nil
return map[string]any{"rollout": obj, "analysisRun": ars[0], "time": timeExprs}
}
ars, err := analysisRunInformer.Lister().AnalysisRuns(ro.Namespace).List(labels.Everything())
if err != nil {
log.Errorf("failed to fetch analysisRuns in the given namespace: %s%v", err)

Check failure on line 248 in utils/record/record.go

View workflow job for this annotation

GitHub Actions / Lint Go code

printf: github.com/sirupsen/logrus.Errorf format %v reads arg #2, but call has 1 arg (govet)

Check failure on line 248 in utils/record/record.go

View workflow job for this annotation

GitHub Actions / Lint Go code

printf: github.com/sirupsen/logrus.Errorf format %v reads arg #2, but call has 1 arg (govet)
return map[string]any{"rollout": obj, "time": timeExprs}
}
if len(ars) != 0 {
sort.Slice(ars[:], func(i, j int) bool {
ts1 := ars[i].ObjectMeta.CreationTimestamp.Time
ts2 := ars[j].ObjectMeta.CreationTimestamp.Time
return ts1.After(ts2)
})
return map[string]any{"rollout": obj, "analysisRun": ars[0], "time": timeExprs}

} else {
return map[string]any{"rollout": obj, "time": timeExprs}

}
}, nil
},
}
Expand Down
25 changes: 22 additions & 3 deletions utils/record/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import (
"time"

"github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
argofake "github.com/argoproj/argo-rollouts/pkg/client/clientset/versioned/fake"
argoinformers "github.com/argoproj/argo-rollouts/pkg/client/informers/externalversions"
"github.com/argoproj/argo-rollouts/utils/defaults"

"github.com/argoproj/notifications-engine/pkg/api"
notificationapi "github.com/argoproj/notifications-engine/pkg/api"
"github.com/argoproj/notifications-engine/pkg/mocks"
Expand All @@ -28,6 +31,10 @@ import (
"k8s.io/client-go/kubernetes/fake"
)

var (
noResyncPeriodFunc = func() time.Duration { return 0 }
)

func TestRecordLog(t *testing.T) {
prevOutput := log.StandardLogger().Out
defer func() {
Expand Down Expand Up @@ -178,13 +185,18 @@ func TestSendNotificationsWhenConditionTime(t *testing.T) {

k8sClient := fake.NewSimpleClientset()
sharedInformers := informers.NewSharedInformerFactory(k8sClient, 0)

f := argofake.NewSimpleClientset()
rolloutsI := argoinformers.NewSharedInformerFactory(f, noResyncPeriodFunc())
arInformer := rolloutsI.Argoproj().V1alpha1().AnalysisRuns()

cmInformer := sharedInformers.Core().V1().ConfigMaps().Informer()
secretInformer := sharedInformers.Core().V1().Secrets().Informer()

secretInformer.GetIndexer().Add(secret)
cmInformer.GetIndexer().Add(cm)

apiFactory := notificationapi.NewFactory(NewAPIFactorySettings(), defaults.Namespace(), secretInformer, cmInformer)
apiFactory := notificationapi.NewFactory(NewAPIFactorySettings(arInformer), defaults.Namespace(), secretInformer, cmInformer)
api, err := apiFactory.GetAPI()
assert.NoError(t, err)

Expand Down Expand Up @@ -224,8 +236,11 @@ func TestSendNotificationsWhenConditionTime(t *testing.T) {

secretInformer.GetIndexer().Add(secret)
cmInformer.GetIndexer().Add(cm)
f := argofake.NewSimpleClientset()
rolloutsI := argoinformers.NewSharedInformerFactory(f, noResyncPeriodFunc())
arInformer := rolloutsI.Argoproj().V1alpha1().AnalysisRuns()

apiFactory := notificationapi.NewFactory(NewAPIFactorySettings(), defaults.Namespace(), secretInformer, cmInformer)
apiFactory := notificationapi.NewFactory(NewAPIFactorySettings(arInformer), defaults.Namespace(), secretInformer, cmInformer)
api, err := apiFactory.GetAPI()
assert.NoError(t, err)

Expand Down Expand Up @@ -423,7 +438,11 @@ func TestSendNotificationsNoTrigger(t *testing.T) {
}

func TestNewAPIFactorySettings(t *testing.T) {
settings := NewAPIFactorySettings()
f := argofake.NewSimpleClientset()
rolloutsI := argoinformers.NewSharedInformerFactory(f, noResyncPeriodFunc())
arInformer := rolloutsI.Argoproj().V1alpha1().AnalysisRuns()

settings := NewAPIFactorySettings(arInformer)
assert.Equal(t, NotificationConfigMap, settings.ConfigMapName)
assert.Equal(t, NotificationSecret, settings.SecretName)
getVars, err := settings.InitGetVars(nil, nil, nil)
Expand Down

0 comments on commit 7199f6c

Please sign in to comment.