Skip to content

Commit

Permalink
chore: prioritize exact match for service version (#8614)
Browse files Browse the repository at this point in the history
  • Loading branch information
leon-inf authored Dec 10, 2024
1 parent ab6ce57 commit 03504a8
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 4 deletions.
31 changes: 27 additions & 4 deletions pkg/controller/component/component_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,36 @@ func findMatchedImagesFromCompVersions(compVersions []*appsv1alpha1.ComponentVer
}
}
}

exactMatchedServiceVersion := func(releases map[string]appNameVersionImage) []string {
names := make([]string, 0)
for name, r := range releases {
if r.version == serviceVersion {
names = append(names, name)
}
}
return names
}

apps := make(map[string]appNameVersionImage)
for name, releases := range appsWithReleases {
names := maps.Keys(releases)
slices.Sort(names)
for appName, releases := range appsWithReleases {
releaseNames := maps.Keys(releases)
if names := exactMatchedServiceVersion(releases); len(names) > 0 {
releaseNames = names
}
slices.Sort(releaseNames)
// use the latest release
apps[name] = releases[names[len(names)-1]]
apps[appName] = releases[releaseNames[len(releaseNames)-1]]
}

matched := appNameVersionImage{}
for name, app := range apps {
if len(matched.version) > 0 && app.version != matched.version {
return nil, fmt.Errorf("multiple service versions matched: %v, %v", matched, app)
}
matched = apps[name]
}

return apps, nil
}

Expand Down
85 changes: 85 additions & 0 deletions pkg/controller/component/component_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,90 @@ var _ = Describe("Component Version", func() {
Expect(err).Should(Succeed())
Expect(compDefObj.Spec.Runtime.Containers[0].Image).Should(Equal(releases[2].Images[testapps.AppName]))
})

It("exact matched service version", func() {
compDefObj := testapps.NewComponentDefinitionFactory(testapps.CompDefName("v1")).
SetRuntime(&corev1.Container{Name: testapps.AppName}).
GetObject()

releases := []appsv1alpha1.ComponentVersionRelease{
{
Name: testapps.ReleaseID("r0"), // v0.0.1-r0
ServiceVersion: testapps.ServiceVersion(""), // 8.0.30
Images: map[string]string{
testapps.AppName: testapps.AppImage(testapps.AppName, testapps.ReleaseID("r0")),
},
},
{
Name: testapps.ReleaseID("r0-opt"), // // v0.0.1-r0-opt, has a newer release name
ServiceVersion: testapps.ServiceVersion("opt"), // 8.0.30-opt
Images: map[string]string{
testapps.AppName: testapps.AppImage(testapps.AppName, testapps.ReleaseID("r0-opt")),
},
},
}

compVersionObj := testapps.NewComponentVersionFactory(testapps.CompVersionName).
SetSpec(appsv1alpha1.ComponentVersionSpec{
CompatibilityRules: []appsv1alpha1.ComponentVersionCompatibilityRule{
{
CompDefs: []string{compDefObj.Name},
Releases: []string{releases[0].Name, releases[1].Name},
},
},
Releases: []appsv1alpha1.ComponentVersionRelease{releases[0], releases[1]},
}).
GetObject()

By("resolve images with service version 8.0.30")
err := resolveImagesWithCompVersions(compDefObj, []*appsv1alpha1.ComponentVersion{compVersionObj}, testapps.ServiceVersion(""))
Expect(err).Should(Succeed())
Expect(compDefObj.Spec.Runtime.Containers[0].Image).Should(Equal(releases[0].Images[testapps.AppName]))
})

It("matched from different service versions", func() {
var (
app1, app2 = "app1", "app2"
)

compDefObj := testapps.NewComponentDefinitionFactory(testapps.CompDefName("v1")).
SetRuntime(&corev1.Container{Name: app1}).
SetRuntime(&corev1.Container{Name: app2}).
GetObject()

releases := []appsv1alpha1.ComponentVersionRelease{
{
Name: testapps.ReleaseID("r0"), // v0.0.1-r0
ServiceVersion: testapps.ServiceVersion(""), // 8.0.30
Images: map[string]string{
app1: testapps.AppImage(app1, testapps.ReleaseID("r0")),
},
},
{
Name: testapps.ReleaseID("r0-opt"), // // v0.0.1-r0-opt, has a newer release name
ServiceVersion: testapps.ServiceVersion("opt"), // 8.0.30-opt
Images: map[string]string{
app1: testapps.AppImage(app1, testapps.ReleaseID("r0-opt")),
app2: testapps.AppImage(app2, testapps.ReleaseID("r0-opt")),
},
},
}

compVersionObj := testapps.NewComponentVersionFactory(testapps.CompVersionName).
SetSpec(appsv1alpha1.ComponentVersionSpec{
CompatibilityRules: []appsv1alpha1.ComponentVersionCompatibilityRule{
{
CompDefs: []string{compDefObj.Name},
Releases: []string{releases[0].Name, releases[1].Name},
},
},
Releases: []appsv1alpha1.ComponentVersionRelease{releases[0], releases[1]},
}).
GetObject()

By("resolve images with service version 8.0.30")
err := resolveImagesWithCompVersions(compDefObj, []*appsv1alpha1.ComponentVersion{compVersionObj}, testapps.ServiceVersion(""))
Expect(err).ShouldNot(BeNil())
})
})
})

0 comments on commit 03504a8

Please sign in to comment.