Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(engine): ignore terraform cache folders #6240

Merged
merged 26 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5198e09
feat: ignore terraform cache folders
dim-ops Mar 21, 2023
a80785d
style: fmt and delete comment
dim-ops Mar 28, 2023
6032e2e
feat: ignore terraform cache folders
dim-ops Mar 28, 2023
a95d261
chore: improve comment
dim-ops Mar 28, 2023
abef0ca
feat(go): add unit tests
Apr 5, 2023
dc1a481
fix: err not handled
Apr 5, 2023
fea9933
fix: UT TestFileSystemSourceProvider_checkConditions
dim-ops Apr 6, 2023
371eb3e
fix: add resolved
Apr 6, 2023
7632af6
Merge branch 'master' into feat/ignore-terraform-cahce-files
gabriel-cx Apr 17, 2023
a3c4e50
Merge branch 'master' into feat/ignore-terraform-cahce-files
gabriel-cx Feb 2, 2024
8d09561
Merge branch 'master' into pr/6240
JoaoAtGit Feb 8, 2024
43244e4
improve terraform cache
JoaoAtGit Feb 8, 2024
ff0c129
Merge branch 'master' into feat/ignore-terraform-cahce-files
JoaoAtGit Feb 8, 2024
8be4638
Merge branch 'master' into feat/ignore-terraform-cahce-files
JoaoAtGit Feb 8, 2024
c8a5c99
more testes
JoaoAtGit Feb 8, 2024
453ea64
Merge branch 'feat/ignore-terraform-cahce-files' of https://github.co…
JoaoAtGit Feb 8, 2024
4c1980b
Merge branch 'master' into feat/ignore-terraform-cahce-files
JoaoAtGit Feb 8, 2024
56d1e82
add tests to terragrunt
JoaoAtGit Feb 8, 2024
1eb6279
Merge branch 'master' into feat/ignore-terraform-cahce-files
gabriel-cx Feb 9, 2024
204dad2
test not skipt with terra on path
JoaoAtGit Feb 9, 2024
bedd71e
change the type of lock
JoaoAtGit Feb 15, 2024
59c5394
clean code
JoaoAtGit Feb 15, 2024
9f7b447
Merge branch 'master' into feat/ignore-terraform-cahce-files
JoaoAtGit Feb 16, 2024
3097e41
remove lock logic
JoaoAtGit Feb 16, 2024
94ebf93
Merge branch 'master' into feat/ignore-terraform-cahce-files
gabriel-cx Feb 16, 2024
cb7a7f6
Merge branch 'master' into feat/ignore-terraform-cahce-files
gabriel-cx Feb 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions pkg/engine/provider/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
ioFs "io/fs"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"syscall"
Expand All @@ -26,8 +27,11 @@ type FileSystemSourceProvider struct {
mu sync.RWMutex
}

// ErrNotSupportedFile - error representing when a file format is not supported by KICS
var ErrNotSupportedFile = errors.New("invalid file format")
var (
queryRegexExcludeTerraCache = regexp.MustCompile(fmt.Sprintf(`^(.*?%s)?\.terra.*`, regexp.QuoteMeta(string(os.PathSeparator))))
// ErrNotSupportedFile - error representing when a file format is not supported by KICS
ErrNotSupportedFile = errors.New("invalid file format")
)

