Skip to content

Commit 6ca6334

Browse files
committed
Print deamon info on startup
Adds a VERSION file with the current daemon version, sets the NVR from that file as a string during build and prints that version along with other information during the daemon and oneshot commands so that operator using selinuxd as an operand can know what exact selinuxd version was being used.
1 parent 06e1ec5 commit 6ca6334

File tree

5 files changed

+70
-3
lines changed

5 files changed

+70
-3
lines changed

Makefile

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
PROJECT=selinuxd
12
BIN=$(BINDIR)/selinuxdctl
23
BINDIR=bin
34
POLICYDIR=/etc/selinux.d
@@ -6,6 +7,8 @@ SRC=$(shell find . -name "*.go")
67

78
GO?=go
89

10+
GO_PROJECT := github.com/containers/$(PROJECT)
11+
912
# External Helper variables
1013

1114
GOLANGCI_LINT_VERSION=1.33.0
@@ -17,7 +20,7 @@ GOLANGCI_LINT_URL=https://github.com/golangci/golangci-lint/releases/download/v$
1720

1821
CONTAINER_RUNTIME?=podman
1922

20-
IMAGE_NAME=selinuxd
23+
IMAGE_NAME=$(PROJECT)
2124
IMAGE_TAG=latest
2225

2326
IMAGE_REF=$(IMAGE_NAME):$(IMAGE_TAG)
@@ -28,6 +31,14 @@ FEDORA_IMAGE_REPO?=quay.io/security-profiles-operator/$(IMAGE_NAME)-fedora:$(IMA
2831

2932
TEST_OS?=fedora
3033

34+
DATE_FMT = +'%Y-%m-%dT%H:%M:%SZ'
35+
BUILD_DATE ?= $(shell date -u "$(DATE_FMT)")
36+
VERSION := $(shell cat VERSION)
37+
38+
LDVARS := \
39+
-X $(GO_PROJECT)/pkg/version.buildDate=$(BUILD_DATE) \
40+
-X $(GO_PROJECT)/pkg/version.version=$(VERSION)
41+
3142
SEMODULE_BACKEND?=policycoreutils
3243
ifeq ($(SEMODULE_BACKEND), semanage)
3344
BUILDTAGS:=semanage
@@ -45,11 +56,11 @@ all: build
4556
build: $(BIN)
4657

4758
$(BIN): $(BINDIR) $(SRC) pkg/semodule/semanage/callbacks.c
48-
$(GO) build -tags '$(BUILDTAGS)' -o $(BIN) .
59+
$(GO) build -ldflags "$(LDVARS)" -tags '$(BUILDTAGS)' -o $(BIN) .
4960

5061
.PHONY: test
5162
test:
52-
$(GO) test -tags '$(BUILDTAGS)' -race github.com/containers/selinuxd/pkg/...
63+
$(GO) test -tags '$(BUILDTAGS)' -race $(GO_PROJECT)/pkg/...
5364

5465
.PHONY: e2e
5566
e2e:

VERSION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.2.99

cmd/daemon.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package cmd
1717

1818
import (
1919
"fmt"
20+
"github.com/containers/selinuxd/pkg/version"
2021
"os"
2122
"os/signal"
2223
"syscall"
@@ -100,6 +101,8 @@ func daemonCmdFunc(rootCmd *cobra.Command, _ []string) {
100101
syscall.Exit(1)
101102
}
102103

104+
version.PrintInfoPermissive(logger)
105+
103106
exitSignal := make(chan os.Signal, 1)
104107
done := make(chan bool)
105108
signal.Notify(exitSignal, syscall.SIGINT, syscall.SIGTERM)

cmd/oneshot.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package cmd
1717

1818
import (
1919
"fmt"
20+
"github.com/containers/selinuxd/pkg/version"
2021
"os"
2122
"syscall"
2223

@@ -79,6 +80,8 @@ func oneshotCmdFunc(rootCmd *cobra.Command, _ []string) {
7980
syscall.Exit(1)
8081
}
8182

83+
version.PrintInfoPermissive(logger)
84+
8285
opts, err := parseOneShotFlags(rootCmd)
8386
if err != nil {
8487
logger.Error(err, "Parsing flags")

pkg/version/version.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package version
2+
3+
import (
4+
"fmt"
5+
"github.com/go-logr/logr"
6+
"runtime"
7+
)
8+
9+
var (
10+
buildDate string // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
11+
version string // the current version of the daemon
12+
)
13+
14+
type Info struct {
15+
Version string `json:"version,omitempty"`
16+
BuildDate string `json:"buildDate,omitempty"`
17+
Compiler string `json:"compiler,omitempty"`
18+
Platform string `json:"platform,omitempty"`
19+
}
20+
21+
// AsKeyValues returns a key value slice of the info.
22+
func (i *Info) AsKeyValues() []interface{} {
23+
return []interface{}{
24+
"version", i.Version,
25+
"buildDate", i.BuildDate,
26+
"compiler", i.Compiler,
27+
"platform", i.Platform,
28+
}
29+
}
30+
31+
func getInfo() *Info {
32+
return &Info{
33+
Version: version,
34+
BuildDate: buildDate,
35+
Compiler: runtime.Compiler,
36+
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
37+
}
38+
}
39+
40+
func printInfo(logger logr.Logger, info *Info) {
41+
logger.Info(
42+
"selinuxd information",
43+
info.AsKeyValues()...,
44+
)
45+
}
46+
47+
func PrintInfoPermissive(logger logr.Logger) {
48+
printInfo(logger, getInfo())
49+
}

0 commit comments

Comments
 (0)