Skip to content

Commit

Permalink
Make binary statically compiled again (#27)
Browse files Browse the repository at this point in the history
* Fixes #26 so the distroless container works again
* Add smoke test for containers in CI pipeline
  • Loading branch information
RichardoC authored Jun 12, 2023
1 parent 6a60f55 commit d99aad7
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 46 deletions.
16 changes: 12 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,29 @@ jobs:
- checkout
- run:
name: Build Alpine Docker Image
command: docker build -f Dockerfile-builder . --rm=false -t thoughtmachine/prometheus-cardinality-exporter:"$CIRCLE_SHA1"
command: |
docker build -f Dockerfile-builder . --rm=false -t thoughtmachine/prometheus-cardinality-exporter:"$CIRCLE_SHA1"
docker run --rm -it thoughtmachine/prometheus-cardinality-exporter:"$CIRCLE_SHA1" --help
- run:
name: Build distroless Docker Image
command: docker build -f Dockerfile-builder_distroless . --rm=false -t thoughtmachine/prometheus-cardinality-exporter:"$CIRCLE_SHA1"_distroless
command: |
docker build -f Dockerfile-builder_distroless . --rm=false -t thoughtmachine/prometheus-cardinality-exporter:"$CIRCLE_SHA1"_distroless
docker run --rm -it thoughtmachine/prometheus-cardinality-exporter:"$CIRCLE_SHA1"_distroless --help
build_and_release:
working_directory: ~/prometheus-cardinality-exporter
machine: true
steps:
- checkout
- run:
name: Build Alpine Docker Image
command: docker build -f Dockerfile-builder . --rm=false -t thoughtmachine/prometheus-cardinality-exporter:"$CIRCLE_SHA1"
command: |
docker build -f Dockerfile-builder . --rm=false -t thoughtmachine/prometheus-cardinality-exporter:"$CIRCLE_SHA1"
docker run --rm -it thoughtmachine/prometheus-cardinality-exporter:"$CIRCLE_SHA1" --help
- run:
name: Build distroless Docker Image
command: docker build -f Dockerfile-builder_distroless . --rm=false -t thoughtmachine/prometheus-cardinality-exporter:"$CIRCLE_SHA1"_distroless
command: |
docker build -f Dockerfile-builder_distroless . --rm=false -t thoughtmachine/prometheus-cardinality-exporter:"$CIRCLE_SHA1"_distroless
docker run --rm -it thoughtmachine/prometheus-cardinality-exporter:"$CIRCLE_SHA1"_distroless --help
- run:
name: Publish Docker Images to Docker Hub
command: |
Expand Down
10 changes: 4 additions & 6 deletions Dockerfile-builder
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@ RUN ln -s /usr/local/go/bin/go /usr/local/bin/go

RUN apk add --no-cache curl wget gcc make bash git musl-dev libc6-compat gettext

RUN touch /root/.profile


WORKDIR /go/github.com/thought-machine/prometheus-cardinality-exporter

COPY . .

# So the binary is where we expect it
RUN go build .

RUN go build ./...

RUN go test ./... -race

# So the binary is where we expect it
# env var and flags ensure a static binary
RUN CGO_ENABLED=0 go build -ldflags="-extldflags=-static" .

# alpine:3.18.0
FROM alpine@sha256:c0669ef34cdc14332c0f1ab0c2c01acb91d96014b172f1a76f3a39e63d1f0bda

Expand Down
9 changes: 4 additions & 5 deletions Dockerfile-builder_distroless
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@ RUN ln -s /usr/local/go/bin/go /usr/local/bin/go

RUN apk add --no-cache curl wget gcc make bash git musl-dev libc6-compat gettext

RUN touch /root/.profile

WORKDIR /go/github.com/thought-machine/prometheus-cardinality-exporter

COPY . .

# So the binary is where we expect it
RUN go build .

RUN go build ./...

RUN go test ./... -race

# So the binary is where we expect it
# env var and flags ensure a static binary
RUN CGO_ENABLED=0 go build -ldflags="-extldflags=-static" .

FROM scratch

EXPOSE 9090
Expand Down
16 changes: 0 additions & 16 deletions Dockerfile-prometheus-cardinality-exporter

This file was deleted.

10 changes: 0 additions & 10 deletions Dockerfile-prometheus-cardinality-exporter_distroless

This file was deleted.

8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ There are 4 types of metric exposed:
```
## Options

(```plz run //:prometheus-cardinality-exporter -- [OPTIONS]```)
(```go run . [OPTIONS]```)

| Short Flag | Long Flag | Description |
|------------|---------------------|--------------------------------------------------------------------------------------|
Expand Down Expand Up @@ -73,8 +73,10 @@ See https://hub.docker.com/r/thoughtmachine/prometheus-cardinality-exporter
```go run . --port=<port-to-serve-on> --proms=<prometheus-instance-to-expose> [--proms=<prometheus-instance-to-expose>...] --freq=<frequency-to-ping-api>```

### Running Within a Kubernetes Cluster (with service discovery)
#### In order to deploy to a kubernetes cluster, run:
```plz run //k8s:k8s_push```
#### In order to deploy to a kubernetes cluster:

Tweak and apply the files in k8s/

#### Make sure you alter the k8s/deployment.yaml such that it contains the options that you require:
In the example below, all of the possible flags that can be used with the ```--service_discovery``` option are included.\
NOTE: not all flags are required, for example, you do not need the ```--auth``` flag if none of your Prometheus instances require authorization to access.
Expand Down
11 changes: 9 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,17 @@ func collectMetrics() {
}

func main() {
flags.Parse(&opts)
_, err := flags.Parse(&opts)

// Exit gracefully if help flag used
if err != nil && flags.WroteHelp(err) {
os.Exit(0)
} else if err != nil {
log.Fatalf("%+v", err)
}

if len(opts.PrometheusInstances) > 0 && opts.ServiceDiscovery {
log.Fatal("Cannot parse Prometheus Instances (--proms) AND use Service Discorvery (--service_discovery), these options are mutually exclusive.")
log.Fatal("Cannot parse Prometheus Instances (--proms) AND use Service Discovery (--service_discovery), these options are mutually exclusive.")
} else if len(opts.PrometheusInstances) > 0 {
log.Info("Obtaining metics from prometheus instances specified as arguments.")
} else if opts.ServiceDiscovery {
Expand Down

0 comments on commit d99aad7

Please sign in to comment.