|
1 | 1 | package version |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bufio" |
| 5 | + "context" |
| 6 | + "errors" |
4 | 7 | "fmt" |
| 8 | + "io" |
| 9 | + "maps" |
| 10 | + "net/http" |
| 11 | + "os" |
| 12 | + "path/filepath" |
| 13 | + "slices" |
| 14 | + "strings" |
| 15 | + "time" |
| 16 | + |
| 17 | + "github.com/k0sproject/version/internal/cache" |
| 18 | + "github.com/k0sproject/version/internal/github" |
5 | 19 | ) |
6 | 20 |
|
| 21 | +// CacheMaxAge is the maximum duration a cached version list is considered fresh |
| 22 | +// before forcing a refresh from GitHub. |
| 23 | +const CacheMaxAge = 60 * time.Minute |
| 24 | + |
| 25 | +// ErrCacheMiss is returned when no cached version data is available. |
| 26 | +var ErrCacheMiss = errors.New("version: cache miss") |
| 27 | + |
7 | 28 | // Collection is a type that implements the sort.Interface interface |
8 | 29 | // so that versions can be sorted. |
9 | 30 | type Collection []*Version |
@@ -31,3 +52,183 @@ func (c Collection) Less(i, j int) bool { |
31 | 52 | func (c Collection) Swap(i, j int) { |
32 | 53 | c[i], c[j] = c[j], c[i] |
33 | 54 | } |
| 55 | + |
| 56 | +// newCollectionFromCache returns the cached versions and the file's modification time. |
| 57 | +// It returns ErrCacheMiss when no usable cache exists. |
| 58 | +func newCollectionFromCache() (Collection, time.Time, error) { |
| 59 | + path, err := cache.File() |
| 60 | + if err != nil { |
| 61 | + return nil, time.Time{}, fmt.Errorf("locate cache: %w", err) |
| 62 | + } |
| 63 | + |
| 64 | + f, err := os.Open(path) |
| 65 | + if err != nil { |
| 66 | + if errors.Is(err, os.ErrNotExist) { |
| 67 | + return nil, time.Time{}, ErrCacheMiss |
| 68 | + } |
| 69 | + return nil, time.Time{}, fmt.Errorf("open cache: %w", err) |
| 70 | + } |
| 71 | + defer func() { |
| 72 | + _ = f.Close() |
| 73 | + }() |
| 74 | + |
| 75 | + info, err := f.Stat() |
| 76 | + if err != nil { |
| 77 | + return nil, time.Time{}, fmt.Errorf("stat cache: %w", err) |
| 78 | + } |
| 79 | + |
| 80 | + collection, readErr := readCollection(f) |
| 81 | + if readErr != nil { |
| 82 | + return nil, time.Time{}, fmt.Errorf("read cache: %w", readErr) |
| 83 | + } |
| 84 | + if len(collection) == 0 { |
| 85 | + return nil, info.ModTime(), ErrCacheMiss |
| 86 | + } |
| 87 | + |
| 88 | + return collection, info.ModTime(), nil |
| 89 | +} |
| 90 | + |
| 91 | +// writeCache persists the collection to the cache file, one version per line. |
| 92 | +func (c Collection) writeCache() error { |
| 93 | + path, err := cache.File() |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + |
| 98 | + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + |
| 102 | + ordered := slices.Clone(c) |
| 103 | + ordered = slices.DeleteFunc(ordered, func(v *Version) bool { |
| 104 | + return v == nil |
| 105 | + }) |
| 106 | + slices.SortFunc(ordered, func(a, b *Version) int { |
| 107 | + return a.Compare(b) |
| 108 | + }) |
| 109 | + slices.Reverse(ordered) |
| 110 | + |
| 111 | + var b strings.Builder |
| 112 | + for _, v := range ordered { |
| 113 | + b.WriteString(v.String()) |
| 114 | + b.WriteByte('\n') |
| 115 | + } |
| 116 | + |
| 117 | + return os.WriteFile(path, []byte(b.String()), 0o644) |
| 118 | +} |
| 119 | + |
| 120 | +// All returns all known k0s versions using the provided context. It refreshes |
| 121 | +// the local cache by querying GitHub for tags newer than the cache |
| 122 | +// modification time when the cache is older than CacheMaxAge. The cache is |
| 123 | +// skipped if the remote lookup fails and no cached data exists. |
| 124 | +func All(ctx context.Context) (Collection, error) { |
| 125 | + result, err := loadAll(ctx, sharedHTTPClient, false) |
| 126 | + return result.versions, err |
| 127 | +} |
| 128 | + |
| 129 | +// Refresh fetches versions from GitHub regardless of cache freshness, updating the cache on success. |
| 130 | +func Refresh() (Collection, error) { |
| 131 | + return RefreshContext(context.Background()) |
| 132 | +} |
| 133 | + |
| 134 | +// RefreshContext fetches versions from GitHub regardless of cache freshness, |
| 135 | +// updating the cache on success using the provided context. |
| 136 | +func RefreshContext(ctx context.Context) (Collection, error) { |
| 137 | + result, err := loadAll(ctx, sharedHTTPClient, true) |
| 138 | + return result.versions, err |
| 139 | +} |
| 140 | + |
| 141 | +type loadResult struct { |
| 142 | + versions Collection |
| 143 | + usedFallback bool |
| 144 | +} |
| 145 | + |
| 146 | +func loadAll(ctx context.Context, httpClient *http.Client, force bool) (loadResult, error) { |
| 147 | + cached, modTime, cacheErr := newCollectionFromCache() |
| 148 | + if cacheErr != nil && !errors.Is(cacheErr, ErrCacheMiss) { |
| 149 | + return loadResult{}, cacheErr |
| 150 | + } |
| 151 | + |
| 152 | + known := make(map[string]*Version, len(cached)) |
| 153 | + for _, v := range cached { |
| 154 | + if v == nil { |
| 155 | + continue |
| 156 | + } |
| 157 | + known[v.String()] = v |
| 158 | + } |
| 159 | + |
| 160 | + cacheStale := force || errors.Is(cacheErr, ErrCacheMiss) || modTime.IsZero() || time.Since(modTime) > CacheMaxAge |
| 161 | + if !cacheStale { |
| 162 | + return loadResult{versions: collectionFromMap(known)}, nil |
| 163 | + } |
| 164 | + |
| 165 | + client := github.NewClient(httpClient) |
| 166 | + tags, err := client.TagsSince(ctx, modTime) |
| 167 | + if err != nil { |
| 168 | + if force || len(known) == 0 { |
| 169 | + return loadResult{}, err |
| 170 | + } |
| 171 | + return loadResult{versions: collectionFromMap(known), usedFallback: true}, nil |
| 172 | + } |
| 173 | + |
| 174 | + var updated bool |
| 175 | + for _, tag := range tags { |
| 176 | + version, err := NewVersion(tag) |
| 177 | + if err != nil { |
| 178 | + continue |
| 179 | + } |
| 180 | + key := version.String() |
| 181 | + if _, exists := known[key]; exists { |
| 182 | + continue |
| 183 | + } |
| 184 | + known[key] = version |
| 185 | + updated = true |
| 186 | + } |
| 187 | + |
| 188 | + result := collectionFromMap(known) |
| 189 | + |
| 190 | + if updated || errors.Is(cacheErr, ErrCacheMiss) || force { |
| 191 | + if err := result.writeCache(); err != nil { |
| 192 | + return loadResult{}, err |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + return loadResult{versions: result}, nil |
| 197 | +} |
| 198 | + |
| 199 | +func collectionFromMap(m map[string]*Version) Collection { |
| 200 | + if len(m) == 0 { |
| 201 | + return nil |
| 202 | + } |
| 203 | + values := slices.Collect(maps.Values(m)) |
| 204 | + values = slices.DeleteFunc(values, func(v *Version) bool { |
| 205 | + return v == nil |
| 206 | + }) |
| 207 | + slices.SortFunc(values, func(a, b *Version) int { |
| 208 | + return a.Compare(b) |
| 209 | + }) |
| 210 | + return Collection(values) |
| 211 | +} |
| 212 | + |
| 213 | +func readCollection(r io.Reader) (Collection, error) { |
| 214 | + var collection Collection |
| 215 | + scanner := bufio.NewScanner(r) |
| 216 | + for scanner.Scan() { |
| 217 | + line := strings.TrimSpace(scanner.Text()) |
| 218 | + if line == "" || strings.HasPrefix(line, "#") { |
| 219 | + continue |
| 220 | + } |
| 221 | + |
| 222 | + v, err := NewVersion(line) |
| 223 | + if err != nil { |
| 224 | + continue |
| 225 | + } |
| 226 | + collection = append(collection, v) |
| 227 | + } |
| 228 | + |
| 229 | + if err := scanner.Err(); err != nil { |
| 230 | + return nil, err |
| 231 | + } |
| 232 | + |
| 233 | + return collection, nil |
| 234 | +} |
0 commit comments