forked from argoproj/argo-cd
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
12dd9b2
commit 89f06b3
Showing
2 changed files
with
69 additions
and
15 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
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,50 @@ | ||
package application | ||
|
||
import ( | ||
appv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" | ||
log "github.com/sirupsen/logrus" | ||
"k8s.io/apimachinery/pkg/watch" | ||
) | ||
|
||
type channelPayload struct { | ||
Application appv1.Application | ||
Type watch.EventType | ||
} | ||
|
||
type ApplicationEventChannelSelector interface { | ||
Subscribe(application appv1.Application, eventType watch.EventType, callback func(application channelPayload)) | ||
} | ||
|
||
type channelPerApplicationChannelSelector struct { | ||
channels map[string]chan channelPayload | ||
} | ||
|
||
func NewChannelPerApplicationChannelSelector() ApplicationEventChannelSelector { | ||
return &channelPerApplicationChannelSelector{ | ||
channels: map[string]chan channelPayload{}, | ||
} | ||
} | ||
|
||
func (s *channelPerApplicationChannelSelector) Subscribe(application appv1.Application, eventType watch.EventType, callback func(application channelPayload)) { | ||
log.Infof("Subscribing to application %s", application.Name) | ||
if s.channels[application.Name] == nil { | ||
s.channels[application.Name] = make(chan channelPayload, 1000) | ||
go func(channel chan channelPayload) { | ||
for { | ||
select { | ||
case app := <-channel: | ||
log.Infof("Received event for application %s", app.Application.Name) | ||
callback(app) | ||
} | ||
} | ||
}(s.channels[application.Name]) | ||
} | ||
|
||
go func() { | ||
log.Infof("Publish to application %s", application.Name) | ||
s.channels[application.Name] <- channelPayload{ | ||
Application: application, | ||
Type: eventType, | ||
} | ||
}() | ||
} |