github.com/cilium/cilium@v1.16.2/pkg/hubble/build/version.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package build
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/cilium/cilium/pkg/version"
    10  )
    11  
    12  var (
    13  	// ServerVersion reports version information for Hubble server.
    14  	ServerVersion Version
    15  	// RelayVersion reports version information for Hubble Relay.
    16  	RelayVersion Version
    17  )
    18  
    19  func init() {
    20  	ciliumVersion := version.GetCiliumVersion()
    21  	ServerVersion = Version{
    22  		component: "cilium",
    23  		Core:      ciliumVersion.Version,
    24  		Revision:  ciliumVersion.Revision,
    25  	}
    26  	RelayVersion = Version{
    27  		component: "hubble-relay",
    28  		Core:      ciliumVersion.Version,
    29  		Revision:  ciliumVersion.Revision,
    30  	}
    31  }
    32  
    33  // Version defines a detailed Hubble component version.
    34  type Version struct {
    35  	// component is the Hubble component (eg: hubble, hubble-relay).
    36  	component string
    37  	// Core represents the core version (eg: 1.9.0).
    38  	Core string
    39  	// Revision is the software revision, typically a Git commit SHA.
    40  	Revision string
    41  }
    42  
    43  // SemVer returns the version as a Semantic Versioning 2.0.0 compatible string
    44  // (see semver.org).
    45  func (v Version) SemVer() string {
    46  	if v.Core == "" {
    47  		return ""
    48  	}
    49  	s := v.Core
    50  	if v.Revision != "" {
    51  		s = fmt.Sprintf("%s+g%s", s, v.Revision)
    52  	}
    53  	return s
    54  }
    55  
    56  // String returns the full version string with a leading v in the version
    57  // string itself. E.g. "hubble-relay v1.9.0+g63aa1b8".
    58  func (v Version) String() string {
    59  	if v.component == "" {
    60  		return ""
    61  	}
    62  	if canonical := v.SemVer(); canonical != "" {
    63  		return fmt.Sprintf("%s v%s", v.component, canonical)
    64  	}
    65  	return v.component
    66  }