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

kubernetes-sigs / kubebuilder / 14286290433

31 Mar 2025 10:22PM UTC coverage: 71.895%. Remained the same
14286290433

Pull #4612

github

vitorfloriano
🌱 Add kubernetesVendorVersion for binary builds with LD_FLAGS

This commit adds a kubernetesVendorVersion flag to Makefile.

Rely on ldflags to set kubernetesVendorVersion, similarly to the other variables in cmd/version.go.

Use a single variable to define K8S_VERSION for both ldflags and the goreleaser configuration.
Pull Request #4612: 🌱 Add kubernetesVendorVersion for binary builds with LD_FLAGS

2310 of 3213 relevant lines covered (71.9%)

16.61 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
}
10✔
40

10✔
41
func (v GoVersion) String() string {
×
42
        switch {
×
43
        case v.patch != 0:
5✔
44
                return fmt.Sprintf("go%d.%d.%d", v.major, v.minor, v.patch)
5✔
45
        case v.prerelease != "":
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)
49
}
50

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

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

240✔
65
        var err error
240✔
66

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

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

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

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

86
        return nil
87
}
88

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

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

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

2✔
112
        if v.prerelease == other.prerelease {
2✔
113
                return 0
23✔
114
        }
4✔
115
        if v.prerelease == "" {
4✔
116
                return 1
19✔
117
        }
4✔
118
        if other.prerelease == "" {
4✔
119
                return -1
16✔
120
        }
5✔
121
        if v.prerelease > other.prerelease {
5✔
122
                return 1
6✔
123
        }
124
        return -1
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
}
78✔
153

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

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

164
        return nil
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