kata-env: import new struct VersionInfo

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 <penny.zheng@arm.com>
This commit is contained in:
Penny Zheng 2020-01-08 13:18:00 +08:00
parent 0c3b2c0972
commit a4b3c65c16
2 changed files with 84 additions and 28 deletions

View File

@ -74,11 +74,18 @@ type RuntimeInfo struct {
Path string Path string
} }
type VersionInfo struct {
Semver string
Major uint64
Minor uint64
Patch uint64
Commit string
}
// RuntimeVersionInfo stores details of the runtime version // RuntimeVersionInfo stores details of the runtime version
type RuntimeVersionInfo struct { type RuntimeVersionInfo struct {
Semver string Version VersionInfo
Commit string OCI string
OCI string
} }
// HypervisorInfo stores hypervisor details // HypervisorInfo stores hypervisor details
@ -100,7 +107,7 @@ type HypervisorInfo struct {
// ProxyInfo stores proxy details // ProxyInfo stores proxy details
type ProxyInfo struct { type ProxyInfo struct {
Type string Type string
Version string Version VersionInfo
Path string Path string
Debug bool Debug bool
} }
@ -108,7 +115,7 @@ type ProxyInfo struct {
// ShimInfo stores shim details // ShimInfo stores shim details
type ShimInfo struct { type ShimInfo struct {
Type string Type string
Version string Version VersionInfo
Path string Path string
Debug bool Debug bool
} }
@ -140,7 +147,7 @@ type HostInfo struct {
// NetmonInfo stores netmon details // NetmonInfo stores netmon details
type NetmonInfo struct { type NetmonInfo struct {
Version string Version VersionInfo
Path string Path string
Debug bool Debug bool
Enable bool Enable bool
@ -171,10 +178,12 @@ func getMetaInfo() MetaInfo {
} }
func getRuntimeInfo(configFile string, config oci.RuntimeConfig) RuntimeInfo { func getRuntimeInfo(configFile string, config oci.RuntimeConfig) RuntimeInfo {
runtimeVersionInfo := constructVersionInfo(version)
runtimeVersionInfo.Commit = commit
runtimeVersion := RuntimeVersionInfo{ runtimeVersion := RuntimeVersionInfo{
Semver: version, Version: runtimeVersionInfo,
Commit: commit, OCI: specs.Version,
OCI: specs.Version,
} }
runtimeConfig := RuntimeConfigInfo{ runtimeConfig := RuntimeConfigInfo{
@ -250,43 +259,48 @@ func getHostInfo() (HostInfo, error) {
return host, nil return host, nil
} }
func getProxyInfo(config oci.RuntimeConfig) (ProxyInfo, error) { func getProxyInfo(config oci.RuntimeConfig) ProxyInfo {
if config.ProxyType == vc.NoProxyType { if config.ProxyType == vc.NoProxyType {
return ProxyInfo{Type: string(config.ProxyType)}, nil return ProxyInfo{Type: string(config.ProxyType)}
} }
proxyConfig := config.ProxyConfig proxyConfig := config.ProxyConfig
version, err := getCommandVersion(proxyConfig.Path)
if err != nil { var proxyVersionInfo VersionInfo
version = unknown if version, err := getCommandVersion(proxyConfig.Path); err != nil {
proxyVersionInfo = unknownVersionInfo
} else {
proxyVersionInfo = constructVersionInfo(version)
} }
proxy := ProxyInfo{ proxy := ProxyInfo{
Type: string(config.ProxyType), Type: string(config.ProxyType),
Version: version, Version: proxyVersionInfo,
Path: proxyConfig.Path, Path: proxyConfig.Path,
Debug: proxyConfig.Debug, Debug: proxyConfig.Debug,
} }
return proxy, nil return proxy
} }
func getNetmonInfo(config oci.RuntimeConfig) (NetmonInfo, error) { func getNetmonInfo(config oci.RuntimeConfig) NetmonInfo {
netmonConfig := config.NetmonConfig netmonConfig := config.NetmonConfig
version, err := getCommandVersion(netmonConfig.Path) var netmonVersionInfo VersionInfo
if err != nil { if version, err := getCommandVersion(netmonConfig.Path); err != nil {
version = unknown netmonVersionInfo = unknownVersionInfo
} else {
netmonVersionInfo = constructVersionInfo(version)
} }
netmon := NetmonInfo{ netmon := NetmonInfo{
Version: version, Version: netmonVersionInfo,
Path: netmonConfig.Path, Path: netmonConfig.Path,
Debug: netmonConfig.Debug, Debug: netmonConfig.Debug,
Enable: netmonConfig.Enable, Enable: netmonConfig.Enable,
} }
return netmon, nil return netmon
} }
func getCommandVersion(cmd string) (string, error) { func getCommandVersion(cmd string) (string, error) {
@ -301,14 +315,16 @@ func getShimInfo(config oci.RuntimeConfig) (ShimInfo, error) {
shimPath := shimConfig.Path shimPath := shimConfig.Path
version, err := getCommandVersion(shimPath) var shimVersionInfo VersionInfo
if err != nil { if version, err := getCommandVersion(shimConfig.Path); err != nil {
version = unknown shimVersionInfo = unknownVersionInfo
} else {
shimVersionInfo = constructVersionInfo(version)
} }
shim := ShimInfo{ shim := ShimInfo{
Type: string(config.ShimType), Type: string(config.ShimType),
Version: version, Version: shimVersionInfo,
Path: shimPath, Path: shimPath,
Debug: shimConfig.Debug, Debug: shimConfig.Debug,
} }
@ -378,9 +394,9 @@ func getEnvInfo(configFile string, config oci.RuntimeConfig) (env EnvInfo, err e
return EnvInfo{}, err return EnvInfo{}, err
} }
proxy, _ := getProxyInfo(config) proxy := getProxyInfo(config)
netmon, _ := getNetmonInfo(config) netmon := getNetmonInfo(config)
shim, err := getShimInfo(config) shim, err := getShimInfo(config)
if err != nil { if err != nil {

View File

@ -12,6 +12,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/blang/semver"
"github.com/kata-containers/runtime/pkg/katautils" "github.com/kata-containers/runtime/pkg/katautils"
) )
@ -26,6 +27,11 @@ var (
// Clear Linux has a different path (for stateless support) // Clear Linux has a different path (for stateless support)
osReleaseClr = "/usr/lib/os-release" osReleaseClr = "/usr/lib/os-release"
unknownVersionInfo = VersionInfo{
Semver: unknown,
Commit: unknown,
}
) )
func getKernelVersion() (string, error) { func getKernelVersion() (string, error) {
@ -143,3 +149,37 @@ func parseBoolOrAuto(s string) (*bool, error) {
b, err := strconv.ParseBool(s) b, err := strconv.ParseBool(s)
return &b, err 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,
}
}