-
Notifications
You must be signed in to change notification settings - Fork 508
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
added kustomize support #378
added kustomize support #378
Conversation
@@ -85,7 +85,7 @@ func (k *K8sV1) getNormalizedName(kind string) string { | |||
} | |||
|
|||
// normalize takes the input document and normalizes it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Linter error! change normalize
to Normalize
return res, nil | ||
} | ||
|
||
/* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove the unwanted code block
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Testdata folder is added but no tests are added
pkg/utils/path.go
Outdated
|
||
// FindFilesBySuffixInCurrentDir finds all the immediate files within a given directory that have the specified suffixes | ||
// Returns an array for string pointers as a list of files | ||
func FindFilesBySuffixInCurrentDir(basePath string, suffixes []string) ([]*string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you searching for files in current dir or in basePath
?
May be a better name would be FindFilesBySuffixInCurrentDir
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't that the name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry copy paste mistake.
Can we use the FindFilesBySuffix
method here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. we don't want a recursive search here. We just want to look up files on a single depth level. No looking into subdirectories.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updating the function name though
) | ||
|
||
// LoadIacDir loads the kustomize directory | ||
func (k *KustomizeV3) LoadIacDir(absRootDir string) (output.AllResourceConfigs, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a sincere request to add comments at function level and inside the function as well, so as to help the user understand what's going on. A new reader may not be able to immediately understand what task is being achieved in the function
95ea924
to
2f8eceb
Compare
Codecov Report
@@ Coverage Diff @@
## master #378 +/- ##
==========================================
- Coverage 65.90% 63.55% -2.36%
==========================================
Files 80 85 +5
Lines 1830 1904 +74
==========================================
+ Hits 1206 1210 +4
- Misses 521 591 +70
Partials 103 103
|
} | ||
} | ||
|
||
func getFullFileName(file, ext string) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would AddSuffix
be a better name for this functon? And would it make sense to add it to utils
package?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, makes sense.
|
||
if len(files) == 0 { | ||
err := errors.New("could not find a kustomization.yaml/yml file in the directory") | ||
zap.S().Warn("error while searching for iac files", zap.String("root dir", absRootDir), zap.Error(err)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be an error level log (zap.S().Error
)?
|
||
files, err := utils.FindFilesBySuffixInCurrentDir(absRootDir, KustomizeFileNames()) | ||
if err != nil { | ||
zap.S().Warn("error while searching for iac files", zap.String("root dir", absRootDir), zap.Error(err)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be an error level log (zap.S().Error)?
pkg/utils/path.go
Outdated
|
||
// FindFilesBySuffixInCurrentDir finds all the immediate files within a given directory that have the specified suffixes | ||
// Returns an array for string pointers as a list of files | ||
func FindFilesBySuffixInCurrentDir(basePath string, suffixes []string) ([]*string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry copy paste mistake.
Can we use the FindFilesBySuffix
method here?
|
||
// LoadYAMLString loads a YAML String. Can return one or more IaC Documents. | ||
// Besides reading in file data, its main purpose is to determine and store line number and filename metadata | ||
func LoadYAMLString(data, absFilePath string) ([]*IacDocument, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see a lot of code duplication between LoadYaml
and LoadYamlString
methods.
Would it make sense to have something like LoadYamlBytes
and let LoadYamlString
and LoadYaml
both call LoadYamBytes
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah good point
|
||
if len(files) > 1 { | ||
err := errors.New("a directory cannot have more than 1 kustomization.yaml/yml file") | ||
zap.S().Warn("error while searching for iac files", zap.String("root dir", absRootDir), zap.Error(err)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be an error level log (zap.S().Error)?
|
||
var yamlkustomizeobj map[string]interface{} | ||
var kustomizeFileName string | ||
for _, filename := range KustomizeFileNames() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use files
variable rather than calling KustomizeFileNames
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch
) | ||
|
||
const ( | ||
kustomizedirectory string = "kustomize_directory" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recommend to use either "kustomization" or "kustomization_file" here instead, as that's the proper terminology as per https://kubectl.docs.kubernetes.io/references/kustomize/glossary/. Also, just a side note on coding style--several linters will flag the variable names if not using camel case, and so usually we like to see that convention followed, otherwise it adds noise to the linter/IDE
} | ||
|
||
if len(files) == 0 { | ||
err := errors.New("could not find a kustomization.yaml/yml file in the directory") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although the compiler is fine, linters will flag the redeclaration of variables (err) in this case and the next
var config output.ResourceConfig | ||
config.Type = kustomizedirectory | ||
config.Name = filepath.Dir(absRootDir) | ||
config.Line = 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
config.Line default should be 1. My original change used 0, so it was a bad example--please change to 1 as the line numbers will start from 1.
|
||
allResourcesConfig := make(map[string][]output.ResourceConfig) | ||
|
||
files, err := utils.FindFilesBySuffixInCurrentDir(absRootDir, KustomizeFileNames()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it difficult to support digesting all kustomizations within a directory hierarchy similarly to kustomize? This will likely end up being a pain point for folks who are heavily using kustomize. If I understand how kustomize works correctly, we should render the entire directory structure, otherwise we may not be scanning output 100% equivalent to the true kustomize output.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kustomize takes care of the directory structure by itself. So I didn't have to do anything special to achieve that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
errLoadIacFileNotSupported = fmt.Errorf("load iac file is not supported for kustomize") | ||
) | ||
|
||
// LoadIacFile is not supported for helm. Only loading chart directories are supported |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kustomize (not helm)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aaaah. thanks for pointing out
@dev-gaur, need to rebase this PR branch with master. Some merge conflicts. |
c37b9bc
to
92aa420
Compare
92aa420
to
442e39a
Compare
Hi @kanchwala-yusuf @williepaul I have addressed all the changes request. PTAL. |
pkg/utils/yaml.go
Outdated
|
||
// getIacDocumentList provides one or more IaC Documents. | ||
// Besides reading in file data, its main purpose is to determine and store line number and filename metadata | ||
func getIacDocumentList(scanner *bufio.Scanner, bytearray []byte, filePath string) ([]*IacDocument, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be better if the function name some how mentions YAML, as yaml documents are being created here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
technically we're passing a scanner object as input
e0c0d6b
to
8325926
Compare
) | ||
|
||
const ( | ||
kustomizedirectory string = "kustomization_directory" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you change this to "kustomization"
8325926
to
25f258c
Compare
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
Adds iac-provider that enables scanning directly from kustomize directories.