Skip to content

Commit

Permalink
Merge pull request #2767 from takirala/tga/add-ignore-field
Browse files Browse the repository at this point in the history
Add `--ignore-paths` flag to `flux create source (git|bucket)`
  • Loading branch information
stefanprodan authored May 28, 2022
2 parents 5ebb985 + ed88e9d commit b816471
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 10 deletions.
30 changes: 20 additions & 10 deletions cmd/flux/create_source_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -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())
Expand All @@ -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),
}
Expand Down Expand Up @@ -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,
Expand All @@ -131,6 +140,7 @@ func createSourceBucketCmdRun(cmd *cobra.Command, args []string) error {
Interval: metav1.Duration{
Duration: createArgs.interval,
},
Ignore: ignorePaths,
},
}

Expand Down
10 changes: 10 additions & 0 deletions cmd/flux/create_source_git.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"net/url"
"os"
"strings"

"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -59,6 +60,7 @@ type sourceGitFlags struct {
privateKeyFile string
recurseSubmodules bool
silent bool
ignorePaths []string
}

var createSourceGitCmd = &cobra.Command{
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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,
Expand All @@ -202,6 +211,7 @@ func createSourceGitCmdRun(cmd *cobra.Command, args []string) error {
},
RecurseSubmodules: sourceGitArgs.recurseSubmodules,
Reference: &sourcev1.GitRepositoryRef{},
Ignore: ignorePaths,
},
}

Expand Down
25 changes: 25 additions & 0 deletions cmd/flux/create_source_git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
15 changes: 15 additions & 0 deletions cmd/flux/testdata/create_source_git/export.golden
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b816471

Please sign in to comment.