Skip to content

Commit

Permalink
Merge pull request #1 from zvlb/restruct
Browse files Browse the repository at this point in the history
Fixes and restruct
  • Loading branch information
zvlb authored Sep 27, 2022
2 parents 870b781 + b2d08e2 commit aaa4bb9
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 109 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
### Added

### Release v0.1.0
- Cleanup: restruct code
- Cleanup: migrate from vzemtsov to zvlb repo
- Fix: fix error with Unarchive

### Release v0.0.4
- Fix: fix error in init mode
- Logging: Add unarchive logs
Expand Down
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ export GO111MODULE := on
GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)

ORG := github.com/vzemtsov
ORG := github.com/zvlb
REPOPATH ?= $(ORG)/config-reloader
DOCKER_IMAGE_NAME ?= vlzemtsov/config-reloader
DOCKER_IMAGE_NAME ?= zvlb/config-reloader
DOCKER_IMAGE_TAG ?= develop
BINARY=config-reloader

LDFLAGS := -extldflags '-static'

.PHONY: build-local
build: clean
build-local: clean
GOARCH=$(GOARCH) GOOS=$(GOOS) CGO_ENABLED=0 \
go build -ldflags="$(LDFLAGS)" -o out/$(BINARY) cmd/app/main.go
go build -ldflags="$(LDFLAGS)" -o out/$(BINARY) cmd/configreloader/main.go

.PHONY: build
build: clean
GOARCH=amd64 GOOS=linux CGO_ENABLED=0 \
go build -ldflags="$(LDFLAGS)" -o out/$(BINARY) cmd/app/main.go
go build -ldflags="$(LDFLAGS)" -o out/$(BINARY) cmd/configreloader/main.go

.PHONY: run
run: build-local
Expand Down
2 changes: 1 addition & 1 deletion OWNERS
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
owners:
- vzemtsov
- zvlb
9 changes: 4 additions & 5 deletions cmd/app/main.go → cmd/configreloader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ package main
import (
"log"

"github.com/vzemtsov/config-reloader/config"
"github.com/vzemtsov/config-reloader/internal/app"
"github.com/vzemtsov/config-reloader/pkg/metrics"
"github.com/zvlb/config-reloader/pkg/configreloader"
"github.com/zvlb/config-reloader/pkg/metrics"
)

