Skip to content
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

Version commit hash #191

Merged
merged 29 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
2dab094
added check dirty script
ehearneRedHat Jul 15, 2024
4e75e96
added target level vars .
ehearneRedHat Jul 15, 2024
523b2fc
added ldflag strings
ehearneRedHat Jul 15, 2024
9c3630c
added docker args
ehearneRedHat Jul 15, 2024
88c098d
added target level args + docker envs
ehearneRedHat Jul 18, 2024
5a3d19d
added build args to workflow
ehearneRedHat Jul 22, 2024
1a62830
remove unnecessary var
ehearneRedHat Jul 23, 2024
504aa0f
Merge branch 'main' into version-commit-hash
ehearneRedHat Jul 23, 2024
24e55c8
change commit to gitsha args + envs
ehearneRedHat Jul 29, 2024
650b73a
change commit target vars to gitsha target vars
ehearneRedHat Jul 29, 2024
613b189
change commit build args to gitsha build args
ehearneRedHat Jul 29, 2024
b335047
move version to separate file + commit change to gitsha
ehearneRedHat Jul 29, 2024
e5736c4
Merge branch 'version-commit-hash' of github.com:Kuadrant/dns-operato…
ehearneRedHat Jul 29, 2024
4b8832b
Merge branch 'main' into version-commit-hash
ehearneRedHat Jul 29, 2024
622abb9
added func printControllerMetaInfo()
ehearneRedHat Jul 29, 2024
4400824
new test tools for pkg/log/log_test.go
ehearneRedHat Jul 29, 2024
1f642d3
import new log go file + brought over dns-operator's log pkg
ehearneRedHat Jul 29, 2024
893de4f
add copy for new log pkg
ehearneRedHat Jul 29, 2024
22f2a57
added new changes to go.mod
ehearneRedHat Jul 29, 2024
35fc04b
add new line for import
ehearneRedHat Jul 29, 2024
704035e
move version to internal/version
ehearneRedHat Aug 9, 2024
fe5fa3a
version now copy in internal/
ehearneRedHat Aug 9, 2024
d348e42
revert logger config
ehearneRedHat Aug 9, 2024
075768f
move printcontrollermetainfo() to below setuplogger
ehearneRedHat Aug 9, 2024
65bd458
remove copy pkg
ehearneRedHat Aug 9, 2024
ef9ad74
fix from casing issue
ehearneRedHat Aug 9, 2024
9c4ef60
Merge branch 'main' into version-commit-hash
ehearneRedHat Aug 9, 2024
c0cb348
add build info string to controllermetainfo
ehearneRedHat Aug 26, 2024
03cc372
Merge branch 'main' of github.com:Kuadrant/dns-operator into version-…
ehearneRedHat Aug 26, 2024
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
7 changes: 7 additions & 0 deletions .github/workflows/build-images.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ jobs:
image: ${{ env.OPERATOR_NAME }}
tags: ${{ env.IMG_TAGS }}
platforms: linux/amd64,linux/arm64
build-args: |
GIT_SHA=${{ github.sha }}
DIRTY=false
dockerfiles: |
./Dockerfile
Expand Down Expand Up @@ -92,6 +96,9 @@ jobs:
image: ${{ env.OPERATOR_NAME }}-bundle
tags: ${{ needs.build.outputs.build-tags }}
platforms: linux/amd64,linux/arm64
build-args: |
GIT_SHA=${{ github.sha }}
DIRTY=false
dockerfiles: |
./bundle.Dockerfile
Expand Down
11 changes: 10 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ FROM golang:1.21 as builder
ARG TARGETOS
ARG TARGETARCH

ARG GIT_SHA
ARG DIRTY

ENV GIT_SHA=${GIT_SHA:-unknown}
ENV DIRTY=${DIRTY:-unknown}


WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
Expand All @@ -15,13 +22,15 @@ RUN go mod download
COPY cmd/main.go cmd/main.go
COPY api/ api/
COPY internal/ internal/
COPY version/ version/
COPY pkg/ pkg/
ehearneRedHat marked this conversation as resolved.
Show resolved Hide resolved

# Build
# the GOARCH has not a default value to allow the binary be built according to the host where the command
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -ldflags "-X main.gitSHA=${GIT_SHA} -X main.dirty=${DIRTY}" -a -o manager cmd/main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand Down
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -214,19 +214,25 @@ local-deploy-namespaced: docker-build kind-load-image ## Deploy the dns operator
##@ Build

