Skip to content

Commit

Permalink
fix gflag to version
Browse files Browse the repository at this point in the history
Signed-off-by: dongjiang <[email protected]>
  • Loading branch information
dongjiang1989 committed Jan 16, 2025
1 parent 851c785 commit e1acbcf
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
14 changes: 14 additions & 0 deletions hack/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -707,9 +707,21 @@ function util::get_version() {
git describe --tags --dirty
}

function util::get_branch() {
git branch --show-current
}

function util::get_revision() {
git rev-parse --short HEAD
}

function util::version_ldflags() {
# Git information
GIT_VERSION=$(util::get_version)
# Git branch
GIT_BRANCH=$(util::get_branch)
# Git revision
GIT_REVISION=$(util::get_revision)
GIT_COMMIT_HASH=$(git rev-parse HEAD)
if git_status=$(git status --porcelain 2>/dev/null) && [[ -z ${git_status} ]]; then
GIT_TREESTATE="clean"
Expand All @@ -720,6 +732,8 @@ function util::version_ldflags() {
LDFLAGS="-X github.com/karmada-io/karmada/pkg/version.gitVersion=${GIT_VERSION} \
-X github.com/karmada-io/karmada/pkg/version.gitCommit=${GIT_COMMIT_HASH} \
-X github.com/karmada-io/karmada/pkg/version.gitTreeState=${GIT_TREESTATE} \
-X github.com/karmada-io/karmada/pkg/version.gitBranch=${GIT_BRANCH} \
-X github.com/karmada-io/karmada/pkg/version.gitRevision=${GIT_REVISION} \
-X github.com/karmada-io/karmada/pkg/version.buildDate=${BUILDDATE}"
echo $LDFLAGS
}
Expand Down
3 changes: 1 addition & 2 deletions operator/cmd/operator/app/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"regexp"

"github.com/prometheus/client_golang/prometheus/collectors"
prometheusversion "github.com/prometheus/client_golang/prometheus/collectors/version"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/sets"
restclient "k8s.io/client-go/rest"
Expand Down Expand Up @@ -133,7 +132,7 @@ func Run(ctx context.Context, o *options.Options) error {
),
)
ctrlmetrics.Registry.MustRegister(
prometheusversion.NewCollector("karmada_operator"),
version.NewCollector("karmada_operator"),
)

controllerCtx := ctrlctx.Context{
Expand Down
2 changes: 2 additions & 0 deletions pkg/version/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ var (
gitVersion = "v0.0.0-master"
gitCommit = "unknown" // sha1 from git, output of $(git rev-parse HEAD)
gitTreeState = "unknown" // state of git tree, either "clean" or "dirty"
gitBranch = "unknown"
gitRevision = "unknown"

buildDate = "unknown" // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
)
32 changes: 32 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ package version
import (
"fmt"
"runtime"

"github.com/prometheus/client_golang/prometheus"
)

// Info contains versioning information.
type Info struct {
GitVersion string `json:"gitVersion"`
GitCommit string `json:"gitCommit"`
GitRevision string `json:"gitRevision"`
GitTreeState string `json:"gitTreeState"`
GitBranch string `json:"gitBranch"`
BuildDate string `json:"buildDate"`
GoVersion string `json:"goVersion"`
Compiler string `json:"compiler"`
Expand All @@ -42,11 +46,39 @@ func (info Info) String() string {
func Get() Info {
return Info{
GitVersion: gitVersion,
GitRevision: gitRevision,
GitCommit: gitCommit,
GitTreeState: gitTreeState,
GitBranch: gitBranch,
BuildDate: buildDate,
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}
}

// NewCollector returns a collector that exports metrics about current version
// information.
func NewCollector(program string) prometheus.Collector {
return prometheus.NewGaugeFunc(
prometheus.GaugeOpts{
Namespace: program,
Name: "build_info",
Help: fmt.Sprintf(
"A metric with a constant '1' value labeled by version, revision, branch, goversion from which %s was built, and the goos and goarch for the build.",
program,
),
ConstLabels: prometheus.Labels{
"version": Get().GitVersion,
"revision": Get().GitRevision,
"branch": Get().GitBranch,
"goversion": runtime.Version(),
"goos": runtime.GOOS,
"goarch": runtime.GOARCH,
"compiler": runtime.Compiler,
"platform": fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
},
},
func() float64 { return 1 },
)
}
4 changes: 3 additions & 1 deletion pkg/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ func TestInfo_String(t *testing.T) {
GitVersion: "1.3.0",
GitCommit: "da070e68f3318410c8c70ed8186a2bc4736dacbd",
GitTreeState: "clean",
GitRevision: "851c78564",
GitBranch: "v1.3.0",
BuildDate: "2022-08-31T13:09:22Z",
GoVersion: "go1.18.3",
Compiler: "gc",
Platform: "linux/amd64",
},
want: `version.Info{GitVersion:"1.3.0", GitCommit:"da070e68f3318410c8c70ed8186a2bc4736dacbd", GitTreeState:"clean", BuildDate:"2022-08-31T13:09:22Z", GoVersion:"go1.18.3", Compiler:"gc", Platform:"linux/amd64"}`,
want: `version.Info{GitVersion:"1.3.0", GitCommit:"da070e68f3318410c8c70ed8186a2bc4736dacbd", GitRevision:"851c78564", GitTreeState:"clean", GitBranch:"v1.3.0", BuildDate:"2022-08-31T13:09:22Z", GoVersion:"go1.18.3", Compiler:"gc", Platform:"linux/amd64"}`,
},
}
for _, tt := range tests {
Expand Down

0 comments on commit e1acbcf

Please sign in to comment.