func main() {
cfg, err := config.New()
cfg, err := configreloader.New()
if err != nil {
log.Fatalln(err)
}

err = app.Run(cfg)
err = cfg.Run()
if err != nil {
log.Fatalln(err)
}
Expand Down
71 changes: 0 additions & 71 deletions config/config.go

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/vzemtsov/config-reloader
module github.com/zvlb/config-reloader

go 1.19

Expand Down
74 changes: 48 additions & 26 deletions internal/app/app.go → pkg/configreloader/configreloader.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package app
package configreloader

import (
"compress/gzip"
"flag"
"fmt"
"io"
"io/ioutil"
Expand All @@ -13,37 +14,55 @@ import (
"time"

fsnotify "github.com/fsnotify/fsnotify"
"github.com/vzemtsov/config-reloader/config"
"github.com/vzemtsov/config-reloader/pkg/metrics"
"github.com/zvlb/config-reloader/pkg/metrics"
)

func Run(cfg *config.Config) error {
func New() (*ConfigReloader, error) {
cfg := &ConfigReloader{}
cfg.InitMode = flag.Bool("init-mode", false, "Init mode for unarchive files. Works only if volume-dir-archive exist. Default - false")
cfg.DirForUnarchive = flag.String("dir-for-unarchive", "/tmp/test-unarchive", "Directory where the archives will be unpacked")
cfg.Webhook.Method = flag.String("webhook-method", "POST", "the HTTP method url to use to send the webhook")
cfg.Webhook.StatusCode = flag.Int("webhook-status-code", 200, "the HTTP status code indicating successful triggering of reload")
cfg.Webhook.Retries = flag.Int("webhook-retries", 1, "the amount of times to retry the webhook reload request")

err := checks(cfg)
flag.Var(&cfg.VolumeDirs, "volume-dir", "the config map volume directory to watch for updates; may be used multiple times")
flag.Var(&cfg.VolumeDirsArchive, "volume-dir-archive", "the config map volume directory to watch for updates and unarchiving; may be used multiple times")
flag.Var(&cfg.Webhook.Urls, "webhook-url", "the url to send a request to when the specified config map volume directory has been updated")
flag.Parse()

return cfg, nil
}

func (cfg *ConfigReloader) Run() error {

err := cfg.checks()
if err != nil {
return err
}

if len(cfg.VolumeDirs) > 0 {
volumeDirWatcher(cfg)
err := cfg.volumeDirWatcher()
if err != nil {
return err
}
}
if len(cfg.VolumeDirsArchive) > 0 {
for _, vda := range cfg.VolumeDirsArchive {
fmt.Printf("VDA: %s\n", vda)
unarchiveDir(vda, cfg)
// fmt.Printf("VDA: %s\n", vda)
cfg.unarchiveDir(vda)
}
if *cfg.InitMode {
log.Println("Init mode completed")
return nil
}
volumeDirArchiveWatcher(cfg)
cfg.volumeDirArchiveWatcher()
}

return nil

}

func volumeDirWatcher(cfg *config.Config) error {
func (cfg *ConfigReloader) volumeDirWatcher() error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
Expand All @@ -63,7 +82,7 @@ func volumeDirWatcher(cfg *config.Config) error {
}

log.Println("ConfigMap or Secret updated")
sendWebHook(cfg)
cfg.sendWebHook()
case err, ok := <-watcher.Errors:
if !ok {
continue
Expand All @@ -81,11 +100,10 @@ func volumeDirWatcher(cfg *config.Config) error {
return err
}
}

return nil
}

func volumeDirArchiveWatcher(cfg *config.Config) error {
func (cfg *ConfigReloader) volumeDirArchiveWatcher() error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
Expand All @@ -104,13 +122,13 @@ func volumeDirArchiveWatcher(cfg *config.Config) error {
continue
}

err := unarchiveDir(event.Name, cfg)
err := cfg.unarchiveDir(event.Name)
if err != nil {
log.Println("Error:", err)
}

log.Println("ConfigMap or Secret updated")
sendWebHook(cfg)
cfg.sendWebHook()
case err, ok := <-watcher.Errors:
if !ok {
continue
Expand All @@ -133,7 +151,7 @@ func volumeDirArchiveWatcher(cfg *config.Config) error {
return nil
}

func checks(cfg *config.Config) error {
func (cfg *ConfigReloader) checks() error {
if (len(cfg.VolumeDirs) < 1) && (len(cfg.VolumeDirsArchive) < 1) {
return fmt.Errorf("%s", "Missing volume-dir or volume-dir-archive")
}
Expand Down Expand Up @@ -164,7 +182,7 @@ func isValidEvent(event fsnotify.Event) bool {
return true
}

func sendWebHook(cfg *config.Config) {
func (cfg *ConfigReloader) sendWebHook() {
for _, h := range cfg.Webhook.Urls {
begun := time.Now()
req, err := http.NewRequest(*cfg.Webhook.Method, h.String(), nil)
Expand Down Expand Up @@ -214,31 +232,35 @@ func sendWebHook(cfg *config.Config) {

}

func unarchiveDir(path string, cfg *config.Config) error {
kuberPath := path + "/..data"
files, err := ioutil.ReadDir(kuberPath)
func (cfg *ConfigReloader) unarchiveDir(path string) error {
// fmt.Println(path)
// kuberPath := path + "/..data"
// fmt.Println(kuberPath)
files, err := ioutil.ReadDir(path)
if err != nil {
return err
}

for _, file := range files {
if file.Name()[len(file.Name())-3:] != ".gz" {
continue
}
fullFilePath := path + "/" + file.Name()
fmt.Printf("file: %s", fullFilePath)
err := unarchiveFile(fullFilePath, cfg)
err := cfg.unarchiveFile(fullFilePath)
if err != nil {
return err
}
}
return nil
}

func unarchiveFile(path string, cfg *config.Config) error {
func (cfg *ConfigReloader) unarchiveFile(path string) error {
outFileName := *cfg.DirForUnarchive + "/" + filepath.Base(path)[0:len(filepath.Base(path))-3]
log.Printf("Unarhive file from %s to %s", path, outFileName)

if path[len(path)-3:] != ".gz" {
return fmt.Errorf("File %s is not a .gz archive. Do nothing", path)
}
// if path[len(path)-3:] != ".gz" {
// return fmt.Errorf("File %s is not a .gz archive. Do nothing", path)
// }

gzipFile, err := os.Open(path)
if err != nil {
Expand Down
54 changes: 54 additions & 0 deletions pkg/configreloader/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package configreloader

import (
"fmt"
"net/url"
)

type ConfigReloader struct {
InitMode *bool
VolumeDirs volumeDirsFlag
VolumeDirsArchive volumeDirsArchiveFlag
DirForUnarchive *string
Webhook Webhook
}

type Webhook struct {
Urls urlsFlag
Method *string
StatusCode *int
Retries *int
}

type volumeDirsFlag []string
type volumeDirsArchiveFlag []string
type urlsFlag []*url.URL

func (v *volumeDirsFlag) Set(value string) error {
*v = append(*v, value)
return nil
}
func (v *volumeDirsFlag) String() string {
return fmt.Sprint(*v)
}

func (v *volumeDirsArchiveFlag) Set(value string) error {
*v = append(*v, value)
return nil
}
func (v *volumeDirsArchiveFlag) String() string {
return fmt.Sprint(*v)
}

func (v *urlsFlag) Set(value string) error {
u, err := url.Parse(value)
if err != nil {
return fmt.Errorf("invalid URL: %v", err)
}
*v = append(*v, u)
return nil
}

func (v *urlsFlag) String() string {
return fmt.Sprint(*v)
}

0 comments on commit aaa4bb9

Please sign in to comment.