build: update build configuration to use internal metadata package and improve ldflags injection, fix defer resp.Body.Close() usage, replace os.Setenv with t.Setenv in tests, correct error message casing, and adjust Dockerfile and Makefile for cmd/reloader structure

This commit is contained in:
TheiLLeniumStudios
2025-12-28 08:47:57 +01:00
parent 841f6f3868
commit 0c40064872
10 changed files with 43 additions and 45 deletions
+8 -1
View File
@@ -1,5 +1,7 @@
builds:
- env:
- main: ./cmd/reloader
binary: reloader
env:
- CGO_ENABLED=0
goos:
- windows
@@ -11,6 +13,11 @@ builds:
- arm
- arm64
- ppc64le
ldflags:
- -s -w
- -X github.com/stakater/Reloader/internal/pkg/metadata.Version={{.Version}}
- -X github.com/stakater/Reloader/internal/pkg/metadata.Commit={{.Commit}}
- -X github.com/stakater/Reloader/internal/pkg/metadata.BuildDate={{.Date}}
archives:
- name_template: "{{ .ProjectName }}_v{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}"
snapshot:
+6 -6
View File
@@ -23,9 +23,8 @@ COPY go.sum go.sum
RUN go mod download
# Copy the go source
COPY main.go main.go
COPY cmd/ cmd/
COPY internal/ internal/
COPY pkg/ pkg/
# Build
RUN CGO_ENABLED=0 \
@@ -34,10 +33,11 @@ RUN CGO_ENABLED=0 \
GOPROXY=${GOPROXY} \
GOPRIVATE=${GOPRIVATE} \
GO111MODULE=on \
go build -ldflags="-s -w -X github.com/stakater/Reloader/pkg/common.Version=${VERSION} \
-X github.com/stakater/Reloader/pkg/common.Commit=${COMMIT} \
-X github.com/stakater/Reloader/pkg/common.BuildDate=${BUILD_DATE}" \
-installsuffix 'static' -mod=mod -a -o manager ./
go build -ldflags="-s -w \
-X github.com/stakater/Reloader/internal/pkg/metadata.Version=${VERSION} \
-X github.com/stakater/Reloader/internal/pkg/metadata.Commit=${COMMIT} \
-X github.com/stakater/Reloader/internal/pkg/metadata.BuildDate=${BUILD_DATE}" \
-installsuffix 'static' -mod=mod -a -o manager ./cmd/reloader
# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
+1
View File
@@ -1,6 +1,7 @@
ARG BUILDER_IMAGE
ARG BASE_IMAGE
# First stage: Build the binary (using the standard Dockerfile as builder)
FROM --platform=${BUILDPLATFORM} ${BUILDER_IMAGE} AS SRC
FROM ${BASE_IMAGE:-registry.access.redhat.com/ubi9/ubi:latest} AS ubi
+11 -4
View File
@@ -20,10 +20,17 @@ BUILD=
GOCMD = go
GOFLAGS ?= $(GOFLAGS:)
LDFLAGS =
GOPROXY ?=
GOPRIVATE ?=
# Version information for ldflags
GIT_COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
BUILD_DATE ?= $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
LDFLAGS = -s -w \
-X github.com/stakater/Reloader/internal/pkg/metadata.Version=$(VERSION) \
-X github.com/stakater/Reloader/internal/pkg/metadata.Commit=$(GIT_COMMIT) \
-X github.com/stakater/Reloader/internal/pkg/metadata.BuildDate=$(BUILD_DATE)
## Location to install dependencies to
LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
@@ -97,10 +104,10 @@ install:
"$(GOCMD)" mod download
run:
go run ./main.go
go run ./cmd/reloader
build:
"$(GOCMD)" build ${GOFLAGS} ${LDFLAGS} -o "${BINARY}"
"$(GOCMD)" build ${GOFLAGS} -ldflags '${LDFLAGS}' -o "${BINARY}" ./cmd/reloader
lint: golangci-lint ## Run golangci-lint on the codebase
$(GOLANGCI_LINT) run ./...
@@ -140,7 +147,7 @@ manifest:
docker manifest annotate --arch $(ARCH) $(REPOSITORY_GENERIC) $(REPOSITORY_ARCH)
test:
"$(GOCMD)" test -timeout 1800s -v ./...
"$(GOCMD)" test -timeout 1800s -v ./cmd/... ./internal/...
stop:
@docker stop "${BINARY}"
+1 -1
View File
@@ -46,7 +46,7 @@ func (c *httpClient) post(ctx context.Context, url string, body []byte) error {
if err != nil {
return fmt.Errorf("sending request: %w", err)
}
defer resp.Body.Close()
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
body, _ := io.ReadAll(resp.Body)
+9 -23
View File
@@ -3,7 +3,6 @@ package metadata
import (
"context"
"encoding/json"
"os"
"testing"
"github.com/go-logr/logr"
@@ -112,12 +111,8 @@ func TestNewReloaderOptions(t *testing.T) {
func TestMetaInfo_ToConfigMap(t *testing.T) {
// Set environment variables
os.Setenv(EnvReloaderNamespace, "reloader-ns")
os.Setenv(EnvReloaderDeploymentName, "reloader-deploy")
defer func() {
os.Unsetenv(EnvReloaderNamespace)
os.Unsetenv(EnvReloaderDeploymentName)
}()
t.Setenv(EnvReloaderNamespace, "reloader-ns")
t.Setenv(EnvReloaderDeploymentName, "reloader-deploy")
cfg := config.NewDefault()
metaInfo := NewMetaInfo(cfg)
@@ -164,8 +159,8 @@ func TestMetaInfo_ToConfigMap(t *testing.T) {
}
func TestPublisher_Publish_NoNamespace(t *testing.T) {
// Ensure RELOADER_NAMESPACE is not set
os.Unsetenv(EnvReloaderNamespace)
// Ensure RELOADER_NAMESPACE is not set (empty value)
t.Setenv(EnvReloaderNamespace, "")
scheme := runtime.NewScheme()
_ = corev1.AddToScheme(scheme)
@@ -182,12 +177,8 @@ func TestPublisher_Publish_NoNamespace(t *testing.T) {
func TestPublisher_Publish_CreateNew(t *testing.T) {
// Set environment variables
os.Setenv(EnvReloaderNamespace, "test-ns")
os.Setenv(EnvReloaderDeploymentName, "test-deploy")
defer func() {
os.Unsetenv(EnvReloaderNamespace)
os.Unsetenv(EnvReloaderDeploymentName)
}()
t.Setenv(EnvReloaderNamespace, "test-ns")
t.Setenv(EnvReloaderDeploymentName, "test-deploy")
scheme := runtime.NewScheme()
_ = corev1.AddToScheme(scheme)
@@ -215,12 +206,8 @@ func TestPublisher_Publish_CreateNew(t *testing.T) {
func TestPublisher_Publish_UpdateExisting(t *testing.T) {
// Set environment variables
os.Setenv(EnvReloaderNamespace, "test-ns")
os.Setenv(EnvReloaderDeploymentName, "test-deploy")
defer func() {
os.Unsetenv(EnvReloaderNamespace)
os.Unsetenv(EnvReloaderDeploymentName)
}()
t.Setenv(EnvReloaderNamespace, "test-ns")
t.Setenv(EnvReloaderDeploymentName, "test-deploy")
scheme := runtime.NewScheme()
_ = corev1.AddToScheme(scheme)
@@ -273,8 +260,7 @@ func TestPublisher_Publish_UpdateExisting(t *testing.T) {
func TestPublishMetaInfoConfigMap(t *testing.T) {
// Set environment variables
os.Setenv(EnvReloaderNamespace, "test-ns")
defer os.Unsetenv(EnvReloaderNamespace)
t.Setenv(EnvReloaderNamespace, "test-ns")
scheme := runtime.NewScheme()
_ = corev1.AddToScheme(scheme)
+4 -7
View File
@@ -1,7 +1,6 @@
package metrics
import (
"os"
"testing"
"github.com/prometheus/client_golang/prometheus"
@@ -97,8 +96,7 @@ func TestRecordReload_MultipleIncrements(t *testing.T) {
func TestRecordReload_WithNamespaceTracking(t *testing.T) {
// Enable namespace tracking
os.Setenv("METRICS_COUNT_BY_NAMESPACE", "enabled")
defer os.Unsetenv("METRICS_COUNT_BY_NAMESPACE")
t.Setenv("METRICS_COUNT_BY_NAMESPACE", "enabled")
collectors := NewCollectors()
collectors.RecordReload(true, "kube-system")
@@ -117,8 +115,8 @@ func TestRecordReload_WithNamespaceTracking(t *testing.T) {
}
func TestRecordReload_WithoutNamespaceTracking(t *testing.T) {
// Ensure namespace tracking is disabled
os.Unsetenv("METRICS_COUNT_BY_NAMESPACE")
// Ensure namespace tracking is disabled (t.Setenv to empty resets it)
t.Setenv("METRICS_COUNT_BY_NAMESPACE", "")
collectors := NewCollectors()
collectors.RecordReload(true, "kube-system")
@@ -139,8 +137,7 @@ func TestNilCollectors_NoPanic(t *testing.T) {
}
func TestRecordReload_DifferentNamespaces(t *testing.T) {
os.Setenv("METRICS_COUNT_BY_NAMESPACE", "enabled")
defer os.Unsetenv("METRICS_COUNT_BY_NAMESPACE")
t.Setenv("METRICS_COUNT_BY_NAMESPACE", "enabled")
collectors := NewCollectors()
collectors.RecordReload(true, "namespace-a")
+1 -1
View File
@@ -73,7 +73,7 @@ func (c *Client) Send(ctx context.Context, payload Payload) error {
if err != nil {
return fmt.Errorf("sending request: %w", err)
}
defer resp.Body.Close()
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return fmt.Errorf("webhook returned status %d", resp.StatusCode)
+1 -1
View File
@@ -74,7 +74,7 @@ func TestSend_MarshalPayload(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
json.Unmarshal(body, &receivedPayload)
_ = json.Unmarshal(body, &receivedPayload)
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
+1 -1
View File
@@ -73,7 +73,7 @@ func (r *Registry) FromObject(obj client.Object) (WorkloadAccessor, error) {
return NewCronJobWorkload(o), nil
case *argorolloutv1alpha1.Rollout:
if !r.argoRolloutsEnabled {
return nil, fmt.Errorf("Argo Rollouts support is not enabled")
return nil, fmt.Errorf("argo Rollouts support is not enabled")
}
return NewRolloutWorkload(o), nil
default: