Skip to content

Commit 53a0db2

Browse files
authored
Uplift to use x/events (#35)
* move to events.ChangeMessage Signed-off-by: Matt Siwiec <[email protected]> * move to x/events Signed-off-by: Matt Siwiec <[email protected]> * help target Signed-off-by: Matt Siwiec <[email protected]> * nats jetstream Signed-off-by: Matt Siwiec <[email protected]> * changes to support event integration testing Signed-off-by: Matt Siwiec <[email protected]> * use appConfig, remove prefix checks Signed-off-by: Matt Siwiec <[email protected]> --------- Signed-off-by: Matt Siwiec <[email protected]>
1 parent b4e521b commit 53a0db2

File tree

17 files changed

+584
-437
lines changed

17 files changed

+584
-437
lines changed

.devcontainer/devcontainer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"dev",
99
"haproxy",
1010
"nats-auth",
11-
"nats-server"
11+
"nats"
1212
],
1313
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
1414
"customizations": {

.devcontainer/docker-compose.yaml

+37-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
services:
22
dev:
3+
image: devcontainer
34
build:
45
context: .
56
dockerfile: Dockerfile
67
command: sleep infinity
8+
depends_on:
9+
- nats-init
710
volumes:
811
- ..:/workspaces:cached
912
- nats-auth:/etc/nats-auth
@@ -12,29 +15,61 @@ services:
1215
# - "127.0.0.1:2222:2222"
1316
# Use "forwardPorts" in **devcontainer.json** to forward a port locally.
1417
# (Adding the "ports" property to this file will not forward from a Codespace.)
18+
1519
haproxy:
1620
build:
1721
context: .
1822
dockerfile: Dockerfile.haproxy
1923
network_mode: service:dev
24+
2025
nats-auth:
2126
image: natsio/nats-box:0.13.8
2227
command:
2328
- /tmp/configure-nats
2429
volumes:
2530
- nats-auth:/etc/nats-auth
2631
- ./setup-scripts:/tmp
27-
nats-server:
32+
33+
nats:
2834
image: nats:alpine
35+
restart: unless-stopped
2936
command:
37+
- --jetstream
3038
- -c
3139
- /etc/nats/resolver.conf
3240
- -D
3341
depends_on:
3442
- nats-auth
35-
network_mode: service:dev
3643
volumes:
3744
- nats-auth:/etc/nats
3845

46+
nats-init:
47+
image: devcontainer
48+
depends_on:
49+
- nats
50+
restart: on-failure
51+
command:
52+
[
53+
"nats",
54+
"stream",
55+
"--server=nats",
56+
"add",
57+
"loadbalancer-manager-haproxy",
58+
"--subjects=com.infratographer.>",
59+
"--storage=memory",
60+
"--replicas=1",
61+
"--retention=limits",
62+
"--discard=old",
63+
"--max-msgs=-1",
64+
"--max-msgs-per-subject=-1",
65+
"--max-bytes=-1",
66+
"--max-age=-1",
67+
"--max-msg-size=-1",
68+
"--dupe-window='2m0s'",
69+
"--no-allow-rollup",
70+
"--deny-delete",
71+
"--deny-purge"
72+
]
73+
3974
volumes:
4075
nats-auth:

.devcontainer/setup-scripts/configure-nats

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
#!/bin/sh
1+
#!/usr/bin/env sh
2+
3+
set -e
24

35
echo "Creating NATS Operator"
46
nsc add operator -n TEST-OPERATOR --sys --generate-signing-key
@@ -14,4 +16,4 @@ mkdir /etc/nats-auth
1416
nsc generate config --mem-resolver --sys-account SYS > /etc/nats-auth/resolver.conf
1517

1618
echo "Save NATS User creds"
17-
nsc generate creds -n MANAGER > /etc/nats-auth/manager.creds
19+
nsc generate creds -n MANAGER > /etc/nats-auth/manager.creds

Makefile

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,40 @@
11
all: lint test
2-
PHONY: test coverage lint golint clean vendor docker-up docker-down unit-test
2+
PHONY: test unit-test coverage lint golint clean build vendor
33
GOOS=linux
44
APP_NAME=loadbalancer-manager-haproxy
55

6+
help: Makefile ## Print help
7+
@grep -h "##" $(MAKEFILE_LIST) | grep -v grep | sed -e 's/:.*##/#/' | column -c 2 -t -s#
8+
69
test: | lint unit-test
710

8-
unit-test:
11+
unit-test: ## Run unit tests
912
@echo Running unit tests...
1013
@go test -cover -short -tags testtools ./...
1114

12-
coverage:
15+
coverage: ## Run unit tests with coverage
1316
@echo Generating coverage report...
1417
@go test ./... -race -coverprofile=coverage.out -covermode=atomic -tags testtools -p 1
1518
@go tool cover -func=coverage.out
1619
@go tool cover -html=coverage.out
1720

18-
lint: golint
21+
lint: golint ## Runs linting
1922

2023
golint: | vendor
2124
@echo Linting Go files...
2225
@golangci-lint run --build-tags "-tags testtools"
2326

24-
build:
27+
build: ## Build the binary
2528
@go mod download
2629
@CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o bin/${APP_NAME}
2730

28-
clean:
31+
clean: ## Clean up all the things
2932
@echo Cleaning...
3033
@rm -f bin/${APP_NAME}
3134
@rm -rf ./dist/
3235
@rm -rf coverage.out
3336
@go clean -testcache
3437

35-
vendor:
38+
vendor: ## Vendors dependencies
3639
@go mod download
3740
@go mod tidy

cmd/errors.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,26 @@ package cmd
33
import "errors"
44

55
var (
6-
// ErrNATSURLRequired is returned when a NATS url is missing
7-
ErrNATSURLRequired = errors.New("nats url is required and cannot be empty")
6+
// ErrSubscriberURLRequired is returned when a event subscriber url is missing
7+
ErrSubscriberURLRequired = errors.New("nats url is required and cannot be empty")
88

9-
// ErrNATSSubjectPrefixRequired is returned when a NATS subject prefix is missing
10-
ErrNATSSubjectPrefixRequired = errors.New("nats subject prefix is required and cannot be empty")
9+
// ErrSubscriberPrefixRequired is returned when a NATS subject prefix is missing
10+
ErrSubscriberPrefixRequired = errors.New("env LOADBALANCER_MANAGER_HAPROXY_EVENTS_SUBSCRIBER_PREFIX is required and cannot be empty")
11+
12+
ErrSubscriberTopicsRequired = errors.New("event-topics is required and cannot be empty")
1113

1214
// ErrNATSAuthRequired is returned when a NATS auth method is missing
13-
ErrNATSAuthRequired = errors.New("nats creds are required and cannot be empty")
15+
ErrNATSAuthRequired = errors.New("env LOADBALANCER_MANAGER_HAPROXY_EVENTS_SUBSCRIBER_NATS_CREDSFILE is required and cannot be empty")
1416

1517
// ErrHAProxyBaseConfigRequired is returned when the base HAProxy config is missing
16-
ErrHAProxyBaseConfigRequired = errors.New("base haproxy config is required and cannot be empty")
18+
ErrHAProxyBaseConfigRequired = errors.New("base-haproxy-config is required and cannot be empty")
1719

1820
// ErrLBAPIURLRequired is returned when the LB API url is missing
19-
ErrLBAPIURLRequired = errors.New("loadbalancer api url is required and cannot be empty")
21+
ErrLBAPIURLRequired = errors.New("loadbalancer-api-url is required and cannot be empty")
2022

2123
// ErrLBIDRequired is the loadbalancer id to watch for changes on the msg queue
22-
ErrLBIDRequired = errors.New("loadbalancer gidx is required and cannot be empty")
24+
ErrLBIDRequired = errors.New("loadbalancer-id is required and cannot be empty")
2325

2426
// ErrLBIDInvalid is returned when the loadbalancer gidx is invalid
25-
ErrLBIDInvalid = errors.New("loadbalancer gidx is invalid")
27+
ErrLBIDInvalid = errors.New("loadbalancer-id (gidx) is invalid")
2628
)

cmd/root.go

+21-27
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@
22
package cmd
33

44
import (
5+
"fmt"
6+
"os"
57
"strings"
68

79
homedir "github.com/mitchellh/go-homedir"
810
"github.com/spf13/cobra"
911
"github.com/spf13/pflag"
1012
"github.com/spf13/viper"
1113
"go.uber.org/zap"
14+
15+
"go.infratographer.com/x/loggingx"
16+
"go.infratographer.com/x/versionx"
17+
18+
"go.infratographer.com/loadbalancer-manager-haproxy/internal/config"
1219
)
1320

14-
// TODO: update app name
1521
const appName = "loadbalancer-manager-haproxy"
1622

1723
var (
@@ -37,14 +43,11 @@ func init() {
3743

3844
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/."+appName+".yaml)")
3945

40-
rootCmd.PersistentFlags().Bool("debug", false, "enable debug logging")
41-
viperBindFlag("logging.debug", rootCmd.PersistentFlags().Lookup("debug"))
42-
43-
rootCmd.PersistentFlags().Bool("pretty", false, "enable pretty (human readable) logging output")
44-
viperBindFlag("logging.pretty", rootCmd.PersistentFlags().Lookup("pretty"))
46+
// Logging flags
47+
loggingx.MustViperFlags(viper.GetViper(), rootCmd.PersistentFlags())
4548

46-
rootCmd.PersistentFlags().Bool("development", false, "enable development settings")
47-
viperBindFlag("development", rootCmd.PersistentFlags().Lookup("development"))
49+
// Register version command
50+
versionx.RegisterCobraCommand(rootCmd, func() { versionx.PrintVersion(logger) })
4851
}
4952

5053
// initConfig reads in config file and ENV variables if set.
@@ -63,12 +66,13 @@ func initConfig() {
6366
}
6467

6568
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
66-
// TODO: This needs to match [a-z]+, this may not be true for your app name
6769
viper.SetEnvPrefix("loadbalancer-manager-haproxy")
6870

6971
viper.AutomaticEnv() // read in environment variables that match
7072

71-
setupLogging()
73+
setupAppConfig()
74+
75+
logger = loggingx.InitLogger(appName, config.AppConfig.Logging)
7276

7377
// If a config file is found, read it in.
7478
err := viper.ReadInConfig()
@@ -79,25 +83,15 @@ func initConfig() {
7983
}
8084
}
8185

82-
func setupLogging() {
83-
cfg := zap.NewProductionConfig()
84-
if viper.GetBool("logging.pretty") {
85-
cfg = zap.NewDevelopmentConfig()
86-
}
87-
88-
if viper.GetBool("logging.debug") {
89-
cfg.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
90-
} else {
91-
cfg.Level = zap.NewAtomicLevelAt(zap.InfoLevel)
92-
}
93-
94-
l, err := cfg.Build()
86+
// setupAppConfig loads our config.AppConfig struct with the values bound by
87+
// viper. Then, anywhere we need these values, we can just return to AppConfig
88+
// instead of performing viper.GetString(...), viper.GetBool(...), etc.
89+
func setupAppConfig() {
90+
err := viper.Unmarshal(&config.AppConfig)
9591
if err != nil {
96-
panic(err)
92+
fmt.Printf("unable to decode app config: %s", err)
93+
os.Exit(1)
9794
}
98-
99-
logger = l.Sugar().With("app", appName)
100-
defer logger.Sync() //nolint:errcheck
10195
}
10296

10397
// viperBindFlag provides a wrapper around the viper bindings that handles error checks

0 commit comments

Comments
 (0)