Skip to content

Commit

Permalink
use upstream k8s version lib (#657)
Browse files Browse the repository at this point in the history
Signed-off-by: Scott Nichols <n3wscott@chainguard.dev>
  • Loading branch information
n3wscott authored Feb 10, 2022
1 parent 7b70197 commit b1b97fb
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 247 deletions.
10 changes: 6 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

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

all: rekor-cli rekor-server
all: rekor-cli rekor-server

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

FLAG_PKG=github.com/sigstore/rekor/pkg/api
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)
REKOR_LDFLAGS=-X sigs.k8s.io/release-utils/version.gitVersion=$(GIT_VERSION) \
-X sigs.k8s.io/release-utils/version.gitCommit=$(GIT_HASH) \
-X sigs.k8s.io/release-utils/version.gitTreeState=$(GIT_TREESTATE) \
-X sigs.k8s.io/release-utils/version.buildDate=$(BUILD_DATE)

CLI_LDFLAGS=$(REKOR_LDFLAGS)
SERVER_LDFLAGS=$(REKOR_LDFLAGS)
Expand All @@ -66,7 +68,7 @@ validate-openapi: $(SWAGGER)

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


lint:
$(GOBIN)/golangci-lint run -v ./...
Expand Down
15 changes: 0 additions & 15 deletions cmd/rekor-cli/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ package app

