Commit graph

43 commits

Author SHA1 Message Date
Paul Stemmet 2a32700371
Makefile: tidy up random touch 2022-12-10 20:04:30 +00:00
Paul Stemmet 57101f87d6
merge: master <--- feature/logging-improvements
* HEAD
| \
| * be92e2c <github@luxolus.com> config: pad log level
| * 5f53e60 <github@luxolus.com> config: use unix epoch for time, spaces over tabs
| /
| master

Signed-off-by: Bazaah <github@luxolus.com>
2022-12-10 19:41:47 +00:00
Paul Stemmet be92e2c705
config: pad log level
longest that can be created is DPANIC, or 6 wide.

Upstream-Ref: https://github.com/uber-go/zap/blob/v1.24.0/zapcore/level.go#L135
2022-12-10 19:39:57 +00:00
Paul Stemmet 5f53e60ace
config: use unix epoch for time, spaces over tabs 2022-12-10 19:31:32 +00:00
Paul Stemmet 9d8ab7b055
merge: master <--- fix/#3-readme-usage
* HEAD
| \
| * ab1d9fb <github@luxolus.com> doc: correct #usage guidance
| /
| master

Closes: #3
Signed-off-by: Bazaah <github@luxolus.com>
2022-12-10 19:20:13 +00:00
Paul Stemmet ab1d9fb064
doc: correct #usage guidance
We erroneously instructed users to use `notify_fifo_script`, which is
wrong.

Update the section to note the need for a running kdnotify daemon, and
point at the systemd files we provide to that end
2022-12-10 19:17:48 +00:00
Paul Stemmet fc51e39d55
merge: master <--- feature/#2-systemd-service
* HEAD
| \
| * 288b882 <github@luxolus.com> Makefile: add Systemd= configure, mv Prefix= to configure
| * dc343ac <github@luxolus.com> systemd: add kdnotify.service, kdnotify.socket
| /
| master

Closes: #2
Signed-off-by: Bazaah <github@luxolus.com>
2022-12-10 19:10:54 +00:00
Paul Stemmet 288b882984
Makefile: add Systemd= configure, mv Prefix= to configure
This allows a user to completely configure the build system, rather than
needing to provide directions to install specifically.
2022-12-10 19:06:36 +00:00
Paul Stemmet dc343acf44
systemd: add kdnotify.service, kdnotify.socket
As we expect this project to be mostly run as a system service, provide
out of the box configuration files for systemd.

Notably, we add a .socket file allowing systemd to lazily start kdnotify
when a notify event is received on its socket.

We also hardlink the socket out into the /run/ directory, such that
keepalived does not need to peak inside kdnotify runtime dir.

We also link the lifecycle of kdnotify to keepalived, as once it has
been stopped it is unlikely we'll receive new messages.
2022-12-10 19:01:35 +00:00
Paul Stemmet d9ff2c6477
merge: master <--- feature/#1-tidy-instance
* HEAD
| \
| * 83cca8c <github@luxolus.com> schema/notify: add tests for Message parsing
| * c277b1e <github@luxolus.com> schema/notify: strip quotes and whitespace from INSTANCE fields
| /
| master

Signed-off-by: Bazaah <github@luxolus.com>
2022-12-10 17:04:08 +00:00
Paul Stemmet 83cca8cc6f
schema/notify: add tests for Message parsing 2022-12-10 17:01:44 +00:00
Paul Stemmet c277b1e813
schema/notify: strip quotes and whitespace from INSTANCE fields
Keepalived returns dirty instance names, containing any and all quotes
from the declaration in keepalived.conf, plus an additional pair of
double quotes.

Here we strip those out, as they will just cause confusion and make it
difficult to properly match of instances in WatchRules.

As an demonstration observed from keepalived (v2.2.7):

| .conf line    | notify INSTANCE |
| -             | -               |
| my_instance   | "my_instance"   |
| 'my.instance' | "'my.instance'" |
| "my.instance" | ""my.instance"" |

After this commit, each will have all of its quotes stripped

Closes: #1
2022-12-10 16:53:02 +00:00
Paul Stemmet 4267651ba9
doc: update README with Usage and Development sections 2022-12-10 08:50:00 +00:00
Paul Stemmet 7570abb8c1
doc: add example config file for kdnotify watch
This should serve as a good starter for documenting how to use the watch
command of kdnotify.
2022-12-10 08:25:17 +00:00
Paul Stemmet fdc681e8e7
gitignore: expressly ignore makepkg src,pkg dirs 2022-12-09 19:23:02 +00:00
Paul Stemmet ea811c98eb
Makefile: tidy remaining LDFLAGS refs 2022-12-09 19:22:19 +00:00
Paul Stemmet c9d4347ff7
Makefile/build: don't clobber LDFLAGS, buildmode=pie
Avoid clobbering exported LDFLAGS (like from makepkg builds), and
expressly include them via -buildmode=pie, -extldflags=...
2022-12-09 19:08:24 +00:00
Paul Stemmet 21235fa2af
Makefile: add install target 2022-12-09 18:07:23 +00:00
Paul Stemmet a55e1bfcdc
Makefile: add basic recipes for repo
We add targets for build, configure, clean and check. Covering most of
the important bases.

