feat: add k8s components (#2589)

Co-authored-by: knqyf263 <knqyf263@gmail.com>
This commit is contained in:
Jose Donizetti
2022-10-24 08:51:02 -03:00
committed by GitHub
parent 5e25182c98
commit 9b0e9794cb
20 changed files with 705 additions and 144 deletions

View File

@@ -22,5 +22,6 @@ func clusterRun(ctx context.Context, opts flag.Options, cluster k8s.Cluster) err
return xerrors.Errorf("get k8s artifacts error: %w", err)
}
return run(ctx, opts, cluster.GetCurrentContext(), artifacts, true)
runner := newRunner(opts, cluster.GetCurrentContext())
return runner.run(ctx, artifacts)
}

View File

@@ -24,7 +24,8 @@ func namespaceRun(ctx context.Context, opts flag.Options, cluster k8s.Cluster) e
return xerrors.Errorf("get k8s artifacts error: %w", err)
}
return run(ctx, opts, cluster.GetCurrentContext(), artifacts, true)
runner := newRunner(opts, cluster.GetCurrentContext())
return runner.run(ctx, artifacts)
}
func getNamespace(opts flag.Options, currentNamespace string) string {

View File

@@ -22,6 +22,7 @@ func resourceRun(ctx context.Context, args []string, opts flag.Options, cluster
}
trivyk8s := trivyk8s.New(cluster, log.Logger).Namespace(getNamespace(opts, cluster.GetCurrentNamespace()))
runner := newRunner(opts, cluster.GetCurrentContext())
if len(name) == 0 { // pods or configmaps etc
if err = validateReportArguments(opts); err != nil {
@@ -33,7 +34,7 @@ func resourceRun(ctx context.Context, args []string, opts flag.Options, cluster
return err
}
return run(ctx, opts, cluster.GetCurrentContext(), targets, false)
return runner.run(ctx, targets)
}
// pod/NAME or pod NAME etc
@@ -42,7 +43,7 @@ func resourceRun(ctx context.Context, args []string, opts flag.Options, cluster
return err
}
return run(ctx, opts, cluster.GetCurrentContext(), []*artifacts.Artifact{artifact}, false)
return runner.run(ctx, []*artifacts.Artifact{artifact})
}
func extractKindAndName(args []string) (string, string, error) {

View File

@@ -44,8 +44,17 @@ func Run(ctx context.Context, args []string, opts flag.Options) error {
}
}
func run(ctx context.Context, opts flag.Options, cluster string, artifacts []*artifacts.Artifact, showEmpty bool) error {
ctx, cancel := context.WithTimeout(ctx, opts.Timeout)
type runner struct {
flagOpts flag.Options
cluster string
}
func newRunner(flagOpts flag.Options, cluster string) *runner {
return &runner{flagOpts, cluster}
}
func (r *runner) run(ctx context.Context, artifacts []*artifacts.Artifact) error {
ctx, cancel := context.WithTimeout(ctx, r.flagOpts.Timeout)
defer cancel()
var err error
@@ -55,7 +64,7 @@ func run(ctx context.Context, opts flag.Options, cluster string, artifacts []*ar
}
}()
runner, err := cmd.NewRunner(ctx, opts)
runner, err := cmd.NewRunner(ctx, r.flagOpts)
if err != nil {
if errors.Is(err, cmd.SkipScan) {
return nil
@@ -68,23 +77,25 @@ func run(ctx context.Context, opts flag.Options, cluster string, artifacts []*ar
}
}()
s := scanner.NewScanner(cluster, runner, opts)
s := scanner.NewScanner(r.cluster, runner, r.flagOpts)
r, err := s.Scan(ctx, artifacts)
rpt, err := s.Scan(ctx, artifacts)
if err != nil {
return xerrors.Errorf("k8s scan error: %w", err)
}
if err := report.Write(r, report.Option{
Format: opts.Format,
Report: opts.ReportFormat,
Output: opts.Output,
Severities: opts.Severities,
}, opts.ScanOptions.SecurityChecks, showEmpty); err != nil {
if err := report.Write(rpt, report.Option{
Format: r.flagOpts.Format,
Report: r.flagOpts.ReportFormat,
Output: r.flagOpts.Output,
Severities: r.flagOpts.Severities,
Components: r.flagOpts.Components,
SecurityChecks: r.flagOpts.ScanOptions.SecurityChecks,
}); err != nil {
return xerrors.Errorf("unable to write results: %w", err)
}
cmd.Exit(opts, r.Failed())
cmd.Exit(r.flagOpts, rpt.Failed())
return nil
}