diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index 40905596..9b007810 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -6,7 +6,7 @@ on: pull_request: env: - GOLANGCI_LINT_VERSION: v1.55.2 + GOLANGCI_LINT_VERSION: v1.56.2 jobs: lint: diff --git a/.github/workflows/master.yaml b/.github/workflows/master.yaml index 0977635d..34450a84 100644 --- a/.github/workflows/master.yaml +++ b/.github/workflows/master.yaml @@ -12,9 +12,9 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 15 steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - - uses: actions/setup-go@v4 + - uses: actions/setup-go@v5 with: go-version-file: './go.mod' cache-dependency-path: './go.sum' @@ -40,7 +40,7 @@ jobs: comment-on-alert: true - name: Save benchmark JSON to cache - uses: actions/cache/save@v3 + uses: actions/cache/save@v4 with: path: ./cache/benchmark-data.json # Save with commit hash to avoid "cache already exists" diff --git a/.github/workflows/on-pull-request.yml b/.github/workflows/on-pull-request.yml index 76da76b0..d89825f7 100644 --- a/.github/workflows/on-pull-request.yml +++ b/.github/workflows/on-pull-request.yml @@ -4,6 +4,9 @@ on: pull_request: branches: [ master ] +env: + GOLANGCI_LINT_VERSION: v1.56.2 + jobs: test: name: test @@ -25,12 +28,12 @@ jobs: run: ./contrib/check-version.sh - name: Set up Go - uses: actions/setup-go@v4 + uses: actions/setup-go@v5 with: go-version: ${{ matrix.go-version }} - name: Cache deps - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: ~/go/pkg/mod key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} @@ -40,6 +43,12 @@ jobs: - name: Install deps run: go mod download + - name: golangci-lint + uses: golangci/golangci-lint-action@v4 + with: + version: ${{ env.GOLANGCI_LINT_VERSION }} + skip-cache: true + - name: Test run: go test -v -race -p=1 -count=1 go-bench: @@ -47,12 +56,12 @@ jobs: timeout-minutes: 30 steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: fetch-depth: 0 # to be able to retrieve the last commit in master branch - name: Set up Go - uses: actions/setup-go@v4 + uses: actions/setup-go@v5 with: go-version-file: './go.mod' cache-dependency-path: './go.sum' @@ -71,7 +80,7 @@ jobs: - name: Get benchmark JSON from Master branch id: cache - uses: actions/cache/restore@v3 + uses: actions/cache/restore@v4 with: path: ./cache/benchmark-data.json key: ${{ steps.get-master-branch-sha.outputs.sha }}-${{ runner.os }}-go-benchmark diff --git a/.github/workflows/on-release.yml b/.github/workflows/on-release.yml index ff5bd161..c4a19c66 100644 --- a/.github/workflows/on-release.yml +++ b/.github/workflows/on-release.yml @@ -33,7 +33,7 @@ jobs: password: ${{ github.token }} - name: Build and push - uses: docker/build-push-action@v3 + uses: docker/build-push-action@v5 with: tags: | ghcr.io/mailgun/gubernator:${{ github.event.release.tag_name }} diff --git a/Makefile b/Makefile index 713cd3f0..7c77cca8 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ VERSION=$(shell cat version) LDFLAGS="-X main.Version=$(VERSION)" GOLANGCI_LINT = $(GOPATH)/bin/golangci-lint -GOLANGCI_LINT_VERSION = 1.55.2 +GOLANGCI_LINT_VERSION = 1.56.2 $(GOLANGCI_LINT): curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin $(GOLANGCI_LINT_VERSION) diff --git a/cmd/gubernator-cli/main.go b/cmd/gubernator-cli/main.go index 4e0a96b2..f75753bd 100644 --- a/cmd/gubernator-cli/main.go +++ b/cmd/gubernator-cli/main.go @@ -89,7 +89,11 @@ func main() { cmdLine := strings.Join(os.Args[1:], " ") logrus.WithContext(ctx).Info("Command line: " + cmdLine) - conf, err := guber.SetupDaemonConfig(log, configFile) + configFileReader, err := os.Open(configFile) + if err != nil { + return fmt.Errorf("while opening config file: %s", err) + } + conf, err := guber.SetupDaemonConfig(log, configFileReader) if err != nil { return err } diff --git a/cmd/gubernator/main.go b/cmd/gubernator/main.go index f0308691..875dfc05 100644 --- a/cmd/gubernator/main.go +++ b/cmd/gubernator/main.go @@ -74,7 +74,11 @@ func main() { } // Read our config from the environment or optional environment config file - conf, err := gubernator.SetupDaemonConfig(logrus.StandardLogger(), configFile) + configFileReader, err := os.Open(configFile) + if err != nil { + log.WithError(err).Fatal("while opening config file") + } + conf, err := gubernator.SetupDaemonConfig(logrus.StandardLogger(), configFileReader) checkErr(err, "while getting config") ctx, cancel := context.WithTimeout(ctx, clock.Second*10) diff --git a/config.go b/config.go index 14c9b592..122ffa22 100644 --- a/config.go +++ b/config.go @@ -263,9 +263,9 @@ func (d *DaemonConfig) ServerTLS() *tls.Config { return nil } -// SetupDaemonConfig returns a DaemonConfig object as configured by reading the provided config file -// and environment. -func SetupDaemonConfig(logger *logrus.Logger, configFile string) (DaemonConfig, error) { +// SetupDaemonConfig returns a DaemonConfig object that is the result of merging the lines +// in the provided configFile and the environment variables. See `example.conf` for all available config options and their descriptions. +func SetupDaemonConfig(logger *logrus.Logger, configFile io.Reader) (DaemonConfig, error) { log := logrus.NewEntry(logger) var conf DaemonConfig var logLevel string @@ -273,7 +273,7 @@ func SetupDaemonConfig(logger *logrus.Logger, configFile string) (DaemonConfig, var advAddr, advPort string var err error - if configFile != "" { + if configFile != nil { log.Infof("Loading env config: %s", configFile) if err := fromEnvFile(log, configFile); err != nil { return conf, err @@ -628,15 +628,10 @@ func getEnvSlice(name string) []string { return strings.Split(v, ",") } -// Take values from a file in the format `GUBER_CONF_ITEM=my-value` and put them into the environment -// lines that begin with `#` are ignored -func fromEnvFile(log logrus.FieldLogger, configFile string) error { - fd, err := os.Open(configFile) - if err != nil { - return fmt.Errorf("while opening config file: %s", err) - } - - contents, err := io.ReadAll(fd) +// Take values from a file in the format `GUBER_CONF_ITEM=my-value` and sets them as environment variables. +// Lines that begin with `#` are ignored +func fromEnvFile(log logrus.FieldLogger, configFile io.Reader) error { + contents, err := io.ReadAll(configFile) if err != nil { return fmt.Errorf("while reading config file '%s': %s", configFile, err) } diff --git a/config_test.go b/config_test.go new file mode 100644 index 00000000..290b329f --- /dev/null +++ b/config_test.go @@ -0,0 +1,40 @@ +package gubernator + +import ( + "fmt" + "os" + "strings" + "testing" + + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/require" +) + +func TestParsesGrpcAddress(t *testing.T) { + os.Clearenv() + s := ` +# a comment +GUBER_GRPC_ADDRESS=10.10.10.10:9000` + daemonConfig, err := SetupDaemonConfig(logrus.StandardLogger(), strings.NewReader(s)) + require.NoError(t, err) + require.Equal(t, "10.10.10.10:9000", daemonConfig.GRPCListenAddress) + require.NotEmpty(t, daemonConfig.InstanceID) +} + +func TestDefaultGrpcAddress(t *testing.T) { + os.Clearenv() + s := ` +# a comment` + daemonConfig, err := SetupDaemonConfig(logrus.StandardLogger(), strings.NewReader(s)) + require.NoError(t, err) + require.Equal(t, fmt.Sprintf("%s:81", LocalHost()), daemonConfig.GRPCListenAddress) + require.NotEmpty(t, daemonConfig.InstanceID) +} + +func TestDefaultInstanceId(t *testing.T) { + os.Clearenv() + s := `` + daemonConfig, err := SetupDaemonConfig(logrus.StandardLogger(), strings.NewReader(s)) + require.NoError(t, err) + require.NotEmpty(t, daemonConfig.InstanceID) +}