cmd

package
v0.38.52 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2021 License: Apache-2.0 Imports: 48 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Docs = &cobra.Command{
	Use:    "docs",
	Short:  "Generate docs ",
	Hidden: true,
	Args:   cobra.MinimumNArgs(0),
	Run: func(cmd *cobra.Command, args []string) {
		printAPIDocs(args)
	},
}
View Source
var Operator = &cobra.Command{
	Use:   "operator",
	Short: "Start the kubernetes operator",
	Run:   run,
}
View Source
var Root = &cobra.Command{
	Use: "canary-checker",
	PersistentPreRun: func(cmd *cobra.Command, args []string) {
		logger.UseZap(cmd.Flags())
	},
}
View Source
var Run = &cobra.Command{
	Use:   "run <canary.yaml>",
	Short: "Execute checks and return",
	Run: func(cmd *cobra.Command, configFiles []string) {
		timer := timer.NewTimer()
		if len(configFiles) == 0 {
			log.Fatalln("Must specify at least one canary")
		}
		kommonsClient, err := pkg.NewKommonsClient()
		if err != nil {
			logger.Warnf("Failed to get kommons client, features that read kubernetes configs will fail: %v", err)
		}
		var results = []*pkg.CheckResult{}

		wg := sync.WaitGroup{}
		queue := make(chan []*pkg.CheckResult, 1)

		for _, configfile := range configFiles {
			logger.Infof("Checking %s", configfile)
			configs, err := pkg.ParseConfig(configfile)
			if err != nil {
				logger.Errorf("Could not parse %s: %v", configfile, err)
				continue
			}
			for _, config := range configs {
				if namespace != "" {
					config.Namespace = namespace
				}
				if config.Name == "" {
					config.Name = CleanupFilename(configfile)
				}
				wg.Add(1)
				_config := config
				go func() {
					queue <- checks.RunChecks(context.New(kommonsClient, _config))
					wg.Done()
				}()
			}
		}
		failed := 0
		passed := 0

		go func() {
			wg.Wait()
			close(queue)
		}()

		for item := range queue {
			for _, result := range item {
				if !result.Pass {
					failed++
				} else {
					passed++
				}
				fmt.Printf("%s \t%s\t\n", time.Now().Format(time.RFC3339), result.String())
				results = append(results, result)
			}
		}

		if junit {
			report := output.GetJunitReport(results)
			if err := output.HandleOutput(report, outputFile); err != nil {
				logger.Fatalf("error writing output file: %v", err)
			}
		}
		if csv {
			report, err := output.GetCSVReport(results)
			if err != nil {
				logger.Fatalf("error generating CSV file: %v", err)
			}
			if err := output.HandleOutput(report, outputFile); err != nil {
				logger.Fatalf("error writing output file: %v", err)
			}
		}

		logger.Infof("%d passed, %d failed in %s", passed, failed, timer)

		if failed > 0 {
			os.Exit(1)
		}
	},
}
View Source
var Serve = &cobra.Command{
	Use:   "serve",
	Short: "Start a server to execute checks ",
	Run: func(cmd *cobra.Command, args []string) {

		configs, err := pkg.ParseConfig(configFile)
		if err != nil {
			logger.Fatalf("could not parse %s: %v", configFile, err)
		}
		kommonsClient, err := pkg.NewKommonsClient()
		if err != nil {
			logger.Warnf("Failed to get kommons client, features that read kubernetes config will fail: %v", err)
		}
		cron := cron.New()

		for _, canary := range configs {
			if schedule == "" {
				if canary.Spec.Schedule != "" {
					schedule = canary.Spec.Schedule
				} else if canary.Spec.Interval > 0 {
					schedule = fmt.Sprintf("@every %ds", canary.Spec.Interval)
				}
			}
			canary.SetRunnerName(runner.RunnerName)
			for _, _c := range checks.All {
				c := _c
				if !checks.Checks(canary.Spec.GetAllChecks()).Includes(c) {
					continue
				}
				cron.AddFunc(schedule, func() {
					go func() {
						for _, result := range checks.RunChecks(context.New(kommonsClient, canary)) {
							if logPass && result.Pass || logFail && !result.Pass {
								logger.Infof(result.String())
							}
							cache.AddCheck(canary, result)
							metrics.Record(canary, result)
						}
					}()
				})
			}
		}
		cron.Start()
		serve()
	},
}

Functions

func CleanupFilename added in v0.33.0

func CleanupFilename(fileName string) string

func ParseDocumentationFrom

func ParseDocumentationFrom(srcs []string) map[string]*Struct

ParseDocumentationFrom gets all types' documentation and returns them as an array. Each type is again represented as an array (we have to use arrays as we need to be sure for the order of the fields). This function returns fields and struct definitions that have no documentation as {name, ""}.

func ServerFlags added in v0.38.4

func ServerFlags(flags *pflag.FlagSet)

Types

type Struct

type Struct struct {
	Name, Doc  string
	StructType *ast.StructType
	DocType    *doc.Type
	Fields     StructFields
}

func (Struct) Print added in v0.38.32

func (strukt Struct) Print(structs map[string]*Struct) string

type StructField

type StructField struct {
	Name, Doc, Type, ID string
	Mandatory           bool
	Markdown            string
}

func NewStructField

func NewStructField(field *ast.Field) StructField

func (StructField) MarkdownRow added in v0.38.32

func (f StructField) MarkdownRow(structs map[string]*Struct) string

type StructFields added in v0.38.32

type StructFields []StructField

func (StructFields) GetAllFields added in v0.38.32

func (s StructFields) GetAllFields(structs map[string]*Struct) StructFields

func (StructFields) Len added in v0.38.32

func (s StructFields) Len() int

func (StructFields) Less added in v0.38.32

func (s StructFields) Less(i, j int) bool

func (StructFields) Swap added in v0.38.32

func (s StructFields) Swap(i, j int)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL