...

Source file src/k8s.io/kubernetes/pkg/kubelet/winstats/version.go

Documentation: k8s.io/kubernetes/pkg/kubelet/winstats

     1  //go:build windows
     2  // +build windows
     3  
     4  /*
     5  Copyright 2017 The Kubernetes Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11      http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package winstats
    21  
    22  import (
    23  	"fmt"
    24  	"golang.org/x/sys/windows/registry"
    25  )
    26  
    27  // OSInfo is a convenience class for retrieving Windows OS information
    28  type OSInfo struct {
    29  	BuildNumber, ProductName        string
    30  	MajorVersion, MinorVersion, UBR uint64
    31  }
    32  
    33  // GetOSInfo reads Windows version information from the registry
    34  func GetOSInfo() (*OSInfo, error) {
    35  	// for log detail
    36  	var keyPrefix string = `regedit:LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion`
    37  
    38  	k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
    39  	if err != nil {
    40  		return nil, fmt.Errorf("getOSInfo, open Windows %s failed: %w", keyPrefix, err)
    41  	}
    42  	defer k.Close()
    43  
    44  	buildNumber, _, err := k.GetStringValue("CurrentBuildNumber")
    45  	if err != nil {
    46  		return nil, fmt.Errorf("getOSInfo, get %s\\CurrentBuildNumber failed: %w", keyPrefix, err)
    47  	}
    48  
    49  	majorVersionNumber, _, err := k.GetIntegerValue("CurrentMajorVersionNumber")
    50  	if err != nil {
    51  		return nil, fmt.Errorf("getOSInfo, get %s\\CurrentMajorVersionNumber failed: %w", keyPrefix, err)
    52  	}
    53  
    54  	minorVersionNumber, _, err := k.GetIntegerValue("CurrentMinorVersionNumber")
    55  	if err != nil {
    56  		return nil, fmt.Errorf("getOSInfo, get %s\\CurrentMinorVersionNumber failed: %w", keyPrefix, err)
    57  	}
    58  
    59  	revision, _, err := k.GetIntegerValue("UBR")
    60  	if err != nil {
    61  		return nil, fmt.Errorf("getOSInfo, get %s\\UBR failed: %w", keyPrefix, err)
    62  	}
    63  
    64  	productName, _, err := k.GetStringValue("ProductName")
    65  	if err != nil {
    66  		return nil, fmt.Errorf("getOSInfo, get %s\\ProductName failed: %w", keyPrefix, err)
    67  	}
    68  
    69  	return &OSInfo{
    70  		BuildNumber:  buildNumber,
    71  		ProductName:  productName,
    72  		MajorVersion: majorVersionNumber,
    73  		MinorVersion: minorVersionNumber,
    74  		UBR:          revision,
    75  	}, nil
    76  }
    77  
    78  // GetPatchVersion returns full OS version with patch
    79  func (o *OSInfo) GetPatchVersion() string {
    80  	return fmt.Sprintf("%d.%d.%s.%d", o.MajorVersion, o.MinorVersion, o.BuildNumber, o.UBR)
    81  }
    82  
    83  // GetBuild returns OS version upto build number
    84  func (o *OSInfo) GetBuild() string {
    85  	return fmt.Sprintf("%d.%d.%s", o.MajorVersion, o.MinorVersion, o.BuildNumber)
    86  }
    87  

View as plain text