Debian Codegraph @

api/provenance/provenance.go

raw
1 // Copyright 2019 The Kubernetes Authors.
2 // SPDX-License-Identifier: Apache-2.0
3
4 package provenance
5
6 import (
7 "fmt"
8 "runtime"
9 "runtime/debug"
10 "strings"
11
12 "github.com/blang/semver/v4"
13 )
14
15 // These variables are set at build time using ldflags.
16 //
17 //nolint:gochecknoglobals
18 var (
19 // During a release, this will be set to the release tag, e.g. "kustomize/v4.5.7"
20 version = developmentVersion
21 // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
22 buildDate = unknown
23 )
24
25 const (
26 // This default value, (devel), matches
27 // the value debug.BuildInfo uses for an unset main module version.
28 developmentVersion = "(devel)"
29
30 // ModulePath is kustomize module path, defined in kustomize/go.mod
31 ModulePath = "sigs.k8s.io/kustomize/kustomize/v5"
32
33 // This is default value, unknown, substituted when
34 // the value can't be determined from debug.BuildInfo.
35 unknown = "unknown"
36 )
37
38 // Provenance holds information about the build of an executable.
39 type Provenance struct {
40 // Version of the kustomize binary.
41 Version string `json:"version,omitempty" yaml:"version,omitempty"`
42 // GitCommit is a git commit
43 GitCommit string `json:"gitCommit,omitempty" yaml:"gitCommit,omitempty"`
44 // BuildDate is date of the build.
45 BuildDate string `json:"buildDate,omitempty" yaml:"buildDate,omitempty"`
46 // GoOs holds OS name.
47 GoOs string `json:"goOs,omitempty" yaml:"goOs,omitempty"`
48 // GoArch holds architecture name.
49 GoArch string `json:"goArch,omitempty" yaml:"goArch,omitempty"`
50 // GoVersion holds Go version.
51 GoVersion string `json:"goVersion,omitempty" yaml:"goVersion,omitempty"`
52 }
53
54 // GetProvenance returns an instance of Provenance.
55 func GetProvenance() Provenance {
56 p := Provenance{
57 BuildDate: buildDate,
58 Version: version,
59 GitCommit: unknown,
60 GoOs: runtime.GOOS,
61 GoArch: runtime.GOARCH,
62 GoVersion: runtime.Version(),
63 }
64 info, ok := debug.ReadBuildInfo()
65 if !ok {
66 return p
67 }
68
69 for _, setting := range info.Settings {
70 // For now, the git commit is the only information of interest.
71 // We could consider adding other info such as the commit date in the future.
72 if setting.Key == "vcs.revision" {
73 p.GitCommit = setting.Value
74 break
75 }
76 }
77 p.Version = FindVersion(info, p.Version)
78
79 return p
80 }
81
82 // FindVersion searches for a version in the depth of dependencies including replacements,
83 // otherwise, it tries to get version from debug.BuildInfo Main.
84 func FindVersion(info *debug.BuildInfo, version string) string {
85 for _, dep := range info.Deps {
86 if dep != nil && dep.Path == ModulePath {
87 if dep.Version == developmentVersion {
88 continue
89 }
90 v, err := GetMostRecentTag(*dep)
91 if err != nil {
92 fmt.Printf("failed to get most recent tag for %s: %v\n", dep.Path, err)
93 continue
94 }
95
96 return v
97 }
98 }
99
100 if version == developmentVersion && info.Main.Version != "" {
101 return info.Main.Version
102 }
103
104 return version
105 }
106
107 func GetMostRecentTag(m debug.Module) (string, error) {
108 for m.Replace != nil {
109 m = *m.Replace
110 }
111
112 split := strings.Split(m.Version, "-")
113 sv, err := semver.Parse(strings.TrimPrefix(split[0], "v"))
114
115 if err != nil {
116 return "", fmt.Errorf("failed to parse version %s: %w", m.Version, err)
117 }
118
119 if len(split) > 1 && sv.Patch > 0 {
120 sv.Patch -= 1
121 }
122 return fmt.Sprintf("v%s", sv.FinalizeVersion()), nil
123 }
124
125 // Short returns the shortened provenance stamp.
126 func (v Provenance) Short() string {
127 return fmt.Sprintf(
128 "%v",
129 Provenance{
130 Version: v.Version,
131 BuildDate: v.BuildDate,
132 })
133 }
134
135 // Semver returns the semantic version of kustomize.
136 // kustomize version is set in format "kustomize/vX.X.X" in every release.
137 // X.X.X is a semver. If the version string is not in this format,
138 // return the original version string
139 func (v Provenance) Semver() string {
140 return strings.TrimPrefix(v.Version, "kustomize/")
141 }