diff --git a/Dockerfile b/Dockerfile index 9a5858f..7d3107c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/docs/config.md b/docs/config.md index 7f32015..1afed0d 100644 --- a/docs/config.md +++ b/docs/config.md @@ -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 | diff --git a/service/notification.go b/service/notification.go index 6c36b87..8d5932c 100644 --- a/service/notification.go +++ b/service/notification.go @@ -8,6 +8,7 @@ import ( "net/url" "strings" "time" + "os" ) type Notification struct { @@ -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) } } diff --git a/service/notification_test.go b/service/notification_test.go index 3d918aa..b354618 100644 --- a/service/notification_test.go +++ b/service/notification_test.go @@ -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) } @@ -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" diff --git a/service/service.go b/service/service.go index f791a01..9569702 100644 --- a/service/service.go +++ b/service/service.go @@ -9,6 +9,7 @@ import ( "os" "strings" "time" + "fmt" ) var Services map[string]swarm.Service @@ -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()) diff --git a/service/service_test.go b/service/service_test.go index a632770..5405f5f 100644 --- a/service/service_test.go +++ b/service/service_test.go @@ -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)