Skip to content

Commit ea42abb

Browse files
authored
Merge pull request #179 from rancherlabs/mirrored
actions: Use `latest` as fallback version
2 parents 84bdc12 + 9818bdc commit ea42abb

File tree

8 files changed

+180
-71
lines changed

8 files changed

+180
-71
lines changed

actions/install-slsactl/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ runs:
4444
set -x
4545
if [[ "${VERSION}" == "latest" ]]; then
4646
echo 'Checking what the latest version is'
47-
VERSION=$(curl -s 'https://api.github.com/repos/rancherlabs/slsactl/releases/latest' | jq -r '.tag_name')
47+
VERSION=$(curl -s 'https://api.github.com/repos/rancherlabs/slsactl/releases/latest' | jq -r '.tag_name' || echo 'latest')
4848
4949
echo "Using last tag ${VERSION} as version"
5050
fi

internal/imagelist/imagelist.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func NewProcessor(registry string) *Processor {
3838
}
3939
}
4040

41-
func (p *Processor) Process(url string) (*Result, error) {
41+
func (p *Processor) Verify(url string) (*Result, error) {
4242
url = strings.TrimSpace(url)
4343
if len(url) == 0 {
4444
return nil, ErrURLCannotBeEmpty
@@ -88,7 +88,7 @@ func (p *Processor) Process(url string) (*Result, error) {
8888

8989
s.UpdateStatus(image)
9090

91-
entry := p.ip.Process(image)
91+
entry := p.ip.Verify(image)
9292

9393
result.Entries = append(result.Entries, entry)
9494
}

internal/imagelist/imagelist_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ func TestProcess(t *testing.T) {
3030
[]byte("image:v1\nimage2:v2\n"),
3131
)), nil)
3232

33-
m.On("Process", "some.registry/image:v1").
33+
m.On("Verify", "some.registry/image:v1").
3434
Return(Entry{Image: "some.registry/image:v1"})
35-
m.On("Process", "some.registry/image2:v2").
35+
m.On("Verify", "some.registry/image2:v2").
3636
Return(Entry{Image: "some.registry/image2:v2"})
3737
},
3838
want: &Result{
@@ -51,7 +51,7 @@ func TestProcess(t *testing.T) {
5151
[]byte("\n\nimage:v1\n"),
5252
)), nil)
5353

54-
m.On("Process", "some.registry/image:v1").
54+
m.On("Verify", "some.registry/image:v1").
5555
Return(Entry{Image: "some.registry/image:v1"})
5656
},
5757
want: &Result{
@@ -69,7 +69,7 @@ func TestProcess(t *testing.T) {
6969
[]byte("\n# some:image\nimage:v1\n"),
7070
)), nil)
7171

72-
m.On("Process", "some.registry/image:v1").
72+
m.On("Verify", "some.registry/image:v1").
7373
Return(Entry{Image: "some.registry/image:v1"})
7474
},
7575
want: &Result{
@@ -98,12 +98,12 @@ func TestProcess(t *testing.T) {
9898
[]byte("image:v1\nimage2:v2\n"),
9999
)), nil)
100100

101-
m.On("Process", "some.registry/image:v1").
101+
m.On("Verify", "some.registry/image:v1").
102102
Return(Entry{
103103
Image: "some.registry/image:v1",
104104
Error: errors.New("image not found"),
105105
})
106-
m.On("Process", "some.registry/image2:v2").
106+
m.On("Verify", "some.registry/image2:v2").
107107
Return(Entry{Image: "some.registry/image2:v2"})
108108
},
109109
want: &Result{
@@ -138,7 +138,7 @@ func TestProcess(t *testing.T) {
138138
[]byte("docker.io/image:v1\n"),
139139
)), nil)
140140

141-
m.On("Process", "some.registry/image:v1").
141+
m.On("Verify", "some.registry/image:v1").
142142
Return(Entry{Image: "some.registry/image:v1"})
143143
},
144144
want: &Result{
@@ -154,7 +154,7 @@ func TestProcess(t *testing.T) {
154154
[]byte("registry.com/image:v1\n"),
155155
)), nil)
156156

