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
Add README and documentation #5
Conversation
viswajithiii
commented
Sep 17, 2020
•
edited
edited
- Add README and documentation.
- Write commands to print out the list of checks and templates, both plain and in markdown format. The markdown docs are checked in to the repo for better documentation.
README.md
Outdated
with `.yaml` or `.yml` extensions are parsed, and Kubernetes objects are loaded from them. If a file is passed, | ||
it is parsed irrespective of extension. | ||
|
||
Users can pass a config file using the `--config` file to control which checks are executed, and to configure custom checks. |
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
flag
docs/README.md
Outdated
with `.yaml` or `.yml` extensions are parsed, and Kubernetes objects are loaded from them. If a file is passed, | ||
it is parsed irrespective of extension. | ||
|
||
Users can pass a config file using the `--config` file to control which checks are executed, and to configure custom checks. |
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.
flag again (is there some way to not duplicate the contents between this and the top-level readme)?
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.
Fixed. And I just removed the duplicate content from the top-level README in favour of just linking.
If you have `go` installed, you can run `go get golang.stackrox.io/kube-linter/cmd/kube-linter`. | ||
|
||
`kube-linter` binaries can be downloaded from [the Releases page](https://github.com/stackrox/kube-linter/releases). | ||
Download the `kube-linter` binary, and add it to your PATH. |
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.
IMHO a readme in an open source repository should always include custom build instructions and contributing guidelines (or references to these)
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.
👍
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.
Added instructions for building from source. Contributing guidelines, will do in a follow-up -- we still need to iron those out.
internal/command/checks/command.go
Outdated
}() | ||
|
||
formatsToRenderFuncs = map[string]func([]check.Check, io.Writer){ | ||
common.PlainFormat: func(checks []check.Check, out io.Writer) { |
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.
Move these to separate functions instead of inlining them. I would also encourage you to use Go templates for formatting. Especially the markdown formatting (a) looks ugly in code and (b) is extremely limited as you cannot add any custom styling, notes, etc.
(You might need to add either a custom function or translate the checks into a separate type to realize the "enabled by default" flag)
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.
Done. Also moved to templates.
internal/command/checks/command.go
Outdated
return sb.String() | ||
}() | ||
|
||
formatsToRenderFuncs = map[string]func([]check.Check, io.Writer){ |
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.
Possibly irrelevant (see below), but the brace following the closing parens without space in between looks weird - doesn't the linter flag this?
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.
That brace is actually consistent with go fmt
since it applies to the map
, and not the func
(although I agree it pattern matches with braces around a func
and looks weird).
) | ||
var ( | ||
// AllSupportedFormats contains the list of all supported formats. | ||
AllSupportedFormats = set.NewStringSet() |
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.
Does Go have some form of dependency resolution when calling global variable initializers? To me it looks like there is nothing ensuring that this initialization has taken place at them time when newFormat("plain")
above calls AllSupportedFormats.Add
.
It also seems weird that we store a set of supported formats. That should be a derived property, derived from a map mapping the IDs to something semantic like a render function. In short - why not derive this from formatToRenderFunc
?
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.
Does Go have some form of dependency resolution when calling global variable initializers? To me it looks like there is nothing ensuring that this initialization has taken place at them time when newFormat("plain") above calls AllSupportedFormats.Add.
Yup, Go handles this really well. See https://golang.org/ref/spec#Program_initialization_and_execution.
In short - why not derive this from formatToRenderFunc?
The problem is that it's shared between the checks
and templates
command, which have their own formatToRenderFunc
s. I've written unit tests to ensure the keys of that map and this are in sync.
return sb.String() | ||
}() | ||
|
||
formatsToRenderFuncs = map[string]func([]check.Template, io.Writer){ |
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.
See above
} | ||
) | ||
|
||
func renderPlain(checks []check.Check, out io.Writer) error { //nolint:unparam // The function signature is required to match formatToRenderFuncs |
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.
Why don't you use a Go template for this?
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.
This function is pretty simple, so I think it's better to keep it as is. I find normal code easier to read (plus the compile-time safety is nice).
} | ||
|
||
const ( | ||
markDownTemplateStr = `The following table enumerates built-in checks: |
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.
Ok so what I actually had in mind was something like --format go-template="{{ template str }}"
or --format go-template=@file
. It is fairly established in the Go world to allow users to specify Go templates for all "print"-like commands (e.g., go list
, kubectl get
.. etc) (although the file variant is something non-standard, but useful here).
This works for the initial iteration but having to resort to {{backtick}}
is.. ugly..
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.
Ahh got it. Yeah, I planned to support Go template output for the lint
command but wasn't sure if it was worth it for these commands... Will do in a follow-up.
internal/command/common/markdown.go
Outdated
"backtick": func() string { | ||
return "`" | ||
}, | ||
"joinstrings": strings.Join, |
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.
tbh I'd prefer to use a standard Go template library like sprig instead, especially if you want to eventually allow users to provide their own templates
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.
Done.
) | ||
|
||
var ( | ||
dashes = func() 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.
Maybe worth adding a stringutils.Repeat
?
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.
Done.
markDownTemplate = common.MustInstantiateTemplate(markDownTemplateStr) | ||
) | ||
|
||
func renderPlain(templates []check.Template, out io.Writer) error { //nolint:unparam // The function signature is required to match formatToRenderFuncs |
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.
which parameter is unused? oO
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.
error
is alwasys nil
.
config.yaml.example
Outdated
key: "app" | ||
checks: | ||
# if doNotAutoAddDefaults is true, default checks are not automatically added. | ||
# Otherwise, they are. |
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.
Remove, this is pretty obvious based on the previous line
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.
Also, I think doNotAutoAddDefaults is backwards, should be autoAddDefaults that defaults to true
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's also just a bit too verbose
internal/ternary/ternary.go
Outdated
@@ -0,0 +1,9 @@ | |||
package ternary |
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.
why is this in the PR? Also, adding a ternary is a bit of a strange thing in golang as it feels like it should be a language level thing
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 I was using it initially but not anymore. Removed.
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.
Minor stuff. Looks good