Browse Source

Begin refactoring into packages

master
Chris Smith 5 years ago
parent
commit
8eb3db4442
6 changed files with 64 additions and 48 deletions
  1. 0
    1
      .pre-commit-config.yaml
  2. 1
    0
      certificate_manager.go
  3. 12
    5
      docker/monitor.go
  4. 13
    32
      dotege.go
  5. 32
    0
      model/model.go
  6. 6
    10
      template_generator.go

+ 0
- 1
.pre-commit-config.yaml View File

@@ -9,7 +9,6 @@ repos:
9 9
     rev: v1.1.6
10 10
     hooks:
11 11
       - id: remove-crlf
12
-      - id: forbid-tabs
13 12
   - repo: https://github.com/dnephin/pre-commit-golang
14 13
     rev: v0.3.2
15 14
     hooks:

+ 1
- 0
certificate_manager.go View File

@@ -0,0 +1 @@
1
+package main

container_monitor.go → docker/monitor.go View File

@@ -1,7 +1,8 @@
1
-package main
1
+package docker
2 2
 
3 3
 import (
4 4
 	"context"
5
+	"github.com/csmith/dotege/model"
5 6
 	"github.com/docker/docker/api/types"
6 7
 	"github.com/docker/docker/api/types/filters"
7 8
 	"github.com/docker/docker/client"
@@ -9,9 +10,12 @@ import (
9 10
 	"time"
10 11
 )
11 12
 
13
+// ContainerMonitor watches for newly created and destroyed containers, and emits their information on a channel.
14
+// Destroyed containers are given a grace period before being announced, to allow for restarts etc to be less
15
+// disruptive.
12 16
 type ContainerMonitor struct {
13 17
 	logger             *zap.SugaredLogger
14
-	newContainers      chan<- Container
18
+	newContainers      chan<- model.Container
15 19
 	goneContainerNames chan<- string
16 20
 	client             *client.Client
17 21
 	expiryTimes        map[string]time.Time
@@ -20,7 +24,8 @@ type ContainerMonitor struct {
20 24
 	expiryTimer        *time.Timer
21 25
 }
22 26
 
23
-func NewContainerMonitor(logger *zap.SugaredLogger, client *client.Client, newContainerChannel chan<- Container, goneContainerChannel chan<- string) *ContainerMonitor {
27
+// NewContainerMonitor creates a new container monitor
28
+func NewContainerMonitor(logger *zap.SugaredLogger, client *client.Client, newContainerChannel chan<- model.Container, goneContainerChannel chan<- string) *ContainerMonitor {
24 29
 	timer := time.NewTimer(time.Hour)
25 30
 	timer.Stop()
26 31
 
@@ -36,6 +41,8 @@ func NewContainerMonitor(logger *zap.SugaredLogger, client *client.Client, newCo
36 41
 	}
37 42
 }
38 43
 
44
+// Monitor starts monitoring for changes, and publishes info on any pre-existing containers. It blocks indefinitely,
45
+// and should be run from a goroutine.
39 46
 func (c *ContainerMonitor) Monitor() {
40 47
 	args := filters.NewArgs()
41 48
 	args.Add("type", "container")
@@ -71,7 +78,7 @@ func (c *ContainerMonitor) publishExistingContainers() {
71 78
 
72 79
 	for _, container := range containers {
73 80
 		c.logger.Infof("Found existing container %s", container.Names[0][1:])
74
-		c.newContainers <- Container{
81
+		c.newContainers <- model.Container{
75 82
 			Id:     container.ID,
76 83
 			Name:   container.Names[0][1:],
77 84
 			Labels: container.Labels,
@@ -84,7 +91,7 @@ func (c *ContainerMonitor) publishNewContainer(id string) {
84 91
 	if err != nil {
85 92
 		c.logger.Fatal("Error received trying to inspect container", err)
86 93
 	}
87
-	c.newContainers <- Container{
94
+	c.newContainers <- model.Container{
88 95
 		Id:     container.ID,
89 96
 		Name:   container.Name[1:],
90 97
 		Labels: container.Config.Labels,

+ 13
- 32
dotege.go View File

@@ -2,6 +2,8 @@ package main
2 2
 
3 3
 import (
4 4
 	"fmt"
5
+	"github.com/csmith/dotege/docker"
6
+	"github.com/csmith/dotege/model"
5 7
 	"github.com/docker/docker/client"
6 8
 	"go.uber.org/zap"
7 9
 	"go.uber.org/zap/zapcore"
@@ -12,27 +14,6 @@ import (
12 14
 	"time"
13 15
 )
14 16
 
15
-type Container struct {
16
-	Id     string
17
-	Name   string
18
-	Labels map[string]string
19
-}
20
-
21
-type LabelConfig struct {
22
-	Hostnames string
23
-}
24
-
25
-type Hostname struct {
26
-	Name         string
27
-	Alternatives map[string]bool
28
-	Containers   []Container
29
-}
30
-
31
-type Config struct {
32
-	Templates []TemplateConfig
33
-	Labels    LabelConfig
34
-}
35
-
36 17
 func monitorSignals() <-chan bool {
37 18
 	signals := make(chan os.Signal, 1)
38 19
 	done := make(chan bool, 1)
@@ -59,9 +40,9 @@ func main() {
59 40
 	sugar.Info("Dotege is starting")
60 41
 
61 42
 	done := monitorSignals()
62
-	containerChan := make(chan Container, 1)
43
+	containerChan := make(chan model.Container, 1)
63 44
 	expiryChan := make(chan string, 1)
64
-	labelConfig := LabelConfig{
45
+	labelConfig := model.LabelConfig{
65 46
 		Hostnames: "com.chameth.vhost",
66 47
 	}
67 48
 
@@ -71,20 +52,20 @@ func main() {
71 52
 	}
72 53
 
73 54
 	templateGenerator := NewTemplateGenerator(sugar)
74
-	templateGenerator.AddTemplate(TemplateConfig{
55
+	templateGenerator.AddTemplate(model.TemplateConfig{
75 56
 		Source:      "./templates/domains.txt.tpl",
76 57
 		Destination: "domains.txt",
77 58
 	})
78
-	templateGenerator.AddTemplate(TemplateConfig{
59
+	templateGenerator.AddTemplate(model.TemplateConfig{
79 60
 		Source:      "./templates/haproxy.cfg.tpl",
80 61
 		Destination: "haproxy.cfg",
81 62
 	})
82 63
 
83
-	monitor := NewContainerMonitor(sugar, cli, containerChan, expiryChan)
64
+	monitor := docker.NewContainerMonitor(sugar, cli, containerChan, expiryChan)
84 65
 	go monitor.Monitor()
85 66
 
86 67
 	go func() {
87
-		containers := make(map[string]Container)
68
+		containers := make(map[string]model.Container)
88 69
 		timer := time.NewTimer(time.Hour)
89 70
 		timer.Stop()
90 71
 
@@ -113,18 +94,18 @@ func main() {
113 94
 	}
114 95
 }
115 96
 
116
-func getHostnames(containers map[string]Container, config LabelConfig) (hostnames map[string]Hostname) {
117
-	hostnames = make(map[string]Hostname)
97
+func getHostnames(containers map[string]model.Container, config model.LabelConfig) (hostnames map[string]model.Hostname) {
98
+	hostnames = make(map[string]model.Hostname)
118 99
 	for _, container := range containers {
119 100
 		if label, ok := container.Labels[config.Hostnames]; ok {
120 101
 			names := strings.Split(strings.Replace(label, ",", " ", -1), " ")
121 102
 			if hostname, ok := hostnames[names[0]]; ok {
122 103
 				hostname.Containers = append(hostname.Containers, container)
123 104
 			} else {
124
-				hostnames[names[0]] = Hostname{
105
+				hostnames[names[0]] = model.Hostname{
125 106
 					Name:         names[0],
126 107
 					Alternatives: make(map[string]bool),
127
-					Containers:   []Container{container},
108
+					Containers:   []model.Container{container},
128 109
 				}
129 110
 			}
130 111
 			addAlternatives(hostnames[names[0]], names[1:])
@@ -133,7 +114,7 @@ func getHostnames(containers map[string]Container, config LabelConfig) (hostname
133 114
 	return
134 115
 }
135 116
 
136
-func addAlternatives(hostname Hostname, alternatives []string) {
117
+func addAlternatives(hostname model.Hostname, alternatives []string) {
137 118
 	for _, alternative := range alternatives {
138 119
 		hostname.Alternatives[alternative] = true
139 120
 	}

+ 32
- 0
model/model.go View File

@@ -0,0 +1,32 @@
1
+package model
2
+
3
+// Container models a docker container that is running on the system.
4
+type Container struct {
5
+	Id     string
6
+	Name   string
7
+	Labels map[string]string
8
+}
9
+
10
+// LabelConfig describes the labels used for various properties.
11
+type LabelConfig struct {
12
+	Hostnames string
13
+}
14
+
15
+// Hostname describes a DNS name used for proxying, retrieving certificates, etc.
16
+type Hostname struct {
17
+	Name         string
18
+	Alternatives map[string]bool
19
+	Containers   []Container
20
+}
21
+
22
+// Config is the user-definable configuration for Dotege.
23
+type Config struct {
24
+	Templates []TemplateConfig
25
+	Labels    LabelConfig
26
+}
27
+
28
+// TemplateConfig configures a single template for the generator.
29
+type TemplateConfig struct {
30
+	Source      string
31
+	Destination string
32
+}

+ 6
- 10
template_generator.go View File

@@ -1,6 +1,7 @@
1 1
 package main
2 2
 
3 3
 import (
4
+	"github.com/csmith/dotege/model"
4 5
 	"go.uber.org/zap"
5 6
 	"io/ioutil"
6 7
 	"path"
@@ -10,23 +11,18 @@ import (
10 11
 )
11 12
 
12 13
 type Context struct {
13
-	Containers map[string]Container
14
-	Hostnames map[string]Hostname
15
-}
16
-
17
-type TemplateConfig struct {
18
-	Source      string
19
-	Destination string
14
+	Containers map[string]model.Container
15
+	Hostnames  map[string]model.Hostname
20 16
 }
21 17
 
22 18
 type Template struct {
23
-	config   TemplateConfig
19
+	config   model.TemplateConfig
24 20
 	content  string
25 21
 	template *template.Template
26 22
 }
27 23
 
28 24
 type TemplateGenerator struct {
29
-	logger *zap.SugaredLogger
25
+	logger    *zap.SugaredLogger
30 26
 	templates []*Template
31 27
 }
32 28
 
@@ -47,7 +43,7 @@ func NewTemplateGenerator(logger *zap.SugaredLogger) *TemplateGenerator {
47 43
 	}
48 44
 }
49 45
 
50
-func (t *TemplateGenerator) AddTemplate(config TemplateConfig) {
46
+func (t *TemplateGenerator) AddTemplate(config model.TemplateConfig) {
51 47
 	t.logger.Infof("Adding template from %s, writing to %s", config.Source, config.Destination)
52 48
 	tmpl, err := template.New(path.Base(config.Source)).Funcs(funcMap).ParseFiles(config.Source)
53 49
 	if err != nil {

Loading…
Cancel
Save