Skip to content

Commit

Permalink
Move getBuildInfo to utils.go
Browse files Browse the repository at this point in the history
  • Loading branch information
ciarams87 committed Jun 23, 2022
1 parent 66ac6bb commit e07ec92
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 32 deletions.
43 changes: 12 additions & 31 deletions cmd/nginx-ingress/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package main
import (
"flag"
"fmt"
"net"
"os"
"net"
"regexp"
"runtime"
"runtime/debug"
"strings"

"github.com/golang/glog"
Expand Down Expand Up @@ -177,12 +175,12 @@ var (
)

//gocyclo:ignore
func parseFlags() {
func parseFlags(versionInfo string, binaryInfo string) {
flag.Parse()

initialChecks()

binaryInfo, versionInfo := getBuildInfo()
printVersionInfo(versionInfo, binaryInfo)

glog.Infof("Starting NGINX Ingress Controller %v PlusFlag=%v", versionInfo, *nginxPlus)
glog.Info(binaryInfo)
Expand Down Expand Up @@ -267,6 +265,15 @@ func initialChecks() {
}
}

// printVersionInfo prints the the version and binary info before exiting if the flag is set
func printVersionInfo(versionInfo string, binaryInfo string) {
if *versionFlag {
fmt.Println(versionInfo)
fmt.Println(binaryInfo)
os.Exit(0)
}
}

// validationChecks checks the values for various flags
func validationChecks() {
healthStatusURIValidationError := validateLocation(*healthStatusURI)
Expand Down Expand Up @@ -391,29 +398,3 @@ func validateLocation(location string) error {
}
return nil
}

func getBuildInfo() (string, string) {
info, ok := debug.ReadBuildInfo()
if !ok {
return "", ""
}
for _, kv := range info.Settings {
switch kv.Key {
case "vcs.revision":
commitHash = kv.Value
case "vcs.time":
commitTime = kv.Value
case "vcs.modified":
dirtyBuild = kv.Value == "true"
}
}
binaryInfo := fmt.Sprintf("Commit=%v Date=%v DirtyState=%v Arch=%v/%v Go=%v", commitHash, commitTime, dirtyBuild, runtime.GOOS, runtime.GOARCH, runtime.Version())
versionInfo := fmt.Sprintf("Version=%v", version)

if *versionFlag {
fmt.Println(versionInfo)
fmt.Println(binaryInfo)
os.Exit(0)
}
return versionInfo, binaryInfo
}
3 changes: 2 additions & 1 deletion cmd/nginx-ingress/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ import (
)

func main() {
parseFlags()
binaryInfo, versionInfo := getBuildInfo()
parseFlags(binaryInfo, versionInfo)

config, kubeClient := createConfigAndKubeClient()

Expand Down
28 changes: 28 additions & 0 deletions cmd/nginx-ingress/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"fmt"
"runtime"
"runtime/debug"
)

func getBuildInfo() (string, string) {
info, ok := debug.ReadBuildInfo()
if !ok {
return "", ""
}
for _, kv := range info.Settings {
switch kv.Key {
case "vcs.revision":
commitHash = kv.Value
case "vcs.time":
commitTime = kv.Value
case "vcs.modified":
dirtyBuild = kv.Value == "true"
}
}
binaryInfo := fmt.Sprintf("Commit=%v Date=%v DirtyState=%v Arch=%v/%v Go=%v", commitHash, commitTime, dirtyBuild, runtime.GOOS, runtime.GOARCH, runtime.Version())
versionInfo := fmt.Sprintf("Version=%v", version)

return versionInfo, binaryInfo
}

0 comments on commit e07ec92

Please sign in to comment.