Skip to content

Commit

Permalink
updated to go 1.21.4
Browse files Browse the repository at this point in the history
  • Loading branch information
kuzmik committed Nov 16, 2023
1 parent bb4e45c commit 3edafa4
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 51 deletions.
7 changes: 7 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/.github

#/.git

*.md

proxysql-agent
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.21.3'
go-version: '1.21.4'

- name: Build
run: go build -v ./...
Expand Down
43 changes: 36 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,44 @@
# syntax=docker/dockerfile:1

FROM golang:1.21.3-bookworm
# Stage 1
FROM golang:1.21.4-alpine AS builder

ARG BUILD_SHA
ARG BUILD_TIME
ARG VERSION

ENV GO111MODULE=on

# Set destination for COPY
WORKDIR /app
WORKDIR /build

COPY go.sum go.mod .

COPY . .
RUN go mod download

# Build
RUN CGO_ENABLED=0 GOOS=linux go build -o /proxysql-agent
COPY . .

RUN apk update && apk add --no-cache git && rm -rf /var/cache/apk/*

RUN CGO_ENABLED="0" \
GOOS="linux" \
GOARCH="amd64" \
go build -ldflags "-s -w -X 'main.Version=${VERSION}' -X 'main.Build=${BUILD_SHA}' -X 'main.BuildTime=${BUILD_TIME}'" -o proxysql-agent .

# Stage 2
FROM alpine:3.18.4 as runner

RUN apk update \
&& apk add --no-cache bash bind-tools \
&& rm -rf /var/cache/apk/* \
&& addgroup agent \
&& adduser -S agent -u 1000 -G agent

WORKDIR /app

COPY --chown=agent:agent --from=builder --chmod=700 /build/proxysql-agent /app/
COPY --chown=agent:agent --from=builder --chmod=600 /build/config.yaml /app/

USER agent

# Run
CMD ["/proxysql-agent"]
ENTRYPOINT ["/proxysql-agent"]
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ TARGET := $(shell echo $${PWD\#\#*/})

# These will be provided to the target
VERSION := 0.1.0
BUILD := `git rev-parse HEAD`
BUILD_TIME=`date +%FT%T%z`
BUILD_SHA := `git rev-parse HEAD`
BUILD_TIME := `date +%FT%T%z`

# Use linker flags to provide version/build settings to the target.
# If we don't need debugging symbols, add -s and -w to make a smaller binary
LDFLAGS=-ldflags "-X=main.Version=$(VERSION) -X=main.Build=$(BUILD) -X=main.BuildTime=$BUILD_TIME)"
LDFLAGS=-ldflags "-X 'main.Version=$(VERSION)' -X 'main.Build=$(BUILD_SHA)' -X 'main.BuildTime=$(BUILD_TIME)'"

# go source files, ignore vendor directory
SRC=$(shell find . -type f -name '*.go')
Expand Down Expand Up @@ -38,4 +38,4 @@ run: build
@./$(TARGET)

docker: clean
@docker build . -t proxysql-agent
@docker build --build-arg="VERSION=${VERSION}" --build-arg="BUILD_TIME=${BUILD_TIME}" --build-arg="BUILD_SHA=${BUILD_SHA}" . -t proxysql-agent
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@ There are some linear tickets, but here's a high level overview of what I have i

- *P1* - Health checks; replace the ruby health probe with this
- *P1* - Replace the pre-stop ruby script with this
- *P1* - Slim down the docker container, it doesn't need to be full-on debian; use multistage builds (probably)
- *P2* - Dump the contents of `stats_mysql_query_digests` to a file on disk; will be used to get the data into snowflake. File format TBD
- *P2* - HTTP API for controlling the agent. Much to do here, many ideas
- health checks
- get proxysql admin status
- force a satellite resync (if running in satellite mode)
- etc
- *P3* - Leader election; elect one core pod and have it pod be responsible for managing cluster state
- *P3* - Address all the random `FIXME`/`TODO` comments littered around the code
- *P3* - Leader election; elect one core pod and have it be responsible for managing cluster state

### See also

Expand Down
53 changes: 21 additions & 32 deletions configuration.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package main