Run 'make help' for the menu
2022-12-09 18:07:23 +00:00
Paul Stemmet 0049df847d
tool/fmtck: gofmt err exit wrapper
The go devs in their infinite wisdom have decided that extending gofmt /
go fmt to error on exit is simply too complicated, so we have to script
away their inadequacies for them.
2022-12-09 18:07:23 +00:00
Paul Stemmet 5fceb121c9
tool/make2doc: script for creating help docs for Makefiles
This will become important in a few commits ;)
2022-12-09 18:07:23 +00:00
Paul Stemmet 41fb22376f
gitignore: allow *.sh files 2022-12-09 18:07:23 +00:00
Paul Stemmet 8961f49bf6
main: wire up CLI 2022-12-09 18:06:01 +00:00
Paul Stemmet 296c7b8d5e
cmd: add CLI
the only command added is 'watch', which serves the only currently
expected functionality of the binary for this lib.
2022-12-09 18:06:01 +00:00
Paul Stemmet 16ab40f44d
buildinfo: compile time variables
These expose entrypoints for -ldflag injections at compile time,
containing metadata about build env + SCM
2022-12-09 18:06:00 +00:00
Paul Stemmet 26485ab1e8
handler: tests for exec.Cmd and templating 2022-12-09 18:06:00 +00:00
Paul Stemmet 315a334732
handler: types and funcs for handling notify events
The primary type exported is VrrpHandler, which interprets the given
WatchRules, applying them as necessary.

It implements watcher.MsgHandler, and can be considered the primary
implementation.
2022-12-09 18:05:59 +00:00
Paul Stemmet 256351013b
watcher: add tests for EventWatcher 2022-12-09 18:05:59 +00:00
Paul Stemmet 196bdde70f
watcher/mock: MockHandler
a mock event handler that can be primed with a series of expected
messages.

Useful for testing watcher
2022-12-09 18:05:58 +00:00
Paul Stemmet c72d92bbd2
watcher: MsgHandler, EventWatcher
This package is responsible for maintaining the lifecycle of a
fifo.EventChannel.

The EventWatcher type handles a single instance of an EventChannel,
feeding messages it receives to a provided watcher.MsgHandler interface.
2022-12-09 18:05:58 +00:00
Paul Stemmet 760bcac716
config: tests for WatchConfig 2022-12-09 18:05:57 +00:00
Paul Stemmet c1afc0d510
config: WatchConfig, WatchRules
These are representations of user defined rules and contextual
information, which can be used to handle notify events.

Specifically, we allow this user to provide (and later access via .Cxt)
arbitrary data under the watch.context key, while providing us rules
under watch.rules.

Each rule consists of an 'Exec' template, and three conditions for
running it (State, Type, Instance). Omitted conditions are ignored.

The Exec value consists of a golang templated command to be executed
based on the conditions listed above.

It has access to two keys, '.Event', and '.Cxt'. .Event contains the
'.Instance', '.State' and '.Type' of the event which triggered this
Exec, while '.Cxt' refers to the arbitrary watch.context passed in.
2022-12-09 18:05:57 +00:00
Paul Stemmet 6ab808c6f5
fifo/mock: MockEvents
Mock producer which implements fifo.EventChannel. Useful elsewhere in
the code, and also to kdnotify/fifo users.
2022-12-09 18:05:57 +00:00
Paul Stemmet 65660e3286
fifo: EventChannel, FifoChannel
This commit adds the fifo.EventChannel interface for consuming (VRRP)
events from keepalived.

This is what package users should accept when expecting a handle to the
event stream.

fifo.FifoChannel is an implementation of this against an IPC FIFO channel
opened via MkFifo(), and can be considered the primary "producer" of
fifo.EventChannel events, but importantly we can create other
producers, notably for testing.
2022-12-09 18:05:56 +00:00
Paul Stemmet 69ceb4eab3
schema/notify: Message, VrrpMessage
The in-memory representation of a keepalived notify event.

Message is not strictly accurate for non VRRP events, but as we don't
care about IPVS events now this is fine for our purposes. We do
explicitly provide a VrrpMessage type however which is guaranteed to be
a VRRP event.
2022-12-09 18:05:56 +00:00
Paul Stemmet a44d541cb7
schema/notify: prio.Priority
A uint number between 0 and 255, representing the new priority of the
associated INSTANCE. In newer versions of the VRRP protocol (v2+) a
instance's priority must be 255 to be considered MASTER, but older
versions only require the highest current priority to be MASTER.

This field is also _optional_ keepalived will only output it for VRRP
events, and then only if a setting is enabled in the conf file,
'vrrp_notify_priority_changes'.

  TYPE INSTANCE STATE PRIORITY
                      ^^^^^^^^
2022-12-09 18:05:56 +00:00
Paul Stemmet 30de46d643
schema/notify: state.State
Representation of the STATE component in a keepalived notify event.

Can be one of: MASTER,BACKUP,STOP (for VRRP events)

  TYPE INSTANCE STATE PRIORITY
                ^^^^^
2022-12-09 18:05:55 +00:00
Paul Stemmet 97be796dfc
schema/notify: ty.Type
These correspond to the TYPE produced by keepalived's notify events.

It may be one of: INSTANCE,GROUP,VS,RS as of this commit.

Our focus is only on supporting VRRP event types at present, but we do
recognize all the current event types.

  TYPE INSTANCE STATE PRIORITY
  ^^^^
2022-12-09 18:05:55 +00:00
Paul Stemmet f0bacb8000
config: LibCxt
This is a "global" context object currently containing a cancellation
context and logger.

This struct can be considered as the lib's generic configuration, and
may be asked for in any public function.
2022-12-09 18:05:55 +00:00
Paul Stemmet 21aa140aa1
go.mod: add shellwords + tidy
Used in handler/template.go for parsing the cmdline passed through by
users in config
2022-12-09 18:05:54 +00:00
Paul Stemmet 59204d6bd1
fifo: add MkPipe/1
for creating the underlying IPC channel that we'll use for communication
with keepalived
2022-12-09 18:05:54 +00:00
Paul Stemmet 7741d7b119
mod: +containerd/fifo +stretchr/testify 2022-11-24 14:20:39 +00:00
Paul Stemmet 5aff11c0a8
repo: Genesis 2022-11-24 12:43:17 +00:00