.PHONY: build
build: GIT_SHA=$(shell git rev-parse HEAD || echo "unknown")
build: DIRTY=$(shell hack/check-git-dirty.sh || echo "unknown")
build: manifests generate fmt vet ## Build manager binary.
go build -o bin/manager cmd/main.go
go build -ldflags "-X main.gitSHA=${GIT_SHA} -X main.dirty=${DIRTY}" -o bin/manager cmd/main.go

.PHONY: run
run: GIT_SHA=$(shell git rev-parse HEAD || echo "unknown")
run: DIRTY=$(shell hack/check-git-dirty.sh || echo "unknown")
mikenairn marked this conversation as resolved.
Show resolved Hide resolved
run: manifests generate fmt vet ## Run a controller from your host.
go run ./cmd/main.go --zap-devel --provider inmemory,aws,google,azure
go run -ldflags "-X main.gitSHA=${GIT_SHA} -X main.dirty=${DIRTY}" ./cmd/main.go --zap-devel --provider inmemory,aws,google,azure
mikenairn marked this conversation as resolved.
Show resolved Hide resolved

# If you wish built the manager image targeting other platforms you can use the --platform flag.
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
.PHONY: docker-build
docker-build: GIT_SHA=$(shell git rev-parse HEAD || echo "unknown")
docker-build: DIRTY=$(shell hack/check-git-dirty.sh || echo "unknown")
docker-build: ## Build docker image with the manager.
$(CONTAINER_TOOL) build -t ${IMG} .
$(CONTAINER_TOOL) build -t ${IMG} . --build-arg GIT_SHA=$(GIT_SHA) --build-arg DIRTY=$(DIRTY)

