From a4b3c65c16f63f1690966f68426a703cd3af7ab5 Mon Sep 17 00:00:00 2001 From: Penny Zheng Date: Wed, 8 Jan 2020 13:18:00 +0800 Subject: [PATCH] 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 --- cli/kata-env.go | 72 ++++++++++++++++++++++++++++++------------------- cli/utils.go | 40 +++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 28 deletions(-) diff --git a/cli/kata-env.go b/cli/kata-env.go index c973961aae..1c7fb0748d 100644 --- a/cli/kata-env.go +++ b/cli/kata-env.go @@ -74,11 +74,18 @@ type RuntimeInfo struct { Path string } +type VersionInfo struct { + Semver string + Major uint64 + Minor uint64 + Patch uint64 + Commit string +} + // RuntimeVersionInfo stores details of the runtime version type RuntimeVersionInfo struct { - Semver string - Commit string - OCI string + Version VersionInfo + OCI string } // HypervisorInfo stores hypervisor details @@ -100,7 +107,7 @@ type HypervisorInfo struct { // ProxyInfo stores proxy details type ProxyInfo struct { Type string - Version string + Version VersionInfo Path string Debug bool } @@ -108,7 +115,7 @@ type ProxyInfo struct { // ShimInfo stores shim details type ShimInfo struct { Type string - Version string + Version VersionInfo Path string Debug bool } @@ -140,7 +147,7 @@ type HostInfo struct { // NetmonInfo stores netmon details type NetmonInfo struct { - Version string + Version VersionInfo Path string Debug bool Enable bool @@ -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{ @@ -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) { @@ -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, } @@ -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 { diff --git a/cli/utils.go b/cli/utils.go index e4298162aa..3d17df37c1 100644 --- a/cli/utils.go +++ b/cli/utils.go @@ -12,6 +12,7 @@ import ( "strconv" "strings" + "github.com/blang/semver" "github.com/kata-containers/runtime/pkg/katautils" ) @@ -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) { @@ -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, + } + +}