...

Source file src/sigs.k8s.io/kustomize/api/provenance/provenance.go

Documentation: sigs.k8s.io/kustomize/api/provenance

     1  // Copyright 2019 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package provenance
     5  
     6  import (
     7  	"fmt"
     8  	"runtime"
     9  	"runtime/debug"
    10  	"strings"
    11  
    12  	"github.com/blang/semver/v4"
    13  )
    14  
    15  // These variables are set at build time using ldflags.
    16  //
    17  //nolint:gochecknoglobals
    18  var (
    19  	// During a release, this will be set to the release tag, e.g. "kustomize/v4.5.7"
    20  	version = developmentVersion
    21  	// build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
    22  	buildDate = "unknown"
    23  )
    24  
    25  // This default value, (devel), matches
    26  // the value debug.BuildInfo uses for an unset main module version.
    27  const developmentVersion = "(devel)"
    28  
    29  // Provenance holds information about the build of an executable.
    30  type Provenance struct {
    31  	// Version of the kustomize binary.
    32  	Version string `json:"version,omitempty" yaml:"version,omitempty"`
    33  	// GitCommit is a git commit
    34  	GitCommit string `json:"gitCommit,omitempty" yaml:"gitCommit,omitempty"`
    35  	// BuildDate is date of the build.
    36  	BuildDate string `json:"buildDate,omitempty" yaml:"buildDate,omitempty"`
    37  	// GoOs holds OS name.
    38  	GoOs string `json:"goOs,omitempty" yaml:"goOs,omitempty"`
    39  	// GoArch holds architecture name.
    40  	GoArch string `json:"goArch,omitempty" yaml:"goArch,omitempty"`
    41  	// GoVersion holds Go version.
    42  	GoVersion string `json:"goVersion,omitempty" yaml:"goVersion,omitempty"`
    43  }
    44  
    45  // GetProvenance returns an instance of Provenance.
    46  func GetProvenance() Provenance {
    47  	p := Provenance{
    48  		BuildDate: buildDate,
    49  		Version:   version,
    50  		GitCommit: "unknown",
    51  		GoOs:      runtime.GOOS,
    52  		GoArch:    runtime.GOARCH,
    53  		GoVersion: runtime.Version(),
    54  	}
    55  	info, ok := debug.ReadBuildInfo()
    56  	if !ok {
    57  		return p
    58  	}
    59  
    60  	for _, setting := range info.Settings {
    61  		// For now, the git commit is the only information of interest.
    62  		// We could consider adding other info such as the commit date in the future.
    63  		if setting.Key == "vcs.revision" {
    64  			p.GitCommit = setting.Value
    65  		}
    66  	}
    67  
    68  	for _, dep := range info.Deps {
    69  		if dep != nil && dep.Path == "sigs.k8s.io/kustomize/kustomize/v5" {
    70  			if dep.Version != "devel" {
    71  				continue
    72  			}
    73  			v, err := GetMostRecentTag(*dep)
    74  			if err != nil {
    75  				fmt.Printf("failed to get most recent tag for %s: %v\n", dep.Path, err)
    76  				continue
    77  			}
    78  			p.Version = v
    79  		}
    80  	}
    81  
    82  	return p
    83  }
    84  
    85  func GetMostRecentTag(m debug.Module) (string, error) {
    86  	for m.Replace != nil {
    87  		m = *m.Replace
    88  	}
    89  
    90  	split := strings.Split(m.Version, "-")
    91  	sv, err := semver.Parse(strings.TrimPrefix(split[0], "v"))
    92  
    93  	if err != nil {
    94  		return "", fmt.Errorf("failed to parse version %s: %w", m.Version, err)
    95  	}
    96  
    97  	if len(split) > 1 && sv.Patch > 0 {
    98  		sv.Patch -= 1
    99  	}
   100  	return fmt.Sprintf("v%s", sv.FinalizeVersion()), nil
   101  }
   102  
   103  // Short returns the shortened provenance stamp.
   104  func (v Provenance) Short() string {
   105  	return fmt.Sprintf(
   106  		"%v",
   107  		Provenance{
   108  			Version:   v.Version,
   109  			BuildDate: v.BuildDate,
   110  		})
   111  }
   112  
   113  // Semver returns the semantic version of kustomize.
   114  // kustomize version is set in format "kustomize/vX.X.X" in every release.
   115  // X.X.X is a semver. If the version string is not in this format,
   116  // return the original version string
   117  func (v Provenance) Semver() string {
   118  	return strings.TrimPrefix(v.Version, "kustomize/")
   119  }
   120  

View as plain text