Move run checks into the checks package
parent
4ac0ffbf52
commit
7b76406624
|
@ -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
|
||||||
|
}
|
|
@ -21,13 +21,11 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/digitalocean/clusterlint/checks"
|
"github.com/digitalocean/clusterlint/checks"
|
||||||
"github.com/digitalocean/clusterlint/kube"
|
"github.com/digitalocean/clusterlint/kube"
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
"golang.org/x/sync/errgroup"
|
|
||||||
|
|
||||||
// Side-effect import to get all the checks registered.
|
// Side-effect import to get all the checks registered.
|
||||||
_ "github.com/digitalocean/clusterlint/checks/all"
|
_ "github.com/digitalocean/clusterlint/checks/all"
|
||||||
|
@ -133,46 +131,14 @@ func runChecks(c *cli.Context) error {
|
||||||
return err
|
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"))
|
filter, err := checks.NewCheckFilter(c.StringSlice("g"), c.StringSlice("G"), c.StringSlice("c"), c.StringSlice("C"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
all, err := filter.FilterChecks()
|
level := checks.Severity(c.String("level"))
|
||||||
if err != nil {
|
diagnostics, err := checks.Run(client, filter, level)
|
||||||
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
|
|
||||||
|
|
||||||
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)
|
write(diagnostics, c)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
|
@ -180,12 +146,10 @@ func run(objects *kube.Objects, c *cli.Context) error {
|
||||||
|
|
||||||
func write(diagnostics []checks.Diagnostic, c *cli.Context) error {
|
func write(diagnostics []checks.Diagnostic, c *cli.Context) error {
|
||||||
output := c.String("output")
|
output := c.String("output")
|
||||||
level := checks.Severity(c.String("level"))
|
|
||||||
filtered := filterEnabled(diagnostics)
|
|
||||||
filtered = filterSeverity(level, filtered)
|
|
||||||
switch output {
|
switch output {
|
||||||
case "json":
|
case "json":
|
||||||
err := json.NewEncoder(os.Stdout).Encode(filtered)
|
err := json.NewEncoder(os.Stdout).Encode(diagnostics)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -196,7 +160,7 @@ func write(diagnostics []checks.Diagnostic, c *cli.Context) error {
|
||||||
e := color.New(color.FgRed)
|
e := color.New(color.FgRed)
|
||||||
w := color.New(color.FgYellow)
|
w := color.New(color.FgYellow)
|
||||||
s := color.New(color.FgBlue)
|
s := color.New(color.FgBlue)
|
||||||
for _, d := range filtered {
|
for _, d := range diagnostics {
|
||||||
switch d.Severity {
|
switch d.Severity {
|
||||||
case checks.Error:
|
case checks.Error:
|
||||||
e.Println(d)
|
e.Println(d)
|
||||||
|
@ -212,26 +176,3 @@ func write(diagnostics []checks.Diagnostic, c *cli.Context) error {
|
||||||
|
|
||||||
return nil
|
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
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue