Skip to content

Commit b1b97fb

Browse files
authored
use upstream k8s version lib (sigstore#657)
Signed-off-by: Scott Nichols <n3wscott@chainguard.dev>
1 parent 7b70197 commit b1b97fb

File tree

9 files changed

+47
-247
lines changed

9 files changed

+47
-247
lines changed

Makefile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
.PHONY: all test clean clean-gen lint gosec ko ko-local sign-container cross-cli
1717

18-
all: rekor-cli rekor-server
18+
all: rekor-cli rekor-server
1919

2020
GENSRC = pkg/generated/client/%.go pkg/generated/models/%.go pkg/generated/restapi/%.go
2121
OPENAPIDEPS = openapi.yaml $(shell find pkg/types -iname "*.json")
@@ -49,8 +49,10 @@ export KO_DOCKER_REPO=$(KO_PREFIX)
4949
SWAGGER := $(TOOLS_BIN_DIR)/swagger
5050
GO-FUZZ-BUILD := $(TOOLS_BIN_DIR)/go-fuzz-build
5151

52-
FLAG_PKG=github.com/sigstore/rekor/pkg/api
53-
REKOR_LDFLAGS=-X $(FLAG_PKG).GitVersion=$(GIT_VERSION) -X $(FLAG_PKG).GitCommit=$(GIT_HASH) -X $(FLAG_PKG).GitTreeState=$(GIT_TREESTATE) -X $(FLAG_PKG).BuildDate=$(BUILD_DATE)
52+
REKOR_LDFLAGS=-X sigs.k8s.io/release-utils/version.gitVersion=$(GIT_VERSION) \
53+
-X sigs.k8s.io/release-utils/version.gitCommit=$(GIT_HASH) \
54+
-X sigs.k8s.io/release-utils/version.gitTreeState=$(GIT_TREESTATE) \
55+
-X sigs.k8s.io/release-utils/version.buildDate=$(BUILD_DATE)
5456

5557
CLI_LDFLAGS=$(REKOR_LDFLAGS)
5658
SERVER_LDFLAGS=$(REKOR_LDFLAGS)
@@ -66,7 +68,7 @@ validate-openapi: $(SWAGGER)
6668

6769
# this exists to override pattern match rule above since this file is in the generated directory but should not be treated as generated code
6870
pkg/generated/restapi/configure_rekor_server.go: $(OPENAPIDEPS)
69-
71+
7072

7173
lint:
7274
$(GOBIN)/golangci-lint run -v ./...

cmd/rekor-cli/app/root.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@ package app
1717

1818
import (
1919
"fmt"
20-
"runtime/debug"
2120
"strings"
2221

2322
homedir "github.com/mitchellh/go-homedir"
2423
"github.com/spf13/cobra"
2524
"github.com/spf13/pflag"
2625
"github.com/spf13/viper"
2726

28-
"github.com/sigstore/rekor/pkg/api"
2927
"github.com/sigstore/rekor/pkg/log"
3028

3129
// these imports are to call the packages' init methods
@@ -71,19 +69,6 @@ func init() {
7169
if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil {
7270
log.CliLogger.Fatal(err)
7371
}
74-
75-
// look for the default version and replace it, if found, from runtime build info
76-
if api.GitVersion != "devel" {
77-
return
78-
}
79-
80-
bi, ok := debug.ReadBuildInfo()
81-
if !ok {
82-
return
83-
}
84-
// Version is set in artifacts built with -X github.com/sigstore/rekor/cmd/rekor-cli/app.GitVersion=1.2.3
85-
// Ensure version is also set when installed via go install github.com/sigstore/rekor/cmd/rekor-cli
86-
api.GitVersion = bi.Main.Version
8772
}
8873

8974
func initConfig(cmd *cobra.Command) error {

cmd/rekor-cli/app/version.go

Lines changed: 2 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -16,101 +16,9 @@
1616
package app
1717

1818
import (
19-
"encoding/json"
20-
"fmt"
21-
"runtime"
22-
"strings"
23-
"text/tabwriter"
24-
25-
"github.com/pkg/errors"
26-
"github.com/sigstore/rekor/pkg/api"
27-
"github.com/spf13/cobra"
19+
"sigs.k8s.io/release-utils/version"
2820
)
2921

30-
type versionOptions struct {
31-
json bool
32-
}
33-
34-
var versionOpts = &versionOptions{}
35-
36-
// verifyCmd represents the verify command
37-
var versionCmd = &cobra.Command{
38-
Use: "version",
39-
Short: "rekor-cli version",
40-
RunE: func(cmd *cobra.Command, args []string) error {
41-
return runVersion(versionOpts)
42-
},
43-
}
44-
4522
func init() {
46-
versionCmd.PersistentFlags().BoolVarP(&versionOpts.json, "json", "j", false,
47-
"print JSON instead of text")
48-
rootCmd.AddCommand(versionCmd)
49-
}
50-
51-
func runVersion(opts *versionOptions) error {
52-
v := VersionInfo()
53-
res := v.String()
54-
55-
if opts.json {
56-
j, err := v.JSONString()
57-
if err != nil {
58-
return errors.Wrap(err, "unable to generate JSON from version info")
59-
}
60-
res = j
61-
}
62-
63-
fmt.Println(res)
64-
return nil
65-
}
66-
67-
type Info struct {
68-
GitVersion string
69-
GitCommit string
70-
GitTreeState string
71-
BuildDate string
72-
GoVersion string
73-
Compiler string
74-
Platform string
75-
}
76-
77-
func VersionInfo() Info {
78-
// These variables typically come from -ldflags settings and in
79-
// their absence fallback to the global defaults set above.
80-
return Info{
81-
GitVersion: api.GitVersion,
82-
GitCommit: api.GitCommit,
83-
GitTreeState: api.GitTreeState,
84-
BuildDate: api.BuildDate,
85-
GoVersion: runtime.Version(),
86-
Compiler: runtime.Compiler,
87-
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
88-
}
89-
}
90-
91-
// String returns the string representation of the version info
92-
func (i *Info) String() string {
93-
b := strings.Builder{}
94-
w := tabwriter.NewWriter(&b, 0, 0, 2, ' ', 0)
95-
96-
fmt.Fprintf(w, "GitVersion:\t%s\n", i.GitVersion)
97-
fmt.Fprintf(w, "GitCommit:\t%s\n", i.GitCommit)
98-
fmt.Fprintf(w, "GitTreeState:\t%s\n", i.GitTreeState)
99-
fmt.Fprintf(w, "BuildDate:\t%s\n", i.BuildDate)
100-
fmt.Fprintf(w, "GoVersion:\t%s\n", i.GoVersion)
101-
fmt.Fprintf(w, "Compiler:\t%s\n", i.Compiler)
102-
fmt.Fprintf(w, "Platform:\t%s\n", i.Platform)
103-
104-
w.Flush() // #nosec
105-
return b.String()
106-
}
107-
108-
// JSONString returns the JSON representation of the version info
109-
func (i *Info) JSONString() (string, error) {
110-
b, err := json.MarshalIndent(i, "", " ")
111-
if err != nil {
112-
return "", err
113-
}
114-
115-
return string(b), nil
23+
rootCmd.AddCommand(version.Version())
11624
}

cmd/rekor-server/app/root.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,11 @@ import (
2020
"net/http"
2121
"net/http/pprof"
2222
"os"
23-
"runtime/debug"
2423

2524
homedir "github.com/mitchellh/go-homedir"
25+
"github.com/sigstore/rekor/pkg/log"
2626
"github.com/spf13/cobra"
2727
"github.com/spf13/viper"
28-
29-
"github.com/sigstore/rekor/pkg/api"
30-
"github.com/sigstore/rekor/pkg/log"
3128
)
3229

3330
var (
@@ -91,10 +88,6 @@ func init() {
9188

9289
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
9390

94-
// look for the default version and replace it, if found, from runtime build info
95-
if api.GitVersion != "devel" {
96-
return
97-
}
9891
log.Logger.Debugf("pprof enabled %v", enablePprof)
9992
// Enable pprof
10093
if enablePprof {
@@ -110,14 +103,6 @@ func init() {
110103
}
111104
}()
112105
}
113-
114-
bi, ok := debug.ReadBuildInfo()
115-
if !ok {
116-
return
117-
}
118-
// Version is set in artifacts built with -X github.com/sigstore/rekor/cmd/rekor-server/app.GitVersion=1.2.3
119-
// Ensure version is also set when installed via go install github.com/sigstore/rekor/cmd/rekor-server
120-
api.GitVersion = bi.Main.Version
121106
}
122107

123108
// initConfig reads in config file and ENV variables if set.

cmd/rekor-server/app/serve.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/prometheus/client_golang/prometheus/promhttp"
2424
"github.com/spf13/cobra"
2525
"github.com/spf13/viper"
26+
"sigs.k8s.io/release-utils/version"
2627

2728
"github.com/sigstore/rekor/pkg/api"
2829
"github.com/sigstore/rekor/pkg/generated/restapi"
@@ -62,7 +63,7 @@ var serveCmd = &cobra.Command{
6263
// from https://github.com/golang/glog/commit/fca8c8854093a154ff1eb580aae10276ad6b1b5f
6364
_ = flag.CommandLine.Parse([]string{})
6465

65-
vi := VersionInfo()
66+
vi := version.GetVersionInfo()
6667
viStr, err := vi.JSONString()
6768
if err != nil {
6869
viStr = vi.String()

cmd/rekor-server/app/version.go

Lines changed: 2 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -16,101 +16,9 @@
1616
package app
1717

1818
import (
19-
"encoding/json"
20-
"fmt"
21-
"runtime"
22-
"strings"
23-
"text/tabwriter"
24-
25-
"github.com/pkg/errors"
26-
"github.com/sigstore/rekor/pkg/api"
27-
"github.com/spf13/cobra"
19+
"sigs.k8s.io/release-utils/version"
2820
)
2921

30-
type versionOptions struct {
31-
json bool
32-
}
33-
34-
var versionOpts = &versionOptions{}
35-
36-
// verifyCmd represents the verify command
37-
var versionCmd = &cobra.Command{
38-
Use: "version",
39-
Short: "rekor-server version",
40-
RunE: func(cmd *cobra.Command, args []string) error {
41-
return runVersion(versionOpts)
42-
},
43-
}
44-
4522
func init() {
46-
versionCmd.PersistentFlags().BoolVarP(&versionOpts.json, "json", "j", false,
47-
"print JSON instead of text")
48-
rootCmd.AddCommand(versionCmd)
49-
}
50-
51-
func runVersion(opts *versionOptions) error {
52-
v := VersionInfo()
53-
res := v.String()
54-
55-
if opts.json {
56-
j, err := v.JSONString()
57-
if err != nil {
58-
return errors.Wrap(err, "unable to generate JSON from version info")
59-
}
60-
res = j
61-
}
62-
63-
fmt.Println(res)
64-
return nil
65-
}
66-
67-
type Info struct {
68-
GitVersion string
69-
GitCommit string
70-
GitTreeState string
71-
BuildDate string
72-
GoVersion string
73-
Compiler string
74-
Platform string
75-
}
76-
77-
func VersionInfo() Info {
78-
// These variables typically come from -ldflags settings and in
79-
// their absence fallback to the global defaults set above.
80-
return Info{
81-
GitVersion: api.GitVersion,
82-
GitCommit: api.GitCommit,
83-
GitTreeState: api.GitTreeState,
84-
BuildDate: api.BuildDate,
85-
GoVersion: runtime.Version(),
86-
Compiler: runtime.Compiler,
87-
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
88-
}
89-
}
90-
91-
// String returns the string representation of the version info
92-
func (i *Info) String() string {
93-
b := strings.Builder{}
94-
w := tabwriter.NewWriter(&b, 0, 0, 2, ' ', 0)
95-
96-
fmt.Fprintf(w, "GitVersion:\t%s\n", i.GitVersion)
97-
fmt.Fprintf(w, "GitCommit:\t%s\n", i.GitCommit)
98-
fmt.Fprintf(w, "GitTreeState:\t%s\n", i.GitTreeState)
99-
fmt.Fprintf(w, "BuildDate:\t%s\n", i.BuildDate)
100-
fmt.Fprintf(w, "GoVersion:\t%s\n", i.GoVersion)
101-
fmt.Fprintf(w, "Compiler:\t%s\n", i.Compiler)
102-
fmt.Fprintf(w, "Platform:\t%s\n", i.Platform)
103-
104-
w.Flush() // #nosec
105-
return b.String()
106-
}
107-
108-
// JSONString returns the JSON representation of the version info
109-
func (i *Info) JSONString() (string, error) {
110-
b, err := json.MarshalIndent(i, "", " ")
111-
if err != nil {
112-
return "", err
113-
}
114-
115-
return string(b), nil
23+
rootCmd.AddCommand(version.Version())
11624
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,5 @@ require (
5959
google.golang.org/grpc v1.44.0
6060
google.golang.org/protobuf v1.27.1
6161
gopkg.in/ini.v1 v1.66.3
62+
sigs.k8s.io/release-utils v0.4.1-0.20220207182343-6dadf2228617
6263
)

0 commit comments

Comments
 (0)