...
1
2
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
16
17
18 var (
19
20 version = developmentVersion
21
22 buildDate = "unknown"
23 )
24
25
26
27 const developmentVersion = "(devel)"
28
29
30 type Provenance struct {
31
32 Version string `json:"version,omitempty" yaml:"version,omitempty"`
33
34 GitCommit string `json:"gitCommit,omitempty" yaml:"gitCommit,omitempty"`
35
36 BuildDate string `json:"buildDate,omitempty" yaml:"buildDate,omitempty"`
37
38 GoOs string `json:"goOs,omitempty" yaml:"goOs,omitempty"`
39
40 GoArch string `json:"goArch,omitempty" yaml:"goArch,omitempty"`
41
42 GoVersion string `json:"goVersion,omitempty" yaml:"goVersion,omitempty"`
43 }
44
45
46 func GetProvenance() Provenance {
47 p := Provenance{
48 BuildDate: buildDate,
49 Version: version,
50 GitCommit: "unknown",
51 GoOs: runtime.GOOS,
52 GoArch: runtime.GOARCH,
53 GoVersion: runtime.Version(),
54 }
55 info, ok := debug.ReadBuildInfo()
56 if !ok {
57 return p
58 }
59
60 for _, setting := range info.Settings {
61
62
63 if setting.Key == "vcs.revision" {
64 p.GitCommit = setting.Value
65 }
66 }
67
68 for _, dep := range info.Deps {
69 if dep != nil && dep.Path == "sigs.k8s.io/kustomize/kustomize/v5" {
70 if dep.Version != "devel" {
71 continue
72 }
73 v, err := GetMostRecentTag(*dep)
74 if err != nil {
75 fmt.Printf("failed to get most recent tag for %s: %v\n", dep.Path, err)
76 continue
77 }
78 p.Version = v
79 }
80 }
81
82 return p
83 }
84
85 func GetMostRecentTag(m debug.Module) (string, error) {
86 for m.Replace != nil {
87 m = *m.Replace
88 }
89
90 split := strings.Split(m.Version, "-")
91 sv, err := semver.Parse(strings.TrimPrefix(split[0], "v"))
92
93 if err != nil {
94 return "", fmt.Errorf("failed to parse version %s: %w", m.Version, err)
95 }
96
97 if len(split) > 1 && sv.Patch > 0 {
98 sv.Patch -= 1
99 }
100 return fmt.Sprintf("v%s", sv.FinalizeVersion()), nil
101 }
102
103
104 func (v Provenance) Short() string {
105 return fmt.Sprintf(
106 "%v",
107 Provenance{
108 Version: v.Version,
109 BuildDate: v.BuildDate,
110 })
111 }
112
113
114
115
116
117 func (v Provenance) Semver() string {
118 return strings.TrimPrefix(v.Version, "kustomize/")
119 }
120
View as plain text