Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
94 lines (79 loc) · 1.92 KB
/
Copy pathmain.go
File metadata and controls
94 lines (79 loc) · 1.92 KB
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
package dockersync
import (
"context"
"time"
"github.com/Altinity/docker-sync/config"
"github.com/Altinity/docker-sync/internal/sync"
"github.com/Altinity/docker-sync/internal/telemetry"
"github.com/Altinity/docker-sync/structs"
"github.com/rs/zerolog/log"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.uber.org/multierr"
)
func Run(ctx context.Context) error {
if config.TelemetryEnabled.Bool() {
go func() {
if err := telemetry.Start(ctx); err != nil {
log.Error().
Err(err).
Msg("Failed to start telemetry")
}
}()
}
images := config.SyncImages.Images()
for {
if err := RunOnce(ctx, images); err != nil {
return err
}
dur := config.SyncInterval.Duration()
log.Info().Dur("interval", dur).Msg("Waiting for next sync")
time.Sleep(dur)
}
}
func RunOnce(ctx context.Context, images []*structs.Image) error {
var merr error
for k := range images {
telemetry.MonitoredImages.Record(ctx, int64(len(images)))
select {
case <-ctx.Done():
return nil
default:
image := images[k]
// Initialize telemetry for the image
telemetry.ImageSyncErrors.Add(ctx, 0,
metric.WithAttributes(
attribute.KeyValue{
Key: "image",
Value: attribute.StringValue(image.Source),
},
),
)
if err := sync.SyncImage(ctx, image); err != nil {
log.Error().
Err(err).
Str("source", image.Source).
Msg("Failed to sync image")
telemetry.ImageSyncErrors.Add(ctx, 1,
metric.WithAttributes(
attribute.KeyValue{
Key: "image",
Value: attribute.StringValue(image.Source),
},
attribute.KeyValue{
Key: "error",
Value: attribute.StringValue(err.Error()),
},
),
)
merr = multierr.Append(merr, err)
if config.SyncMaxErrors.Int() > 0 {
if len(multierr.Errors(merr)) >= config.SyncMaxErrors.Int() {
return merr
}
}
}
}
}
return nil
}