Merge pull request #36 from digitalocean/varsha/refactor-run-checks

Move run checks into the checks package
image-warning-sha256
Varsha Varadarajan 2019-07-11 15:20:26 -04:00 committed by GitHub
commit 48f302945b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 99 additions and 64 deletions

View File

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

88
checks/run_checks.go Normal file
View File

@ -0,0 +1,88 @@
/*
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 (
"errors"
"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, diagnosticFilter DiagnosticFilter) ([]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, 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
var g errgroup.Group
for _, check := range all {
check := check
g.Go(func() error {
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(diagnosticFilter.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
}

View File

@ -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,15 @@ 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
diagnosticFilter := checks.DiagnosticFilter{Severity: checks.Severity(c.String("level"))}
diagnostics, err := checks.Run(client, filter, diagnosticFilter)
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 +147,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 +161,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 +177,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
}