Skip to content

Commit

Permalink
refactor: expose kubeconform as a module
Browse files Browse the repository at this point in the history
  • Loading branch information
aabouzaid committed Apr 10, 2023
1 parent 16d5280 commit 8c19e7e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 29 deletions.
37 changes: 12 additions & 25 deletions cmd/kubeconform/main.go → cmd/kubeconform/validate.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package kubeconform

import (
"context"
Expand All @@ -15,8 +15,6 @@ import (
"github.com/yannh/kubeconform/pkg/validator"
)

var version = "development"

func processResults(cancel context.CancelFunc, o output.Output, validationResults <-chan validator.Result, exitOnError bool) <-chan bool {
success := true
result := make(chan bool)
Expand All @@ -28,7 +26,7 @@ func processResults(cancel context.CancelFunc, o output.Output, validationResult
}
if o != nil {
if err := o.Write(res); err != nil {
fmt.Fprint(os.Stderr, "failed writing log\n")
log.Fatal("failed writing log: ", err)
}
}
if !success && exitOnError {
Expand All @@ -46,29 +44,23 @@ func processResults(cancel context.CancelFunc, o output.Output, validationResult
return result
}

func realMain() int {
cfg, out, err := config.FromFlags(os.Args[0], os.Args[1:])
func Validate(cfg config.Config, out string) int {
if out != "" {
o := os.Stderr
o := cfg.Stream.Error
errCode := 1
if cfg.Help {
o = os.Stdout
o = cfg.Stream.Output
errCode = 0
}
fmt.Fprintln(o, out)
return errCode
}

if cfg.Version {
fmt.Println(version)
fmt.Fprintln(cfg.Stream.Output, out)
return 0
}

if err != nil {
fmt.Fprintf(os.Stderr, "failed parsing command line: %s\n", err.Error())
return 1
}

cpuProfileFile := os.Getenv("KUBECONFORM_CPUPROFILE_FILE")
if cpuProfileFile != "" {
f, err := os.Create(cpuProfileFile)
Expand All @@ -93,13 +85,12 @@ func realMain() int {
useStdin = true
}

var o output.Output
if o, err = output.New(cfg.OutputFormat, cfg.Summary, useStdin, cfg.Verbose); err != nil {
fmt.Fprintln(os.Stderr, err)
o, err := output.New(cfg.Stream.Output, cfg.OutputFormat, cfg.Summary, useStdin, cfg.Verbose)
if err != nil {
fmt.Fprintln(cfg.Stream.Error, err)
return 1
}
var v validator.Validator
v, err = validator.New(cfg.SchemaLocations, validator.Opts{
v, err := validator.New(cfg.SchemaLocations, validator.Opts{
Cache: cfg.Cache,
Debug: cfg.Debug,
SkipTLS: cfg.SkipTLS,
Expand All @@ -110,7 +101,7 @@ func realMain() int {
IgnoreMissingSchemas: cfg.IgnoreMissingSchemas,
})
if err != nil {
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(cfg.Stream.Error, err)
return 1
}

Expand All @@ -121,7 +112,7 @@ func realMain() int {
var resourcesChan <-chan resource.Resource
var errors <-chan error
if useStdin {
resourcesChan, errors = resource.FromStream(ctx, "stdin", os.Stdin)
resourcesChan, errors = resource.FromStream(ctx, "stdin", cfg.Stream.Input)
} else {
resourcesChan, errors = resource.FromFiles(ctx, cfg.Files, cfg.IgnoreFilenamePatterns)
}
Expand Down Expand Up @@ -176,7 +167,3 @@ func realMain() int {

return 0
}

func main() {
os.Exit(realMain())
}
20 changes: 20 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"fmt"
"os"

"github.com/yannh/kubeconform/cmd/kubeconform"
"github.com/yannh/kubeconform/pkg/config"
)

var version = "development"

func main() {
cfg, out, err := config.FromFlags(os.Args[0], os.Args[1:])
if err != nil {
fmt.Fprintf(os.Stderr, "failed parsing command line: %s\n", err.Error())
os.Exit(1)
}
os.Exit(kubeconform.Validate(cfg, out))
}
11 changes: 11 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ import (
"bytes"
"flag"
"fmt"
"io"
"os"
"strings"
)

type Stream struct {
Input io.Reader
Output io.Writer
Error io.Writer
}

type Config struct {
Cache string
Debug bool
Expand All @@ -26,6 +34,7 @@ type Config struct {
IgnoreFilenamePatterns []string
Help bool
Version bool
Stream *Stream
}

type arrayParam []string
Expand Down Expand Up @@ -62,7 +71,9 @@ func FromFlags(progName string, args []string) (Config, string, error) {

c := Config{}
c.Files = []string{}
c.Stream = &Stream{os.Stdin, os.Stdout, os.Stderr}

flags.SetOutput(c.Stream.Output)
flags.StringVar(&c.KubernetesVersion, "kubernetes-version", "master", "version of Kubernetes to validate against, e.g.: 1.18.0")
flags.Var(&schemaLocationsParam, "schema-location", "override schemas location search path (can be specified multiple times)")
flags.StringVar(&skipKindsCSV, "skip", "", "comma-separated list of kinds or GVKs to ignore")
Expand Down
6 changes: 2 additions & 4 deletions pkg/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package output

import (
"fmt"
"os"
"io"

"github.com/yannh/kubeconform/pkg/validator"
)
Expand All @@ -12,9 +12,7 @@ type Output interface {
Flush() error
}

func New(outputFormat string, printSummary, isStdin, verbose bool) (Output, error) {
w := os.Stdout

func New(w io.Writer, outputFormat string, printSummary, isStdin, verbose bool) (Output, error) {
switch {
case outputFormat == "json":
return jsonOutput(w, printSummary, isStdin, verbose), nil
Expand Down

0 comments on commit 8c19e7e

Please sign in to comment.