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"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/fjogeleit/policy-reporter/pkg/config"
|
|
|
|
"github.com/fjogeleit/policy-reporter/pkg/report"
|
|
|
|
"github.com/fjogeleit/policy-reporter/pkg/target"
|
|
|
|
"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 newSendCMD() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "send",
|
|
|
|
Short: "Send all current PolicyReportResults to the configured targets",
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resolver := config.NewResolver(c, k8sConfig)
|
2021-02-27 18:11:49 +00:00
|
|
|
|
2021-03-05 13:26:47 +00:00
|
|
|
client, err := resolver.PolicyResultClient(context.Background())
|
2021-02-27 18:11:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
clients := resolver.TargetClients()
|
|
|
|
|
|
|
|
if len(clients) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-05 13:26:47 +00:00
|
|
|
results, err := client.FetchPolicyResults()
|
2021-02-27 18:11:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(len(results) * len(clients))
|
|
|
|
|
|
|
|
for _, result := range results {
|
|
|
|
for _, client := range clients {
|
|
|
|
go func(c target.Client, r report.Result) {
|
|
|
|
c.Send(r)
|
|
|
|
wg.Done()
|
|
|
|
}(client, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// For local usage
|
|
|
|
cmd.PersistentFlags().StringP("kubeconfig", "k", "", "absolute path to the kubeconfig file")
|
|
|
|
cmd.PersistentFlags().StringP("config", "c", "", "target configuration file")
|
2021-04-22 18:16:42 +00:00
|
|
|
cmd.PersistentFlags().StringP("crd-version", "v", "v1alpha1", "Policy Reporter CRD Version")
|
2021-02-27 18:11:49 +00:00
|
|
|
|
|
|
|
cmd.PersistentFlags().String("loki", "", "loki host: http://loki:3100")
|
2021-04-06 08:17:22 +00:00
|
|
|
cmd.PersistentFlags().String("loki-minimum-priority", "", "Minimum Priority to send Results to Loki (info < warning < critical < error)")
|
2021-02-27 18:11:49 +00:00
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|