Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

Commit

Permalink
Adding docker swarm service support. (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
kungfoochris authored Sep 30, 2022
1 parent 4f11556 commit be1bb96
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 10 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ The full list of labels are:

## Environment Variables

| Key | Description | Default |
|---------------------|-----------------------------------|------------------------------|
| `LOG_LEVEL` | The level of log verbosity | `Info` |
| `HOMER_BASE_CONFIG` | Where the base config is located | `/base.yml` |
| `HOMER_CONFIG` | Where the Homer config is located | `/config.yml` |
| `SERVICE_DISCOVERY` | Either `Docker` or `Consul` | `Docker` |
| `CONSUL_HOST` | Host for Consul connnection | `127.0.0.1:8500` |
| Key | Description | Default |
|---------------------|-----------------------------------------------|------------------------------|
| `LOG_LEVEL` | The level of log verbosity | `Info` |
| `HOMER_BASE_CONFIG` | Where the base config is located | `/base.yml` |
| `HOMER_CONFIG` | Where the Homer config is located | `/config.yml` |
| `SERVICE_DISCOVERY` | Either `Docker` or `Consul` | `Docker` |
| `HOMER_DOCKER_SWARM`| Docker Swarm supprt. Either `true` or `false` | `false` |
| `CONSUL_HOST` | Host for Consul connnection | `127.0.0.1:8500` |
23 changes: 21 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@ func main() {

if conf.ServiceDiscovery == config.Docker {
logger.Info("Start watching for container creations and deletions")
eventsc, errc := docker.WatchEvents(ctx, conf.Docker)
if conf.HomerDockerSwarmMode {
logger.Info("Docker Swarm mode enabled")
logger.Info("Start watching for service creations, updates, and removal")
}
eventsc, errc := docker.WatchEvents(ctx, conf.Docker, conf.HomerDockerSwarmMode)
for {
select {
case event := <-eventsc:
if event.Action == "start" || event.Action == "die" || strings.HasPrefix(event.Action, "health_status") {
if event.Action == "start" || event.Action == "die" || strings.HasPrefix(event.Action, "health_status") || (event.Type == "service" && (event.Action == "create" || event.Action == "update" || event.Action == "remove")) {
logger.Trace(fmt.Sprintf("%+v", event))
logger.Debug("A " + event.Action + " event occurred")
logger.Info(fmt.Sprintf("Event '%s' received from %s. Generating Homer config...", event.Action, event.Actor.Attributes["name"]))
Expand Down Expand Up @@ -103,6 +107,21 @@ func generateConfig(ctx context.Context, conf config.Config) error {
logger.Debug(fmt.Sprintf("Inspected container %s", parsedContainer.Name))
parsedEntry = append(parsedEntry, parsedContainer)
}

if conf.HomerDockerSwarmMode {
logger.Debug("Getting Docker services")
services, err := docker.ListRunningServices(ctx, conf.Docker)
if err != nil {
logger.Fatal("Failed to list swarm services for Docker", err)
}
for _, service := range services {
parsedService, err := docker.ParseService(ctx, conf.Docker, service)
if err != nil {
logger.Error(fmt.Sprintf("Failed to inspect service %s", service.Spec.Name), err)
}
parsedEntry = append(parsedEntry, parsedService)
}
}
} else if conf.ServiceDiscovery == config.Consul {
logger.Debug("Getting Consul service")
services := consul.ListServices(conf.Consul)
Expand Down
3 changes: 3 additions & 0 deletions internal/config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"fmt"

"github.com/calvinbui/homer-docker-service-discovery/internal/consul"
"github.com/calvinbui/homer-docker-service-discovery/internal/docker"
"github.com/calvinbui/homer-docker-service-discovery/internal/logger"
Expand Down Expand Up @@ -30,6 +31,8 @@ type Config struct {
HomerBaseConfigPath string `env:"HOMER_BASE_CONFIG" envDefault:"/base.yml"`

HomerConfigPath string `env:"HOMER_CONFIG" envDefault:"/config.yml"`

HomerDockerSwarmMode bool `env:"HOMER_DOCKER_SWARM" envDefault:"false"`
}

func New() (Config, error) {
Expand Down
6 changes: 5 additions & 1 deletion internal/docker/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ import (
"github.com/docker/docker/client"
)

func WatchEvents(ctx context.Context, docker client.APIClient) (<-chan events.Message, <-chan error) {
func WatchEvents(ctx context.Context, docker client.APIClient, swarmMode bool) (<-chan events.Message, <-chan error) {
f := filters.NewArgs()
f.Add("type", "container")

if swarmMode {
f.Add("type", "service")
}

return docker.Events(ctx, types.EventsOptions{Filters: f})
}
42 changes: 42 additions & 0 deletions internal/docker/services.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package docker

import (
"context"

"github.com/calvinbui/homer-docker-service-discovery/internal/entry"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client"
)

func ListRunningServices(ctx context.Context, docker client.APIClient) ([]swarm.Service, error) {
if ctx == nil {
ctx = context.Background()
}

services, err := docker.ServiceList(ctx, types.ServiceListOptions{Status: true})

if err != nil {
return nil, err
}

return services, nil
}

func ParseService(ctx context.Context, docker client.APIClient, service swarm.Service) (entry.RawEntry, error) {
i, _, err := docker.ServiceInspectWithRaw(ctx, service.ID, types.ServiceInspectOptions{})

if err != nil {
return entry.RawEntry{}, err
}

s := entry.RawEntry{
Name: i.Spec.Annotations.Name,
}

if i.Spec.Labels != nil {
s.Labels = i.Spec.Annotations.Labels
}

return s, nil
}

0 comments on commit be1bb96

Please sign in to comment.