2021-02-27 18:11:49 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2021-03-05 13:26:47 +00:00
|
|
|
"context"
|
2021-02-27 18:11:49 +00:00
|
|
|
"flag"
|
|
|
|
"net/http"
|
|
|
|
|
2021-07-23 08:05:20 +00:00
|
|
|
"github.com/kyverno/policy-reporter/pkg/config"
|
|
|
|
"github.com/kyverno/policy-reporter/pkg/metrics"
|
|
|
|
"github.com/kyverno/policy-reporter/pkg/report"
|
|
|
|
"github.com/kyverno/policy-reporter/pkg/target"
|
2021-02-27 18:11:49 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
|
|
"github.com/spf13/cobra"
|
2021-03-05 13:26:47 +00:00
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
2021-02-27 18:11:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func newRunCMD() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "run",
|
|
|
|
Short: "Run PolicyReporter Watcher & HTTP Metrics Server",
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
c, err := loadConfig(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-05 13:26:47 +00:00
|
|
|
var k8sConfig *rest.Config
|
|
|
|
if c.Kubeconfig != "" {
|
|
|
|
k8sConfig, err = clientcmd.BuildConfigFromFlags("", c.Kubeconfig)
|
|
|
|
} else {
|
|
|
|
k8sConfig, err = rest.InClusterConfig()
|
|
|
|
}
|
2021-02-27 18:11:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-05 13:26:47 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
resolver := config.NewResolver(c, k8sConfig)
|
|
|
|
|
2021-05-18 11:50:02 +00:00
|
|
|
client, err := resolver.PolicyReportClient(ctx)
|
2021-02-27 18:11:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-18 11:50:02 +00:00
|
|
|
client.RegisterCallback(metrics.CreateMetricsCallback())
|
2021-02-27 18:11:49 +00:00
|
|
|
|
2021-03-05 13:26:47 +00:00
|
|
|
targets := resolver.TargetClients()
|
2021-02-27 18:11:49 +00:00
|
|
|
|
2021-03-05 13:26:47 +00:00
|
|
|
if len(targets) > 0 {
|
2021-05-18 11:50:02 +00:00
|
|
|
client.RegisterPolicyResultCallback(func(r report.Result, e bool) {
|
2021-02-27 18:11:49 +00:00
|
|
|
for _, t := range targets {
|
|
|
|
go func(target target.Client, result report.Result, preExisted bool) {
|
|
|
|
if preExisted && target.SkipExistingOnStartup() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
target.Send(result)
|
|
|
|
}(t, r, e)
|
|
|
|
}
|
2021-03-05 13:26:47 +00:00
|
|
|
})
|
|
|
|
|
2021-05-18 11:50:02 +00:00
|
|
|
client.RegisterPolicyResultWatcher(resolver.SkipExistingOnStartup())
|
2021-03-05 13:26:47 +00:00
|
|
|
}
|
2021-02-27 18:11:49 +00:00
|
|
|
|
2021-04-24 10:32:15 +00:00
|
|
|
errorChan := make(chan error)
|
2021-03-13 18:56:38 +00:00
|
|
|
|
2021-05-18 11:50:02 +00:00
|
|
|
go func() { errorChan <- client.StartWatching() }()
|
2021-04-24 10:32:15 +00:00
|
|
|
|
2021-08-09 18:53:04 +00:00
|
|
|
go func() { errorChan <- resolver.APIServer().Start() }()
|
|
|
|
|
2021-04-24 10:32:15 +00:00
|
|
|
go func() {
|
2021-02-27 18:11:49 +00:00
|
|
|
http.Handle("/metrics", promhttp.Handler())
|
|
|
|
|
2021-04-24 10:32:15 +00:00
|
|
|
errorChan <- http.ListenAndServe(":2112", nil)
|
|
|
|
}()
|
2021-02-27 18:11:49 +00:00
|
|
|
|
2021-04-24 10:32:15 +00:00
|
|
|
return <-errorChan
|
2021-02-27 18:11:49 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// For local usage
|
|
|
|
cmd.PersistentFlags().StringP("kubeconfig", "k", "", "absolute path to the kubeconfig file")
|
|
|
|
cmd.PersistentFlags().StringP("config", "c", "", "target configuration file")
|
2021-05-29 08:57:06 +00:00
|
|
|
cmd.PersistentFlags().IntP("apiPort", "a", 8080, "http port for the optional rest api")
|
2021-02-27 18:11:49 +00:00
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|