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"
2021-04-30 09:47:28 +00:00
"log"
2021-02-27 18:11:49 +00:00
"net/http"
"github.com/fjogeleit/policy-reporter/pkg/config"
2021-03-05 13:26:47 +00:00
"github.com/fjogeleit/policy-reporter/pkg/metrics"
2021-02-27 18:11:49 +00:00
"github.com/fjogeleit/policy-reporter/pkg/report"
"github.com/fjogeleit/policy-reporter/pkg/target"
"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-04-30 09:47:28 +00:00
log . Printf ( "[INFO] Configured DebounceTime %d" , c . CleanupDebounceTime )
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 )
pClient , err := resolver . PolicyReportClient ( ctx )
2021-02-27 18:11:49 +00:00
if err != nil {
return err
}
2021-03-05 13:26:47 +00:00
cpClient , err := resolver . ClusterPolicyReportClient ( ctx )
if err != nil {
return err
}
rClient , err := resolver . PolicyResultClient ( ctx )
2021-02-27 18:11:49 +00:00
if err != nil {
return err
}
2021-03-05 13:26:47 +00:00
cpClient . RegisterCallback ( metrics . CreateClusterPolicyReportMetricsCallback ( ) )
pClient . RegisterCallback ( metrics . CreatePolicyReportMetricsCallback ( ) )
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 {
rClient . 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
} )
rClient . RegisterPolicyResultWatcher ( resolver . SkipExistingOnStartup ( ) )
}
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
if c . API . Enabled {
2021-04-24 10:32:15 +00:00
go func ( ) { errorChan <- resolver . APIServer ( ) . Start ( ) } ( )
2021-03-13 18:56:38 +00:00
}
2021-04-24 10:32:15 +00:00
go func ( ) { errorChan <- cpClient . StartWatching ( ) } ( )
go func ( ) { errorChan <- pClient . StartWatching ( ) } ( )
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-04-22 18:16:42 +00:00
cmd . PersistentFlags ( ) . StringP ( "crd-version" , "v" , "v1alpha1" , "Policy Reporter CRD Version" )
2021-04-30 09:47:28 +00:00
cmd . PersistentFlags ( ) . IntP ( "cleanup-debounce-time" , "t" , 20 , "DebounceTime in Seconds after a Report cleanup started." )
2021-03-13 18:56:38 +00:00
cmd . PersistentFlags ( ) . IntP ( "apiPort" , "a" , 0 , "http port for the optional rest api" )
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
cmd . PersistentFlags ( ) . Bool ( "loki-skip-existing-on-startup" , false , "Skip Results created before PolicyReporter started. Prevent duplicated sending after new deployment" )
flag . Parse ( )
return cmd
}