import (
"log/slog"
"errors"
"fmt"
"strings"

"github.com/spf13/pflag"
Expand Down Expand Up @@ -44,7 +45,7 @@ var Config config
// 2. config file
// 3. ENV variables
// 4. commandline flags
func Configure() {
func Configure() error {
// set up some ENV settings
// the replacer lets us access nested configs, like PROXYSQL_ADDRESS will equate to proxysql.address
replacer := strings.NewReplacer(".", "_")
Expand All @@ -53,7 +54,7 @@ func Configure() {
viper.GetViper().AutomaticEnv()

// set some defaults
viper.GetViper().SetDefault("start_delay", 1)
viper.GetViper().SetDefault("start_delay", 0)
viper.GetViper().SetDefault("log_level", "INFO")
viper.GetViper().SetDefault("run_mode", nil)

Expand All @@ -72,31 +73,18 @@ func Configure() {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath("/etc/proxysql-agent")
viper.AddConfigPath("$HOME/.config/proxysql-agent")
viper.AddConfigPath(".")

if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found; ignore error and move on
} else {
slog.Error(
"Error loading config file",
slog.String("config_file", viper.GetViper().ConfigFileUsed()),
slog.Any("err", err),
)
return err
}
}

file := viper.GetViper().ConfigFileUsed()
if file != "" {
slog.Debug(
"Loaded configuration from file",
slog.String("config_file", file),
)
}

// commandline flags
pflag.Int("start_delay", 1, "seconds to pause before starting agent")
pflag.Int("start_delay", 0, "seconds to pause before starting agent")
pflag.String("log_level", "INFO", "the log level for the agent; defaults to INFO")
pflag.String("run_mode", "", "mode to run the agent in; valid values: [core OR satellite]")

Expand All @@ -110,41 +98,42 @@ func Configure() {

pflag.Int("satellite.interval", 10, "seconds to sleep in the satellite clustering loop")

pflag.Bool("show-config", false, "Dump the configuration for debugging")
pflag.CommandLine.MarkHidden("show-config")

pflag.Parse()
viper.BindPFlags(pflag.CommandLine)

slog.Debug("Settings", slog.Any("settings map", viper.GetViper().AllSettings()))
// we are only dumping the config if the secret flag show-config is specified, because the config
// contains the proxysql admin password
if viper.GetViper().GetBool("show-config") {
fmt.Println("settings", viper.GetViper().AllSettings())
}

// run some validations before proceeding
if viper.GetViper().IsSet("run_mode") {
run_mode := viper.GetViper().GetString("run_mode")
if run_mode != "core" && run_mode != "satellite" {
msg := "run_mode must be either 'core' or 'satellite'"
slog.Error(msg, slog.String("run_mode", run_mode))
panic(msg)
return errors.New("run_mode must be either 'core' or 'satellite'")
}
}

if delay := viper.GetViper().GetInt("start_delay"); delay < 0 {
msg := "start_delay cannot be less < 0"
slog.Error(msg)
panic(msg)
return errors.New("start_delay cannot be less < 0")
}

if cinterval := viper.GetViper().GetInt("core.interval"); cinterval < 0 {
msg := "core.interval cannot be less < 0"
slog.Error(msg, slog.Int("core.interval", cinterval))
panic(msg)
return errors.New("core.interval cannot be less < 0")
}

if sinterval := viper.GetViper().GetInt("satellite.interval"); sinterval < 0 {
msg := "satellite.interval cannot be less < 0"
slog.Error(msg, slog.Int("start_delay", sinterval))
panic(msg)
return errors.New("satellite.interval cannot be less < 0")
}

err := viper.Unmarshal(&Config)
if err != nil {
slog.Error("Unable to unmarshal onto config struct", slog.Any("error", err))
return err
}

return nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/persona-id/proxysql-agent

go 1.21.3
go 1.21.4

require (
github.com/go-sql-driver/mysql v1.7.1
Expand Down
16 changes: 13 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@ import (
"time"
)

var (
// compile time info
Build = ""
BuildTime = ""
Version = ""
)

func main() {
Configure()
err := Configure()
if err != nil {
panic(err)
}

setupLogger()

slog.Info("configured values", slog.Any("config", Config))
slog.Info("build info", slog.Any("version", Version), slog.Any("time", BuildTime), slog.Any("build", Build))

// if defined, pause before booting; this allows the proxysql pods to fully come up before connecting
if Config.StartDelay > 0 {
Expand All @@ -21,7 +31,7 @@ func main() {

// open a connection to proxysql
var psql *ProxySQL
psql, err := psql.New()
psql, err = psql.New()
if err != nil {
slog.Error("Unable to connect to ProxySQL", slog.Any("error", err))
panic(err)
Expand Down

0 comments on commit 3edafa4

Please sign in to comment.