Skip to content

Add Makefile targets to create stable releases, add version.go and print version #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 57 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
PROJECT=selinuxd
BIN=$(BINDIR)/selinuxdctl
BINDIR=bin
POLICYDIR=/etc/selinux.d
Expand All @@ -6,6 +7,8 @@ SRC=$(shell find . -name "*.go")

GO?=go

GO_PROJECT := github.com/containers/$(PROJECT)

# External Helper variables

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

CONTAINER_RUNTIME?=podman

IMAGE_NAME=selinuxd
IMAGE_NAME=$(PROJECT)
IMAGE_TAG=latest

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

TEST_OS?=fedora

DATE_FMT = +'%Y-%m-%dT%H:%M:%SZ'
BUILD_DATE ?= $(shell date -u "$(DATE_FMT)")
VERSION := $(shell cat VERSION)

LDVARS := \
-X $(GO_PROJECT)/pkg/version.buildDate=$(BUILD_DATE) \
-X $(GO_PROJECT)/pkg/version.version=$(VERSION)

SEMODULE_BACKEND?=policycoreutils
ifeq ($(SEMODULE_BACKEND), semanage)
BUILDTAGS:=semanage
Expand All @@ -45,11 +56,11 @@ all: build
build: $(BIN)

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

.PHONY: test
test:
$(GO) test -tags '$(BUILDTAGS)' -race github.com/containers/selinuxd/pkg/...
$(GO) test -tags '$(BUILDTAGS)' -race $(GO_PROJECT)/pkg/...

.PHONY: e2e
e2e:
Expand Down Expand Up @@ -97,9 +108,19 @@ $(GOPATH)/bin/golangci-lint:
GOLANGCI_LINT_CACHE=/tmp/golangci-cache $(GOPATH)/bin/golangci-lint version
GOLANGCI_LINT_CACHE=/tmp/golangci-cache $(GOPATH)/bin/golangci-lint linters

.PHONY: set-release-tag
set-release-tag:
$(eval IMAGE_TAG = $(VERSION))

.PHONY: image
image: default-image centos-image fedora-image

.PHONY: release-image
release-image: set-release-tag default-image centos-image fedora-image push push-fedora
# This will ensure that we also push to the latest tag
$(eval IMAGE_TAG = latest)
$(MAKE) push

.PHONY: default-image
default-image:
$(CONTAINER_RUNTIME) build -f images/Dockerfile.centos -t $(IMAGE_REPO) .
Expand All @@ -113,9 +134,13 @@ fedora-image:
$(CONTAINER_RUNTIME) build -f images/Dockerfile.fedora -t $(FEDORA_IMAGE_REPO) .

.PHONY: push
push:
push: default-image
$(CONTAINER_RUNTIME) push $(IMAGE_REPO)

.PHONY: push-fedora
push-fedora: fedora-image
$(CONTAINER_RUNTIME) push $(FEDORA_IMAGE_REPO)

image.tar:
$(MAKE) $(TEST_OS)-image && \
$(CONTAINER_RUNTIME) save -o image.tar quay.io/security-profiles-operator/$(IMAGE_NAME)-$(TEST_OS):$(IMAGE_TAG); \
Expand All @@ -126,3 +151,31 @@ vagrant-up: image.tar ## Boot the vagrant based test VM
# Retry in case provisioning failed because of some temporarily unavailable
# remote resource (like the VM image)
vagrant up || vagrant up || vagrant up

.PHONY: check-release-version
check-release-version:
ifndef RELEASE_VERSION
$(error RELEASE_VERSION must be defined)
endif

.PHONY: commit-release-version
commit-release-version: check-release-version
echo $(RELEASE_VERSION) > VERSION
git add VERSION
git commit -s -m "Release v$(RELEASE_VERSION)"
$(eval VERSION = $(RELEASE_VERSION))

.PHONY: next-version
next-version:
@grep '^[0-9]\+.[0-9]\+.[0-9]\+$$' VERSION
sed -i "s/\([0-9]\+\.[0-9]\+\.[0-9]\+\)/\1.99/" VERSION
git add VERSION
git commit -s -m "Prepare VERSION for the next release"

.PHONY: tag-release
tag-release:
git tag "v$(VERSION)"
git push origin "v$(VERSION)"

.PHONY: release
release: commit-release-version tag-release release-image next-version
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.2.99
3 changes: 3 additions & 0 deletions cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/containers/selinuxd/pkg/daemon"
"github.com/containers/selinuxd/pkg/datastore"
"github.com/containers/selinuxd/pkg/semodule"
"github.com/containers/selinuxd/pkg/version"
)

// daemonCmd represents the daemon command
Expand Down Expand Up @@ -100,6 +101,8 @@ func daemonCmdFunc(rootCmd *cobra.Command, _ []string) {
syscall.Exit(1)
}

version.PrintInfoPermissive(logger)

exitSignal := make(chan os.Signal, 1)
done := make(chan bool)
signal.Notify(exitSignal, syscall.SIGINT, syscall.SIGTERM)
Expand Down
3 changes: 3 additions & 0 deletions cmd/oneshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/containers/selinuxd/pkg/datastore"
"github.com/containers/selinuxd/pkg/semodule"
seiface "github.com/containers/selinuxd/pkg/semodule/interface"
"github.com/containers/selinuxd/pkg/version"
)

// oneshotCmd represents the oneshot command
Expand Down Expand Up @@ -79,6 +80,8 @@ func oneshotCmdFunc(rootCmd *cobra.Command, _ []string) {
syscall.Exit(1)
}

version.PrintInfoPermissive(logger)

opts, err := parseOneShotFlags(rootCmd)
if err != nil {
logger.Error(err, "Parsing flags")
Expand Down
50 changes: 50 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package version

import (
"fmt"
"runtime"

"github.com/go-logr/logr"
)

var (
buildDate string // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
version string // the current version of the daemon
)

type Info struct {
Version string `json:"version,omitempty"`
BuildDate string `json:"buildDate,omitempty"`
Compiler string `json:"compiler,omitempty"`
Platform string `json:"platform,omitempty"`
}

// AsKeyValues returns a key value slice of the info.
func (i *Info) AsKeyValues() []interface{} {
return []interface{}{
"version", i.Version,
"buildDate", i.BuildDate,
"compiler", i.Compiler,
"platform", i.Platform,
}
}

func getInfo() *Info {
return &Info{
Version: version,
BuildDate: buildDate,
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}
}

func printInfo(logger logr.Logger, info *Info) {
logger.Info(
"selinuxd information",
info.AsKeyValues()...,
)
}

func PrintInfoPermissive(logger logr.Logger) {
printInfo(logger, getInfo())
}