• 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/prepare.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
        "encoding/json"
21
        "fmt"
22
        "net/http"
23
        "strings"
24

25
        log "github.com/sirupsen/logrus"
26

27
        "sigs.k8s.io/kubebuilder/v4/pkg/cli/alpha/internal/common"
28
        "sigs.k8s.io/kubebuilder/v4/pkg/config/store"
29
)
30

31
type releaseResponse struct {
32
        TagName string `json:"tag_name"`
33
}
34

35
// Prepare resolves version and binary URL details after validation.
36
// Should be called after Validate().
37
func (opts *Update) Prepare() error {
×
38
        if opts.FromBranch == "" {
×
39
                // TODO: Check if is possible to use get to determine the default branch
×
40
                log.Warning("No --from-branch specified, using 'main' as default")
×
41
                opts.FromBranch = "main"
×
42
        }
×
43

44
        path, err := common.GetInputPath("")
×
45
        if err != nil {
×
46
                return fmt.Errorf("failed to determine project path: %w", err)
×
47
        }
×
48
        config, err := common.LoadProjectConfig(path)
×
49
        if err != nil {
×
50
                return fmt.Errorf("failed to load PROJECT config: %w", err)
×
51
        }
×
52
        opts.FromVersion, err = opts.defineFromVersion(config)
×
53
        if err != nil {
×
54
                return fmt.Errorf("failed to determine the version to use for the upgrade from: %w", err)
×
55
        }
×
56
        opts.ToVersion = opts.defineToVersion()
×
57
        return nil
×
58
}
59

60
// defineFromVersion will return the CLI version to be used for the update with the v prefix.
61
func (opts *Update) defineFromVersion(config store.Store) (string, error) {
×
62
        if len(opts.FromBranch) == 0 && len(config.Config().GetCliVersion()) == 0 {
×
63
                return "", fmt.Errorf("no version specified in PROJECT file. " +
×
64
                        "Please use --from-version flag to specify the version to update from")
×
65
        }
×
66

67
        if opts.FromVersion != "" {
×
68
                if !strings.HasPrefix(opts.FromVersion, "v") {
×
69
                        return "v" + opts.FromVersion, nil
×
70
                }
×
71
                return opts.FromVersion, nil
×
72
        }
73
        return "v" + config.Config().GetCliVersion(), nil
×
74
}
75

76
func (opts *Update) defineToVersion() string {
×
77
        if len(opts.ToVersion) != 0 {
×
78
                if !strings.HasPrefix(opts.ToVersion, "v") {
×
79
                        return "v" + opts.ToVersion
×
80
                }
×
81
                return opts.ToVersion
×
82
        }
83

84
        opts.ToVersion, _ = fetchLatestRelease()
×
85

×
86
        return opts.ToVersion
×
87
}
88

89
func fetchLatestRelease() (string, error) {
×
90
        resp, err := http.Get("https://api.github.com/repos/kubernetes-sigs/kubebuilder/releases/latest")
×
91
        if err != nil {
×
92
                return "", fmt.Errorf("failed to fetch latest release: %w", err)
×
93
        }
×
94

95
        defer func() {
×
96
                if err := resp.Body.Close(); err != nil {
×
97
                        log.Infof("failed to close connection: %s", err)
×
98
                }
×
99
        }()
100

101
        if resp.StatusCode != http.StatusOK {
×
102
                return "", fmt.Errorf("unexpected status code: %d", resp.StatusCode)
×
103
        }
×
104

105
        var release releaseResponse
×
106
        if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
×
107
                return "", fmt.Errorf("failed to parse response: %w", err)
×
108
        }
×
109

110
        return release.TagName, nil
×
111
}
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