• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kubernetes-sigs / kubebuilder / 13433040450

20 Feb 2025 10:18AM UTC coverage: 74.078%. Remained the same
13433040450

Pull #4570

github

camilamacedo86
Upgrade controller-runtime from v0.20.1 to v0.20.2 and K8s version from v1.32.0 to 1.32.1
Pull Request #4570: ✨ Upgrade controller-runtime from v0.20.1 to v0.20.2

2309 of 3117 relevant lines covered (74.08%)

14.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

68.75
/pkg/plugins/golang/go_version.go
1
/*
2
Copyright 2018 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 golang
18

19
import (
20
        "fmt"
21
        "os/exec"
22
        "regexp"
23
        "strconv"
24
        "strings"
25
)
26

27
const (
28
        goVerPattern = `^go(?P<major>[0-9]+)\.(?P<minor>[0-9]+)(?:\.(?P<patch>[0-9]+)|(?P<pre>(?:alpha|beta|rc)[0-9]+))?$`
29
)
30

31
var (
32
        goVerRegexp = regexp.MustCompile(goVerPattern)
33
)
34

35
// GoVersion describes a Go version.
36
type GoVersion struct {
37
        major, minor, patch int
38
        prerelease          string
39
}
40

41
func (v GoVersion) String() string {
10✔
42
        switch {
10✔
43
        case v.patch != 0:
×
44
                return fmt.Sprintf("go%d.%d.%d", v.major, v.minor, v.patch)
×
45
        case v.prerelease != "":
5✔
46
                return fmt.Sprintf("go%d.%d%s", v.major, v.minor, v.prerelease)
5✔
47
        }
48
        return fmt.Sprintf("go%d.%d", v.major, v.minor)
5✔
49
}
50

51
// MustParse will panic if verStr does not match the expected Go version string spec.
52
func MustParse(verStr string) (v GoVersion) {
4✔
53
        if err := v.parse(verStr); err != nil {
4✔
54
                panic(err)
×
55
        }
56
        return v
4✔
57
}
58

59
func (v *GoVersion) parse(verStr string) error {
93✔
60
        m := goVerRegexp.FindStringSubmatch(verStr)
93✔
61
        if m == nil {
101✔
62
                return fmt.Errorf("invalid version string")
8✔
63
        }
8✔
64

65
        var err error
85✔
66

85✔
67
        v.major, err = strconv.Atoi(m[1])
85✔
68
        if err != nil {
85✔
69
                return fmt.Errorf("error parsing major version '%s': %s", m[1], err)
×
70
        }
×
71

72
        v.minor, err = strconv.Atoi(m[2])
85✔
73
        if err != nil {
85✔
74
                return fmt.Errorf("error parsing minor version '%s': %s", m[2], err)
×
75
        }
×
76

77
        if m[3] != "" {
145✔
78
                v.patch, err = strconv.Atoi(m[3])
60✔
79
                if err != nil {
60✔
80
                        return fmt.Errorf("error parsing patch version '%s': %s", m[2], err)
×
81
                }
×
82
        }
83

84
        v.prerelease = m[4]
85✔
85

85✔
86
        return nil
85✔
87
}
88

89
// Compare returns -1, 0, or 1 if v < other, v == other, or v > other, respectively.
90
func (v GoVersion) Compare(other GoVersion) int {
191✔
91
        if v.major > other.major {
196✔
92
                return 1
5✔
93
        }
5✔
94
        if v.major < other.major {
266✔
95
                return -1
80✔
96
        }
80✔
97

98
        if v.minor > other.minor {
163✔
99
                return 1
57✔
100
        }
57✔
101
        if v.minor < other.minor {
56✔
102
                return -1
7✔
103
        }
7✔
104

105
        if v.patch > other.patch {
61✔
106
                return 1
19✔
107
        }
19✔
108
        if v.patch < other.patch {
25✔
109
                return -1
2✔
110
        }
2✔
111

112
        if v.prerelease == other.prerelease {
23✔
113
                return 0
2✔
114
        }
2✔
115
        if v.prerelease == "" {
23✔
116
                return 1
4✔
117
        }
4✔
118
        if other.prerelease == "" {
19✔
119
                return -1
4✔
120
        }
4✔
121
        if v.prerelease > other.prerelease {
16✔
122
                return 1
5✔
123
        }
5✔
124
        return -1
6✔
125
}
126

127
// ValidateGoVersion verifies that Go is installed and the current go version is supported by a plugin.
128
func ValidateGoVersion(minVersion, maxVersion GoVersion) error {
×
129
        err := fetchAndCheckGoVersion(minVersion, maxVersion)
×
130
        if err != nil {
×
131
                return fmt.Errorf("%s. You can skip this check using the --skip-go-version-check flag", err)
×
132
        }
×
133
        return nil
×
134
}
135

136
func fetchAndCheckGoVersion(minVersion, maxVersion GoVersion) error {
×
137
        cmd := exec.Command("go", "version")
×
138
        out, err := cmd.Output()
×
139
        if err != nil {
×
140
                return fmt.Errorf("failed to retrieve 'go version': %v", string(out))
×
141
        }
×
142

143
        split := strings.Split(string(out), " ")
×
144
        if len(split) < 3 {
×
145
                return fmt.Errorf("found invalid Go version: %q", string(out))
×
146
        }
×
147
        goVer := split[2]
×
148
        if err := checkGoVersion(goVer, minVersion, maxVersion); err != nil {
×
149
                return fmt.Errorf("go version '%s' is incompatible because '%s'", goVer, err)
×
150
        }
×
151
        return nil
×
152
}
153

154
func checkGoVersion(verStr string, minVersion, maxVersion GoVersion) error {
77✔
155
        var version GoVersion
77✔
156
        if err := version.parse(verStr); err != nil {
78✔
157
                return err
1✔
158
        }
1✔
159

160
        if version.Compare(minVersion) < 0 || version.Compare(maxVersion) >= 0 {
81✔
161
                return fmt.Errorf("plugin requires %s <= version < %s", minVersion, maxVersion)
5✔
162
        }
5✔
163

164
        return nil
71✔
165
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc