k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/cmd/kubeadm/app/preflight/utils.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package preflight 18 19 import ( 20 "regexp" 21 "strings" 22 23 "github.com/pkg/errors" 24 25 "k8s.io/apimachinery/pkg/util/version" 26 utilsexec "k8s.io/utils/exec" 27 ) 28 29 // GetKubeletVersion is helper function that returns version of kubelet available in $PATH 30 func GetKubeletVersion(execer utilsexec.Interface) (*version.Version, error) { 31 kubeletVersionRegex := regexp.MustCompile(`^\s*Kubernetes v((0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)([-\w.+]*)?)\s*$`) 32 33 command := execer.Command("kubelet", "--version") 34 out, err := command.Output() 35 if err != nil { 36 return nil, errors.Wrap(err, "cannot execute 'kubelet --version'") 37 } 38 39 cleanOutput := strings.TrimSpace(string(out)) 40 subs := kubeletVersionRegex.FindAllStringSubmatch(cleanOutput, -1) 41 if len(subs) != 1 || len(subs[0]) < 2 { 42 return nil, errors.Errorf("Unable to parse output from Kubelet: %q", cleanOutput) 43 } 44 return version.ParseSemantic(subs[0][1]) 45 }