Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli/config): ipc file location can be set #42

Merged
merged 4 commits into from
Jul 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cmd/salt-exporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/kpetremann/salt-exporter/internal/metrics"
"github.com/kpetremann/salt-exporter/pkg/listener"
"github.com/spf13/viper"
)

Expand Down Expand Up @@ -35,6 +36,7 @@ type Config struct {

ListenAddress string `mapstructure:"listen-address"`
ListenPort int `mapstructure:"listen-port"`
IPCFile string `mapstructure:"ipc-file"`
TLS struct {
Enabled bool
Key string
Expand All @@ -50,6 +52,7 @@ func parseFlags() bool {

flag.String("host", "", "listen address")
flag.Int("port", defaultPort, "listen port")
flag.String("ipc-file", listener.DefaultIPCFilepath, "file location of the salt-master event bus")
flag.Bool("tls", false, "enable TLS")
flag.String("tls-cert", "", "TLS certificated")
flag.String("tls-key", "", "TLS private key")
Expand All @@ -71,6 +74,7 @@ func parseFlags() bool {
func setDefaults(healthMinions bool) {
viper.SetDefault("log-level", defaultLogLevel)
viper.SetDefault("listen-port", defaultPort)
viper.SetDefault("ipc-file", listener.DefaultIPCFilepath)
viper.SetDefault("metrics.health-minions", defaultHealthMinion)
viper.SetDefault("metrics.salt_new_job_total.enabled", true)
viper.SetDefault("metrics.salt_expected_responses_total.enabled", true)
Expand Down
7 changes: 7 additions & 0 deletions cmd/salt-exporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/kpetremann/salt-exporter/internal/metrics"
"github.com/kpetremann/salt-exporter/pkg/listener"
"github.com/spf13/viper"
)

Expand All @@ -26,6 +27,7 @@ func TestReadConfigFlagOnly(t *testing.T) {
LogLevel: defaultLogLevel,
ListenAddress: "127.0.0.1",
ListenPort: 8080,
IPCFile: listener.DefaultIPCFilepath,
TLS: struct {
Enabled bool
Key string
Expand Down Expand Up @@ -102,6 +104,7 @@ func TestReadConfigFlagOnly(t *testing.T) {
flags: []string{
"-host=127.0.0.1",
"-port=8080",
"-ipc-file=/dev/null",
"-health-minions=false",
"-health-functions-filter=test.sls",
"-health-states-filter=nop",
Expand All @@ -115,6 +118,7 @@ func TestReadConfigFlagOnly(t *testing.T) {
LogLevel: defaultLogLevel,
ListenAddress: "127.0.0.1",
ListenPort: 8080,
IPCFile: "/dev/null",
TLS: struct {
Enabled bool
Key string
Expand Down Expand Up @@ -223,6 +227,7 @@ func TestConfigFileOnly(t *testing.T) {
LogLevel: "info",
ListenAddress: "127.0.0.1",
ListenPort: 2113,
IPCFile: "/dev/null",
TLS: struct {
Enabled bool
Key string
Expand Down Expand Up @@ -320,6 +325,7 @@ func TestConfigFileWithFlags(t *testing.T) {
"-health-functions-filter=test.sls",
"-health-states-filter=nop",
"-ignore-mock",
"-ipc-file=/somewhere",
}

os.Args = append([]string{name}, flags...)
Expand All @@ -331,6 +337,7 @@ func TestConfigFileWithFlags(t *testing.T) {
LogLevel: "info",
ListenAddress: "127.0.0.1",
ListenPort: 8080,
IPCFile: "/somewhere",
TLS: struct {
Enabled bool
Key string
Expand Down
3 changes: 2 additions & 1 deletion cmd/salt-exporter/config_test.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
listen-address: "127.0.0.1"
listen-port: 2113

ipc-file: /dev/null

log-level: "info"
tls:
enabled: true
key: "/path/to/key"
certificate: "/path/to/certificate"


metrics:
global:
filters:
Expand Down
1 change: 1 addition & 0 deletions cmd/salt-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func start(config Config) {
// listen and expose metric
parser := parser.NewEventParser(false)
eventListener := listener.NewEventListener(ctx, parser, eventChan)
eventListener.SetIPCFilepath(config.IPCFile)

go eventListener.ListenEvents()
go metrics.ExposeMetrics(ctx, eventChan, config.Metrics)
Expand Down
2 changes: 2 additions & 0 deletions cmd/salt-live/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func main() {
maxItems := flag.Int("max-events", 1000, "maximum events to keep in memory")
bufferSize := flag.Int("buffer-size", 1000, "buffer size in number of events")
filter := flag.String("hard-filter", "", "filter when received (filtered out events are discarded forever)")
ipcFilepath := flag.String("ipc-file", listener.DefaultIPCFilepath, "file location of the salt-master event bus")
versionCmd := flag.Bool("version", false, "print version")
debug := flag.Bool("debug", false, "enable debug mode (log to debug.log)")
flag.Parse()
Expand All @@ -52,6 +53,7 @@ func main() {
eventChan := make(chan event.SaltEvent, *bufferSize)
parser := parser.NewEventParser(true)
eventListener := listener.NewEventListener(ctx, parser, eventChan)
eventListener.SetIPCFilepath(*ipcFilepath)
go eventListener.ListenEvents()

p := tea.NewProgram(tui.NewModel(eventChan, *maxItems, *filter), tea.WithMouseCellMotion())
Expand Down
6 changes: 3 additions & 3 deletions pkg/listener/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type eventParser interface {
Parse(message map[string]interface{}) (event.SaltEvent, error)
}

const defaultIPCFilepath = "/var/run/salt/master/master_event_pub.ipc"
const DefaultIPCFilepath = "/var/run/salt/master/master_event_pub.ipc"

// EventListener listens to the salt-master event bus and sends events to the event channel
type EventListener struct {
Expand All @@ -39,7 +39,7 @@ type EventListener struct {

// Open opens the salt-master event bus
func (e *EventListener) Open() {
log.Info().Msg("connecting to salt-master event bus")
log.Info().Str("file", e.iPCFilepath).Msg("connecting to salt-master event bus")
var err error

for {
Expand Down Expand Up @@ -90,7 +90,7 @@ func NewEventListener(ctx context.Context, eventParser eventParser, eventChan ch
ctx: ctx,
eventChan: eventChan,
eventParser: eventParser,
iPCFilepath: defaultIPCFilepath,
iPCFilepath: DefaultIPCFilepath,
}
return &e
}
Expand Down