...
1
16
17 package metrics
18
19 import (
20 "fmt"
21 "regexp"
22
23 "github.com/blang/semver/v4"
24
25 apimachineryversion "k8s.io/apimachinery/pkg/version"
26 )
27
28 const (
29 versionRegexpString = `^v(\d+\.\d+\.\d+)`
30 )
31
32 var (
33 versionRe = regexp.MustCompile(versionRegexpString)
34 )
35
36 func parseSemver(s string) *semver.Version {
37 if s != "" {
38 sv := semver.MustParse(s)
39 return &sv
40 }
41 return nil
42 }
43 func parseVersion(ver apimachineryversion.Info) semver.Version {
44 matches := versionRe.FindAllStringSubmatch(ver.String(), -1)
45
46 if len(matches) != 1 {
47 panic(fmt.Sprintf("version string \"%v\" doesn't match expected regular expression: \"%v\"", ver.String(), versionRe.String()))
48 }
49 return semver.MustParse(matches[0][1])
50 }
51
View as plain text