[lxc-devel] [lxd/master] shared/simplestreams: Fix architecture filtering

stgraber on Github lxc-bot at linuxcontainers.org
Thu Jan 23 23:04:48 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 499 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200123/f4e3ed67/attachment-0001.bin>
-------------- next part --------------
From 5ccb215b9a558401fc4a3b854ddf6ef0b91e7243 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Fri, 24 Jan 2020 01:03:19 +0200
Subject: [PATCH] shared/simplestreams: Fix architecture filtering
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

We don't actually want to filter other architectures, instead we want to
keep the top aliases that match our native or foreign architecture.

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 shared/simplestreams/simplestreams.go | 28 +++++++++++-------
 shared/simplestreams/sort.go          | 42 +++++++++++++++++++++++++++
 2 files changed, 60 insertions(+), 10 deletions(-)

diff --git a/shared/simplestreams/simplestreams.go b/shared/simplestreams/simplestreams.go
index 11984de5ec..59841b419e 100644
--- a/shared/simplestreams/simplestreams.go
+++ b/shared/simplestreams/simplestreams.go
@@ -374,12 +374,19 @@ func (s *SimpleStreams) ListAliases() ([]api.ImageAliasesEntry, error) {
 		return nil, err
 	}
 
-	aliases := []api.ImageAliasesEntry{}
-
-	architectureName, _ := osarch.ArchitectureGetLocal()
+	// Sort the list ahead of dedup
+	sort.Sort(sortedAliases(aliasesList))
 
+	aliases := []api.ImageAliasesEntry{}
 	for _, entry := range aliasesList {
-		if entry.Architecture != architectureName {
+		dup := false
+		for _, v := range aliases {
+			if v.Name == entry.Name && v.Type == entry.Type {
+				dup = true
+			}
+		}
+
+		if dup {
 			continue
 		}
 
@@ -402,7 +409,8 @@ func (s *SimpleStreams) GetAlias(imageType string, name string) (*api.ImageAlias
 		return nil, err
 	}
 
-	architectureName, _ := osarch.ArchitectureGetLocal()
+	// Sort the list ahead of dedup
+	sort.Sort(sortedAliases(aliasesList))
 
 	var match *api.ImageAliasesEntry
 	for _, entry := range aliasesList {
@@ -410,16 +418,16 @@ func (s *SimpleStreams) GetAlias(imageType string, name string) (*api.ImageAlias
 			continue
 		}
 
-		if entry.Architecture != architectureName {
-			continue
-		}
-
 		if entry.Type != imageType && imageType != "" {
 			continue
 		}
 
 		if match != nil {
-			return nil, fmt.Errorf("More than one match for alias '%s'", name)
+			if match.Type != entry.Type {
+				return nil, fmt.Errorf("More than one match for alias '%s'", name)
+			}
+
+			continue
 		}
 
 		match = entry.Alias
diff --git a/shared/simplestreams/sort.go b/shared/simplestreams/sort.go
index 78872e4e7d..d8bc1fd1ab 100644
--- a/shared/simplestreams/sort.go
+++ b/shared/simplestreams/sort.go
@@ -3,8 +3,11 @@ package simplestreams
 import (
 	"github.com/lxc/lxd/shared"
 	"github.com/lxc/lxd/shared/api"
+	"github.com/lxc/lxd/shared/osarch"
 )
 
+var nativeName, _ = osarch.ArchitectureGetLocal()
+
 type sortedImages []api.Image
 
 func (a sortedImages) Len() int {
@@ -60,3 +63,42 @@ func (a sortedImages) Less(i, j int) bool {
 
 	return a[i].Properties["os"] < a[j].Properties["os"]
 }
+
+type sortedAliases []extendedAlias
+
+func (a sortedAliases) Len() int {
+	return len(a)
+}
+
+func (a sortedAliases) Swap(i, j int) {
+	a[i], a[j] = a[j], a[i]
+}
+
+func (a sortedAliases) Less(i, j int) bool {
+	if nativeName == a[i].Architecture {
+		return true
+	}
+
+	archId, err := osarch.ArchitectureId(nativeName)
+	if err != nil {
+		return false
+	}
+
+	personalities, err := osarch.ArchitecturePersonalities(archId)
+	if err != nil {
+		return false
+	}
+
+	for _, personality := range personalities {
+		personalityName, err := osarch.ArchitectureName(personality)
+		if err != nil {
+			return false
+		}
+
+		if personalityName == a[i].Architecture {
+			return true
+		}
+	}
+
+	return false
+}


More information about the lxc-devel mailing list