github.com/argoproj/argo-cd/v3@v3.2.1/common/version.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  
     7  	log "github.com/sirupsen/logrus"
     8  )
     9  
    10  // Version information set by link flags during build. We fall back to these sane
    11  // default values when we build outside the Makefile context (e.g. go run, go build, or go test).
    12  var (
    13  	version        = "99.99.99"             // value from VERSION file
    14  	buildDate      = "1970-01-01T00:00:00Z" // output from `date -u +'%Y-%m-%dT%H:%M:%SZ'`
    15  	gitCommit      = ""                     // output from `git rev-parse HEAD`
    16  	gitTag         = ""                     // output from `git describe --exact-match --tags HEAD` (if clean tree state)
    17  	gitTreeState   = ""                     // determined from `git status --porcelain`. either 'clean' or 'dirty'
    18  	kubectlVersion = ""                     // determined from go.mod file
    19  	extraBuildInfo = ""                     // extra build information for vendors to populate during build
    20  )
    21  
    22  // Version contains Argo version information
    23  type Version struct {
    24  	Version        string
    25  	BuildDate      string
    26  	GitCommit      string
    27  	GitTag         string
    28  	GitTreeState   string
    29  	GoVersion      string
    30  	Compiler       string
    31  	Platform       string
    32  	KubectlVersion string
    33  	ExtraBuildInfo string
    34  }
    35  
    36  func (v Version) String() string {
    37  	return v.Version
    38  }
    39  
    40  func (v Version) LogStartupInfo(componentName string, fields map[string]any) {
    41  	if fields == nil {
    42  		fields = map[string]any{}
    43  	}
    44  	fields["version"] = v.Version
    45  	fields["commit"] = v.GitCommit
    46  	fields["built"] = v.BuildDate
    47  	log.WithFields(log.Fields(fields)).Infof("%s is starting", componentName)
    48  }
    49  
    50  // GetVersion returns the version information
    51  func GetVersion() Version {
    52  	var versionStr string
    53  
    54  	if gitCommit != "" && gitTag != "" && gitTreeState == "clean" {
    55  		// if we have a clean tree state and the current commit is tagged,
    56  		// this is an official release.
    57  		versionStr = gitTag
    58  	} else {
    59  		// otherwise formulate a version string based on as much metadata
    60  		// information we have available.
    61  		versionStr = "v" + version
    62  		if len(gitCommit) >= 7 {
    63  			versionStr += "+" + gitCommit[0:7]
    64  			if gitTreeState != "clean" {
    65  				versionStr += ".dirty"
    66  			}
    67  		} else {
    68  			versionStr += "+unknown"
    69  		}
    70  	}
    71  
    72  	return Version{
    73  		Version:        versionStr,
    74  		BuildDate:      buildDate,
    75  		GitCommit:      gitCommit,
    76  		GitTag:         gitTag,
    77  		GitTreeState:   gitTreeState,
    78  		GoVersion:      runtime.Version(),
    79  		Compiler:       runtime.Compiler,
    80  		Platform:       fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
    81  		KubectlVersion: kubectlVersion,
    82  		ExtraBuildInfo: extraBuildInfo,
    83  	}
    84  }