-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
440 lines (373 loc) · 11 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"reflect"
"sync"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/client"
"github.com/huxcrux/docker-manager/pkg/config"
"github.com/huxcrux/docker-manager/pkg/docker"
"github.com/huxcrux/docker-manager/pkg/metrics"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
// Global variable
var (
cfg *config.Config
cfgMu sync.RWMutex
)
func updateConfig() error {
newcfg, err := config.Read()
if err != nil {
return fmt.Errorf("error reading config: %v", err)
}
log.Debugf("New config: %+v", newcfg)
// Use the mutex to prevent race conditions
cfgMu.Lock()
cfg = newcfg
cfgMu.Unlock()
log.Info("Config reloaded")
return nil
}
// isContainerUpToDate checks if a running container is using the latest available image
func isContainerUpToDate(cli *client.Client, containerID string, config docker.ContainerConfig) (bool, error) {
ctx := context.Background()
// Get the running container's image ID
inspect, err := cli.ContainerInspect(ctx, containerID)
if err != nil {
return false, err
}
runningImageID := inspect.Image
// Pull the latest image
reader, err := cli.ImagePull(ctx, config.Image, image.PullOptions{})
if err != nil {
return false, err
}
defer reader.Close()
// Consume the reader to complete the image pull
_, _ = io.Copy(io.Discard, reader)
// Get the latest image ID
images, err := cli.ImageList(ctx, image.ListOptions{})
if err != nil {
return false, err
}
var latestImageID string
for _, img := range images {
for _, tag := range img.RepoTags {
if tag == config.Image {
latestImageID = img.ID
break
}
}
}
if latestImageID == "" {
return false, fmt.Errorf("could not find the latest image for %s", config.Image)
}
// Compare the image IDs
result := runningImageID == latestImageID
if result {
log.Debugf("Container %s is up to date\n", config.Name)
} else {
log.Debugf("Container %s is not up to date\n", config.Name)
}
// Compare the image IDs
return result, nil
}
// ensureContainerConfig checks if a running container matches the given ContainerConfig and recreates it if necessary
func ensureContainerConfig(cli *client.Client, config docker.ContainerConfig) error {
ctx := context.Background()
containers, err := cli.ContainerList(ctx, container.ListOptions{All: true})
if err != nil {
return err
}
for _, container := range containers {
if container.Names[0] == "/"+config.Name {
inspect, err := cli.ContainerInspect(ctx, container.ID)
if err != nil {
return err
}
// Validate container configuration
needsUpdate := false
// Check environment variables
// Some env vars is set by container. We need to match the ones we care about. Unclear how we track vars that is unset over time.
// Skipping for now and will return to this later on.
//if !reflect.DeepEqual(inspect.Config.Env, config.Env) {
// log.Debugf("Container %s environment does not match\n", config.Name)
// needsUpdate = true
//}
// Check port bindings
if !reflect.DeepEqual(inspect.Config.ExposedPorts, config.ExposedPorts) {
log.Debugf("Container %s exposed ports do not match\n", config.Name)
needsUpdate = true
}
if !reflect.DeepEqual(inspect.HostConfig.PortBindings, config.PortBindings) {
log.Debugf("Container %s port bindings do not match\n", config.Name)
needsUpdate = true
}
// Check image
if !reflect.DeepEqual(inspect.Config.Image, config.Image) {
log.Debugf("Container %s image does not match\n", config.Name)
needsUpdate = true
}
// Check command
if config.Cmd != nil {
if !reflect.DeepEqual(inspect.Config.Cmd, config.Cmd) {
log.Debugf("Container %s command does not match\n", config.Name)
needsUpdate = true
}
}
if needsUpdate {
log.Infof("Container %s configuration does not match, recreating it...\n", config.Name)
err = docker.DeleteContainer(cli, container.ID)
if err != nil {
return err
}
// create container with the correct configuration
err, created := docker.CreateContainer(cli, config)
if err != nil {
return err
}
if created {
log.Infof("Container %s recreated with the correct configuration\n", config.Name)
}
} else {
log.Debugf("Config for container %s already up to date\n", config.Name)
}
return nil
}
}
log.Infof("Container %s not found, creating it...\n", config.Name)
_, err = cli.ContainerCreate(ctx, &container.Config{
Image: config.Image,
ExposedPorts: config.ExposedPorts,
Env: config.Env,
Cmd: config.Cmd,
}, &container.HostConfig{
PortBindings: config.PortBindings,
}, nil, nil, config.Name)
if err != nil {
return err
}
return nil
}
// createContainers creates multiple Docker containers based on the provided configurations
func ensureContainers(cli *client.Client, desierdContainers []docker.ContainerConfig, updateCheck bool) error {
// get running containers
runningContainers, err := docker.ListAllContariners(cli)
if err != nil {
return err
}
for _, container := range desierdContainers {
// check if container already exists
found := false
if len(runningContainers) > 0 {
for _, runningContainer := range runningContainers {
if runningContainer.Names[0] == "/"+container.Name {
log.Debugf("Container %s already exists\n", container.Name)
found = true
break
}
}
}
// Create container if not found
var created bool
if !found {
err, created = docker.CreateContainer(cli, container)
if err != nil {
return err
}
if created {
log.Infof("Container %s created", container.Name)
}
}
if !created {
err = ensureContainerConfig(cli, container)
if err != nil {
log.Fatalf("Error ensuring container configuration: %v", err)
}
}
// Get cintainer ID from name
ctid, err := docker.GetContainerIDByName(cli, container.Name)
if err != nil {
return err
}
// Check if container is up to date
if updateCheck && !created {
upToDate, err := isContainerUpToDate(cli, ctid, container)
if err != nil {
return err
}
if !upToDate {
log.Infof("Container %v is not up to date, recreating ...\n", container.Name)
err = docker.DeleteContainer(cli, ctid)
if err != nil {
return err
}
err, _ := docker.CreateContainer(cli, container)
if err != nil {
return err
}
// Fetch new container ID
ctid, err = docker.GetContainerIDByName(cli, container.Name)
if err != nil {
return err
}
}
}
// Ensure container is running
err = docker.EnsureRunningContainers(cli, ctid)
if err != nil {
return err
}
log.Infof("Container %v ensured\n", container.Name)
}
return nil
}
func removeUnwantedContainers(cli *client.Client, configs []docker.ContainerConfig) error {
// get running containers
containers, err := docker.ListAllContariners(cli)
if err != nil {
return err
}
// check if container is not specified in configs
for _, container := range containers {
found := false
for _, config := range configs {
if container.Names[0] == "/"+config.Name {
found = true
break
}
}
if !found {
log.Infof("Container %s (%s) not desired, removing ...\n", container.Names[0], container.ID)
err = docker.DeleteContainer(cli, container.ID)
if err != nil {
return err
}
log.Debug("Container removed\n")
}
}
return nil
}
// Handler to update metrics and then serve Prometheus metrics
func GenerateMetrics(dm *metrics.DockerMetrics, cli *client.Client) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// List all containers
containers, err := docker.ListAllContariners(cli)
if err != nil {
http.Error(w, "Could not list containers", http.StatusInternalServerError)
return
}
var wg sync.WaitGroup
statsChan := make(chan types.StatsJSON, len(containers))
errChan := make(chan error, len(containers))
// Fetch stats for each container concurrently
for _, container := range containers {
wg.Add(1)
go func(containerID string) {
defer wg.Done()
stats, err := cli.ContainerStats(context.Background(), containerID, false)
//cli.ContainerStatsOneShot(context.Background(), containerID)
if err != nil {
errChan <- fmt.Errorf("could not fetch stats for container %s: %v", containerID, err)
return
}
defer stats.Body.Close()
data, err := io.ReadAll(stats.Body)
if err != nil {
errChan <- fmt.Errorf("could not read stats for container %s: %v", containerID, err)
}
var statsJSON types.StatsJSON
err = json.Unmarshal(data, &statsJSON)
if err != nil {
errChan <- fmt.Errorf("could not unmarshal stats for container %s: %v", containerID, err)
}
log.Infof("Updated metrics for container %s\n", containerID)
statsChan <- statsJSON
}(container.ID)
}
// Wait for all goroutines to finish
go func() {
wg.Wait()
close(statsChan)
close(errChan)
}()
// Process results
for statsJSON := range statsChan {
dm.UpdateMetrics(statsJSON)
}
// Handle errors
if len(errChan) > 0 {
var errorMsgs []string
for err := range errChan {
errorMsgs = append(errorMsgs, err.Error())
}
http.Error(w, fmt.Sprintf("Errors occurred: %v", errorMsgs), http.StatusInternalServerError)
return
}
// Serve Prometheus metrics
promhttp.Handler().ServeHTTP(w, r)
})
}
func reconcileContainers(cli *client.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
containers, err := config.ConfigToDockerConfig(*cfg)
if err != nil {
log.Fatalf("Error converting config to Docker config: %v", err)
}
// Delete unwanted containers
if cfg.AppConfig.RemoveUnwantedContainers {
err = removeUnwantedContainers(cli, containers)
if err != nil {
log.Fatalf("Error when removing unwanted containers: %v", err)
}
}
// Create containers and ensure they are up to date
err = ensureContainers(cli, containers, cfg.AppConfig.UpdateCheck)
if err != nil {
log.Fatalf("Error ensuring containers: %v", err)
}
fmt.Fprint(w, "Containers reconciled\n")
}
}
func reloadConfig() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
err := updateConfig()
if err != nil {
log.Fatalf("Error reloading config: %v", err)
}
fmt.Fprint(w, "Config reloaded\n")
}
}
func init() {
// read config
err := updateConfig()
if err != nil {
log.Fatalf("Error reading config: %v", err)
}
}
func main() {
// if debug is enabled, set log level to debug
if cfg.AppConfig.Debug {
log.SetLevel(log.DebugLevel)
}
// Create client
cli, err := docker.CreateClient()
if err != nil {
log.Fatalf("Error creating Docker client: %v", err)
}
// init metrics
metrics := metrics.NewDockerMetrics()
// Expose metrics via HTTP
http.Handle("/metrics", GenerateMetrics(metrics, cli))
http.Handle("/update", reconcileContainers(cli))
http.Handle("/reload", reloadConfig())
fmt.Println("Beginning to serve on port :8082")
http.ListenAndServe(":8082", nil)
}