Skip to content

Commit

Permalink
feat: Upgrade go-vault-client dep
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-form3 committed Oct 11, 2023
1 parent 7326d15 commit 5b512a1
Show file tree
Hide file tree
Showing 16 changed files with 709 additions and 77 deletions.
82 changes: 82 additions & 0 deletions .github/workflows/calculate-tag.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: Calculate new tag

on:
workflow_call:
outputs:
tag:
description: "The the next semantic version tag based on commit messages."
value: ${{ jobs.calculate-tag.outputs.tag }}
inputs:
head_ref:
description: "Head ref to be used as pre-release suffix"
type: string
default: "${{ github.head_ref }}"
f3_tag:
description: "Additional tag to be prefixed to the latest upstream release tag"
type: string
default: "${{ github.sha }}"

jobs:
retrieve_release_tag:
name: Get the release tag (if any)
runs-on: form3-ubuntu-2004
outputs:
release_tag: ${{ steps.release_tag.outputs.version }}
release_name: ${{ steps.release_tag.outputs.release }}
steps:
- name: Get the tag name
if: contains(github.ref, 'refs/tags/')
id: tag_name
run: echo ::set-output name=version::${GITHUB_REF#refs/tags/}
- name: Get release version and name
id: release_tag
env:
TAG_NAME: ${{ steps.tag_name.outputs.version }}
run: |
echo "::set-output name=version::${TAG_NAME:-''}"
echo "::set-output name=release::${APPNAME} ${TAG_NAME:-''}"
- name: Prints the generated release tag and name
env:
RELEASE_TAG: ${{ steps.release_tag.outputs.version }}
RELEASE_NAME: ${{ steps.release_tag.outputs.release }}
run: |
echo $RELEASE_TAG
echo $RELEASE_NAME
calculate-tag:
runs-on: ubuntu-20.04
permissions: read-all
outputs:
tag: "${{ steps.tag.outputs.tag }}"
steps:
- uses: actions/checkout@8f4b7f84864484a7bf31766abe9204da3cbe65b3 # v3.5.0
with:
fetch-depth: 0

- name: Determine latest upstream release tag
id: latest_tag
if: "!contains(github.ref,'refs/tags/')"
run: |
latest_tag=$(git describe --abbrev=0 --tags)
echo "latest_tag=$latest_tag" >> $GITHUB_OUTPUT
- name: Get the tag name
if: contains(github.ref, 'refs/tags/')
id: tag_name
run: echo "latest_tag=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT

- name: Calculate pre-release suffix
id: tag-suffix
run: |
echo "event name ${{ github.event_name }}"
SUFFIX="${{ inputs.head_ref }}"
if [[ "${{ github.event_name }}" = "push" && "${{ github.ref_type }}" = "branch" && "${{ github.ref_name }}" = "builder" ]]; then
SUFFIX="release-$(date +%Y-%m-%d)"
fi
underscores_and_slashes_to_dashes_suffix="${SUFFIX//[\/_]/-}"
echo "tag-suffix=-${underscores_and_slashes_to_dashes_suffix}" >> $GITHUB_OUTPUT
- name: Compute next tag
id: tag
run: |
latest_tag="${{ steps.latest_tag.outputs.latest_tag }}"
tag="${latest_tag}-f3-${{steps.tag-suffix.outputs.tag-suffix}}"
echo "tag=$tag" >> $GITHUB_OUTPUT
65 changes: 65 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: CI
on:
push:
branches:
- master
tags:
- '**'
pull_request:

jobs:
calculate-tag:
uses: ./.github/workflows/calculate-tag.yaml
test:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@8f4b7f84864484a7bf31766abe9204da3cbe65b3 # v3.5.0
- name: Setup Golang
uses: actions/setup-go@4d34df0c2316fe8122ab82dc22947d607c0c91f9 # v4.0.0
with:
go-version-file: go.mod
- name: Test
run: make test
build:
runs-on: ubuntu-latest
needs: [ calculate-tag, test ]
steps:
- name: Checkout Code
uses: actions/checkout@8f4b7f84864484a7bf31766abe9204da3cbe65b3 # v3.5.0
- name: Setup Golang
uses: actions/setup-go@4d34df0c2316fe8122ab82dc22947d607c0c91f9 # v4.0.0
with:
go-version-file: go.mod
- name: Build
run: make crossbuild
- name: Package
run: make crossbuild-tarballs
- name: Archive artifacts
uses: actions/upload-artifact@v3
with:
name: dist
path: ./.build/*.tar.gz

publish:
runs-on: ubuntu-latest
needs: [ calculate-tag, build ]
permissions: write-all
if: contains(github.event.head_commit.message, 'pre-release') || startsWith(github.base_ref, '/refs/tags')
steps:
- name: Download artifacts
uses: actions/download-artifact@v3
with:
name: dist
- name: Download artifacts
run: ls -ltr
- name: Create GH release
uses: softprops/action-gh-release@v1
id: release
with:
generate_release_notes: true
target_commitish: "${{ github.base_ref }}"
tag_name: ${{ needs.calculate-tag.outputs.tag }}
prerelease: "${{ github.event_name == 'pull_request' }}"
files: |
*.tar.gz
7 changes: 5 additions & 2 deletions .promu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ go:
# This must match .circle/config.yml.
version: 1.20
repository:
path: github.com/prometheus-community/postgres_exporter
path: github.com/form3tech-oss/postgres_exporter
build:
binaries:
- name: postgres_exporter
Expand All @@ -12,8 +12,11 @@ build:
-X github.com/prometheus/common/version.Version={{.Version}}
-X github.com/prometheus/common/version.Revision={{.Revision}}
-X github.com/prometheus/common/version.Branch={{.Branch}}
-X github.com/prometheus/common/version.BuildUser={{user}}@{{host}}
-X github.com/prometheus/common/version.BuildDate={{date "20060102-15:04:05"}}
crossbuild:
platforms:
- linux/amd64
- linux/arm64
tarball:
files:
- LICENSE
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
all::

# Needs to be defined before including Makefile.common to auto-generate targets
DOCKER_ARCHS ?= amd64 armv7 arm64 ppc64le
DOCKER_REPO ?= prometheuscommunity
DOCKER_REPO ?= form3tech
CROSS_BUILD_PROMUOPTS := -p linux/arm64 #-p linux/amd64 -p windows/amd64

include Makefile.common

Expand Down
14 changes: 12 additions & 2 deletions Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ ifeq ($(GOHOSTOS),$(filter $(GOHOSTOS),linux darwin))
endif
endif

PREFIX ?= $(shell pwd)
PREFIX ?= $(shell pwd)/build
BIN_DIR ?= $(shell pwd)
DOCKER_IMAGE_TAG ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD))
DOCKERFILE_PATH ?= ./Dockerfile
Expand Down Expand Up @@ -199,10 +199,20 @@ common-build: promu
@echo ">> building binaries"
$(PROMU) build --prefix $(PREFIX) $(PROMU_BINARIES)

.PHONY: common-crossbuild
common-crossbuild: promu
@echo ">> building binaries"
$(PROMU) crossbuild -v

.PHONY: common-crossbuild-tarballs
common-crossbuild-tarballs: promu
@echo ">> building tarballs"
$(PROMU) crossbuild -v "tarballs"

.PHONY: common-tarball
common-tarball: promu
@echo ">> building release tarball"
$(PROMU) tarball --prefix $(PREFIX) $(BIN_DIR)
$(PROMU) tarball --prefix="$(BIN_DIR)" $(BIN_DIR)/.bin

.PHONY: common-docker $(BUILD_DOCKER_ARCHS)
common-docker: $(BUILD_DOCKER_ARCHS)
Expand Down
59 changes: 59 additions & 0 deletions cmd/postgres_exporter/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"regexp"
"strings"

"github.com/form3tech-oss/go-vault-client/v4/pkg/vaultclient"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
)
Expand Down Expand Up @@ -149,6 +150,21 @@ func getDataSources() ([]string, error) {
pass = os.Getenv("DATA_SOURCE_PASS")
}

if len(user) == 0 || len(pass) == 0 {
secrets, err := loadSecrets()
if err != nil {
panic(err)
}

if len(user) == 0 {
user = secrets["database-username"].(string)
}

if len(pass) == 0 {
pass = secrets["database-password"].(string)
}
}

ui := url.UserPassword(user, pass).String()
dataSrouceURIFile := os.Getenv("DATA_SOURCE_URI_FILE")
if len(dataSrouceURIFile) != 0 {
Expand All @@ -171,3 +187,46 @@ func getDataSources() ([]string, error) {

return []string{dsn}, nil
}

func loadSecrets() (map[string]interface{}, error) {
result := make(map[string]interface{})
vaultAuth, err := vaultclient.NewVaultAuth(vaultclient.NewDefaultConfig())
if err != nil {
return nil, err
}

client, err := vaultAuth.VaultClient()
if err != nil {
return nil, err
}

secret, err := client.Logical().Read("/secret/application")
if err == nil {
for key, value := range secret.Data {
result[key] = value
}
} else {
level.Warn(logger).Log("error reading vault secrets from /secret/application", err)
}

secret, err = client.Logical().Read("/secret/postgres_exporter")
if err == nil && secret != nil {
for key, value := range secret.Data {
result[key] = value
}
} else {
level.Warn(logger).Log("error reading vault secrets from /secret/postgres_exporter", err)
}

dbCredsPath := os.Getenv("VAULT_DB_CREDENTIALS_PATH")
secret, err = client.Logical().Read(dbCredsPath)
if err == nil {
for key, value := range secret.Data {
result[key] = value
}
} else {
level.Warn(logger).Log("error reading vault secrets from "+dbCredsPath, err)
}

return result, nil
}
4 changes: 2 additions & 2 deletions cmd/postgres_exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (
"strings"

"github.com/alecthomas/kingpin/v2"
"github.com/form3tech-oss/postgres_exporter/collector"
"github.com/form3tech-oss/postgres_exporter/config"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus-community/postgres_exporter/collector"
"github.com/prometheus-community/postgres_exporter/config"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promlog"
Expand Down
8 changes: 2 additions & 6 deletions cmd/postgres_exporter/pg_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ func (s *pgSetting) normaliseUnit() (val float64, unit string, err error) {
return
case "ms", "s", "min", "h", "d":
unit = "seconds"
case "B", "kB", "MB", "GB", "TB", "1kB", "2kB", "4kB", "8kB", "16kB", "32kB", "64kB", "16MB", "32MB", "64MB":
case "B", "kB", "MB", "GB", "TB", "4kB", "8kB", "16kB", "32kB", "64kB", "16MB", "32MB", "64MB":
unit = "bytes"
default:
err = fmt.Errorf("Unknown unit for runtime variable: %q", s.unit)
err = fmt.Errorf("unknown unit for runtime variable: %q", s.unit)
return
}

Expand All @@ -158,10 +158,6 @@ func (s *pgSetting) normaliseUnit() (val float64, unit string, err error) {
val *= math.Pow(2, 30)
case "TB":
val *= math.Pow(2, 40)
case "1kB":
val *= math.Pow(2, 10)
case "2kB":
val *= math.Pow(2, 11)
case "4kB":
val *= math.Pow(2, 12)
case "8kB":
Expand Down
Loading

0 comments on commit 5b512a1

Please sign in to comment.