Skip to content

Commit

Permalink
DF_NOTIFY_LABEL
Browse files Browse the repository at this point in the history
  • Loading branch information
vfarcic committed Mar 20, 2017
1 parent 885650c commit ac4adac
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ ENV DF_DOCKER_HOST="unix:///var/run/docker.sock" \
DF_NOTIFICATION_URL="" \
DF_INTERVAL="5" \
DF_RETRY="50" \
DF_RETRY_INTERVAL="5"
DF_RETRY_INTERVAL="5" \
DF_NOTIFY_LABEL="com.df.notify"

EXPOSE 8080

Expand Down
1 change: 1 addition & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The following environment variables can be used when creating the `swarm-listene
|-------------------|----------------------------------------------------------|-------------|-------|
|DF_DOCKER_HOST |Path to the Docker socket |unix:///var/run/docker.sock| |
|DF_NOTIFY_CREATE_SERVICE_URL|Comma separated list of URLs that will be used to send notification requests when a service is created.|url1,url2|
|DF_NOTIFY_LABEL |Label that is used to distinguish whether a service should trigger a notification|com.df.notify|com.df.notifyDev|
|DF_NOTIFY_REMOVE_SERVICE_URL|Comma separated list of URLs that will be used to send notification requests when a service is removed.|url1,url2|
|DF_INTERVAL |Interval (in seconds) between service discovery requests |5 |10 |
|DF_RETRY |Number of notification request retries |50 |100 |
Expand Down
5 changes: 3 additions & 2 deletions service/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/url"
"strings"
"time"
"os"
)

type Notification struct {
Expand All @@ -30,11 +31,11 @@ func NewNotificationFromEnv() *Notification {
func (m *Notification) ServicesCreate(services *[]swarm.Service, retries, interval int) error {
errs := []error{}
for _, s := range *services {
if _, ok := s.Spec.Labels["com.df.notify"]; ok {
if _, ok := s.Spec.Labels[os.Getenv("DF_NOTIFY_LABEL")]; ok {
parameters := url.Values{}
parameters.Add("serviceName", s.Spec.Name)
for k, v := range s.Spec.Labels {
if strings.HasPrefix(k, "com.df") && k != "com.df.notify" {
if strings.HasPrefix(k, "com.df") && k != os.Getenv("DF_NOTIFY_LABEL") {
parameters.Add(strings.TrimPrefix(k, "com.df."), v)
}
}
Expand Down
19 changes: 18 additions & 1 deletion service/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ type NotificationTestSuite struct {
func TestNotificationUnitTestSuite(t *testing.T) {
s := new(NotificationTestSuite)
logPrintfOrig := logPrintf
defer func() { logPrintf = logPrintfOrig }()
defer func() {
logPrintf = logPrintfOrig
os.Unsetenv("DF_NOTIFY_LABEL")
}()
logPrintf = func(format string, v ...interface{}) {}
os.Setenv("DF_NOTIFY_LABEL", "com.df.notify")
suite.Run(t, s)
}

Expand Down Expand Up @@ -94,6 +98,19 @@ func (s *NotificationTestSuite) Test_ServicesCreate_SendsRequests() {
s.verifyNotifyServiceCreate(labels, true, fmt.Sprintf("distribute=true&serviceName=%s", "my-service"))
}

func (s *NotificationTestSuite) Test_ServicesCreate_UsesLabelFromEnvVars() {
notifyLabelOrig := os.Getenv("DF_NOTIFY_LABEL")
defer func() { os.Setenv("DF_NOTIFY_LABEL", notifyLabelOrig) }()
os.Setenv("DF_NOTIFY_LABEL", "com.df.something")

labels := make(map[string]string)
labels["com.df.something"] = "true"
labels["com.df.distribute"] = "true"
labels["label.without.correct.prefix"] = "something"

s.verifyNotifyServiceCreate(labels, true, fmt.Sprintf("distribute=true&serviceName=%s", "my-service"))
}

func (s *NotificationTestSuite) Test_ServicesCreate_ReturnsError_WhenUrlCannotBeParsed() {
labels := make(map[string]string)
labels["com.df.notify"] = "true"
Expand Down
3 changes: 2 additions & 1 deletion service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"strings"
"time"
"fmt"
)

var Services map[string]swarm.Service
Expand All @@ -28,7 +29,7 @@ type Servicer interface {
func (m *Service) GetServices() (*[]swarm.Service, error) {
filter := filters.NewArgs()
// TODO: Add alerts filter
filter.Add("label", "com.df.notify=true")
filter.Add("label", fmt.Sprintf("%s=true", os.Getenv("DF_NOTIFY_LABEL")))
services, err := m.DockerClient.ServiceList(context.Background(), types.ServiceListOptions{Filters: filter})
if err != nil {
logPrintf(err.Error())
Expand Down
6 changes: 5 additions & 1 deletion service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ func TestServiceUnitTestSuite(t *testing.T) {
s.serviceName = "my-service"

logPrintfOrig := logPrintf
defer func() { logPrintf = logPrintfOrig }()
defer func() {
logPrintf = logPrintfOrig
os.Unsetenv("DF_NOTIFY_LABEL")
}()
logPrintf = func(format string, v ...interface{}) {}
os.Setenv("DF_NOTIFY_LABEL", "com.df.notify")

createTestServices()
suite.Run(t, s)
Expand Down

0 comments on commit ac4adac

Please sign in to comment.