-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for .sourceignore and ignore string
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
Showing
7 changed files
with
296 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.