Skip to content

Commit

Permalink
FWI-3600 First implementation of the realtime-reporter with Polaris s…
Browse files Browse the repository at this point in the history
…upport (#840)

* add first implementation of the realtime-reporter with polaris handler

* fix log case, return early on error

* add CLI flag for polaris configuration, add opts to enable polaris as a handler

---------

Co-authored-by: jdesouza <[email protected]>
  • Loading branch information
jslivka and jdesouza authored Jan 29, 2024
1 parent 7b9abfd commit 76fb32f
Show file tree
Hide file tree
Showing 16 changed files with 1,814 additions and 0 deletions.
43 changes: 43 additions & 0 deletions realtime-reporter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# realtime-reporter

## Run

You'll need to provide a configuration file for the reporter. An example configuration is located at `examples/realtime-reporter.yaml`

The Insights authentication token is passed as an environment variable. This is required:

```
export FAIRWINDS_TOKEN=$TOKEN
```

```
curl -o examples/polaris.yaml https://raw.githubusercontent.com/FairwindsOps/polaris/master/examples/config.yaml
go run main.go \
--organization acme-co \
--cluster kind \
--host http://192.168.1.27:3000 \
--config examples/realtime-reporter.yaml \
--polaris-enabled \
--polaris-config examples/polaris.yaml
```

## Example Output

* resource added

```json
{"event_version":1,"timestamp":1702656153316168000,"kube_event":"add","kind":"Namespace","namespace":"default","workload":"nginx-deployment","data":{"Contents":"B64_ENCODED_REPORT","Report":"polaris","Version":"1.0"}}
```

* resource updated

```json
{"event_version":1,"timestamp":1702058147263443000,"kube_event":"update","kind":"Deployment","namespace":"default","workload":"nginx-deployment","data":{"Contents":"B64_ENCODED_REPORT","Report":"polaris","Version":"1.0"}}
```

* resource deleted

```json
{"event_version":1,"timestamp":1702058166269670000,"kube_event":"delete","kind":"Deployment","namespace":"default","workload":"nginx-deployment","data":null}
```
93 changes: 93 additions & 0 deletions realtime-reporter/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package cmd

import (
"os"
"os/signal"
"syscall"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/FairwindsOps/insights-plugins/realtime-reporter/pkg/watcher"
)

var (
configPath string
polarisEnabled bool
polarisConfigPath string
organization string
cluster string
host string
logLevel string
)

var rootCmd = &cobra.Command{
Use: "realtime-reporter",
Short: "A realtime reporter for Fairwinds Insights. Watches for changes in Kubernetes objects, generates reports and uploads them to Insights.",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
parsedLevel, err := logrus.ParseLevel(logLevel)
if err != nil {
logrus.Errorf("log-level flag has invalid value %s", logLevel)
} else {
logrus.SetLevel(parsedLevel)
}

if configPath != "" {
// use config file from the flag.
viper.SetConfigFile(configPath)
} else {
logrus.Error("config must be set")
os.Exit(1)
}

viper.AutomaticEnv()

if err := viper.ReadInConfig(); err == nil {
logrus.Info("Using config file:", viper.ConfigFileUsed())
viper.BindPFlag("organization", cmd.Root().Flags().Lookup("organization"))
viper.BindPFlag("cluster", cmd.Root().Flags().Lookup("cluster"))
viper.BindPFlag("host", cmd.Root().Flags().Lookup("host"))
viper.BindEnv("token", "FAIRWINDS_TOKEN")
viper.BindPFlag("polaris-enabled", cmd.Root().Flags().Lookup("polaris-enabled"))
viper.BindPFlag("polaris-config", cmd.Root().Flags().Lookup("polaris-config"))
}
},
Run: func(cmd *cobra.Command, args []string) {
watcher, err := watcher.NewWatcher()
if err != nil {
logrus.Fatalf("error creating new watcher: %s", err.Error())
}

stopCh := make(chan struct{})
defer close(stopCh)

watcher.Run(stopCh)

sigterm := make(chan os.Signal, 1)
signal.Notify(sigterm, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL, syscall.SIGQUIT, syscall.SIGSTOP)

<-sigterm
},
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
logrus.Error(err)
os.Exit(1)
}
}

