forked from docker-flow/docker-flow-swarm-listener
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Uses queues for notifications and adds node notifications
- Loading branch information
1 parent
6988292
commit 07bcba7
Showing
46 changed files
with
4,141 additions
and
2,060 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM golang:1.9-alpine3.6 AS build | ||
FROM golang:1.10.0-alpine3.7 AS build | ||
|
||
RUN apk add --update git | ||
ADD . /src | ||
|
@@ -8,12 +8,11 @@ RUN go build -v -o docker-flow-swarm-listener | |
|
||
|
||
|
||
FROM alpine:3.6 | ||
MAINTAINER Viktor Farcic <[email protected]> | ||
FROM alpine:3.7 | ||
LABEL maintainer="Viktor Farcic <[email protected]>" | ||
|
||
ENV DF_DOCKER_HOST="unix:///var/run/docker.sock" \ | ||
DF_NOTIFICATION_URL="" \ | ||
DF_INTERVAL="5" \ | ||
DF_RETRY="50" \ | ||
DF_RETRY_INTERVAL="5" \ | ||
DF_NOTIFY_LABEL="com.df.notify" \ | ||
|
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 |
---|---|---|
@@ -1,17 +1,11 @@ | ||
FROM golang:1.9 | ||
FROM docker:17.12.1-ce | ||
|
||
MAINTAINER Viktor Farcic <[email protected]> | ||
|
||
RUN apt-get update && \ | ||
apt-get install -y apt-transport-https ca-certificates curl software-properties-common expect && \ | ||
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - && \ | ||
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable" && \ | ||
apt-get update && \ | ||
apt-get -y install docker-ce | ||
RUN apk add --no-cache gcc musl-dev openssl git go expect curl && \ | ||
go install cmd/... | ||
|
||
COPY . /src | ||
WORKDIR /src | ||
RUN go get -d -v -t | ||
RUN chmod +x /src/run-tests.sh | ||
|
||
CMD ["sh", "-c", "/src/run-tests.sh"] | ||
CMD ["sh", "-c", "/src/run-tests.sh"] |
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
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
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,49 @@ | ||
# Usage | ||
|
||
## Notification Format | ||
|
||
*Docker Flow Swarm Listener*, sends GET notifcations to configured URLs when a service or node is created, updated or removed. Please consult the [configuration](config.md) page on how to configure the URLs. | ||
|
||
### Service Notification | ||
|
||
When a service is created or updated a notification will be sent to **[DF_NOTIFY_CREATE_SERVICE_URL]** with the following parameters: | ||
|
||
| Query | Description | Example | | ||
|-------------|------------------------------------------------------------------------|---------| | ||
| serviceName | Name of service. If `com.df.shortName` is true, and the service is part of a stack the stack name will be trimed off. | `go-demo` | | ||
| replicas | Number of replicas of service. If the service is global, this parameter will be excluded.| `3` | | ||
| nodeInfo | An array of node with its ip on an overlay network. The network is defined with the label: `com.df.scrapeNetwork`. This parameter is included when environment variable, `DF_INCLUDE_NODE_IP_INFO`, is true. | `[["node-3","10.0.0.23"], ["node-2", "10.0.0.22"]]` | | ||
|
||
All service labels prefixed by `com.df.` will be added to the notification. For example, a service with label `com.df.hello=world` will translate to parameter: `hello=world`. | ||
|
||
When a service is removed, a notification will be sent to **[DF_NOTIFY_REMOVE_SERVICE_URL]**. Only the `serviceName` parameter is included. | ||
|
||
### Node Notification | ||
|
||
When a node is created or updated a notification will be sent to **[DF_NOTIFY_CREATE_NODE_UR]** with the following parameters: | ||
|
||
| Query | Description | Example | | ||
|-------|-------------|---------| | ||
| id | The ID of node given by docker | `2pe2xpkrx780xrhujws42a73w` | | ||
| hostname | Hostname of node | `ap1.hostname.com` | | ||
| address | Address of node | `10.0.0.1` | | ||
| versionIndex | The version index of node | `24` | | ||
| state | State of node. [`unknown`, `down`, `ready`, `disconnected`] | `down` | | ||
| role | Role of node. [`worker`, `manager`] | `worker` | | ||
| availability | Availability of node. [`active`, `pause`, `drain` ]| `active` | | ||
|
||
All service labels prefixed by `com.df.` will be added to the notification. For example, a node with label `com.df.hello=world` will translate to parameter: `hello=world`. | ||
|
||
When a node is removed, a notification will be sent to **[DF_NOTIFY_REMOVE_NODE_URl]**. Only the `id`, `hostname`, and `address` parameters are included. | ||
|
||
## API | ||
|
||
*Docker Flow Swarm Listener* exposes a API to query series and to send notifications. | ||
|
||
### Get Services | ||
|
||
The *Get Services* endpoint is used to query all running services with the `DF_NOTIFY_LABEL` label. A `GET` request to **[SWARM_IP]:[SWARM_PORT]/v1/docker-flow-swarm-listener/get-services** returns a json representation of these services. | ||
|
||
### Notify Services | ||
|
||
*DFSL* normally sends out notifcations when a service is created, updated, or removed. The *Notify Services* endpoint will force *DFSL* to send out notifications for all running services with the `DF_NOTIFY_LABEL` label. A `GET` request to **[SWARM_IP]:[SWARM_PORT]/v1/docker-flow-swarm-listener/notify-services** sends out the notifications. |
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 |
---|---|---|
@@ -1,76 +1,29 @@ | ||
package main | ||
|
||
import ( | ||
"./metrics" | ||
"log" | ||
"os" | ||
|
||
"./service" | ||
) | ||
|
||
func main() { | ||
logPrintf("Starting Docker Flow: Swarm Listener") | ||
s := service.NewServiceFromEnv() | ||
n := service.NewNotificationFromEnv() | ||
el := service.NewEventListenerFromEnv() | ||
serve := NewServe(s, n) | ||
go serve.Run() | ||
l := log.New(os.Stdout, "", log.LstdFlags) | ||
|
||
l.Printf("Starting Docker Flow: Swarm Listener") | ||
args := getArgs() | ||
if len(n.CreateServiceAddr) == 0 { | ||
return | ||
} | ||
|
||
logPrintf("Sending notifications for running services") | ||
allServices, err := s.GetServices() | ||
swarmListener, err := service.NewSwarmListenerFromEnv(args.Retry, args.RetryInterval, l) | ||
if err != nil { | ||
metrics.RecordError("GetServices") | ||
} | ||
|
||
newServices, err := s.GetNewServices(allServices) | ||
if err != nil { | ||
metrics.RecordError("GetNewServices") | ||
} | ||
err = n.ServicesCreate( | ||
newServices, | ||
args.Retry, | ||
args.RetryInterval, | ||
) | ||
if err != nil { | ||
metrics.RecordError("ServicesCreate") | ||
l.Printf("Failed to initialize Docker Flow: Swarm Listener") | ||
l.Printf("ERROR: %v", err) | ||
return | ||
} | ||
|
||
logPrintf("Start listening to docker service events") | ||
events, errs := el.ListenForEvents() | ||
for { | ||
select { | ||
case event := <-events: | ||
if event.Action == "create" || event.Action == "update" { | ||
eventServices, err := s.GetServicesFromID(event.ServiceID) | ||
if err != nil { | ||
metrics.RecordError("GetServicesFromID") | ||
} | ||
newServices, err := s.GetNewServices(eventServices) | ||
if err != nil { | ||
metrics.RecordError("GetNewServices") | ||
} | ||
err = n.ServicesCreate( | ||
newServices, | ||
args.Retry, | ||
args.RetryInterval, | ||
) | ||
if err != nil { | ||
metrics.RecordError("ServicesCreate") | ||
} | ||
l.Printf("Sending notifications for running services and nodes") | ||
swarmListener.NotifyServices(true) | ||
swarmListener.NotifyNodes(true) | ||
|
||
} else if event.Action == "remove" { | ||
err = n.ServicesRemove(&[]string{event.ServiceID}, args.Retry, args.RetryInterval) | ||
metrics.RecordService(len(service.CachedServices)) | ||
if err != nil { | ||
metrics.RecordError("ServicesRemove") | ||
} | ||
} | ||
case <-errs: | ||
metrics.RecordError("ListenForEvents") | ||
// Restart listening for events | ||
events, errs = el.ListenForEvents() | ||
} | ||
} | ||
swarmListener.Run() | ||
serve := NewServe(swarmListener, l) | ||
l.Fatal(serve.Run()) | ||
} |
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 |
---|---|---|
@@ -1 +1 @@ | ||
go test --cover ./... --run UnitTest | ||
go test --cover ./... -p 1 |
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
Oops, something went wrong.