Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
kata-env: import new struct VersionInfo
Browse files Browse the repository at this point in the history
We import new struct VersionInfo for better organizing version info of
kata components, in order to follow Semantic Versioning Specification.

Fixes: #2375

Signed-off-by: Penny Zheng <[email protected]>
  • Loading branch information
Pennyzct committed Feb 17, 2020
1 parent 0c3b2c0 commit a4b3c65
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 28 deletions.
72 changes: 44 additions & 28 deletions cli/kata-env.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,18 @@ type RuntimeInfo struct {
Path string
}

// RuntimeVersionInfo stores details of the runtime version
type RuntimeVersionInfo struct {
type VersionInfo struct {
Semver string
Major uint64
Minor uint64
Patch uint64
Commit string
OCI string
}

// RuntimeVersionInfo stores details of the runtime version
type RuntimeVersionInfo struct {
Version VersionInfo
OCI string
}

// HypervisorInfo stores hypervisor details
Expand All @@ -100,15 +107,15 @@ type HypervisorInfo struct {
// ProxyInfo stores proxy details
type ProxyInfo struct {
Type string
Version string
Version VersionInfo
Path string
Debug bool
}

// ShimInfo stores shim details
type ShimInfo struct {
Type string
Version string
Version VersionInfo
Path string
Debug bool
}
Expand Down Expand Up @@ -140,7 +147,7 @@ type HostInfo struct {

// NetmonInfo stores netmon details
type NetmonInfo struct {
Version string
Version VersionInfo
Path string
Debug bool
Enable bool
Expand Down Expand Up @@ -171,10 +178,12 @@ func getMetaInfo() MetaInfo {
}

func getRuntimeInfo(configFile string, config oci.RuntimeConfig) RuntimeInfo {
runtimeVersionInfo := constructVersionInfo(version)
runtimeVersionInfo.Commit = commit

runtimeVersion := RuntimeVersionInfo{
Semver: version,
Commit: commit,
OCI: specs.Version,
Version: runtimeVersionInfo,
OCI: specs.Version,
}

runtimeConfig := RuntimeConfigInfo{
Expand Down Expand Up @@ -250,43 +259,48 @@ func getHostInfo() (HostInfo, error) {
return host, nil
}

func getProxyInfo(config oci.RuntimeConfig) (ProxyInfo, error) {
func getProxyInfo(config oci.RuntimeConfig) ProxyInfo {
if config.ProxyType == vc.NoProxyType {
return ProxyInfo{Type: string(config.ProxyType)}, nil
return ProxyInfo{Type: string(config.ProxyType)}
}

proxyConfig := config.ProxyConfig
version, err := getCommandVersion(proxyConfig.Path)
if err != nil {
version = unknown

var proxyVersionInfo VersionInfo
if version, err := getCommandVersion(proxyConfig.Path); err != nil {
proxyVersionInfo = unknownVersionInfo
} else {
proxyVersionInfo = constructVersionInfo(version)
}

proxy := ProxyInfo{
Type: string(config.ProxyType),
Version: version,
Version: proxyVersionInfo,
Path: proxyConfig.Path,
Debug: proxyConfig.Debug,
}

return proxy, nil
return proxy
}

func getNetmonInfo(config oci.RuntimeConfig) (NetmonInfo, error) {
func getNetmonInfo(config oci.RuntimeConfig) NetmonInfo {
netmonConfig := config.NetmonConfig

version, err := getCommandVersion(netmonConfig.Path)
if err != nil {
version = unknown
var netmonVersionInfo VersionInfo
if version, err := getCommandVersion(netmonConfig.Path); err != nil {
netmonVersionInfo = unknownVersionInfo
} else {
netmonVersionInfo = constructVersionInfo(version)
}

netmon := NetmonInfo{
Version: version,
Version: netmonVersionInfo,
Path: netmonConfig.Path,
Debug: netmonConfig.Debug,
Enable: netmonConfig.Enable,
}

return netmon, nil
return netmon
}

func getCommandVersion(cmd string) (string, error) {
Expand All @@ -301,14 +315,16 @@ func getShimInfo(config oci.RuntimeConfig) (ShimInfo, error) {

shimPath := shimConfig.Path

version, err := getCommandVersion(shimPath)
if err != nil {
version = unknown
var shimVersionInfo VersionInfo
if version, err := getCommandVersion(shimConfig.Path); err != nil {
shimVersionInfo = unknownVersionInfo
} else {
shimVersionInfo = constructVersionInfo(version)
}

shim := ShimInfo{
Type: string(config.ShimType),
Version: version,
Version: shimVersionInfo,
Path: shimPath,
Debug: shimConfig.Debug,
}
Expand Down Expand Up @@ -378,9 +394,9 @@ func getEnvInfo(configFile string, config oci.RuntimeConfig) (env EnvInfo, err e
return EnvInfo{}, err
}

proxy, _ := getProxyInfo(config)
proxy := getProxyInfo(config)

netmon, _ := getNetmonInfo(config)
netmon := getNetmonInfo(config)

shim, err := getShimInfo(config)
if err != nil {
Expand Down
40 changes: 40 additions & 0 deletions cli/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strconv"
"strings"

"github.com/blang/semver"
"github.com/kata-containers/runtime/pkg/katautils"
)

Expand All @@ -26,6 +27,11 @@ var (

// Clear Linux has a different path (for stateless support)
osReleaseClr = "/usr/lib/os-release"

unknownVersionInfo = VersionInfo{
Semver: unknown,
Commit: unknown,
}
)

func getKernelVersion() (string, error) {
Expand Down Expand Up @@ -143,3 +149,37 @@ func parseBoolOrAuto(s string) (*bool, error) {
b, err := strconv.ParseBool(s)
return &b, err
}

// constructVersionInfo constructs VersionInfo-type value from a version string
// in the format of "Kata-Component version Major.Minor.Patch-rc_xxx-Commit".
func constructVersionInfo(version string) VersionInfo {
fields := strings.Split(version, " ")
realVersion := fields[len(fields)-1]

sv, err := semver.Make(realVersion)
if err != nil {
return unknownVersionInfo
}

pres := strings.Split(sv.Pre[0].VersionStr, "-")

// version contains Commit info.
if len(pres) > 1 {
return VersionInfo{
Semver: realVersion,
Major: sv.Major,
Minor: sv.Minor,
Patch: sv.Patch,
Commit: pres[1],
}
}

return VersionInfo{
Semver: realVersion,
Major: sv.Major,
Minor: sv.Minor,
Patch: sv.Patch,
Commit: unknown,
}

}

0 comments on commit a4b3c65

Please sign in to comment.