From ed88e9dec50edcf1e07719cfdc3794cf4d7f040e Mon Sep 17 00:00:00 2001 From: Tarun Gupta Akirala Date: Tue, 24 May 2022 16:52:54 -0700 Subject: [PATCH] feat: add --ignore-paths flag to flux create source (git|bucket) A new --ignore-paths flag is added to following commands: flux create source git --ignore-paths ... flux create source bucket --ignore-paths ... A StringSliceVar is used which supports specifying the flag multiple times to populate a list or either a comma seperated string value A unit test with a golden file is added to validate the flag Signed-off-by: Tarun Gupta Akirala --- cmd/flux/create_source_bucket.go | 30 ++++++++++++------- cmd/flux/create_source_git.go | 10 +++++++ cmd/flux/create_source_git_test.go | 25 ++++++++++++++++ .../testdata/create_source_git/export.golden | 15 ++++++++++ 4 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 cmd/flux/testdata/create_source_git/export.golden diff --git a/cmd/flux/create_source_bucket.go b/cmd/flux/create_source_bucket.go index 4ba4d636..e528deed 100644 --- a/cmd/flux/create_source_bucket.go +++ b/cmd/flux/create_source_bucket.go @@ -20,6 +20,7 @@ import ( "context" "fmt" "os" + "strings" "github.com/spf13/cobra" corev1 "k8s.io/api/core/v1" @@ -63,17 +64,18 @@ For Buckets with static authentication, the credentials are stored in a Kubernet } type sourceBucketFlags struct { - name string - provider flags.SourceBucketProvider - endpoint string - accessKey string - secretKey string - region string - insecure bool - secretRef string + name string + provider flags.SourceBucketProvider + endpoint string + accessKey string + secretKey string + region string + insecure bool + secretRef string + ignorePaths []string } -var sourceBucketArgs = NewSourceBucketFlags() +var sourceBucketArgs = newSourceBucketFlags() func init() { createSourceBucketCmd.Flags().Var(&sourceBucketArgs.provider, "provider", sourceBucketArgs.provider.Description()) @@ -84,11 +86,12 @@ func init() { createSourceBucketCmd.Flags().StringVar(&sourceBucketArgs.region, "region", "", "the bucket region") createSourceBucketCmd.Flags().BoolVar(&sourceBucketArgs.insecure, "insecure", false, "for when connecting to a non-TLS S3 HTTP endpoint") createSourceBucketCmd.Flags().StringVar(&sourceBucketArgs.secretRef, "secret-ref", "", "the name of an existing secret containing credentials") + createSourceBucketCmd.Flags().StringSliceVar(&sourceBucketArgs.ignorePaths, "ignore-paths", nil, "set paths to ignore in bucket resource (can specify multiple paths with commas: path1,path2)") createSourceCmd.AddCommand(createSourceBucketCmd) } -func NewSourceBucketFlags() sourceBucketFlags { +func newSourceBucketFlags() sourceBucketFlags { return sourceBucketFlags{ provider: flags.SourceBucketProvider(sourcev1.GenericBucketProvider), } @@ -116,6 +119,12 @@ func createSourceBucketCmdRun(cmd *cobra.Command, args []string) error { } defer os.RemoveAll(tmpDir) + var ignorePaths *string + if len(sourceBucketArgs.ignorePaths) > 0 { + ignorePathsStr := strings.Join(sourceBucketArgs.ignorePaths, "\n") + ignorePaths = &ignorePathsStr + } + bucket := &sourcev1.Bucket{ ObjectMeta: metav1.ObjectMeta{ Name: name, @@ -131,6 +140,7 @@ func createSourceBucketCmdRun(cmd *cobra.Command, args []string) error { Interval: metav1.Duration{ Duration: createArgs.interval, }, + Ignore: ignorePaths, }, } diff --git a/cmd/flux/create_source_git.go b/cmd/flux/create_source_git.go index a520523e..9dba83ef 100644 --- a/cmd/flux/create_source_git.go +++ b/cmd/flux/create_source_git.go @@ -22,6 +22,7 @@ import ( "fmt" "net/url" "os" + "strings" "github.com/manifoldco/promptui" "github.com/spf13/cobra" @@ -59,6 +60,7 @@ type sourceGitFlags struct { privateKeyFile string recurseSubmodules bool silent bool + ignorePaths []string } var createSourceGitCmd = &cobra.Command{ @@ -139,6 +141,7 @@ func init() { createSourceGitCmd.Flags().BoolVar(&sourceGitArgs.recurseSubmodules, "recurse-submodules", false, "when enabled, configures the GitRepository source to initialize and include Git submodules in the artifact it produces") createSourceGitCmd.Flags().BoolVarP(&sourceGitArgs.silent, "silent", "s", false, "assumes the deploy key is already setup, skips confirmation") + createSourceGitCmd.Flags().StringSliceVar(&sourceGitArgs.ignorePaths, "ignore-paths", nil, "set paths to ignore in git resource (can specify multiple paths with commas: path1,path2)") createSourceCmd.AddCommand(createSourceGitCmd) } @@ -189,6 +192,12 @@ func createSourceGitCmdRun(cmd *cobra.Command, args []string) error { return err } + var ignorePaths *string + if len(sourceGitArgs.ignorePaths) > 0 { + ignorePathsStr := strings.Join(sourceGitArgs.ignorePaths, "\n") + ignorePaths = &ignorePathsStr + } + gitRepository := sourcev1.GitRepository{ ObjectMeta: metav1.ObjectMeta{ Name: name, @@ -202,6 +211,7 @@ func createSourceGitCmdRun(cmd *cobra.Command, args []string) error { }, RecurseSubmodules: sourceGitArgs.recurseSubmodules, Reference: &sourcev1.GitRepositoryRef{}, + Ignore: ignorePaths, }, } diff --git a/cmd/flux/create_source_git_test.go b/cmd/flux/create_source_git_test.go index 6447e52e..227567b2 100644 --- a/cmd/flux/create_source_git_test.go +++ b/cmd/flux/create_source_git_test.go @@ -85,6 +85,31 @@ func (r *reconciler) conditionFunc() (bool, error) { return true, err } +func TestCreateSourceGitExport(t *testing.T) { + var command = "create source git podinfo --url=https://github.com/stefanprodan/podinfo --branch=master --ignore-paths .cosign,non-existent-dir/ -n default --interval 1m --export --timeout=" + testTimeout.String() + + cases := []struct { + name string + args string + assert assertFunc + }{ + { + "ExportSucceeded", + command, + assertGoldenFile("testdata/create_source_git/export.golden"), + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + cmd := cmdTestCase{ + args: tc.args, + assert: tc.assert, + } + cmd.runTestCmd(t) + }) + } +} + func TestCreateSourceGit(t *testing.T) { // Default command used for multiple tests var command = "create source git podinfo --url=https://github.com/stefanprodan/podinfo --branch=master --timeout=" + testTimeout.String() diff --git a/cmd/flux/testdata/create_source_git/export.golden b/cmd/flux/testdata/create_source_git/export.golden new file mode 100644 index 00000000..2b0193df --- /dev/null +++ b/cmd/flux/testdata/create_source_git/export.golden @@ -0,0 +1,15 @@ +--- +apiVersion: source.toolkit.fluxcd.io/v1beta2 +kind: GitRepository +metadata: + name: podinfo + namespace: default +spec: + ignore: |- + .cosign + non-existent-dir/ + interval: 1m0s + ref: + branch: master + url: https://github.com/stefanprodan/podinfo +