From d9331b0c91c844010dbe81e926ef59d8ab0745d6 Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Wed, 17 Mar 2021 17:30:57 +0200 Subject: [PATCH] Add repo path and push branch to image update cmd Signed-off-by: Stefan Prodan --- ...e_updateauto.go => create_image_update.go} | 66 +++++++++++++++---- docs/cmd/flux_create_image_update.md | 32 ++++++++- 2 files changed, 84 insertions(+), 14 deletions(-) rename cmd/flux/{create_image_updateauto.go => create_image_update.go} (59%) diff --git a/cmd/flux/create_image_updateauto.go b/cmd/flux/create_image_update.go similarity index 59% rename from cmd/flux/create_image_updateauto.go rename to cmd/flux/create_image_update.go index c5092ad5..d7179514 100644 --- a/cmd/flux/create_image_updateauto.go +++ b/cmd/flux/create_image_update.go @@ -28,19 +28,38 @@ import ( ) var createImageUpdateCmd = &cobra.Command{ - Use: "update ", + Use: "update [name]", Short: "Create or update an ImageUpdateAutomation object", Long: `The create image update command generates an ImageUpdateAutomation resource. An ImageUpdateAutomation object specifies an automated update to images mentioned in YAMLs in a git repository.`, + Example: ` # Configure image updates for the main repository created by flux bootstrap + flux create image update flux-system \ + --git-repo-ref=flux-system \ + --git-repo-path="./clusters/my-cluster" \ + --checkout-branch=main \ + --author-name=flux \ + --author-email=flux@example.com \ + --commit-template="{{range .Updated.Images}}{{println .}}{{end}}" + + # Configure image updates to push changes to a different branch, if the branch doesn't exists it will be created + flux create image update flux-system \ + --git-repo-ref=flux-system \ + --git-repo-path="./clusters/my-cluster" \ + --checkout-branch=main \ + --push-branch=image-updates \ + --author-name=flux \ + --author-email=flux@example.com \ + --commit-template="{{range .Updated.Images}}{{println .}}{{end}}" +`, RunE: createImageUpdateRun, } type imageUpdateFlags struct { - // git checkout spec - gitRepoRef string - branch string - // commit spec + gitRepoRef string + gitRepoPath string + checkoutBranch string + pushBranch string commitTemplate string authorName string authorEmail string @@ -50,8 +69,10 @@ var imageUpdateArgs = imageUpdateFlags{} func init() { flags := createImageUpdateCmd.Flags() - flags.StringVar(&imageUpdateArgs.gitRepoRef, "git-repo-ref", "", "the name of a GitRepository resource with details of the upstream git repository") - flags.StringVar(&imageUpdateArgs.branch, "branch", "", "the branch to checkout and push commits to") + flags.StringVar(&imageUpdateArgs.gitRepoRef, "git-repo-ref", "", "the name of a GitRepository resource with details of the upstream Git repository") + flags.StringVar(&imageUpdateArgs.gitRepoPath, "git-repo-path", "", "path to the directory containing the manifests to be updated, defaults to the repository root") + flags.StringVar(&imageUpdateArgs.checkoutBranch, "checkout-branch", "", "the branch to checkout") + flags.StringVar(&imageUpdateArgs.pushBranch, "push-branch", "", "the branch to push commits to, defaults to the checkout branch if not specified") flags.StringVar(&imageUpdateArgs.commitTemplate, "commit-template", "", "a template for commit messages") flags.StringVar(&imageUpdateArgs.authorName, "author-name", "", "the name to use for commit author") flags.StringVar(&imageUpdateArgs.authorEmail, "author-email", "", "the email to use for commit author") @@ -69,8 +90,16 @@ func createImageUpdateRun(cmd *cobra.Command, args []string) error { return fmt.Errorf("a reference to a GitRepository is required (--git-repo-ref)") } - if imageUpdateArgs.branch == "" { - return fmt.Errorf("the Git repository branch is required (--branch)") + if imageUpdateArgs.checkoutBranch == "" { + return fmt.Errorf("the Git repository branch is required (--checkout-branch)") + } + + if imageUpdateArgs.authorName == "" { + return fmt.Errorf("the author name is required (--author-name)") + } + + if imageUpdateArgs.authorEmail == "" { + return fmt.Errorf("the author email is required (--author-email)") } labels, err := parseLabels() @@ -89,9 +118,11 @@ func createImageUpdateRun(cmd *cobra.Command, args []string) error { GitRepositoryRef: meta.LocalObjectReference{ Name: imageUpdateArgs.gitRepoRef, }, - Branch: imageUpdateArgs.branch, + Branch: imageUpdateArgs.checkoutBranch, + }, + Interval: metav1.Duration{ + Duration: createArgs.interval, }, - Interval: metav1.Duration{Duration: createArgs.interval}, Commit: autov1.CommitSpec{ AuthorName: imageUpdateArgs.authorName, AuthorEmail: imageUpdateArgs.authorEmail, @@ -100,6 +131,19 @@ func createImageUpdateRun(cmd *cobra.Command, args []string) error { }, } + if imageUpdateArgs.pushBranch != "" { + update.Spec.Push = &autov1.PushSpec{ + Branch: imageUpdateArgs.pushBranch, + } + } + + if imageUpdateArgs.gitRepoPath != "" { + update.Spec.Update = &autov1.UpdateStrategy{ + Path: imageUpdateArgs.gitRepoPath, + Strategy: autov1.UpdateStrategySetters, + } + } + if createArgs.export { return printExport(exportImageUpdate(&update)) } diff --git a/docs/cmd/flux_create_image_update.md b/docs/cmd/flux_create_image_update.md index c5ea5140..2e49fa1f 100644 --- a/docs/cmd/flux_create_image_update.md +++ b/docs/cmd/flux_create_image_update.md @@ -9,7 +9,31 @@ An ImageUpdateAutomation object specifies an automated update to images mentioned in YAMLs in a git repository. ``` -flux create image update [flags] +flux create image update [name] [flags] +``` + +### Examples + +``` + # Configure image updates for the main repository created by flux bootstrap + flux create image update flux-system \ + --git-repo-ref=flux-system \ + --git-repo-path="./clusters/my-cluster" \ + --checkout-branch=main \ + --author-name=flux \ + --author-email=flux@example.com \ + --commit-template="{{range .Updated.Images}}{{println .}}{{end}}" + + # Configure image updates to push changes to a different branch, if the branch doesn't exists it will be created + flux create image update flux-system \ + --git-repo-ref=flux-system \ + --git-repo-path="./clusters/my-cluster" \ + --checkout-branch=main \ + --push-branch=image-updates \ + --author-name=flux \ + --author-email=flux@example.com \ + --commit-template="{{range .Updated.Images}}{{println .}}{{end}}" + ``` ### Options @@ -17,10 +41,12 @@ flux create image update [flags] ``` --author-email string the email to use for commit author --author-name string the name to use for commit author - --branch string the branch to checkout and push commits to + --checkout-branch string the branch to checkout --commit-template string a template for commit messages - --git-repo-ref string the name of a GitRepository resource with details of the upstream git repository + --git-repo-path string path to the directory containing the manifests to be updated, defaults to the repository root + --git-repo-ref string the name of a GitRepository resource with details of the upstream Git repository -h, --help help for update + --push-branch string the branch to push commits to, defaults to the checkout branch if not specified ``` ### Options inherited from parent commands