k3s/vendor/k8s.io/kubernetes/pkg/kubelet/winstats/version.go

83 lines
2.1 KiB
Go
Raw Normal View History

2019-01-12 04:58:27 +00:00
// +build windows
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package winstats
import (
"fmt"
2019-12-12 01:27:03 +00:00
"golang.org/x/sys/windows/registry"
2019-01-12 04:58:27 +00:00
)
2019-12-12 01:27:03 +00:00
//OSInfo is a convenience class for retrieving Windows OS information
type OSInfo struct {
BuildNumber, ProductName string
MajorVersion, MinorVersion, UBR uint64
2019-01-12 04:58:27 +00:00
}
2019-12-12 01:27:03 +00:00
// GetOSInfo reads Windows version information from the registry
func GetOSInfo() (*OSInfo, error) {
k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
2019-01-12 04:58:27 +00:00
if err != nil {
2019-12-12 01:27:03 +00:00
return nil, err
2019-01-12 04:58:27 +00:00
}
2019-12-12 01:27:03 +00:00
defer k.Close()
2019-01-12 04:58:27 +00:00
2019-12-12 01:27:03 +00:00
buildNumber, _, err := k.GetStringValue("CurrentBuildNumber")
2019-01-12 04:58:27 +00:00
if err != nil {
2019-12-12 01:27:03 +00:00
return nil, err
2019-01-12 04:58:27 +00:00
}
2019-12-12 01:27:03 +00:00
majorVersionNumber, _, err := k.GetIntegerValue("CurrentMajorVersionNumber")
2019-01-12 04:58:27 +00:00
if err != nil {
2019-12-12 01:27:03 +00:00
return nil, err
2019-01-12 04:58:27 +00:00
}
2019-12-12 01:27:03 +00:00
minorVersionNumber, _, err := k.GetIntegerValue("CurrentMinorVersionNumber")
2019-01-12 04:58:27 +00:00
if err != nil {
2019-12-12 01:27:03 +00:00
return nil, err
2019-01-12 04:58:27 +00:00
}
2019-12-12 01:27:03 +00:00
revision, _, err := k.GetIntegerValue("UBR")
2019-01-12 04:58:27 +00:00
if err != nil {
2019-12-12 01:27:03 +00:00
return nil, err
2019-01-12 04:58:27 +00:00
}
2019-12-12 01:27:03 +00:00
productName, _, err := k.GetStringValue("ProductName")
2019-01-12 04:58:27 +00:00
if err != nil {
2019-12-12 01:27:03 +00:00
return nil, nil
2019-01-12 04:58:27 +00:00
}
2019-12-12 01:27:03 +00:00
return &OSInfo{
BuildNumber: buildNumber,
ProductName: productName,
MajorVersion: majorVersionNumber,
MinorVersion: minorVersionNumber,
UBR: revision,
}, nil
2019-01-12 04:58:27 +00:00
}
2019-12-12 01:27:03 +00:00
//GetPatchVersion returns full OS version with patch
func (o *OSInfo) GetPatchVersion() string {
return fmt.Sprintf("%d.%d.%s.%d", o.MajorVersion, o.MinorVersion, o.BuildNumber, o.UBR)
}
2019-01-12 04:58:27 +00:00
2019-12-12 01:27:03 +00:00
//GetBuild returns OS version upto build number
func (o *OSInfo) GetBuild() string {
return fmt.Sprintf("%d.%d.%s", o.MajorVersion, o.MinorVersion, o.BuildNumber)
2019-01-12 04:58:27 +00:00
}