.PHONY: docker-push
docker-push: ## Push docker image with the manager.
Expand Down
27 changes: 25 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ import (
"flag"
"fmt"
"os"
"runtime"
"strings"
"time"

"k8s.io/apimachinery/pkg/runtime"
k8sruntime "k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/utils/env"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/healthz"
Expand All @@ -41,12 +43,18 @@ import (
_ "github.com/kuadrant/dns-operator/internal/provider/azure"
_ "github.com/kuadrant/dns-operator/internal/provider/google"
_ "github.com/kuadrant/dns-operator/internal/provider/inmemory"
"github.com/kuadrant/dns-operator/pkg/log"
"github.com/kuadrant/dns-operator/version"
//+kubebuilder:scaffold:imports
)

var (
scheme = runtime.NewScheme()
scheme = k8sruntime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
logLevel = env.GetString("LOG_LEVEL", "info")
logMode = env.GetString("LOG_MODE", "production")
gitSHA string // pass ldflag here to display gitSHA hash
dirty string // must be string as passed in by ldflag to determine display .
)

const (
Expand All @@ -60,9 +68,24 @@ func init() {

utilruntime.Must(v1alpha1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme

logger := log.NewLogger(
log.SetLevel(log.ToLevel(logLevel)),
log.SetMode(log.ToMode(logMode)),
log.WriteTo(os.Stdout),
)
log.SetLogger(logger)
ehearneRedHat marked this conversation as resolved.
Show resolved Hide resolved
}

func printControllerMetaInfo() {
setupLog.Info(fmt.Sprintf("go version: %s", runtime.Version()))
setupLog.Info(fmt.Sprintf("go os/arch: %s/%s", runtime.GOOS, runtime.GOARCH))
setupLog.Info("base logger", "log level", logLevel, "log mode", logMode)
setupLog.Info("", "version", version.Version, "commit", gitSHA, "dirty", dirty)
ehearneRedHat marked this conversation as resolved.
Show resolved Hide resolved
}

func main() {
printControllerMetaInfo()
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ require (
github.com/stretchr/objx v0.5.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
go.uber.org/zap v1.26.0
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0 // indirect
Expand All @@ -93,9 +93,10 @@ require (
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools v2.2.0+incompatible
k8s.io/apiextensions-apiserver v0.28.3 // indirect
k8s.io/component-base v0.28.3 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/klog/v2 v2.100.1
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.3.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
k8s.io/api v0.28.3 h1:Gj1HtbSdB4P08C8rs9AR94MfSGpRhJgsS+GF9V26xMM=
Expand Down
15 changes: 15 additions & 0 deletions hack/check-git-dirty.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

if ! command -v git &>/dev/null
then
echo "git not found..." >&2
exit 1
fi

if output=$(git diff --stat 2>/dev/null)
then
[ -n "$output" ] && echo "true" || echo "false"
else
# Not a git repository
exit 1
fi
159 changes: 159 additions & 0 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
Copyright 2021 Red Hat, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package log

import (
"fmt"
"io"
"os"
"strings"

"github.com/go-logr/logr"
"go.uber.org/zap/zapcore"

"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)

var (
// Log is the base logger
Log logr.Logger = logr.New(ctrllog.NullLogSink{})
)

// Level configures the verbosity of the logging.
type Level zapcore.Level

const (
// DebugLevel logs are typically voluminous, and are usually disabled in production.
DebugLevel = Level(zapcore.DebugLevel)
// InfoLevel is the default logging priority.
InfoLevel = Level(zapcore.InfoLevel)
// WarnLevel logs are more important than Info, but don't need individual human review.
WarnLevel = Level(zapcore.WarnLevel)
// ErrorLevel logs are high-priority. If an application is running smoothly,
// it shouldn't generate any error-level logs.
ErrorLevel = Level(zapcore.ErrorLevel)
// DPanicLevel logs are particularly important errors. In development the
// logger panics after writing the message.
DPanicLevel = Level(zapcore.DPanicLevel)
// PanicLevel logs a message, then panics.
PanicLevel = Level(zapcore.PanicLevel)
// FatalLevel logs a message, then calls os.Exit(1).
FatalLevel = Level(zapcore.FatalLevel)
)

// ToLevel converts a string to a log level.
func ToLevel(level string) Level {
var l zapcore.Level
err := l.UnmarshalText([]byte(level))
if err != nil {
panic(err)
}
return Level(l)
}

// Mode defines the log output mode.
type Mode int8

const (
// ModeProd is the log mode for production.
ModeProd Mode = iota
// ModeDev is for more human-readable outputs, extra stack traces
// and logging info. (aka Zap's "development config".)
// https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/log/zap#UseDevMode
ModeDev
)

// ToMode converts a string to a log mode.
// Use either 'production' for `LogModeProd` or 'development' for `LogModeDev`.
func ToMode(mode string) Mode {
switch strings.ToLower(mode) {
case "production":
return ModeProd
case "development":
return ModeDev
default:
panic(fmt.Sprintf("unknown log mode: %s", mode))
}
}

// Opts allows to manipulate Options.
type Opts func(*Options)

// Options contains all possible settings.
type Options struct {
// LogLevel configures the verbosity of the logging.
LogLevel Level
// LogMode defines the log output mode.
LogMode Mode
// DestWriter controls the destination of the log output. Defaults to
// os.Stderr.
DestWriter io.Writer
}

// SetLogger sets a concrete logging implementation for all deferred Loggers.
// Being top level application and not a library or dependency,
// let's delegation logger used by any dependency
func SetLogger(logger logr.Logger) {
Log = logger

ctrl.SetLogger(logger) // fulfills `logger` as the de facto logger used by controller-runtime
klog.SetLogger(logger)
}

// WriteTo configures the logger to write to the given io.Writer, instead of standard error.
// See Options.DestWriter.
func WriteTo(out io.Writer) Opts {
return func(o *Options) {
o.DestWriter = out
}
}

// SetLevel sets Options.LogLevel, which configures the the minimum enabled logging level e.g Debug, Info.
func SetLevel(level Level) func(o *Options) {
return func(o *Options) {
o.LogLevel = level
}
}

// SetMode sets Options.LogMode, which configures to use (or not use) development mode
func SetMode(mode Mode) func(o *Options) {
return func(o *Options) {
o.LogMode = mode
}
}

// NewLogger creates new Logger based on controller runtime zap logger
func NewLogger(opts ...Opts) logr.Logger {
o := &Options{}
for _, opt := range opts {
opt(o)
}

// defaults
if o.DestWriter == nil {
o.DestWriter = os.Stderr
}

return zap.New(
zap.Level(zapcore.Level(o.LogLevel)),
zap.UseDevMode(o.LogMode == ModeDev),
zap.WriteTo(o.DestWriter),
)
}
Loading
Loading