...
1
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
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 }
46
View as plain text