Skip to content

Commit

Permalink
Add support for .sourceignore and ignore string
Browse files Browse the repository at this point in the history
If implemented the generator will be able to take into account a
`.sourceignore` in the base directory  and a provided `ignore` string
while generating/updating the kustomization file before build.

Signed-off-by: Soule BA <bah.soule@gmail.com>
  • Loading branch information
souleb committed Mar 31, 2023
1 parent 6bf578d commit ef0f955
Show file tree
Hide file tree
Showing 7 changed files with 296 additions and 0 deletions.
107 changes: 107 additions & 0 deletions kustomize/filters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
Copyright 2023 The Flux authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package kustomize

import (
"net/url"
"os"
"path/filepath"
"strings"

"github.com/fluxcd/go-git/v5/plumbing/format/gitignore"
"github.com/fluxcd/pkg/sourceignore"
kustypes "sigs.k8s.io/kustomize/api/types"
)

const (
crds = "crds"
resources = "resources"
components = "components"
)

// filter must return true if a file should not be included in the archive after inspecting the given path
// and/or os.FileInfo.
type filter func(p string, fi os.FileInfo) bool

func ignoreFileFilter(ps []gitignore.Pattern, domain []string) filter {
matcher := sourceignore.NewDefaultMatcher(ps, domain)
return func(p string, fi os.FileInfo) bool {
return matcher.Match(strings.Split(p, string(filepath.Separator)), fi.IsDir())
}
}

func filterKsWithIgnoreFiles(ks *kustypes.Kustomization, dirPath string, ignore string) error {
path, err := filepath.Abs(dirPath)
if err != nil {
return err
}

ignoreDomain := strings.Split(path, string(filepath.Separator))
ps, err := sourceignore.LoadIgnorePatterns(path, ignoreDomain)
if err != nil {
return err
}

if ignore != "" {
ps = append(ps, sourceignore.ReadPatterns(strings.NewReader(ignore), ignoreDomain)...)
}

// filter resources first
err = filterSlice(ks, path, &ks.Resources, resources, ignoreFileFilter(ps, ignoreDomain))
if err != nil {
return err
}

// filter components second
err = filterSlice(ks, path, &ks.Components, components, ignoreFileFilter(ps, ignoreDomain))
if err != nil {
return err
}

// filter crds third
err = filterSlice(ks, path, &ks.Crds, crds, ignoreFileFilter(ps, ignoreDomain))
if err != nil {
return err
}

return nil
}

func filterSlice(ks *kustypes.Kustomization, path string, s *[]string, t string, filter filter) error {
for i, res := range *s {
// check if we have a url and skip it
// this is not needed for crds as they are not allowed to be urls
if t != crds {
if u, err := url.ParseRequestURI(res); err == nil && u.Scheme != "" {
continue
}
}
f := filepath.Join(path, res)
info, err := os.Lstat(f)
if err != nil {
return err
}
if filter(f, info) {
if i < len(*s)-1 {
*s = append((*s)[:i], (*s)[i+1:]...)
} else {
*s = (*s)[:i]
}
}
}
return nil
}
9 changes: 9 additions & 0 deletions kustomize/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ go 1.18

replace github.com/fluxcd/pkg/apis/kustomize => ../apis/kustomize

replace github.com/fluxcd/pkg/sourceignore => ../sourceignore

require (
github.com/drone/envsubst v1.0.3
github.com/fluxcd/go-git/v5 v5.0.0-20221219190809-2e5c9d01cfc4
github.com/fluxcd/pkg/apis/kustomize v1.0.0
github.com/fluxcd/pkg/sourceignore v0.3.3
github.com/hashicorp/go-multierror v1.1.1
github.com/onsi/gomega v1.27.2
github.com/otiai10/copy v1.9.0
Expand All @@ -29,11 +33,14 @@ replace (
replace gopkg.in/yaml.v3 => gopkg.in/yaml.v3 v3.0.1

require (
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.10.0 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.1 // indirect
Expand All @@ -46,6 +53,7 @@ require (
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
Expand All @@ -66,6 +74,7 @@ require (
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/client-go v0.26.3 // indirect
Expand Down

0 comments on commit ef0f955

Please sign in to comment.