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

Add README and documentation #5

Merged
merged 19 commits into from Sep 19, 2020
Merged

Add README and documentation #5

merged 19 commits into from Sep 19, 2020

Conversation

viswajithiii
Copy link
Contributor

@viswajithiii viswajithiii commented Sep 17, 2020

  • 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.

@viswajithiii viswajithiii changed the title Add README Add README and documentation Sep 18, 2020
@viswajithiii viswajithiii marked this pull request as ready for review September 18, 2020 04:30
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.
Copy link
Contributor

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.
Copy link
Contributor

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)?

Copy link
Contributor Author

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.
Copy link
Contributor

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)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor Author

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.

}()

formatsToRenderFuncs = map[string]func([]check.Check, io.Writer){
common.PlainFormat: func(checks []check.Check, out io.Writer) {
Copy link
Contributor

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)

Copy link
Contributor Author

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.

return sb.String()
}()

formatsToRenderFuncs = map[string]func([]check.Check, io.Writer){
Copy link
Contributor

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?

Copy link
Contributor Author

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()
Copy link
Contributor

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?

Copy link
Contributor Author

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 formatToRenderFuncs. 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){
Copy link
Contributor

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
Copy link
Contributor

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?

Copy link
Contributor Author

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:
Copy link
Contributor

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..

Copy link
Contributor Author

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.

"backtick": func() string {
return "`"
},
"joinstrings": strings.Join,
Copy link
Contributor

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

Copy link
Contributor Author

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 {
Copy link
Contributor

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?

Copy link
Contributor Author

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
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error is alwasys nil.

key: "app"
checks:
# if doNotAutoAddDefaults is true, default checks are not automatically added.
# Otherwise, they are.
Copy link
Contributor

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

Copy link
Contributor

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

Copy link
Contributor

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

@@ -0,0 +1,9 @@
package ternary
Copy link
Contributor

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

Copy link
Contributor Author

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.

Copy link
Contributor

@connorgorman connorgorman left a 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

@viswajithiii viswajithiii merged commit 51143ce into main Sep 19, 2020
@viswajithiii viswajithiii deleted the viswa/readme branch September 19, 2020 01:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants