Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/master' into leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
miparnisari committed Feb 19, 2024
2 parents a44560e + 452c5b5 commit fb3ca43
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
pull_request:

env:
GOLANGCI_LINT_VERSION: v1.55.2
GOLANGCI_LINT_VERSION: v1.56.2

jobs:
lint:
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/master.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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"
Expand Down
19 changes: 14 additions & 5 deletions .github/workflows/on-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
pull_request:
branches: [ master ]

env:
GOLANGCI_LINT_VERSION: v1.56.2

jobs:
test:
name: test
Expand All @@ -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') }}
Expand All @@ -40,19 +43,25 @@ 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:
runs-on: ubuntu-latest
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'
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/on-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion cmd/gubernator-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/gubernator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 8 additions & 13 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,17 +263,17 @@ 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
var logFormat string
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
Expand Down Expand Up @@ -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)
}
Expand Down
40 changes: 40 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit fb3ca43

Please sign in to comment.