Skip to content

Commit bcc95b9

Browse files
committed
Filter out semver incompatible tags
1 parent c86453c commit bcc95b9

5 files changed

Lines changed: 44 additions & 19 deletions

File tree

cloudprofilesync/runnable.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"slices"
1010
"time"
1111

12+
"github.com/blang/semver/v4"
1213
"github.com/gardener/gardener/pkg/apis/core/v1beta1"
1314
"github.com/go-logr/logr"
1415
"k8s.io/apimachinery/pkg/types"
@@ -56,10 +57,31 @@ func (r *Runnable) patchCloudProfile(ctx context.Context) error {
5657
return nil
5758
}
5859

59-
func (r *Runnable) CheckSource(ctx context.Context, cloudProfile *v1beta1.CloudProfile) error {
60+
func (r *Runnable) getVersions(ctx context.Context) ([]SourceImage, error) {
6061
versions, err := r.Source.GetVersions(ctx)
6162
if err != nil {
62-
return fmt.Errorf("failed to get versions from source: %w", err)
63+
return nil, fmt.Errorf("failed to get versions from source: %w", err)
64+
}
65+
filtered := make([]SourceImage, 0, len(versions))
66+
for _, version := range versions {
67+
_, err := semver.Parse(version.Version)
68+
if err != nil {
69+
r.Log.Info("skipping invalid version", "version", version.Version)
70+
continue
71+
}
72+
if len(version.Architectures) == 0 {
73+
r.Log.Info("skipping version with no architectures", "version", version.Version)
74+
continue
75+
}
76+
filtered = append(filtered, version)
77+
}
78+
return filtered, nil
79+
}
80+
81+
func (r *Runnable) CheckSource(ctx context.Context, cloudProfile *v1beta1.CloudProfile) error {
82+
versions, err := r.getVersions(ctx)
83+
if err != nil {
84+
return err
6385
}
6486
imageIndex := slices.IndexFunc(cloudProfile.Spec.MachineImages, func(mi v1beta1.MachineImage) bool {
6587
return mi.Name == r.Source.Name

cloudprofilesync/runnable_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@ import (
1414
var _ = Describe("Runnable", func() {
1515

1616
It("adds an image from the source to the CloudProfile spec", func(ctx SpecContext) {
17-
mockSource.images = []cloudprofilesync.SourceImage{{Version: "v1.0.0", Architectures: []string{"amd64"}}}
17+
mockSource.images = []cloudprofilesync.SourceImage{{Version: "1.0.0", Architectures: []string{"amd64"}}}
1818
var cloudProfile v1beta1.CloudProfile
1919
Expect(runnable.CheckSource(ctx, &cloudProfile)).To(Succeed())
2020
Expect(cloudProfile.Spec.MachineImages).To(HaveLen(1))
2121
Expect(cloudProfile.Spec.MachineImages[0].Name).To(Equal("test"))
2222
Expect(cloudProfile.Spec.MachineImages[0].Versions).To(HaveLen(1))
23-
Expect(cloudProfile.Spec.MachineImages[0].Versions[0].Version).To(Equal("v1.0.0"))
23+
Expect(cloudProfile.Spec.MachineImages[0].Versions[0].Version).To(Equal("1.0.0"))
2424
Expect(cloudProfile.Spec.MachineImages[0].Versions[0].Architectures).To(Equal([]string{"amd64"}))
2525
})
2626

2727
It("adds multiple images from the source to the CloudProfile spec", func(ctx SpecContext) {
2828
mockSource.images = []cloudprofilesync.SourceImage{
29-
{Version: "v1.0.0", Architectures: []string{"amd64"}},
30-
{Version: "v2.0.0", Architectures: []string{"arm64", "amd64"}},
29+
{Version: "1.0.0", Architectures: []string{"amd64"}},
30+
{Version: "2.0.0", Architectures: []string{"arm64", "amd64"}},
3131
}
3232
var cloudProfile v1beta1.CloudProfile
3333
Expect(runnable.CheckSource(ctx, &cloudProfile)).To(Succeed())
@@ -36,13 +36,13 @@ var _ = Describe("Runnable", func() {
3636
Expect(cloudProfile.Spec.MachineImages[0].Versions).To(ConsistOf([]v1beta1.MachineImageVersion{
3737
{
3838
ExpirableVersion: v1beta1.ExpirableVersion{
39-
Version: "v1.0.0",
39+
Version: "1.0.0",
4040
},
4141
Architectures: []string{"amd64"},
4242
},
4343
{
4444
ExpirableVersion: v1beta1.ExpirableVersion{
45-
Version: "v2.0.0",
45+
Version: "2.0.0",
4646
},
4747
Architectures: []string{"arm64", "amd64"},
4848
},
@@ -58,7 +58,7 @@ var _ = Describe("Runnable", func() {
5858
Versions: []v1beta1.MachineImageVersion{
5959
{
6060
ExpirableVersion: v1beta1.ExpirableVersion{
61-
Version: "v1.0.0",
61+
Version: "1.0.0",
6262
},
6363
Architectures: []string{"amd64"},
6464
},
@@ -68,12 +68,12 @@ var _ = Describe("Runnable", func() {
6868
},
6969
}
7070

71-
mockSource.images = []cloudprofilesync.SourceImage{{Version: "v2.0.0", Architectures: []string{"arm64"}}}
71+
mockSource.images = []cloudprofilesync.SourceImage{{Version: "2.0.0", Architectures: []string{"arm64"}}}
7272
Expect(runnable.CheckSource(ctx, &cloudProfile)).To(Succeed())
7373
Expect(cloudProfile.Spec.MachineImages).To(HaveLen(1))
7474
Expect(cloudProfile.Spec.MachineImages[0].Name).To(Equal("test"))
7575
Expect(cloudProfile.Spec.MachineImages[0].Versions).To(HaveLen(1))
76-
Expect(cloudProfile.Spec.MachineImages[0].Versions[0].Version).To(Equal("v2.0.0"))
76+
Expect(cloudProfile.Spec.MachineImages[0].Versions[0].Version).To(Equal("2.0.0"))
7777
Expect(cloudProfile.Spec.MachineImages[0].Versions[0].Architectures).To(Equal([]string{"arm64"}))
7878
})
7979

@@ -86,7 +86,7 @@ var _ = Describe("Runnable", func() {
8686
Versions: []v1beta1.MachineImageVersion{
8787
{
8888
ExpirableVersion: v1beta1.ExpirableVersion{
89-
Version: "v1.0.0",
89+
Version: "1.0.0",
9090
},
9191
Architectures: []string{"amd64"},
9292
},
@@ -97,7 +97,7 @@ var _ = Describe("Runnable", func() {
9797
Versions: []v1beta1.MachineImageVersion{
9898
{
9999
ExpirableVersion: v1beta1.ExpirableVersion{
100-
Version: "v2.0.0",
100+
Version: "2.0.0",
101101
},
102102
Architectures: []string{"arm64"},
103103
},
@@ -107,15 +107,15 @@ var _ = Describe("Runnable", func() {
107107
},
108108
}
109109

110-
mockSource.images = []cloudprofilesync.SourceImage{{Version: "v1.1.0", Architectures: []string{"arm64"}}}
110+
mockSource.images = []cloudprofilesync.SourceImage{{Version: "1.1.0", Architectures: []string{"arm64"}}}
111111
Expect(runnable.CheckSource(ctx, &cloudProfile)).To(Succeed())
112112
Expect(cloudProfile.Spec.MachineImages).To(ConsistOf([]v1beta1.MachineImage{
113113
{
114114
Name: "test",
115115
Versions: []v1beta1.MachineImageVersion{
116116
{
117117
ExpirableVersion: v1beta1.ExpirableVersion{
118-
Version: "v1.1.0",
118+
Version: "1.1.0",
119119
},
120120
Architectures: []string{"arm64"},
121121
},
@@ -126,7 +126,7 @@ var _ = Describe("Runnable", func() {
126126
Versions: []v1beta1.MachineImageVersion{
127127
{
128128
ExpirableVersion: v1beta1.ExpirableVersion{
129-
Version: "v2.0.0",
129+
Version: "2.0.0",
130130
},
131131
Architectures: []string{"arm64"},
132132
},

cloudprofilesync/source_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ var _ = Describe("OCISource", func() {
3232
MediaType: ocispec.MediaTypeImageManifest,
3333
Size: 0,
3434
Digest: ocispec.DescriptorEmptyJSON.Digest,
35-
Platform: &ocispec.Platform{
36-
Architecture: "amd64",
37-
},
3835
},
3936
},
37+
Annotations: map[string]string{
38+
"architecture": "amd64",
39+
},
4040
}
4141
indexBlob, err := json.Marshal(index)
4242
Expect(err).To(Succeed())

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/cobaltcore-dev/cloud-profile-sync
33
go 1.24.0
44

55
require (
6+
github.com/blang/semver/v4 v4.0.0
67
github.com/distribution/distribution/v3 v3.0.0
78
github.com/gardener/gardener v1.118.1
89
github.com/go-logr/logr v1.4.2

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
1616
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
1717
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
1818
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
19+
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
20+
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
1921
github.com/bshuster-repo/logrus-logstash-hook v1.0.0 h1:e+C0SB5R1pu//O4MQ3f9cFuPGoOVeF2fE4Og9otCc70=
2022
github.com/bshuster-repo/logrus-logstash-hook v1.0.0/go.mod h1:zsTqEiSzDgAa/8GZR7E1qaXrhYNDKBYy5/dWPTIflbk=
2123
github.com/bsm/ginkgo/v2 v2.7.0/go.mod h1:AiKlXPm7ItEHNc/2+OkrNG4E0ITzojb9/xWzvQ9XZ9w=

0 commit comments

Comments
 (0)