From ee5ef22d42c9dba0285c3b7e455184a1e7c9b861 Mon Sep 17 00:00:00 2001 From: Alan Clucas Date: Tue, 18 Aug 2020 13:31:54 +0100 Subject: [PATCH 1/2] Allow configuration of git author, email and check interval --- README.md | 3 +++ cmd/execute.go | 3 ++- cmd/flags.go | 15 +++++++++++ pkg/store/git/git.go | 53 +++++++++++++++++---------------------- pkg/store/git/git_test.go | 4 +-- 5 files changed, 45 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index cf0ae11..31d3fe9 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,7 @@ Available Commands: Flags: -s, --api-server string Kubernetes api-server url + -j, --check-interval int Check interval in seconds (default 10) -c, --config string Configuration file (default "/etc/katafygio/katafygio.yaml") -q, --context string Kubernetes configuration context -d, --dry-run Dry-run mode: don't store anything @@ -67,6 +68,8 @@ Flags: -x, --exclude-kind strings Ressource kind to exclude. Eg. 'deployment' -y, --exclude-object strings Object to exclude. Eg. 'configmap:kube-system/kube-dns' -l, --filter string Label filter. Select only objects matching the label + -b, --git-author string Author for git commits (default "Katafygio") + -f, --git-email string Email address for git commits (default "katafygio@localhost") -t, --git-timeout duration Git operations timeout (default 5m0s) -g, --git-url string Git repository URL -p, --healthcheck-port int Port for answering healthchecks on /health url diff --git a/cmd/execute.go b/cmd/execute.go index fb75aee..3f32f62 100644 --- a/cmd/execute.go +++ b/cmd/execute.go @@ -6,6 +6,7 @@ import ( "os/signal" "path/filepath" "syscall" + "time" "github.com/spf13/afero" "github.com/spf13/cobra" @@ -63,7 +64,7 @@ func runE(cmd *cobra.Command, args []string) (err error) { var repo *git.Store if !noGit { - repo, err = git.New(logger, dryRun, localDir, gitURL, gitTimeout).Start() + repo, err = git.New(logger, dryRun, localDir, gitURL, gitAuthor, gitEmail, gitTimeout, time.Duration(checkInt)*time.Second).Start() } if err != nil { return fmt.Errorf("failed to start git repo handler: %v", err) diff --git a/cmd/flags.go b/cmd/flags.go index a662d42..3f2c40b 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -25,9 +25,12 @@ var ( gitTimeout time.Duration healthP int resyncInt int + checkInt int exclkind []string exclobj []string noGit bool + gitAuthor string + gitEmail string ) func bindPFlag(key string, cmd string) { @@ -94,8 +97,17 @@ func init() { RootCmd.PersistentFlags().IntVarP(&resyncInt, "resync-interval", "i", 900, "Full resync interval in seconds (0 to disable)") bindPFlag("resync-interval", "resync-interval") + RootCmd.PersistentFlags().IntVarP(&checkInt, "check-interval", "j", 10, "Check interval in seconds") + bindPFlag("check-interval", "check-interval") + RootCmd.PersistentFlags().BoolVarP(&noGit, "no-git", "n", false, "Don't version with git") bindPFlag("no-git", "no-git") + + RootCmd.PersistentFlags().StringVarP(&gitAuthor, "git-author", "b", "Katafygio", "Author for git commits") + bindPFlag("git-author", "git-author") + + RootCmd.PersistentFlags().StringVarP(&gitEmail, "git-email", "f", "katafygio@localhost", "Email address for git commits") + bindPFlag("git-email", "git-email") } // for whatever the reason, viper don't auto bind values from config file so we have to tell him @@ -115,7 +127,10 @@ func bindConf(cmd *cobra.Command, args []string) { gitTimeout = viper.GetDuration("git-timeout") healthP = viper.GetInt("healthcheck-port") resyncInt = viper.GetInt("resync-interval") + checkInt = viper.GetInt("check-interval") exclkind = viper.GetStringSlice("exclude-kind") exclobj = viper.GetStringSlice("exclude-object") noGit = viper.GetBool("no-git") + gitAuthor = viper.GetString("git-author") + gitEmail = viper.GetString("git-email") } diff --git a/pkg/store/git/git.go b/pkg/store/git/git.go index 434e9c6..9ba09a5 100644 --- a/pkg/store/git/git.go +++ b/pkg/store/git/git.go @@ -12,15 +12,6 @@ import ( ) var ( - // CheckInterval defines the interval between local directory checks - CheckInterval = 10 * time.Second - - // GitAuthor is the name of the commiter - GitAuthor = "Katafygio" - - // GitEmail is the email of the commiter - GitEmail = "katafygio@localhost" - // GitMsg is the commit message we'll use GitMsg = "Kubernetes cluster change" ) @@ -34,35 +25,37 @@ type logger interface { // Store will maintain a git repository off dumped kube objects type Store struct { - Logger logger - LocalDir string - URL string - Timeout time.Duration - Author string - Email string - Msg string - DryRun bool - stopch chan struct{} - donech chan struct{} + Logger logger + LocalDir string + URL string + Timeout time.Duration + CheckInterval time.Duration + Author string + Email string + Msg string + DryRun bool + stopch chan struct{} + donech chan struct{} } // New instantiate a new git Store. url is optional. -func New(log logger, dryRun bool, dir, url string, timeout time.Duration) *Store { +func New(log logger, dryRun bool, dir, url string, author string, email string, timeout time.Duration, checkInterval time.Duration) *Store { return &Store{ - Logger: log, - LocalDir: dir, - URL: url, - Timeout: timeout, - Author: GitAuthor, - Email: GitEmail, - Msg: GitMsg, - DryRun: dryRun, + Logger: log, + LocalDir: dir, + URL: url, + Timeout: timeout, + CheckInterval: checkInterval, + Author: author, + Email: email, + Msg: GitMsg, + DryRun: dryRun, } } // Start maintains a directory content committed func (s *Store) Start() (*Store, error) { - s.Logger.Infof("Starting git repository synchronizer") + s.Logger.Infof(fmt.Sprintf("Starting git repository synchronizer every %s", s.CheckInterval.String())) s.stopch = make(chan struct{}) s.donech = make(chan struct{}) @@ -72,7 +65,7 @@ func (s *Store) Start() (*Store, error) { } go func() { - checkTick := time.NewTicker(CheckInterval) + checkTick := time.NewTicker(s.CheckInterval) defer checkTick.Stop() defer close(s.donech) diff --git a/pkg/store/git/git_test.go b/pkg/store/git/git_test.go index af6e9c3..9d31d38 100644 --- a/pkg/store/git/git_test.go +++ b/pkg/store/git/git_test.go @@ -35,7 +35,7 @@ func TestGitDryRun(t *testing.T) { appFs = afero.NewMemMapFs() - repo, err := New(new(mockLog), true, "/tmp/ktest", "", timeout).Start() + repo, err := New(new(mockLog), true, "/tmp/ktest", "", "test", "test@test", timeout, 10*time.Second).Start() if err != nil { t.Errorf("failed to start git: %v", err) } @@ -62,7 +62,7 @@ func TestGit(t *testing.T) { defer os.RemoveAll(dir) - repo, err := New(new(mockLog), false, dir, "", timeout).Start() + repo, err := New(new(mockLog), false, dir, "", "test", "test@test", timeout, 10*time.Second).Start() if err != nil { t.Errorf("failed to start git: %v", err) } From 542a0e767e7076f2b1542414201432d0e96848e6 Mon Sep 17 00:00:00 2001 From: Alan Clucas Date: Tue, 18 Aug 2020 14:12:23 +0100 Subject: [PATCH 2/2] Add git config to helm chart --- assets/helm-chart/katafygio/Chart.yaml | 2 +- assets/helm-chart/katafygio/templates/deployment.yaml | 9 +++++++++ assets/helm-chart/katafygio/values.yaml | 9 +++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/assets/helm-chart/katafygio/Chart.yaml b/assets/helm-chart/katafygio/Chart.yaml index 78aaf3c..0771469 100644 --- a/assets/helm-chart/katafygio/Chart.yaml +++ b/assets/helm-chart/katafygio/Chart.yaml @@ -5,7 +5,7 @@ name: katafygio home: https://github.com/bpineau/katafygio sources: - https://github.com/bpineau/katafygio -version: 0.4.3 +version: 0.4.4 keywords: - backup - dump diff --git a/assets/helm-chart/katafygio/templates/deployment.yaml b/assets/helm-chart/katafygio/templates/deployment.yaml index b092995..c870094 100644 --- a/assets/helm-chart/katafygio/templates/deployment.yaml +++ b/assets/helm-chart/katafygio/templates/deployment.yaml @@ -62,6 +62,15 @@ spec: - --exclude-object={{ . }} {{- end }} {{- end }} + {{- if .Values.gitAuthor }} + - --git-author={{ .Values.gitAuthor }} + {{- end }} + {{- if .Values.gitEmail }} + - --git-email={{ .Values.gitEmail }} + {{- end }} + {{- if .Values.checkInterval }} + - --check-interval={{ .Values.checkInterval }} + {{- end }} ports: - name: http containerPort: {{ .Values.healthcheckPort }} diff --git a/assets/helm-chart/katafygio/values.yaml b/assets/helm-chart/katafygio/values.yaml index d01943b..fb2ea83 100644 --- a/assets/helm-chart/katafygio/values.yaml +++ b/assets/helm-chart/katafygio/values.yaml @@ -23,6 +23,15 @@ gitSshConfig: | StrictHostKeyChecking no UserKnownHostsFile /dev/null +# gitAuthor (optional) The author name used for git commits +gitAuthor: "Katafygio" + +# gitEmail (optional) The email address used for git commits +gitEmail: "katafygio@localhost" + +# checkInterval (optional) How long in seconds at minimum between git commits +checkInterval: 10 + # healthcheckPort is the TCP port Katafygio will listen for health check requests. healthcheckPort: 8080