import (
"fmt"
"runtime/debug"
"strings"

homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"

"github.com/sigstore/rekor/pkg/api"
"github.com/sigstore/rekor/pkg/log"

// these imports are to call the packages' init methods
Expand Down Expand Up @@ -71,19 +69,6 @@ func init() {
if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil {
log.CliLogger.Fatal(err)
}

// look for the default version and replace it, if found, from runtime build info
if api.GitVersion != "devel" {
return
}

bi, ok := debug.ReadBuildInfo()
if !ok {
return
}
// Version is set in artifacts built with -X github.com/sigstore/rekor/cmd/rekor-cli/app.GitVersion=1.2.3
// Ensure version is also set when installed via go install github.com/sigstore/rekor/cmd/rekor-cli
api.GitVersion = bi.Main.Version
}

func initConfig(cmd *cobra.Command) error {
Expand Down
96 changes: 2 additions & 94 deletions cmd/rekor-cli/app/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,101 +16,9 @@
package app

import (
"encoding/json"
"fmt"
"runtime"
"strings"
"text/tabwriter"

"github.com/pkg/errors"
"github.com/sigstore/rekor/pkg/api"
"github.com/spf13/cobra"
"sigs.k8s.io/release-utils/version"
)

type versionOptions struct {
json bool
}

var versionOpts = &versionOptions{}

// verifyCmd represents the verify command
var versionCmd = &cobra.Command{
Use: "version",
Short: "rekor-cli version",
RunE: func(cmd *cobra.Command, args []string) error {
return runVersion(versionOpts)
},
}

func init() {
versionCmd.PersistentFlags().BoolVarP(&versionOpts.json, "json", "j", false,
"print JSON instead of text")
rootCmd.AddCommand(versionCmd)
}

func runVersion(opts *versionOptions) error {
v := VersionInfo()
res := v.String()

if opts.json {
j, err := v.JSONString()
if err != nil {
return errors.Wrap(err, "unable to generate JSON from version info")
}
res = j
}

fmt.Println(res)
return nil
}

type Info struct {
GitVersion string
GitCommit string
GitTreeState string
BuildDate string
GoVersion string
Compiler string
Platform string
}

func VersionInfo() Info {
// These variables typically come from -ldflags settings and in
// their absence fallback to the global defaults set above.
return Info{
GitVersion: api.GitVersion,
GitCommit: api.GitCommit,
GitTreeState: api.GitTreeState,
BuildDate: api.BuildDate,
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}
}

// String returns the string representation of the version info
func (i *Info) String() string {
b := strings.Builder{}
w := tabwriter.NewWriter(&b, 0, 0, 2, ' ', 0)

fmt.Fprintf(w, "GitVersion:\t%s\n", i.GitVersion)
fmt.Fprintf(w, "GitCommit:\t%s\n", i.GitCommit)
fmt.Fprintf(w, "GitTreeState:\t%s\n", i.GitTreeState)
fmt.Fprintf(w, "BuildDate:\t%s\n", i.BuildDate)
fmt.Fprintf(w, "GoVersion:\t%s\n", i.GoVersion)
fmt.Fprintf(w, "Compiler:\t%s\n", i.Compiler)
fmt.Fprintf(w, "Platform:\t%s\n", i.Platform)

w.Flush() // #nosec
return b.String()
}

// JSONString returns the JSON representation of the version info
func (i *Info) JSONString() (string, error) {
b, err := json.MarshalIndent(i, "", " ")
if err != nil {
return "", err
}

return string(b), nil
rootCmd.AddCommand(version.Version())
}
17 changes: 1 addition & 16 deletions cmd/rekor-server/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@ import (
"net/http"
"net/http/pprof"
"os"
"runtime/debug"

homedir "github.com/mitchellh/go-homedir"
"github.com/sigstore/rekor/pkg/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/sigstore/rekor/pkg/api"
"github.com/sigstore/rekor/pkg/log"
)

var (
Expand Down Expand Up @@ -91,10 +88,6 @@ func init() {

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

// look for the default version and replace it, if found, from runtime build info
if api.GitVersion != "devel" {
return
}
log.Logger.Debugf("pprof enabled %v", enablePprof)
// Enable pprof
if enablePprof {
Expand All @@ -110,14 +103,6 @@ func init() {
}
}()
}

bi, ok := debug.ReadBuildInfo()
if !ok {
return
}
// Version is set in artifacts built with -X github.com/sigstore/rekor/cmd/rekor-server/app.GitVersion=1.2.3
// Ensure version is also set when installed via go install github.com/sigstore/rekor/cmd/rekor-server
api.GitVersion = bi.Main.Version
}

// initConfig reads in config file and ENV variables if set.
Expand Down
3 changes: 2 additions & 1 deletion cmd/rekor-server/app/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"sigs.k8s.io/release-utils/version"

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

vi := VersionInfo()
vi := version.GetVersionInfo()
viStr, err := vi.JSONString()
if err != nil {
viStr = vi.String()
Expand Down
96 changes: 2 additions & 94 deletions cmd/rekor-server/app/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,101 +16,9 @@
package app

import (
"encoding/json"
"fmt"
"runtime"
"strings"
"text/tabwriter"

"github.com/pkg/errors"
"github.com/sigstore/rekor/pkg/api"
"github.com/spf13/cobra"
"sigs.k8s.io/release-utils/version"
)

type versionOptions struct {
json bool
}

var versionOpts = &versionOptions{}

// verifyCmd represents the verify command
var versionCmd = &cobra.Command{
Use: "version",
Short: "rekor-server version",
RunE: func(cmd *cobra.Command, args []string) error {
return runVersion(versionOpts)
},
}

func init() {
versionCmd.PersistentFlags().BoolVarP(&versionOpts.json, "json", "j", false,
"print JSON instead of text")
rootCmd.AddCommand(versionCmd)
}

func runVersion(opts *versionOptions) error {
v := VersionInfo()
res := v.String()

if opts.json {
j, err := v.JSONString()
if err != nil {
return errors.Wrap(err, "unable to generate JSON from version info")
}
res = j
}

fmt.Println(res)
return nil
}

type Info struct {
GitVersion string
GitCommit string
GitTreeState string
BuildDate string
GoVersion string
Compiler string
Platform string
}

func VersionInfo() Info {
// These variables typically come from -ldflags settings and in
// their absence fallback to the global defaults set above.
return Info{
GitVersion: api.GitVersion,
GitCommit: api.GitCommit,
GitTreeState: api.GitTreeState,
BuildDate: api.BuildDate,
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}
}

// String returns the string representation of the version info
func (i *Info) String() string {
b := strings.Builder{}
w := tabwriter.NewWriter(&b, 0, 0, 2, ' ', 0)

fmt.Fprintf(w, "GitVersion:\t%s\n", i.GitVersion)
fmt.Fprintf(w, "GitCommit:\t%s\n", i.GitCommit)
fmt.Fprintf(w, "GitTreeState:\t%s\n", i.GitTreeState)
fmt.Fprintf(w, "BuildDate:\t%s\n", i.BuildDate)
fmt.Fprintf(w, "GoVersion:\t%s\n", i.GoVersion)
fmt.Fprintf(w, "Compiler:\t%s\n", i.Compiler)
fmt.Fprintf(w, "Platform:\t%s\n", i.Platform)

w.Flush() // #nosec
return b.String()
}

// JSONString returns the JSON representation of the version info
func (i *Info) JSONString() (string, error) {
b, err := json.MarshalIndent(i, "", " ")
if err != nil {
return "", err
}

return string(b), nil
rootCmd.AddCommand(version.Version())
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ require (
google.golang.org/grpc v1.44.0
google.golang.org/protobuf v1.27.1
gopkg.in/ini.v1 v1.66.3
sigs.k8s.io/release-utils v0.4.1-0.20220207182343-6dadf2228617
)
Loading

0 comments on commit b1b97fb

Please sign in to comment.