Skip to content

Commit

Permalink
feat: add --version flag to print the driver version
Browse files Browse the repository at this point in the history
Signed-off-by: Anish Ramasekar <anish.ramasekar@gmail.com>
  • Loading branch information
aramase committed Feb 2, 2023
1 parent c851df5 commit 21694f0
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cmd/secrets-store-csi-driver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ var (
enableProfile = flag.Bool("enable-pprof", false, "enable pprof profiling")
profilePort = flag.Int("pprof-port", 6065, "port for pprof profiling")
maxCallRecvMsgSize = flag.Int("max-call-recv-msg-size", 1024*1024*4, "maximum size in bytes of gRPC response from plugins")
versionInfo = flag.Bool("version", false, "Print the version and exit")

// Enable optional healthcheck for provider clients that exist in memory
providerHealthCheck = flag.Bool("provider-health-check", false, "Enable health check for configured providers")
Expand Down Expand Up @@ -102,6 +103,10 @@ func mainErr() error {
return err
}

if *versionInfo {
return version.PrintVersion()
}

if *enableProfile {
klog.InfoS("Starting profiling", "port", *profilePort)
go func() {
Expand Down
23 changes: 23 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package version

import (
"encoding/json"
"fmt"
"runtime"
)
Expand All @@ -33,3 +34,25 @@ var (
func GetUserAgent(controllerName string) string {
return fmt.Sprintf("csi-secrets-store/%s/%s (%s/%s) %s/%s", controllerName, BuildVersion, runtime.GOOS, runtime.GOARCH, Vcs, BuildTime)
}

// PrintVersion prints the current driver version
func PrintVersion() error {
var err error
pv := struct {
BuildVersion string
GitCommit string
BuildDate string
}{
BuildDate: BuildTime,
BuildVersion: BuildVersion,
GitCommit: Vcs,
}

var res []byte
if res, err = json.Marshal(pv); err != nil {
return err
}

fmt.Printf(string(res) + "\n")
return nil
}
36 changes: 36 additions & 0 deletions pkg/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ limitations under the License.
package version

import (
"bytes"
"fmt"
"io"
"os"
"runtime"
"strings"
"testing"
Expand All @@ -35,3 +38,36 @@ func TestGetUserAgent(t *testing.T) {
t.Fatalf("expected: %s, got: %s", expected, actual)
}
}

func TestPrintVersion(t *testing.T) {
BuildTime = "Now"
BuildVersion = "version"
Vcs = "hash"

old := os.Stdout // keep backup of the real stdout
r, w, _ := os.Pipe()
os.Stdout = w

err := PrintVersion()

outC := make(chan string)
// copy the output in a separate goroutine so printing can't block indefinitely
go func() {
var buf bytes.Buffer
_, _ = io.Copy(&buf, r)
outC <- strings.TrimSpace(buf.String())
}()

// back to normal state
w.Close()
os.Stdout = old // restoring the real stdout
out := <-outC

if err != nil {
t.Fatalf("expected no error, got %v", err)
}
expected := `{"BuildVersion":"version","GitCommit":"hash","BuildDate":"Now"}`
if !strings.EqualFold(out, expected) {
t.Fatalf("PrintVersion() expected %s, got %s", expected, out)
}
}

0 comments on commit 21694f0

Please sign in to comment.