From 7b76406624d0d26ac1b1c575b7264c2d4322cd64 Mon Sep 17 00:00:00 2001 From: Varsha Varadarajan Date: Wed, 10 Jul 2019 17:00:52 -0400 Subject: [PATCH 1/2] Move run checks into the checks package --- checks/run_checks.go | 89 +++++++++++++++++++++++++++++++++++++++++ cmd/clusterlint/main.go | 69 +++----------------------------- 2 files changed, 94 insertions(+), 64 deletions(-) create mode 100644 checks/run_checks.go diff --git a/checks/run_checks.go b/checks/run_checks.go new file mode 100644 index 0000000..2612509 --- /dev/null +++ b/checks/run_checks.go @@ -0,0 +1,89 @@ +/* +Copyright 2019 DigitalOcean + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package checks + +import ( + "fmt" + "sync" + + "github.com/digitalocean/clusterlint/kube" + "golang.org/x/sync/errgroup" +) + +// Run applies the filters and runs the resultant check list in parallel +func Run(client *kube.Client, checkFilter CheckFilter, severity Severity) ([]Diagnostic, error) { + objects, err := client.FetchObjects() + if err != nil { + return nil, err + } + + all, err := checkFilter.FilterChecks() + if err != nil { + return nil, err + } + if len(all) == 0 { + return nil, fmt.Errorf("No checks to run. Are you sure that you provided the right names for groups and checks?") + } + var diagnostics []Diagnostic + var mu sync.Mutex + var g errgroup.Group + + for _, check := range all { + check := check + g.Go(func() error { + fmt.Println("Running check: ", check.Name()) + d, err := check.Run(objects) + if err != nil { + return err + } + mu.Lock() + diagnostics = append(diagnostics, d...) + mu.Unlock() + return nil + }) + } + err = g.Wait() + if err != nil { + return nil, err + } + diagnostics = filterEnabled(diagnostics) + diagnostics = filterSeverity(severity, diagnostics) + return diagnostics, err +} + +func filterEnabled(diagnostics []Diagnostic) []Diagnostic { + var ret []Diagnostic + for _, d := range diagnostics { + if IsEnabled(d.Check, d.Object) { + ret = append(ret, d) + } + } + return ret +} + +func filterSeverity(level Severity, diagnostics []Diagnostic) []Diagnostic { + if level == "" { + return diagnostics + } + var ret []Diagnostic + for _, d := range diagnostics { + if d.Severity == level { + ret = append(ret, d) + } + } + return ret +} diff --git a/cmd/clusterlint/main.go b/cmd/clusterlint/main.go index c49ff45..837ddd8 100644 --- a/cmd/clusterlint/main.go +++ b/cmd/clusterlint/main.go @@ -21,13 +21,11 @@ import ( "fmt" "os" "path/filepath" - "sync" "github.com/digitalocean/clusterlint/checks" "github.com/digitalocean/clusterlint/kube" "github.com/fatih/color" "github.com/urfave/cli" - "golang.org/x/sync/errgroup" // Side-effect import to get all the checks registered. _ "github.com/digitalocean/clusterlint/checks/all" @@ -133,46 +131,14 @@ func runChecks(c *cli.Context) error { return err } - objects, err := client.FetchObjects() - if err != nil { - return err - } - - return run(objects, c) -} - -func run(objects *kube.Objects, c *cli.Context) error { filter, err := checks.NewCheckFilter(c.StringSlice("g"), c.StringSlice("G"), c.StringSlice("c"), c.StringSlice("C")) if err != nil { return err } - all, err := filter.FilterChecks() - if err != nil { - return err - } - if len(all) == 0 { - return fmt.Errorf("No checks to run. Are you sure that you provided the right names for groups and checks?") - } - var diagnostics []checks.Diagnostic - var mu sync.Mutex - var g errgroup.Group + level := checks.Severity(c.String("level")) + diagnostics, err := checks.Run(client, filter, level) - for _, check := range all { - check := check - g.Go(func() error { - fmt.Println("Running check: ", check.Name()) - d, err := check.Run(objects) - if err != nil { - return err - } - mu.Lock() - diagnostics = append(diagnostics, d...) - mu.Unlock() - return nil - }) - } - err = g.Wait() write(diagnostics, c) return err @@ -180,12 +146,10 @@ func run(objects *kube.Objects, c *cli.Context) error { func write(diagnostics []checks.Diagnostic, c *cli.Context) error { output := c.String("output") - level := checks.Severity(c.String("level")) - filtered := filterEnabled(diagnostics) - filtered = filterSeverity(level, filtered) + switch output { case "json": - err := json.NewEncoder(os.Stdout).Encode(filtered) + err := json.NewEncoder(os.Stdout).Encode(diagnostics) if err != nil { return err } @@ -196,7 +160,7 @@ func write(diagnostics []checks.Diagnostic, c *cli.Context) error { e := color.New(color.FgRed) w := color.New(color.FgYellow) s := color.New(color.FgBlue) - for _, d := range filtered { + for _, d := range diagnostics { switch d.Severity { case checks.Error: e.Println(d) @@ -212,26 +176,3 @@ func write(diagnostics []checks.Diagnostic, c *cli.Context) error { return nil } - -func filterEnabled(diagnostics []checks.Diagnostic) []checks.Diagnostic { - var ret []checks.Diagnostic - for _, d := range diagnostics { - if checks.IsEnabled(d.Check, d.Object) { - ret = append(ret, d) - } - } - return ret -} - -func filterSeverity(level checks.Severity, diagnostics []checks.Diagnostic) []checks.Diagnostic { - if level == "" { - return diagnostics - } - var ret []checks.Diagnostic - for _, d := range diagnostics { - if d.Severity == level { - ret = append(ret, d) - } - } - return ret -} From 848515cb45b1a43f00d4820a5cf688168fc6d1a4 Mon Sep 17 00:00:00 2001 From: Varsha Varadarajan Date: Thu, 11 Jul 2019 15:20:05 -0400 Subject: [PATCH 2/2] Introduce DiagnosticFilter to store fields based on which diagnostics will be filtered --- checks/diagnostic.go | 5 +++++ checks/run_checks.go | 9 ++++----- cmd/clusterlint/main.go | 5 +++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/checks/diagnostic.go b/checks/diagnostic.go index be4bd5e..6557c56 100644 --- a/checks/diagnostic.go +++ b/checks/diagnostic.go @@ -37,6 +37,11 @@ func (d Diagnostic) String() string { d.Kind, d.Object.Name, d.Message) } +// DiagnosticFilter indicates conditions to filter diagnostics on +type DiagnosticFilter struct { + Severity Severity +} + // Severity identifies the level of priority for each diagnostic. type Severity string diff --git a/checks/run_checks.go b/checks/run_checks.go index 2612509..9199984 100644 --- a/checks/run_checks.go +++ b/checks/run_checks.go @@ -17,7 +17,7 @@ limitations under the License. package checks import ( - "fmt" + "errors" "sync" "github.com/digitalocean/clusterlint/kube" @@ -25,7 +25,7 @@ import ( ) // Run applies the filters and runs the resultant check list in parallel -func Run(client *kube.Client, checkFilter CheckFilter, severity Severity) ([]Diagnostic, error) { +func Run(client *kube.Client, checkFilter CheckFilter, diagnosticFilter DiagnosticFilter) ([]Diagnostic, error) { objects, err := client.FetchObjects() if err != nil { return nil, err @@ -36,7 +36,7 @@ func Run(client *kube.Client, checkFilter CheckFilter, severity Severity) ([]Dia return nil, err } if len(all) == 0 { - return nil, fmt.Errorf("No checks to run. Are you sure that you provided the right names for groups and checks?") + return nil, errors.New("No checks to run. Are you sure that you provided the right names for groups and checks?") } var diagnostics []Diagnostic var mu sync.Mutex @@ -45,7 +45,6 @@ func Run(client *kube.Client, checkFilter CheckFilter, severity Severity) ([]Dia for _, check := range all { check := check g.Go(func() error { - fmt.Println("Running check: ", check.Name()) d, err := check.Run(objects) if err != nil { return err @@ -61,7 +60,7 @@ func Run(client *kube.Client, checkFilter CheckFilter, severity Severity) ([]Dia return nil, err } diagnostics = filterEnabled(diagnostics) - diagnostics = filterSeverity(severity, diagnostics) + diagnostics = filterSeverity(diagnosticFilter.Severity, diagnostics) return diagnostics, err } diff --git a/cmd/clusterlint/main.go b/cmd/clusterlint/main.go index 837ddd8..59323a0 100644 --- a/cmd/clusterlint/main.go +++ b/cmd/clusterlint/main.go @@ -136,8 +136,9 @@ func runChecks(c *cli.Context) error { return err } - level := checks.Severity(c.String("level")) - diagnostics, err := checks.Run(client, filter, level) + diagnosticFilter := checks.DiagnosticFilter{Severity: checks.Severity(c.String("level"))} + + diagnostics, err := checks.Run(client, filter, diagnosticFilter) write(diagnostics, c)