Skip to content

Commit

Permalink
add version and manifests for service monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
cpanato committed Oct 23, 2021
1 parent 3fba22f commit a735359
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 18 deletions.
17 changes: 8 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,21 @@ jobs:
username: ${{ steps.get_repo_owner.outputs.repo_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Run GoReleaser
- name: Install GoReleaser
uses: goreleaser/goreleaser-action@v2
if: ${{ github.event_name != 'pull_request' }}
with:
version: latest
args: release --rm-dist --snapshot --skip-sign
install-only: true

- name: snaphot
if: ${{ github.event_name != 'pull_request' }}
run: make snapshot
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COSIGN_PASSWORD: ${{ secrets.COSIGN_PASSWORD }}

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
- name: snaphot
if: ${{ github.event_name == 'pull_request' }}
with:
version: latest
args: release --rm-dist --snapshot --skip-publish --skip-sign
run: make snapshot-pr
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COSIGN_PASSWORD: ${{ secrets.COSIGN_PASSWORD }}
8 changes: 5 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ jobs:
username: ${{ steps.get_repo_owner.outputs.repo_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Run GoReleaser
- name: Install GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --rm-dist
install-only: true

- name: release
run: make release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COSIGN_PASSWORD: ${{ secrets.COSIGN_PASSWORD }}
9 changes: 8 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ builds:
- 7
env:
- CGO_ENABLED=0
ldflags:
- "{{ .Env.LDFLAGS }}"

signs:
- id: fan
Expand Down Expand Up @@ -57,6 +59,11 @@ release:
owner: cpanato
name: raspi-fan-control
footer: |
**Image available**: ghcr.io/cpanato/fan-control:{{ .Version }}
Signed with `cosign` to verify:
```shell
cosign verify -key https://raw.githubusercontent.com/cpanato/raspi-fan-control/main/cosign.pub ghcr.io/cpanato/fan-control:{{ .Version }}
```
extra_files:
- glob: "./cosign.pub"
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ COPY raspi-fan-control /bin/raspi-fan-control
USER nobody

ENTRYPOINT [ "/bin/raspi-fan-control" ]

EXPOSE 9001
32 changes: 32 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.PHONY: clean lint build

# Set version variables for LDFLAGS
GIT_VERSION ?= $(shell git describe --tags --always --dirty)
GIT_HASH ?= $(shell git rev-parse HEAD)
DATE_FMT = +'%Y-%m-%dT%H:%M:%SZ'
SOURCE_DATE_EPOCH ?= $(shell git log -1 --pretty=%ct)
ifdef SOURCE_DATE_EPOCH
BUILD_DATE ?= $(shell date -u -d "@$(SOURCE_DATE_EPOCH)" "$(DATE_FMT)" 2>/dev/null || date -u -r "$(SOURCE_DATE_EPOCH)" "$(DATE_FMT)" 2>/dev/null || date -u "$(DATE_FMT)")
else
BUILD_DATE ?= $(shell date "$(DATE_FMT)")
endif
GIT_TREESTATE = "clean"
DIFF = $(shell git diff --quiet >/dev/null 2>&1; if [ $$? -eq 1 ]; then echo "1"; fi)
ifeq ($(DIFF), 1)
GIT_TREESTATE = "dirty"
endif

PKG=github.com/cpanato/raspi-fan-control/cmd
LDFLAGS="-X $(PKG).GitVersion=$(GIT_VERSION) -X $(PKG).gitCommit=$(GIT_HASH) -X $(PKG).gitTreeState=$(GIT_TREESTATE) -X $(PKG).buildDate=$(BUILD_DATE)"

.PHONY: release
release:
LDFLAGS=$(LDFLAGS) goreleaser release --rm-dist

.PHONY: snapshot-pr
snapshot-pr:
LDFLAGS=$(LDFLAGS) goreleaser release --rm-dist --snapshot --skip-publish --skip-sign

.PHONY: snapshot
snapshot:
LDFLAGS=$(LDFLAGS) goreleaser release --rm-dist --snapshot --skip-publish --skip-sign
3 changes: 3 additions & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ func startServer() {
IdleTimeout: 30 * time.Second,
}

v := VersionInfo()
log.Println(v.String())

log.Printf("Listening on %s\n", ":9001")
go func() {
err := server.ListenAndServe()
Expand Down
116 changes: 116 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package cmd

import (
"encoding/json"
"fmt"
"runtime"
"strings"
"text/tabwriter"

"github.com/pkg/errors"
"github.com/spf13/cobra"
)

// Base version information.
//
// This is the fallback data used when version information from git is not
// provided via go ldflags (e.g. via Makefile).
var (
// Output of "git describe". The prerequisite is that the branch should be
// tagged using the correct versioning strategy.
GitVersion string = "devel"
// SHA1 from git, output of $(git rev-parse HEAD)
gitCommit = "unknown"
// State of git tree, either "clean" or "dirty"
gitTreeState = "unknown"
// Build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
buildDate = "unknown"
)

type versionOptions struct {
json bool
}

var versionOpts = &versionOptions{}

// verifyCmd represents the verify command
var versionCmd = &cobra.Command{
Use: "version",
Short: "fan-control version",
RunE: func(cmd *cobra.Command, args []string) error {
return runVersion(versionOpts)
},
}

func init() {
versionCmd.PersistentFlags().BoolVarP(&versionOpts.json, "json", "j", false,
"print JSON instead of text")
rootCmd.AddCommand(versionCmd)
}

func runVersion(opts *versionOptions) error {
v := VersionInfo()
res := v.String()

if opts.json {
j, err := v.JSONString()
if err != nil {
return errors.Wrap(err, "unable to generate JSON from version info")
}
res = j
}

fmt.Println(res)
return nil
}

type Info struct {
GitVersion string
GitCommit string
GitTreeState string
BuildDate string
GoVersion string
Compiler string
Platform string
}

func VersionInfo() Info {
// These variables typically come from -ldflags settings and in
// their absence fallback to the global defaults set above.
return Info{
GitVersion: GitVersion,
GitCommit: gitCommit,
GitTreeState: gitTreeState,
BuildDate: buildDate,
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}
}

// String returns the string representation of the version info
func (i *Info) String() string {
b := strings.Builder{}
w := tabwriter.NewWriter(&b, 0, 0, 2, ' ', 0)

fmt.Fprintf(w, "GitVersion:\t%s\n", i.GitVersion)
fmt.Fprintf(w, "GitCommit:\t%s\n", i.GitCommit)
fmt.Fprintf(w, "GitTreeState:\t%s\n", i.GitTreeState)
fmt.Fprintf(w, "BuildDate:\t%s\n", i.BuildDate)
fmt.Fprintf(w, "GoVersion:\t%s\n", i.GoVersion)
fmt.Fprintf(w, "Compiler:\t%s\n", i.Compiler)
fmt.Fprintf(w, "Platform:\t%s\n", i.Platform)

w.Flush() // #nosec
return b.String()
}

// JSONString returns the JSON representation of the version info
func (i *Info) JSONString() (string, error) {
b, err := json.MarshalIndent(i, "", " ")
if err != nil {
return "", err
}

return string(b), nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.17

require (
github.com/gorilla/mux v1.8.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.11.0
github.com/spf13/cobra v1.2.1
github.com/stianeikeland/go-rpio/v4 v4.5.1
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FI
github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
14 changes: 9 additions & 5 deletions kubernetes/daemonset.yaml
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fan
name: fan-control
labels:
app: fan
app.kubernetes.io/name: fan-control
spec:
selector:
matchLabels:
app: fan
app.kubernetes.io/name: fan-control
template:
metadata:
labels:
app: fan
app.kubernetes.io/name: fan-control
spec:
tolerations:
- key: node-role.kubernetes.io/master
operator: Exists
effect: NoSchedule
containers:
- image: ghcr.io/cpanato/fan-control:main
- image: ghcr.io/cpanato/fan-control:0.1.1
args: ["server"]
ports:
- name: metrics
containerPort: 9001
protocol: TCP
imagePullPolicy: IfNotPresent
name: fan
terminationMessagePath: /var/log/termination-log
Expand Down
14 changes: 14 additions & 0 deletions kubernetes/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
kind: Service
apiVersion: v1
metadata:
name: fan-control
namespace: fan-control
labels:
app.kubernetes.io/name: fan-control
spec:
selector:
app.kubernetes.io/name: fan-control
ports:
- name: metrics
port: 9001
protocol: TCP
16 changes: 16 additions & 0 deletions kubernetes/serviceMonitor.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: fan-control
namespace: monitoring
labels:
app.kubernetes.io/name: fan-control
spec:
namespaceSelector:
matchNames:
- fan-control
selector:
matchLabels:
app.kubernetes.io/name: fan-control
endpoints:
- port: metrics

0 comments on commit a735359

Please sign in to comment.