Skip to content

Commit 9d3f146

Browse files
authored
Introduce option for image expiration grace period and fix log-level. (#5)
1 parent 96421e9 commit 9d3f146

File tree

4 files changed

+37
-37
lines changed

4 files changed

+37
-37
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ release:: all;
1717
.PHONY: start
1818
start: all
1919
bin/metal-image-cache-sync \
20+
# --log-level debug \
2021
--metal-api-endpoint $(METAL_API_ENDPOINT) \
2122
--metal-api-hmac $(METAL_API_HMAC) \
2223
--max-cache-size 10G \

cmd/internal/determine-sync-images/lister.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,20 @@ import (
1717
)
1818

1919
type SyncLister struct {
20-
logger *zap.SugaredLogger
21-
driver *metalgo.Driver
22-
config *api.Config
23-
s3 *s3.S3
24-
excludePaths []string
25-
collector *metrics.Collector
20+
logger *zap.SugaredLogger
21+
driver *metalgo.Driver
22+
config *api.Config
23+
s3 *s3.S3
24+
collector *metrics.Collector
2625
}
2726

2827
func NewSyncLister(logger *zap.SugaredLogger, driver *metalgo.Driver, s3 *s3.S3, collector *metrics.Collector, config *api.Config) *SyncLister {
2928
return &SyncLister{
30-
logger: logger,
31-
driver: driver,
32-
config: config,
33-
s3: s3,
34-
excludePaths: config.ExcludePaths,
35-
collector: collector,
29+
logger: logger,
30+
driver: driver,
31+
config: config,
32+
s3: s3,
33+
collector: collector,
3634
}
3735
}
3836

@@ -49,10 +47,12 @@ func (s *SyncLister) DetermineSyncList() ([]api.OS, error) {
4947

5048
s.collector.SetMetalAPIImageCount(len(resp.Image))
5149

50+
expirationGraceDays := 24 * time.Hour * time.Duration(s.config.ExpirationGraceDays)
51+
5252
images := api.OSImagesByOS{}
5353
for _, img := range resp.Image {
5454
skip := false
55-
for _, exclude := range s.excludePaths {
55+
for _, exclude := range s.config.ExcludePaths {
5656
if strings.Contains(img.URL, exclude) {
5757
skip = true
5858
break
@@ -65,7 +65,7 @@ func (s *SyncLister) DetermineSyncList() ([]api.OS, error) {
6565
}
6666

6767
if img.ExpirationDate != nil {
68-
if time.Since(time.Time(*img.ExpirationDate)) > 0 {
68+
if time.Since(time.Time(*img.ExpirationDate)) > expirationGraceDays {
6969
s.logger.Debugw("not considering expired image, skipping", "id", *img.ID)
7070
continue
7171
}

cmd/main.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ import (
3333
const (
3434
moduleName = "metal-image-cache-sync"
3535
cfgFileType = "yaml"
36-
37-
logLevelFlg = "log-level"
3836
)
3937

4038
var (
@@ -52,8 +50,8 @@ var rootCmd = &cobra.Command{
5250
SilenceErrors: true,
5351
SilenceUsage: true,
5452
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
55-
initLogging()
5653
initConfig()
54+
initLogging()
5755
initSignalHandlers()
5856
return nil
5957
},
@@ -74,6 +72,7 @@ func main() {
7472

7573
func init() {
7674
rootCmd.Flags().String("bind-address", "127.0.0.1:3000", "http server bind address")
75+
rootCmd.Flags().String("log-level", "info", "sets the application log level")
7776

7877
rootCmd.Flags().String("image-store", "metal-stack.io", "url to the image store")
7978
rootCmd.Flags().String("image-store-bucket", "images", "bucket of the image store")
@@ -88,6 +87,8 @@ func init() {
8887
rootCmd.Flags().Int("min-images-per-name", 3, "minimum amount of images to keep of an image variant")
8988
rootCmd.Flags().Int("max-images-per-name", -1, "maximum amount of images to cache for an image variant, unlimited if less than zero")
9089

90+
rootCmd.Flags().Uint("expiration-grace-period", 0, "the amount of days to still sync images even if they have already expired in the metal-api (defaults to zero)")
91+
9192
rootCmd.Flags().String("root-path", "/var/lib/metal-image-cache-sync/images", "root path of where to store the images")
9293
rootCmd.Flags().StringSlice("excludes", []string{"/pull_requests/"}, "url paths to exclude from the sync")
9394

@@ -100,8 +101,8 @@ func init() {
100101
func initLogging() {
101102
level := zap.InfoLevel
102103

103-
if viper.IsSet(logLevelFlg) {
104-
err := level.UnmarshalText([]byte(viper.GetString(logLevelFlg)))
104+
if viper.IsSet("log-level") {
105+
err := level.UnmarshalText([]byte(viper.GetString("log-level")))
105106
if err != nil {
106107
log.Fatalf("can't initialize zap logger: %v", err)
107108
}
@@ -128,7 +129,7 @@ func initConfig() {
128129
if cfgFile != "" {
129130
viper.SetConfigFile(cfgFile)
130131
if err := viper.ReadInConfig(); err != nil {
131-
logger.Fatalw("config file path set explicitly, but unreadable", "error", err)
132+
log.Fatalf("config file path set explicitly, but unreadable: %v", err)
132133
}
133134
} else {
134135
viper.SetConfigName("config")
@@ -138,15 +139,10 @@ func initConfig() {
138139
if err := viper.ReadInConfig(); err != nil {
139140
usedCfg := viper.ConfigFileUsed()
140141
if usedCfg != "" {
141-
logger.Fatalw("config file unreadable", "config-file", usedCfg, "error", err)
142+
log.Fatalf("config file %s unreadable: %v", usedCfg, err)
142143
}
143144
}
144145
}
145-
146-
usedCfg := viper.ConfigFileUsed()
147-
if usedCfg != "" {
148-
logger.Infow("read config file", "config-file", usedCfg)
149-
}
150146
}
151147

152148
func initSignalHandlers() {

pkg/api/config.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,24 @@ type Config struct {
2727
SyncSchedule string `validate:"required"`
2828
DryRun bool
2929
ExcludePaths []string
30+
31+
ExpirationGraceDays uint
3032
}
3133

3234
func NewConfig() (*Config, error) {
3335
c := &Config{
34-
BindAddress: viper.GetString("bind-address"),
35-
ImageCacheRootPath: viper.GetString("root-path"),
36-
MinImagesPerName: viper.GetInt("min-images-per-name"),
37-
MaxImagesPerName: viper.GetInt("max-images-per-name"),
38-
ImageStore: viper.GetString("image-store"),
39-
ImageBucket: viper.GetString("image-store-bucket"),
40-
MetalAPIEndpoint: viper.GetString("metal-api-endpoint"),
41-
MetalAPIHMAC: viper.GetString("metal-api-hmac"),
42-
SyncSchedule: viper.GetString("schedule"),
43-
DryRun: viper.GetBool("dry-run"),
44-
ExcludePaths: viper.GetStringSlice("excludes"),
36+
BindAddress: viper.GetString("bind-address"),
37+
ImageCacheRootPath: viper.GetString("root-path"),
38+
MinImagesPerName: viper.GetInt("min-images-per-name"),
39+
MaxImagesPerName: viper.GetInt("max-images-per-name"),
40+
ImageStore: viper.GetString("image-store"),
41+
ImageBucket: viper.GetString("image-store-bucket"),
42+
MetalAPIEndpoint: viper.GetString("metal-api-endpoint"),
43+
MetalAPIHMAC: viper.GetString("metal-api-hmac"),
44+
SyncSchedule: viper.GetString("schedule"),
45+
DryRun: viper.GetBool("dry-run"),
46+
ExcludePaths: viper.GetStringSlice("excludes"),
47+
ExpirationGraceDays: viper.GetUint("expiration-grace-period"),
4548
}
4649

4750
var err error

0 commit comments

Comments
 (0)