func init() {
rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "", "Location of watch configuration file. Contains a list of resources to watch with an optional field to specify namespaces.")
rootCmd.MarkFlagRequired("config")
rootCmd.PersistentFlags().BoolVar(&polarisEnabled, "polaris-enabled", false, "Run the Polaris handler for Insights realtime-reporter.")
rootCmd.PersistentFlags().StringVar(&polarisConfigPath, "polaris-config", "", "Location of polaris configuration file. Used by the Polaris handler. Example can be found at https://raw.githubusercontent.com/FairwindsOps/polaris/master/examples/config.yaml")
rootCmd.PersistentFlags().StringVar(&organization, "organization", "", "The Insights organization name.")
rootCmd.MarkFlagRequired("organization")
rootCmd.PersistentFlags().StringVar(&cluster, "cluster", "", "The Insights cluster name.")
rootCmd.MarkFlagRequired("cluster")
rootCmd.PersistentFlags().StringVar(&host, "host", "https://insights.fairwinds.com", "The Insights host.")
rootCmd.MarkFlagRequired("host")
rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "", logrus.InfoLevel.String(), "Logrus log level to be output (trace, debug, info, warning, error, fatal, panic).")
}
14 changes: 14 additions & 0 deletions realtime-reporter/examples/realtime-reporter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
resources:
- apps/v1/deployments
- apps/v1/statefulsets
- batch/v1/jobs
- v1/pods
- v1/services
- networking.k8s.io/v1/ingresses
- v1/nodes
- v1/namespaces
- v1/persistentvolumes
- v1/persistentvolumeclaims

namespaces:
- all
116 changes: 116 additions & 0 deletions realtime-reporter/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
module github.com/FairwindsOps/insights-plugins/realtime-reporter

go 1.20

require (
github.com/fairwindsops/insights-plugins/plugins/admission v0.0.0-20231114133200-3e310e6f2f5d
github.com/fairwindsops/polaris v0.0.0-20231102194751-391b802d4d17
github.com/samber/lo v1.39.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.17.0
k8s.io/api v0.28.3
k8s.io/apimachinery v0.28.3
k8s.io/client-go v0.28.3
sigs.k8s.io/controller-runtime v0.15.0
)

require (
github.com/OneOfOne/xxhash v1.2.8 // indirect
github.com/aereal/go-httpretryafter v0.0.0-20201002052800-c08be88dd146 // indirect
github.com/agnivade/levenshtein v1.1.1 // indirect
github.com/avast/retry-go v3.0.0+incompatible // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.10.2 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/fairwindsops/controller-utils v0.3.3 // indirect
github.com/fairwindsops/insights-plugins/plugins/opa v0.0.0-20230329182738-e952cc6f1f84 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gobuffalo/logger v1.0.7 // indirect
github.com/gobuffalo/packd v1.0.2 // indirect
github.com/gobuffalo/packr/v2 v2.8.3 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/karrick/godirwalk v1.17.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/markbates/errx v1.1.0 // indirect
github.com/markbates/oncer v1.0.0 // indirect
github.com/markbates/safe v1.0.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/open-policy-agent/opa v0.51.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.0 // indirect
github.com/qri-io/jsonpointer v0.1.1 // indirect
github.com/qri-io/jsonschema v0.1.2 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/tchap/go-patricia/v2 v2.3.1 // indirect
github.com/thoas/go-funk v0.9.3 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/yashtewari/glob-intersection v0.1.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.13.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.3.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.27.2 // indirect
k8s.io/component-base v0.27.2 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.3.0 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
Loading

0 comments on commit 76fb32f

Please sign in to comment.