mirror of
				https://github.com/fluxcd/flux2.git
				synced 2025-10-31 08:17:19 +08:00 
			
		
		
		
	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 <takirala@users.noreply.github.com>
This commit is contained in:
		| @ -20,6 +20,7 @@ import ( | |||||||
| 	"context" | 	"context" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"os" | 	"os" | ||||||
|  | 	"strings" | ||||||
|  |  | ||||||
| 	"github.com/spf13/cobra" | 	"github.com/spf13/cobra" | ||||||
| 	corev1 "k8s.io/api/core/v1" | 	corev1 "k8s.io/api/core/v1" | ||||||
| @ -71,9 +72,10 @@ type sourceBucketFlags struct { | |||||||
| 	region      string | 	region      string | ||||||
| 	insecure    bool | 	insecure    bool | ||||||
| 	secretRef   string | 	secretRef   string | ||||||
|  | 	ignorePaths []string | ||||||
| } | } | ||||||
|  |  | ||||||
| var sourceBucketArgs = NewSourceBucketFlags() | var sourceBucketArgs = newSourceBucketFlags() | ||||||
|  |  | ||||||
| func init() { | func init() { | ||||||
| 	createSourceBucketCmd.Flags().Var(&sourceBucketArgs.provider, "provider", sourceBucketArgs.provider.Description()) | 	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().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().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().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) | 	createSourceCmd.AddCommand(createSourceBucketCmd) | ||||||
| } | } | ||||||
|  |  | ||||||
| func NewSourceBucketFlags() sourceBucketFlags { | func newSourceBucketFlags() sourceBucketFlags { | ||||||
| 	return sourceBucketFlags{ | 	return sourceBucketFlags{ | ||||||
| 		provider: flags.SourceBucketProvider(sourcev1.GenericBucketProvider), | 		provider: flags.SourceBucketProvider(sourcev1.GenericBucketProvider), | ||||||
| 	} | 	} | ||||||
| @ -116,6 +119,12 @@ func createSourceBucketCmdRun(cmd *cobra.Command, args []string) error { | |||||||
| 	} | 	} | ||||||
| 	defer os.RemoveAll(tmpDir) | 	defer os.RemoveAll(tmpDir) | ||||||
|  |  | ||||||
|  | 	var ignorePaths *string | ||||||
|  | 	if len(sourceBucketArgs.ignorePaths) > 0 { | ||||||
|  | 		ignorePathsStr := strings.Join(sourceBucketArgs.ignorePaths, "\n") | ||||||
|  | 		ignorePaths = &ignorePathsStr | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	bucket := &sourcev1.Bucket{ | 	bucket := &sourcev1.Bucket{ | ||||||
| 		ObjectMeta: metav1.ObjectMeta{ | 		ObjectMeta: metav1.ObjectMeta{ | ||||||
| 			Name:      name, | 			Name:      name, | ||||||
| @ -131,6 +140,7 @@ func createSourceBucketCmdRun(cmd *cobra.Command, args []string) error { | |||||||
| 			Interval: metav1.Duration{ | 			Interval: metav1.Duration{ | ||||||
| 				Duration: createArgs.interval, | 				Duration: createArgs.interval, | ||||||
| 			}, | 			}, | ||||||
|  | 			Ignore: ignorePaths, | ||||||
| 		}, | 		}, | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | |||||||
| @ -22,6 +22,7 @@ import ( | |||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"net/url" | 	"net/url" | ||||||
| 	"os" | 	"os" | ||||||
|  | 	"strings" | ||||||
|  |  | ||||||
| 	"github.com/manifoldco/promptui" | 	"github.com/manifoldco/promptui" | ||||||
| 	"github.com/spf13/cobra" | 	"github.com/spf13/cobra" | ||||||
| @ -59,6 +60,7 @@ type sourceGitFlags struct { | |||||||
| 	privateKeyFile    string | 	privateKeyFile    string | ||||||
| 	recurseSubmodules bool | 	recurseSubmodules bool | ||||||
| 	silent            bool | 	silent            bool | ||||||
|  | 	ignorePaths       []string | ||||||
| } | } | ||||||
|  |  | ||||||
| var createSourceGitCmd = &cobra.Command{ | var createSourceGitCmd = &cobra.Command{ | ||||||
| @ -139,6 +141,7 @@ func init() { | |||||||
| 	createSourceGitCmd.Flags().BoolVar(&sourceGitArgs.recurseSubmodules, "recurse-submodules", false, | 	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") | 		"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().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) | 	createSourceCmd.AddCommand(createSourceGitCmd) | ||||||
| } | } | ||||||
| @ -189,6 +192,12 @@ func createSourceGitCmdRun(cmd *cobra.Command, args []string) error { | |||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	var ignorePaths *string | ||||||
|  | 	if len(sourceGitArgs.ignorePaths) > 0 { | ||||||
|  | 		ignorePathsStr := strings.Join(sourceGitArgs.ignorePaths, "\n") | ||||||
|  | 		ignorePaths = &ignorePathsStr | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	gitRepository := sourcev1.GitRepository{ | 	gitRepository := sourcev1.GitRepository{ | ||||||
| 		ObjectMeta: metav1.ObjectMeta{ | 		ObjectMeta: metav1.ObjectMeta{ | ||||||
| 			Name:      name, | 			Name:      name, | ||||||
| @ -202,6 +211,7 @@ func createSourceGitCmdRun(cmd *cobra.Command, args []string) error { | |||||||
| 			}, | 			}, | ||||||
| 			RecurseSubmodules: sourceGitArgs.recurseSubmodules, | 			RecurseSubmodules: sourceGitArgs.recurseSubmodules, | ||||||
| 			Reference:         &sourcev1.GitRepositoryRef{}, | 			Reference:         &sourcev1.GitRepositoryRef{}, | ||||||
|  | 			Ignore:            ignorePaths, | ||||||
| 		}, | 		}, | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | |||||||
| @ -85,6 +85,31 @@ func (r *reconciler) conditionFunc() (bool, error) { | |||||||
| 	return true, err | 	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) { | func TestCreateSourceGit(t *testing.T) { | ||||||
| 	// Default command used for multiple tests | 	// Default command used for multiple tests | ||||||
| 	var command = "create source git podinfo --url=https://github.com/stefanprodan/podinfo --branch=master --timeout=" + testTimeout.String() | 	var command = "create source git podinfo --url=https://github.com/stefanprodan/podinfo --branch=master --timeout=" + testTimeout.String() | ||||||
|  | |||||||
							
								
								
									
										15
									
								
								cmd/flux/testdata/create_source_git/export.golden
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								cmd/flux/testdata/create_source_git/export.golden
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @ -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 | ||||||
|  |  | ||||||
		Reference in New Issue
	
	Block a user
	 Tarun Gupta Akirala
					Tarun Gupta Akirala