// NewFileSystemSourceProvider initializes a FileSystemSourceProvider with path and files that will be ignored
func NewFileSystemSourceProvider(paths, excludes []string) (*FileSystemSourceProvider, error) {
Expand Down Expand Up @@ -230,7 +234,18 @@ func (s *FileSystemSourceProvider) checkConditions(info os.FileInfo, extensions
path string, resolved bool) (bool, error) {
s.mu.RLock()
defer s.mu.RUnlock()

if info.IsDir() {
// exclude terraform cache folders
if queryRegexExcludeTerraCache.MatchString(path) {
log.Info().Msgf("Directory ignored: %s", path)

err := s.AddExcluded([]string{info.Name()})
if err != nil {
return true, err
}
return true, filepath.SkipDir
}
if f, ok := s.excludes[info.Name()]; ok && containsFile(f, info) {
log.Info().Msgf("Directory ignored: %s", path)
return true, filepath.SkipDir
Expand Down
263 changes: 263 additions & 0 deletions pkg/engine/provider/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,13 @@ func TestFileSystemSourceProvider_checkConditions(t *testing.T) {
}
infoHelm, errHelm := os.Stat(filepath.FromSlash("test/fixtures/test_helm"))
checkStatErr(t, errHelm)
infoHelmTerra, errHelmTerra := os.Stat(filepath.FromSlash("test/fixtures/terra/test_helm"))
checkStatErr(t, errHelmTerra)
infoTerraCache, errTerraCache := os.Stat(filepath.FromSlash("test/fixtures/test_terra_cache"))
checkStatErr(t, errTerraCache)
infoTerraCacheFolder, errTerraCacheFolder := os.Stat(filepath.FromSlash("test/fixtures/test_terra_cache/.terraform"))
checkStatErr(t, errTerraCacheFolder)

type fields struct {
paths []string
excludes map[string][]os.FileInfo
Expand Down Expand Up @@ -330,6 +337,22 @@ func TestFileSystemSourceProvider_checkConditions(t *testing.T) {
err: nil,
},
},
{
name: "check_conditions_chart, with terra on path not skip",
fields: fields{
paths: []string{filepath.FromSlash("test/fixtures/terra/test_helm")},
excludes: nil,
},
args: args{
info: infoHelmTerra,
extensions: model.Extensions{},
path: filepath.FromSlash("test/fixtures/terra/test_helm"),
},
want: want{
got: false,
err: nil,
},
},
{
name: "should_skip_folder",
fields: fields{
Expand All @@ -350,6 +373,246 @@ func TestFileSystemSourceProvider_checkConditions(t *testing.T) {
err: filepath.SkipDir,
},
},
{
name: "check_condition_ignore_terra_cache for .terra",
fields: fields{
paths: []string{filepath.FromSlash(".terra")},
excludes: nil,
},
args: args{
info: infoTerraCache,
extensions: model.Extensions{},
path: filepath.FromSlash(".terra"),
},
want: want{
got: true,
err: filepath.SkipDir,
},
},
{
name: "check_condition_ignore_terragrunt_cache for .terragrunt-cache",
fields: fields{
paths: []string{filepath.FromSlash(".terragrunt-cache")},
excludes: nil,
},
args: args{
info: infoTerraCache,
extensions: model.Extensions{},
path: filepath.FromSlash(".terragrunt-cache"),
},
want: want{
got: true,
err: filepath.SkipDir,
},
},
{
name: "check_condition_ignore_terra_cache for terra, exclude by missing chart.yaml",
fields: fields{
paths: []string{filepath.FromSlash("terra")},
excludes: nil,
},
args: args{
info: infoTerraCache,
extensions: model.Extensions{},
path: filepath.FromSlash("terra"),
},
want: want{
got: true,
err: nil,
},
},
{
name: "check_condition_ignore_terra_cache for .terraform",
fields: fields{
paths: []string{filepath.FromSlash(".terraform")},
excludes: nil,
},
args: args{
info: infoTerraCache,
extensions: model.Extensions{},
path: filepath.FromSlash(".terraform"),
},
want: want{
got: true,
err: filepath.SkipDir,
},
},
{
name: "check_condition_ignore_terra_cache for .terraform, exclude by missing chart.yaml",
fields: fields{
paths: []string{filepath.FromSlash("terraform")},
excludes: nil,
},
args: args{
info: infoTerraCache,
extensions: model.Extensions{},
path: filepath.FromSlash("terraform"),
},
want: want{
got: true,
err: nil,
},
},
{
name: "check_condition_ignore_terra_cache for .terra/lalala",
fields: fields{
paths: []string{filepath.FromSlash(".terra/lalala")},
excludes: nil,
},
args: args{
info: infoTerraCache,
extensions: model.Extensions{},
path: filepath.FromSlash(".terra/lalala"),
},
want: want{
got: true,
err: filepath.SkipDir,
},
},
{
name: "check_condition_ignore_terragrunt_cache for .terragrunt-cache/lalala",
fields: fields{
paths: []string{filepath.FromSlash(".terragrunt-cache/lalala")},
excludes: nil,
},
args: args{
info: infoTerraCache,
extensions: model.Extensions{},
path: filepath.FromSlash(".terragrunt-cache/lalala"),
},
want: want{
got: true,
err: filepath.SkipDir,
},
},
{
name: "check_condition_ignore_terra_cache for .terraform/lalala",
fields: fields{
paths: []string{filepath.FromSlash(".terraform/lalala")},
excludes: nil,
},
args: args{
info: infoTerraCache,
extensions: model.Extensions{},
path: filepath.FromSlash(".terraform/lalala"),
},
want: want{
got: true,
err: filepath.SkipDir,
},
},
{
name: "check_condition_ignore_terra_cache for /.terra",
fields: fields{
paths: []string{filepath.FromSlash("/.terra")},
excludes: nil,
},
args: args{
info: infoTerraCache,
extensions: model.Extensions{},
path: filepath.FromSlash("/.terra"),
},
want: want{
got: true,
err: filepath.SkipDir,
},
},
{
name: "check_condition_ignore_terra_cache for /.terraform",
fields: fields{
paths: []string{filepath.FromSlash("/.terraform")},
excludes: nil,
},
args: args{
info: infoTerraCache,
extensions: model.Extensions{},
path: filepath.FromSlash("/.terraform"),
},
want: want{
got: true,
err: filepath.SkipDir,
},
},
{
name: "check_condition_ignore_terragrunt_cache for /.terragrunt-cache",
fields: fields{
paths: []string{filepath.FromSlash("/.terragrunt-cache")},
excludes: nil,
},
args: args{
info: infoTerraCache,
extensions: model.Extensions{},
path: filepath.FromSlash("/.terragrunt-cache"),
},
want: want{
got: true,
err: filepath.SkipDir,
},
},
{
name: "check_condition_ignore_terra_cache for /.terra/lalala",
fields: fields{
paths: []string{filepath.FromSlash("/.terra/lalala")},
excludes: nil,
},
args: args{
info: infoTerraCache,
extensions: model.Extensions{},
path: filepath.FromSlash("/.terra/lalala"),
},
want: want{
got: true,
err: filepath.SkipDir,
},
},
{
name: "check_condition_ignore_terragrunt_cache for /.terragrunt-cache/lalala",
fields: fields{
paths: []string{filepath.FromSlash("/.terragrunt-cache/lalala")},
excludes: nil,
},
args: args{
info: infoTerraCache,
extensions: model.Extensions{},
path: filepath.FromSlash("/.terragrunt-cache/lalala"),
},
want: want{
got: true,
err: filepath.SkipDir,
},
},
{
name: "check_condition_ignore_terra_cache for /.terraform/lalala",
fields: fields{
paths: []string{filepath.FromSlash("/.terraform/lalala")},
excludes: nil,
},
args: args{
info: infoTerraCache,
extensions: model.Extensions{},
path: filepath.FromSlash("/.terraform/lalala"),
},
want: want{
got: true,
err: filepath.SkipDir,
},
},
{
name: "should_skip_terra_cache_folder",
fields: fields{
paths: []string{filepath.FromSlash("test/fixtures/test_terra_cache/.terraform")},
excludes: nil,
},
args: args{
info: infoTerraCacheFolder,
extensions: model.Extensions{},
path: filepath.FromSlash("test/fixtures/test_terra_cache/.terraform"),
},
want: want{
got: true,
err: filepath.SkipDir,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
23 changes: 23 additions & 0 deletions test/fixtures/terra/test_helm/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
24 changes: 24 additions & 0 deletions test/fixtures/terra/test_helm/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: v2
name: test_helm
description: A Helm chart for Kubernetes

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "1.16.0"
22 changes: 22 additions & 0 deletions test/fixtures/terra/test_helm/templates/NOTES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
1. Get the application URL by running these commands:
{{- if .Values.ingress.enabled }}
{{- range $host := .Values.ingress.hosts }}
{{- range .paths }}
http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }}
{{- end }}
{{- end }}
{{- else if contains "NodePort" .Values.service.type }}
export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "test_helm.fullname" . }})
export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")
echo http://$NODE_IP:$NODE_PORT
{{- else if contains "LoadBalancer" .Values.service.type }}
NOTE: It may take a few minutes for the LoadBalancer IP to be available.
You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "test_helm.fullname" . }}'
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "test_helm.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}")
echo http://$SERVICE_IP:{{ .Values.service.port }}
{{- else if contains "ClusterIP" .Values.service.type }}
export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "test_helm.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}")
export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")
echo "Visit http://127.0.0.1:8080 to use your application"
kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT
{{- end }}
Loading
Loading