157-
m.On("Process", "registry.com/image:v1").
157+
m.On("Verify", "registry.com/image:v1").
158158
Return(Entry{Image: "registry.com/image:v1"})
159159
},
160160
want: &Result{
@@ -173,7 +173,7 @@ func TestProcess(t *testing.T) {
173173
sut.fetcher = m
174174
sut.ip = m
175175

176-
got, err := sut.Process(tc.url)
176+
got, err := sut.Verify(tc.url)
177177

178178
if tc.wantErr == nil {
179179
require.NoError(t, err)
@@ -192,7 +192,7 @@ type DepsMock struct {
192192
mock.Mock
193193
}
194194

195-
func (m *DepsMock) Process(img string) Entry {
195+
func (m *DepsMock) Verify(img string) Entry {
196196
args := m.Called(img)
197197
return args.Get(0).(Entry)
198198
}

internal/imagelist/processor.go renamed to internal/imagelist/verifier.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import (
88
)
99

1010
type ImageProcessor interface {
11-
Process(img string) Entry
11+
Verify(img string) Entry
1212
}
1313

1414
type imageVerifier struct {
1515
m sync.Mutex
1616
}
1717

18-
func (i *imageVerifier) Process(img string) Entry {
18+
func (i *imageVerifier) Verify(img string) Entry {
1919
entry := Entry{
2020
Image: img,
2121
}

internal/product/common.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package product
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"regexp"
7+
"strings"
8+
)
9+
10+
var (
11+
ErrInvalidVersion = errors.New("invalid version")
12+
13+
versionRegex = regexp.MustCompile(`^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(\-(?:alpha|beta|rc)\d+)?$`)
14+
15+
productMapping = map[string]productInfo{
16+
"rancher-prime": {
17+
description: "SUSE Rancher Prime",
18+
imagesUrl: "https://github.com/rancher/rancher/releases/download/%s/rancher-images.txt",
19+
windowsImagesUrl: "https://github.com/rancher/rancher/releases/download/%s/rancher-windows-images.txt",
20+
},
21+
"storage": {
22+
description: "SUSE Storage",
23+
imagesUrl: "https://github.com/longhorn/longhorn/releases/download/%s/longhorn-images.txt",
24+
},
25+
"virtualization": {
26+
description: "SUSE Virtualization",
27+
imagesUrl: "https://github.com/harvester/harvester/releases/download/%s/harvester-images-list-amd64.txt",
28+
},
29+
}
30+
)
31+
32+
type productInfo struct {
33+
description string
34+
imagesUrl string
35+
windowsImagesUrl string
36+
}
37+
38+
type summary struct {
39+
count int
40+
signed int
41+
errors int
42+
}
43+
44+
func product(name, version string) (*productInfo, error) {
45+
if !versionRegex.MatchString(version) {
46+
return nil, fmt.Errorf("%w: %s", ErrInvalidVersion, version)
47+
}
48+
49+
info, found := productMapping[name]
50+
if !found {
51+
var names []string
52+
for name := range productMapping {
53+
names = append(names, name)
54+
}
55+
products := strings.Join(names, ", ")
56+
return nil, fmt.Errorf("product %q not found: options are %s", name, products)
57+
}
58+
59+
return &info, nil
60+
}

internal/product/verify.go

Lines changed: 9 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,64 +2,25 @@ package product
22

33
import (
44
"encoding/json"
5-
"errors"
65
"fmt"
76
"log/slog"
87
"os"
9-
"regexp"
108
"strings"
119
"text/tabwriter"
1210

1311
"github.com/rancherlabs/slsactl/internal/imagelist"
1412
)
1513

16-
type productInfo struct {
17-
description string
18-
imagesUrl string
19-
windowsImagesUrl string
20-
}
21-
22-
var (
23-
ErrInvalidVersion = errors.New("invalid version")
24-
25-
versionRegex = regexp.MustCompile(`^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]d*)(\-(?:alpha|beta|rc)\d+)?$`)
26-
27-
productMapping = map[string]productInfo{
28-
"rancher-prime": {
29-
description: "SUSE Rancher Prime",
30-
imagesUrl: "https://github.com/rancher/rancher/releases/download/%s/rancher-images.txt",
31-
windowsImagesUrl: "https://github.com/rancher/rancher/releases/download/%s/rancher-windows-images.txt",
32-
},
33-
"storage": {
34-
description: "SUSE Storage",
35-
imagesUrl: "https://github.com/longhorn/longhorn/releases/download/%s/longhorn-images.txt",
36-
},
37-
"virtualization": {
38-
description: "SUSE Virtualization",
39-
imagesUrl: "https://github.com/harvester/harvester/releases/download/%s/harvester-images-list-amd64.txt",
40-
},
41-
}
42-
)
43-
4414
func Verify(registry, name, version string, summary bool, outputFile bool) error {
45-
if !versionRegex.MatchString(version) {
46-
return fmt.Errorf("%w: %s", ErrInvalidVersion, version)
47-
}
48-
49-
info, found := productMapping[name]
50-
if !found {
51-
var names []string
52-
for name := range productMapping {
53-
names = append(names, name)
54-
}
55-
products := strings.Join(names, ", ")
56-
return fmt.Errorf("product %q not found: options are %s", name, products)
15+
info, err := product(name, version)
16+
if err != nil {
17+
return err
5718
}
5819

5920
fmt.Printf("Verifying container images for %s %s:\n\n", info.description, version)
6021

6122
p := imagelist.NewProcessor(registry)
62-
result, err := p.Process(fmt.Sprintf(info.imagesUrl, version))
23+
result, err := p.Verify(fmt.Sprintf(info.imagesUrl, version))
6324
if err != nil {
6425
return err
6526
}
@@ -68,7 +29,7 @@ func Verify(registry, name, version string, summary bool, outputFile bool) error
6829
result.Version = version
6930

7031
if len(info.windowsImagesUrl) > 0 {
71-
r2, err := p.Process(fmt.Sprintf(info.windowsImagesUrl, version))
32+
r2, err := p.Verify(fmt.Sprintf(info.windowsImagesUrl, version))
7233
if err == nil {
7334
result.Entries = append(result.Entries, r2.Entries...)
7435
} else {
@@ -77,26 +38,20 @@ func Verify(registry, name, version string, summary bool, outputFile bool) error
7738
}
7839

7940
if summary {
80-
err = printSummary(result)
41+
err = printVerifySummary(result)
8142
if err != nil {
8243
return fmt.Errorf("failed to print summary: %w", err)
8344
}
8445
}
8546

8647
if outputFile {
87-
return saveOutput(result)
48+
return savePrintOutput(result)
8849
}
8950

9051
return nil
9152
}
9253

93-
type summary struct {
94-
count int
95-
signed int
96-
errors int
97-
}
98-
99-
func printSummary(result *imagelist.Result) error {
54+
func printVerifySummary(result *imagelist.Result) error {
10055
w := new(tabwriter.Writer)
10156
w.Init(os.Stdout, 12, 12, 4, ' ', 0)
10257

@@ -131,7 +86,7 @@ func printSummary(result *imagelist.Result) error {
13186
return w.Flush()
13287
}
13388

134-
func saveOutput(result *imagelist.Result) error {
89+
func savePrintOutput(result *imagelist.Result) error {
13590
data, err := json.MarshalIndent(result, "", " ")
13691
if err != nil {
13792
return fmt.Errorf("fail to marshal JSON: %w", err)

pkg/verify/verify.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"crypto"
66
"errors"
7+
"fmt"
78
"os"
89
"strings"
910
"time"
@@ -18,7 +19,7 @@ import (
1819
)
1920

2021
var (
21-
ErrNoVerifierFound = errors.New("no verifier found for this image")
22+
ErrNoVerifierFound = errors.New("no verifier found for image")
2223

2324
cosignVerifier = &cosignImplementation{}
2425

@@ -64,7 +65,7 @@ func Verify(image string) error {
6465
}
6566
}
6667

67-
return ErrNoVerifierFound
68+
return fmt.Errorf("%w: %q", ErrNoVerifierFound, image)
6869
}
6970

7071
type cosignImplementation struct{}

0 commit comments

Comments
 (0)