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

kubernetes-sigs / kubebuilder / 16522004449

25 Jul 2025 12:30PM UTC coverage: 64.151%. Remained the same
16522004449

Pull #4898

github

web-flow
Merge branch 'master' into version/refactor-add-tests
Pull Request #4898: ✨ Improve version command output: add runtime fallbacks and unit tests.

2627 of 4095 relevant lines covered (64.15%)

13.59 hits per line

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

0.0
/pkg/cli/alpha/internal/update/validate.go
1
/*
2
Copyright 2025 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 update
18

19
import (
20
        "fmt"
21
        "net/http"
22
        "os/exec"
23
        "strings"
24

25
        log "github.com/sirupsen/logrus"
26
        "golang.org/x/mod/semver"
27
)
28

29
// Validate checks the input info provided for the update and populates the cliVersion
30
func (opts *Update) Validate() error {
×
31
        if err := opts.validateGitRepo(); err != nil {
×
32
                return fmt.Errorf("failed to validate git repository: %w", err)
×
33
        }
×
34
        if err := opts.validateFromBranch(); err != nil {
×
35
                return fmt.Errorf("failed to validate --from-branch: %w", err)
×
36
        }
×
37
        if err := opts.validateSemanticVersions(); err != nil {
×
38
                return fmt.Errorf("failed to validate the versions: %w", err)
×
39
        }
×
40
        if err := validateReleaseAvailability(opts.FromVersion); err != nil {
×
41
                return fmt.Errorf("unable to find release %s: %w", opts.FromVersion, err)
×
42
        }
×
43
        if err := validateReleaseAvailability(opts.ToVersion); err != nil {
×
44
                return fmt.Errorf("unable to find release %s: %w", opts.ToVersion, err)
×
45
        }
×
46
        return nil
×
47
}
48

49
// validateGitRepo verifies if the current directory is a valid Git repository and checks for uncommitted changes.
50
func (opts *Update) validateGitRepo() error {
×
51
        log.Info("Checking if is a git repository")
×
52
        gitCmd := exec.Command("git", "rev-parse", "--git-dir")
×
53
        if err := gitCmd.Run(); err != nil {
×
54
                return fmt.Errorf("not in a git repository")
×
55
        }
×
56

57
        log.Info("Checking if branch has uncommitted changes")
×
58
        gitCmd = exec.Command("git", "status", "--porcelain")
×
59
        output, err := gitCmd.Output()
×
60
        if err != nil {
×
61
                return fmt.Errorf("failed to check branch status: %w", err)
×
62
        }
×
63
        if len(strings.TrimSpace(string(output))) > 0 {
×
64
                return fmt.Errorf("working directory has uncommitted changes. " +
×
65
                        "Please commit or stash them before updating")
×
66
        }
×
67
        return nil
×
68
}
69

70
// validateFromBranch the branch passed to the --from-branch flag
71
func (opts *Update) validateFromBranch() error {
×
72
        // Check if the branch exists
×
73
        gitCmd := exec.Command("git", "rev-parse", "--verify", opts.FromBranch)
×
74
        if err := gitCmd.Run(); err != nil {
×
75
                return fmt.Errorf("%s branch does not exist locally. "+
×
76
                        "Run 'git branch -a' to see all available branches",
×
77
                        opts.FromBranch)
×
78
        }
×
79
        return nil
×
80
}
81

82
// validateSemanticVersions the version informed by the user via --from-version flag
83
func (opts *Update) validateSemanticVersions() error {
×
84
        if !semver.IsValid(opts.FromVersion) {
×
85
                return fmt.Errorf(" version informed (%s) has invalid semantic version. "+
×
86
                        "Expect: vX.Y.Z (Ex: v4.5.0)", opts.FromVersion)
×
87
        }
×
88
        if !semver.IsValid(opts.ToVersion) {
×
89
                return fmt.Errorf(" version informed (%s) has invalid semantic version. "+
×
90
                        "Expect: vX.Y.Z (Ex: v4.5.0)", opts.ToVersion)
×
91
        }
×
92
        return nil
×
93
}
94

95
// validateReleaseAvailability will verify if the binary to scaffold from-version flag is available
96
func validateReleaseAvailability(version string) error {
×
97
        url := buildReleaseURL(version)
×
98
        resp, err := http.Head(url)
×
99
        if err != nil {
×
100
                return fmt.Errorf("failed to check binary availability: %w", err)
×
101
        }
×
102
        defer func() {
×
103
                if err = resp.Body.Close(); err != nil {
×
104
                        log.Errorf("failed to close connection: %s", err)
×
105
                }
×
106
        }()
107

108
        switch resp.StatusCode {
×
109
        case http.StatusOK:
×
110
                log.Infof("Binary version %v is available", version)
×
111
                return nil
×
112
        case http.StatusNotFound:
×
113
                return fmt.Errorf("binary version %s not found. Check versions available in releases",
×
114
                        version)
×
115
        default:
×
116
                return fmt.Errorf("unexpected response %d when checking binary availability for version %s",
×
117
                        resp.StatusCode, version)
×
118
        }
119
}
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