@@ -2,21 +2,26 @@ package main
22
33import (
44 "context"
5+ "errors"
56 "fmt"
7+ "io/ioutil"
68 "os"
79 "path/filepath"
810 "strings"
911 "sync"
12+ "text/template"
1013 "time"
1114
1215 "github.com/argoproj-labs/argocd-image-updater/pkg/argocd"
16+ "github.com/argoproj-labs/argocd-image-updater/pkg/common"
1317 "github.com/argoproj-labs/argocd-image-updater/pkg/env"
1418 "github.com/argoproj-labs/argocd-image-updater/pkg/health"
1519 "github.com/argoproj-labs/argocd-image-updater/pkg/image"
1620 "github.com/argoproj-labs/argocd-image-updater/pkg/kube"
1721 "github.com/argoproj-labs/argocd-image-updater/pkg/log"
1822 "github.com/argoproj-labs/argocd-image-updater/pkg/metrics"
1923 "github.com/argoproj-labs/argocd-image-updater/pkg/registry"
24+ "github.com/argoproj-labs/argocd-image-updater/pkg/tag"
2025 "github.com/argoproj-labs/argocd-image-updater/pkg/version"
2126
2227 "github.com/spf13/cobra"
@@ -31,6 +36,9 @@ const defaultArgoCDServerAddr = "argocd-server.argocd"
3136// Default path to registry configuration
3237const defaultRegistriesConfPath = "/app/config/registries.conf"
3338
39+ // Default path to Git commit message template
40+ const defaultCommitTemplatePath = "/app/config/commit.template"
41+
3442const applicationsAPIKindK8S = "kubernetes"
3543const applicationsAPIKindArgoCD = "argocd"
3644
@@ -51,6 +59,7 @@ type ImageUpdaterConfig struct {
5159 AppNamePatterns []string
5260 GitCommitUser string
5361 GitCommitMail string
62+ GitCommitMessage * template.Template
5463 DisableKubeEvents bool
5564}
5665
@@ -155,6 +164,7 @@ func runImageUpdater(cfg *ImageUpdaterConfig, warmUp bool) (argocd.ImageUpdaterR
155164 DryRun : dryRun ,
156165 GitCommitUser : cfg .GitCommitUser ,
157166 GitCommitEmail : cfg .GitCommitMail ,
167+ GitCommitMessage : cfg .GitCommitMessage ,
158168 DisableKubeEvents : cfg .DisableKubeEvents ,
159169 }
160170 res := argocd .UpdateApplication (upconf , syncState )
@@ -203,6 +213,7 @@ func newRootCommand() error {
203213 rootCmd .AddCommand (newRunCommand ())
204214 rootCmd .AddCommand (newVersionCommand ())
205215 rootCmd .AddCommand (newTestCommand ())
216+ rootCmd .AddCommand (newTemplateCommand ())
206217 err := rootCmd .Execute ()
207218 return err
208219}
@@ -231,6 +242,55 @@ func newVersionCommand() *cobra.Command {
231242 return versionCmd
232243}
233244
245+ func newTemplateCommand () * cobra.Command {
246+ var (
247+ commitMessageTemplatePath string
248+ tplStr string
249+ )
250+ var runCmd = & cobra.Command {
251+ Use : "template [<PATH>]" ,
252+ Short : "Test & render a commit message template" ,
253+ Long : `
254+ The template command lets you validate your commit message template. It will
255+ parse the template at given PATH and execute it with a defined set of changes
256+ so that you can see how it looks like when being templated by Image Updater.
257+
258+ If PATH is not given, will show you the default message that is used.
259+ ` ,
260+ Run : func (cmd * cobra.Command , args []string ) {
261+ var tpl * template.Template
262+ var err error
263+ if len (args ) != 1 {
264+ tplStr = common .DefaultGitCommitMessage
265+ } else {
266+ commitMessageTemplatePath = args [0 ]
267+ tplData , err := ioutil .ReadFile (commitMessageTemplatePath )
268+ if err != nil {
269+ log .Fatalf ("%v" , err )
270+ }
271+ tplStr = string (tplData )
272+ }
273+ if tpl , err = template .New ("commitMessage" ).Parse (tplStr ); err != nil {
274+ log .Fatalf ("could not parse commit message template: %v" , err )
275+ }
276+ chL := []argocd.ChangeEntry {
277+ {
278+ Image : image .NewFromIdentifier ("gcr.io/example/example:1.0.0" ),
279+ OldTag : tag .NewImageTag ("1.0.0" , time .Now (), "" ),
280+ NewTag : tag .NewImageTag ("1.0.1" , time .Now (), "" ),
281+ },
282+ {
283+ Image : image .NewFromIdentifier ("gcr.io/example/updater@sha256:f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2" ),
284+ OldTag : tag .NewImageTag ("" , time .Now (), "sha256:01d09d19c2139a46aebfb577780d123d7396e97201bc7ead210a2ebff8239dee" ),
285+ NewTag : tag .NewImageTag ("" , time .Now (), "sha256:7aa7a5359173d05b63cfd682e3c38487f3cb4f7f1d60659fe59fab1505977d4c" ),
286+ },
287+ }
288+ fmt .Printf ("%s\n " , argocd .TemplateCommitMessage (tpl , "example-app" , chL ))
289+ },
290+ }
291+ return runCmd
292+ }
293+
234294func newTestCommand () * cobra.Command {
235295 var (
236296 semverConstraint string
@@ -388,6 +448,8 @@ func newRunCommand() *cobra.Command {
388448 var kubeConfig string
389449 var disableKubernetes bool
390450 var warmUpCache bool = true
451+ var commitMessagePath string
452+ var commitMessageTpl string
391453 var runCmd = & cobra.Command {
392454 Use : "run" ,
393455 Short : "Runs the argocd-image-updater with a set of options" ,
@@ -414,6 +476,33 @@ func newRunCommand() *cobra.Command {
414476 getPrintableHealthPort (cfg .HealthPort ),
415477 )
416478
479+ // User can specify a path to a template used for Git commit messages
480+ if commitMessagePath != "" {
481+ tpl , err := ioutil .ReadFile (commitMessagePath )
482+ if err != nil {
483+ if errors .Is (err , os .ErrNotExist ) {
484+ log .Warnf ("commit message template at %s does not exist, using default" , commitMessagePath )
485+ commitMessageTpl = common .DefaultGitCommitMessage
486+ } else {
487+ log .Fatalf ("could not read commit message template: %v" , err )
488+ }
489+ } else {
490+ commitMessageTpl = string (tpl )
491+ }
492+ }
493+
494+ if commitMessageTpl == "" {
495+ log .Infof ("Using default Git commit messages" )
496+ commitMessageTpl = common .DefaultGitCommitMessage
497+ }
498+
499+ if tpl , err := template .New ("commitMessage" ).Parse (commitMessageTpl ); err != nil {
500+ log .Fatalf ("could not parse commit message template: %v" , err )
501+ } else {
502+ log .Debugf ("Successfully parsed commit message template" )
503+ cfg .GitCommitMessage = tpl
504+ }
505+
417506 // Load registries configuration early on. We do not consider it a fatal
418507 // error when the file does not exist, but we emit a warning.
419508 if cfg .RegistriesConf != "" {
@@ -548,6 +637,7 @@ func newRunCommand() *cobra.Command {
548637 runCmd .Flags ().BoolVar (& warmUpCache , "warmup-cache" , true , "whether to perform a cache warm-up on startup" )
549638 runCmd .Flags ().StringVar (& cfg .GitCommitUser , "git-commit-user" , env .GetStringVal ("GIT_COMMIT_USER" , "argocd-image-updater" ), "Username to use for Git commits" )
550639 runCmd .
Flags ().
StringVar (
& cfg .
GitCommitMail ,
"git-commit-email" ,
env .
GetStringVal (
"GIT_COMMIT_EMAIL" ,
"[email protected] " ),
"E-Mail address to use for Git commits" )
640+ runCmd .Flags ().StringVar (& commitMessagePath , "git-commit-message-path" , defaultCommitTemplatePath , "Path to a template to use for Git commit messages" )
551641 runCmd .Flags ().BoolVar (& cfg .DisableKubeEvents , "disable-kube-events" , env .GetBoolVal ("IMAGE_UPDATER_KUBE_EVENTS" , false ), "Disable kubernetes events" )
552642
553643 return runCmd
0 commit comments