Skip to content

Commit

Permalink
docs: add code comments
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusfm committed Apr 3, 2023
1 parent aebd229 commit d3bc18f
Show file tree
Hide file tree
Showing 13 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions pkg/loader/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/undistro/marvin/pkg/types"
)

// Builtins represents the builtins checks
var Builtins []types.Check

func init() {
Expand Down
4 changes: 4 additions & 0 deletions pkg/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/printers/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 1 addition & 0 deletions pkg/printers/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions pkg/printers/md.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions pkg/printers/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions pkg/printers/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions pkg/validator/activation.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
KubeVersionVarName = "kubeVersion"
)

// activation implements the interpreter.Activation
type activation struct {
object map[string]any
podMeta map[string]any
Expand Down
1 change: 1 addition & 0 deletions pkg/validator/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions pkg/validator/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions pkg/validator/podspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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]
Expand Down
1 change: 1 addition & 0 deletions pkg/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d3bc18f

Please sign in to comment.