Skip to content

Commit 669587a

Browse files
committed
cmd: build 'smbmetrics' as stand-alone executable
Bind main to metrics code to produce a stand-alone metrics-exporter executable binary. Signed-off-by: Shachar Sharon <[email protected]>
1 parent 3898b6d commit 669587a

File tree

4 files changed

+850
-0
lines changed

4 files changed

+850
-0
lines changed

Makefile

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,45 @@ define yamls_reformat
5858
YQ=$(YQ) $(CURDIR)/hack/yq-fixup-yamls.sh $(1)
5959
endef
6060

61+
all: build
62+
63+
# Build executable
64+
.PHONY: build
65+
build:
66+
CGO_ENABLED=0 $(GO_CMD) build -o bin/smbmetrics \
67+
-ldflags $(GOLDFLAGS) cmd/main.go
68+
69+
# Run unit tests
70+
.PHONY: test
71+
test: build vet
72+
$(GO_CMD) test ./... -coverprofile cover.out
73+
74+
75+
# Run go fmt to reformat code
76+
.PHONY: reformat
77+
reformat:
78+
$(GO_CMD) fmt ./...
79+
80+
# Run go vet against code
81+
.PHONY: vet
82+
vet: reformat
83+
$(GO_CMD) vet ./...
84+
85+
# Format yaml files for yamllint standard
86+
.PHONY: yaml-fmt
87+
yaml-fmt: yq
88+
$(call yamls_reformat, $(CURDIR))
89+
90+
# Check the code
91+
.PHONY: check check-golangci-lint check-format check-yaml
92+
93+
check: check-golangci-lint vet check-yaml
94+
95+
check-golangci-lint: golangci-lint
96+
$(GOLANGCI_LINT) -c .golangci.yaml run ./...
97+
98+
check-yaml:
99+
$(YAMLLINT_CMD) -c ./.yamllint.yaml ./
61100

62101
# Find or download auxiliary build tools
63102
.PHONY: build-tools golangci-lint yq

cmd/main.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
package main
4+
5+
import (
6+
"os"
7+
goruntime "runtime"
8+
9+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
10+
11+
"github.com/samba-in-kubernetes/smbmetrics/internal/metrics"
12+
)
13+
14+
var (
15+
// Version of the software at compile time.
16+
Version = "(unset)"
17+
// CommitID of the revision used to compile the software.
18+
CommitID = "(unset)"
19+
)
20+
21+
func main() {
22+
log := zap.New(zap.UseDevMode(true))
23+
log.Info("Initializing smbmetrics",
24+
"ProgramName", os.Args[0],
25+
"Version", Version,
26+
"CommitID", CommitID,
27+
"GoVersion", goruntime.Version())
28+
29+
loc, err := metrics.LocateSmbStatus()
30+
if err != nil {
31+
log.Error(err, "Failed to locate smbstatus")
32+
os.Exit(1)
33+
}
34+
ver, err := metrics.RunSmbStatusVersion()
35+
if err != nil {
36+
log.Error(err, "Failed to run smbstatus")
37+
os.Exit(1)
38+
}
39+
log.Info("Located smbstatus", "path", loc, "version", ver)
40+
41+
err = metrics.RunSmbMetricsExporter(log)
42+
if err != nil {
43+
os.Exit(1)
44+
}
45+
}

go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module github.com/samba-in-kubernetes/smbmetrics
2+
3+
go 1.16
4+
5+
require (
6+
github.com/go-logr/logr v0.4.0
7+
github.com/prometheus/client_golang v1.11.1
8+
github.com/stretchr/testify v1.7.0
9+
sigs.k8s.io/controller-runtime v0.10.1
10+
)

0 commit comments

Comments
 (0)