|
1 | 1 | package versions
|
2 | 2 |
|
3 |
| -import "github.com/rancher/rancherd/pkg/config" |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "path" |
| 7 | + "strings" |
| 8 | + "sync" |
4 | 9 |
|
5 |
| -func K8sVersion(config *config.Config) string { |
6 |
| - if config.KubernetesVersion == "" { |
7 |
| - return "v1.21.1+k3s1" |
| 10 | + "github.com/rancher/rancherd/pkg/config" |
| 11 | + "gopkg.in/yaml.v3" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + cachedK8sVersion = map[string]string{} |
| 16 | + cachedRancherVersion = map[string]string{} |
| 17 | + cachedLock sync.Mutex |
| 18 | + redirectClient = &http.Client{ |
| 19 | + CheckRedirect: func(req *http.Request, via []*http.Request) error { |
| 20 | + return http.ErrUseLastResponse |
| 21 | + }, |
| 22 | + } |
| 23 | +) |
| 24 | + |
| 25 | +func getVersionOrURL(urlFormat, def, version string) (_ string, isURL bool) { |
| 26 | + if version == "" { |
| 27 | + version = def |
| 28 | + } |
| 29 | + |
| 30 | + if strings.HasPrefix(version, "v") && len(strings.Split(version, ".")) > 2 { |
| 31 | + return version, false |
| 32 | + } |
| 33 | + |
| 34 | + channelURL := version |
| 35 | + if !strings.HasPrefix(channelURL, "https://") && |
| 36 | + !strings.HasPrefix(channelURL, "http://") { |
| 37 | + if strings.HasSuffix(channelURL, "-head") { |
| 38 | + return channelURL, false |
| 39 | + } |
| 40 | + channelURL = fmt.Sprintf(urlFormat, version) |
8 | 41 | }
|
9 |
| - return config.KubernetesVersion |
| 42 | + |
| 43 | + return channelURL, true |
10 | 44 | }
|
11 | 45 |
|
12 |
| -func RancherVersion(config *config.Config) string { |
13 |
| - if config.RancherVersion == "" { |
14 |
| - return "master-head" |
| 46 | +func K8sVersion(config *config.Config) (string, error) { |
| 47 | + cachedLock.Lock() |
| 48 | + defer cachedLock.Unlock() |
| 49 | + |
| 50 | + cached, ok := cachedK8sVersion[config.KubernetesVersion] |
| 51 | + if ok { |
| 52 | + return cached, nil |
| 53 | + } |
| 54 | + |
| 55 | + versionOrURL, isURL := getVersionOrURL("https://update.k3s.io/v1-release/channels/%s", "testing", config.KubernetesVersion) |
| 56 | + if !isURL { |
| 57 | + return versionOrURL, nil |
| 58 | + } |
| 59 | + |
| 60 | + resp, err := redirectClient.Get(versionOrURL) |
| 61 | + if err != nil { |
| 62 | + return "", fmt.Errorf("getting channel version from (%s): %w", versionOrURL, err) |
| 63 | + } |
| 64 | + defer resp.Body.Close() |
| 65 | + |
| 66 | + url, err := resp.Location() |
| 67 | + if err != nil { |
| 68 | + return "", fmt.Errorf("getting channel version URL from (%s): %w", versionOrURL, err) |
15 | 69 | }
|
16 |
| - return config.RancherVersion |
| 70 | + |
| 71 | + versionOrURL = path.Base(url.Path) |
| 72 | + cachedK8sVersion[config.KubernetesVersion] = versionOrURL |
| 73 | + return versionOrURL, nil |
| 74 | +} |
| 75 | + |
| 76 | +func RancherVersion(config *config.Config) (string, error) { |
| 77 | + cachedLock.Lock() |
| 78 | + defer cachedLock.Unlock() |
| 79 | + |
| 80 | + cached, ok := cachedRancherVersion[config.RancherVersion] |
| 81 | + if ok { |
| 82 | + return cached, nil |
| 83 | + } |
| 84 | + |
| 85 | + versionOrURL, isURL := getVersionOrURL("https://releases.rancher.com/server-charts/%s/index.yaml", "master-head", config.RancherVersion) |
| 86 | + if !isURL { |
| 87 | + return versionOrURL, nil |
| 88 | + } |
| 89 | + |
| 90 | + resp, err := http.Get(versionOrURL) |
| 91 | + if err != nil { |
| 92 | + return "", fmt.Errorf("getting rancher channel version from (%s): %w", versionOrURL, err) |
| 93 | + } |
| 94 | + defer resp.Body.Close() |
| 95 | + |
| 96 | + index := &chartIndex{} |
| 97 | + if err := yaml.NewDecoder(resp.Body).Decode(index); err != nil { |
| 98 | + return "", fmt.Errorf("unmarshalling rancher channel version from (%s): %w", versionOrURL, err) |
| 99 | + } |
| 100 | + |
| 101 | + versions := index.Entries["rancher"] |
| 102 | + if len(versions) == 0 { |
| 103 | + return "", fmt.Errorf("failed to find version for rancher chart at (%s)", versionOrURL) |
| 104 | + } |
| 105 | + |
| 106 | + cachedRancherVersion[config.RancherVersion] = versions[0].Version |
| 107 | + return versions[0].Version, nil |
| 108 | +} |
| 109 | + |
| 110 | +type chartIndex struct { |
| 111 | + Entries map[string][]struct { |
| 112 | + Version string `yaml:"version"` |
| 113 | + } `yaml:"entries"` |
17 | 114 | }
|
0 commit comments