1
0
Fork 0
mirror of https://github.com/kyverno/policy-reporter.git synced 2024-12-14 11:57:32 +00:00
policy-reporter/cmd/root.go
Frank Jogeleit 0a07555e57
Development (#5)
* Implement loki.skipExistingOnStartup to prevent dulicated logs after deployment
* Implement loki.minimumPriority to configure which results should be send
2021-02-22 01:13:35 +01:00

125 lines
2.8 KiB
Go

package cmd
import (
"flag"
"log"
"net/http"
"github.com/fjogeleit/policy-reporter/pkg/config"
"github.com/fjogeleit/policy-reporter/pkg/report"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
type PolicySeverity = string
const (
Fail PolicySeverity = "fail"
Warn PolicySeverity = "warn"
Error PolicySeverity = "error"
Pass PolicySeverity = "pass"
Skip PolicySeverity = "skip"
)
func NewCLI() *cobra.Command {
rootCmd := &cobra.Command{
Use: "run",
Short: "Kyverno Policy API",
Long: `Kyverno Policy API and Monitoring`,
RunE: func(cmd *cobra.Command, args []string) error {
c, err := LoadConfig(cmd)
if err != nil {
return err
}
resolver := config.NewResolver(c)
client, err := resolver.KubernetesClient()
if err != nil {
return err
}
loki := resolver.LokiClient()
if loki != nil {
go client.WatchRuleValidation(func(r report.Result) {
go loki.Send(r)
}, c.Loki.SkipExisting)
}
policyMetrics, err := resolver.PolicyReportMetrics()
if err != nil {
return err
}
go policyMetrics.GenerateMetrics()
clusterPolicyMetrics, err := resolver.ClusterPolicyReportMetrics()
if err != nil {
return err
}
go clusterPolicyMetrics.GenerateMetrics()
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":2112", nil)
return nil
},
}
rootCmd.PersistentFlags().StringP("kubeconfig", "k", "", "absolute path to the kubeconfig file")
rootCmd.PersistentFlags().String("loki", "", "loki host: http://loki:3100")
rootCmd.PersistentFlags().String("loki-minimum-priority", "", "Minimum Priority to send Results to Loki (info < warning < error)")
rootCmd.PersistentFlags().Bool("loki-skip-exising-on-startup", false, "Skip Results created before PolicyReporter started. Prevent duplicated sending after new deployment")
flag.Parse()
return rootCmd
}
func LoadConfig(cmd *cobra.Command) (*config.Config, error) {
v := viper.New()
cfgFile := ""
configFlag := cmd.Flags().Lookup("config")
if configFlag != nil {
cfgFile = configFlag.Value.String()
}
if cfgFile != "" {
v.SetConfigFile(cfgFile)
} else {
v.AddConfigPath(".")
v.SetConfigName("config")
}
v.AutomaticEnv()
if err := v.ReadInConfig(); err != nil {
log.Println("no config provided")
}
if flag := cmd.Flags().Lookup("loki"); flag != nil {
v.BindPFlag("loki.host", flag)
}
if flag := cmd.Flags().Lookup("loki-minimum-priority"); flag != nil {
v.BindPFlag("loki.minimumPriority", flag)
}
if flag := cmd.Flags().Lookup("loki-skip-exising-on-startup"); flag != nil {
v.BindPFlag("loki.skipExistingOnStartup", flag)
}
if flag := cmd.Flags().Lookup("kubeconfig"); flag != nil {
v.BindPFlag("kubeconfig", flag)
}
c := &config.Config{}
err := v.Unmarshal(c)
return c, err
}