From d3bc18f52664d91eb96c6797f55b494260b45856 Mon Sep 17 00:00:00 2001 From: Matheus Moraes Date: Mon, 3 Apr 2023 10:59:41 -0300 Subject: [PATCH] docs: add code comments --- pkg/cmd/scan.go | 1 + pkg/loader/builtin.go | 1 + pkg/loader/loader.go | 4 ++++ pkg/printers/interface.go | 1 + pkg/printers/json.go | 1 + pkg/printers/md.go | 1 + pkg/printers/table.go | 1 + pkg/printers/yaml.go | 1 + pkg/validator/activation.go | 1 + pkg/validator/compiler.go | 1 + pkg/validator/interface.go | 1 + pkg/validator/podspec.go | 3 +++ pkg/validator/validator.go | 1 + 13 files changed, 18 insertions(+) diff --git a/pkg/cmd/scan.go b/pkg/cmd/scan.go index c6e798a..3a8a3a8 100644 --- a/pkg/cmd/scan.go +++ b/pkg/cmd/scan.go @@ -60,6 +60,7 @@ type ScanOptions struct { gvrs map[string]string } +// NewScanOptions returns a ScanOptions with the default values func NewScanOptions() *ScanOptions { return &ScanOptions{ ConfigFlags: genericclioptions.NewConfigFlags(false), diff --git a/pkg/loader/builtin.go b/pkg/loader/builtin.go index 46526ed..92e121b 100644 --- a/pkg/loader/builtin.go +++ b/pkg/loader/builtin.go @@ -22,6 +22,7 @@ import ( "github.com/undistro/marvin/pkg/types" ) +// Builtins represents the builtins checks var Builtins []types.Check func init() { diff --git a/pkg/loader/loader.go b/pkg/loader/loader.go index 1355ae2..668c7e5 100644 --- a/pkg/loader/loader.go +++ b/pkg/loader/loader.go @@ -33,6 +33,7 @@ type ( readFileFunc func(string) ([]byte, error) ) +// toList returns a slice of Check func (cm ChecksMap) toList() []types.Check { if cm == nil { return nil @@ -44,17 +45,20 @@ func (cm ChecksMap) toList() []types.Check { return list } +// supportedExt supported file extensions for checks and tests var supportedExt = map[string]bool{ ".yaml": true, ".yml": true, ".json": true, } +// LoadChecks loads all checks from given path recursively func LoadChecks(root string) ([]types.Check, error) { c, _, err := load(root) return c.toList(), err } +// LoadChecksAndTests loads all checks and their tests from given path recursively func LoadChecksAndTests(root string) (ChecksMap, TestsMap, error) { return load(root) } diff --git a/pkg/printers/interface.go b/pkg/printers/interface.go index 14f1c89..fb8c5c4 100644 --- a/pkg/printers/interface.go +++ b/pkg/printers/interface.go @@ -20,6 +20,7 @@ import ( "github.com/undistro/marvin/pkg/types" ) +// Printer is an interface that defines a method for printing a Report to an io.Writer type Printer interface { PrintObj(types.Report, io.Writer) error } diff --git a/pkg/printers/json.go b/pkg/printers/json.go index b730131..e5736a0 100644 --- a/pkg/printers/json.go +++ b/pkg/printers/json.go @@ -21,6 +21,7 @@ import ( "github.com/undistro/marvin/pkg/types" ) +// JSONPrinter implements a Printer that prints the report in JSON format type JSONPrinter struct{} func (*JSONPrinter) PrintObj(report types.Report, w io.Writer) error { diff --git a/pkg/printers/md.go b/pkg/printers/md.go index 34f6f2d..ef2ec30 100644 --- a/pkg/printers/md.go +++ b/pkg/printers/md.go @@ -22,6 +22,7 @@ import ( "github.com/undistro/marvin/pkg/types" ) +// MarkdownPrinter implements a Printer that prints the report in Markdown format type MarkdownPrinter struct{} func (*MarkdownPrinter) PrintObj(report types.Report, w io.Writer) error { diff --git a/pkg/printers/table.go b/pkg/printers/table.go index b0485d6..eb31a0f 100644 --- a/pkg/printers/table.go +++ b/pkg/printers/table.go @@ -34,6 +34,7 @@ var ( green = color.New(color.FgGreen).SprintfFunc() ) +// TablePrinter implements a Printer that prints the report in table format type TablePrinter struct{} func (*TablePrinter) PrintObj(report types.Report, w io.Writer) error { diff --git a/pkg/printers/yaml.go b/pkg/printers/yaml.go index c39a780..8caf9e2 100644 --- a/pkg/printers/yaml.go +++ b/pkg/printers/yaml.go @@ -23,6 +23,7 @@ import ( "github.com/undistro/marvin/pkg/types" ) +// YAMLPrinter implements a Printer that prints the report in YAML format type YAMLPrinter struct{} func (*YAMLPrinter) PrintObj(report types.Report, w io.Writer) error { diff --git a/pkg/validator/activation.go b/pkg/validator/activation.go index 7fccfb4..5202abc 100644 --- a/pkg/validator/activation.go +++ b/pkg/validator/activation.go @@ -28,6 +28,7 @@ const ( KubeVersionVarName = "kubeVersion" ) +// activation implements the interpreter.Activation type activation struct { object map[string]any podMeta map[string]any diff --git a/pkg/validator/compiler.go b/pkg/validator/compiler.go index 6fc2d04..45d6764 100644 --- a/pkg/validator/compiler.go +++ b/pkg/validator/compiler.go @@ -26,6 +26,7 @@ import ( "github.com/undistro/marvin/pkg/types" ) +// Compile compiles the expressions of the given check and returns a Validator func Compile(check types.Check, apiResources []*metav1.APIResourceList, kubeVersion *version.Info) (Validator, error) { if len(check.Validations) == 0 { return nil, errors.New("invalid check: a check must have at least 1 validation") diff --git a/pkg/validator/interface.go b/pkg/validator/interface.go index f953c4e..4aac131 100644 --- a/pkg/validator/interface.go +++ b/pkg/validator/interface.go @@ -19,6 +19,7 @@ import ( "k8s.io/apimachinery/pkg/version" ) +// Validator is an interface that defines a method for validating a k8s unustructured object type Validator interface { Validate(obj unstructured.Unstructured, params any) (bool, string, error) SetAPIVersions(apiVersions []string) diff --git a/pkg/validator/podspec.go b/pkg/validator/podspec.go index 46a003f..5e3386d 100644 --- a/pkg/validator/podspec.go +++ b/pkg/validator/podspec.go @@ -28,6 +28,7 @@ import ( "github.com/undistro/marvin/pkg/types" ) +// MatchesPodSpec returns true if any rule matches a Pod spec func MatchesPodSpec(rules []types.ResourceRule) bool { for _, r := range rules { gr := r.ToGVR().GroupResource() @@ -50,12 +51,14 @@ var defaultPodSpecResources = map[schema.GroupResource]bool{ batchv1.Resource("cronjobs"): true, } +// HasPodSpec returns true if the given object has a Pod spec func HasPodSpec(u unstructured.Unstructured) bool { gk := u.GroupVersionKind().GroupKind() _, ok := defaultPodSpecTypes[gk] return ok } +// ExtractPodSpec returns the metadata and Pod spec from the given object func ExtractPodSpec(u unstructured.Unstructured) (*metav1.ObjectMeta, *corev1.PodSpec, error) { gk := u.GroupVersionKind().GroupKind() obj, ok := defaultPodSpecTypes[gk] diff --git a/pkg/validator/validator.go b/pkg/validator/validator.go index 196e336..9958c3b 100644 --- a/pkg/validator/validator.go +++ b/pkg/validator/validator.go @@ -26,6 +26,7 @@ import ( marvin "github.com/undistro/marvin/pkg/types" ) +// CELValidator is a Validator that performs CEL expressions type CELValidator struct { check marvin.Check programs []cel.Program