Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reporegistry: do not prepend repositories in confPaths #1179

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions pkg/reporegistry/error.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package reporegistry

import "fmt"
import (
"fmt"
"io/fs"
)

// NoReposLoadedError is an error type that is returned when no repositories
// are loaded from the given paths.
type NoReposLoadedError struct {
Paths []string
FSes []fs.FS
}

func (e *NoReposLoadedError) Error() string {
return fmt.Sprintf("no repositories found in the given paths: %v", e.Paths)
return fmt.Sprintf("no repositories found in the given paths: %v/%v", e.Paths, e.FSes)
}
13 changes: 9 additions & 4 deletions pkg/reporegistry/reporegistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package reporegistry

import (
"fmt"
"io/fs"

"github.com/osbuild/images/pkg/distroidparser"
"github.com/osbuild/images/pkg/rpmmd"
Expand All @@ -14,10 +15,14 @@ type RepoRegistry struct {
repos rpmmd.DistrosRepoConfigs
}

// New returns a new RepoRegistry instance with the data
// loaded from the given repoConfigPaths
func New(repoConfigPaths []string) (*RepoRegistry, error) {
repositories, err := LoadAllRepositories(repoConfigPaths)
// New returns a new RepoRegistry instance with the data loaded from
// the given repoConfigPaths and repoConfigFS instance. The order is
// important here, first the paths are tried, then the FSes.
//
// Note that the confPaths must point directly to the directory with
// the json repo files.
func New(repoConfigPaths []string, repoConfigFS []fs.FS) (*RepoRegistry, error) {
repositories, err := loadAllRepositories(repoConfigPaths, repoConfigFS)
if err != nil {
return nil, err
}
Expand Down
25 changes: 15 additions & 10 deletions pkg/reporegistry/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package reporegistry
import (
"io/fs"
"os"
"path/filepath"
"strings"

"github.com/sirupsen/logrus"
Expand All @@ -12,23 +11,26 @@ import (
"github.com/osbuild/images/pkg/rpmmd"
)

// LoadAllRepositories loads all repositories for given distros from the given list of paths.
// loadAllRepositories loads all repositories for given distros from the given list of paths.
// Behavior is the same as with the LoadRepositories() method.
func LoadAllRepositories(confPaths []string) (rpmmd.DistrosRepoConfigs, error) {
var confFSes []fs.FS
func loadAllRepositories(confPaths []string, confFSes []fs.FS) (rpmmd.DistrosRepoConfigs, error) {
var mergedFSes []fs.FS

for _, confPath := range confPaths {
confFSes = append(confFSes, os.DirFS(filepath.Join(confPath, "repositories")))
mergedFSes = append(mergedFSes, os.DirFS(confPath))
}
for _, confFS := range confFSes {
mergedFSes = append(mergedFSes, confFS)
}

distrosRepoConfigs, err := LoadAllRepositoriesFromFS(confFSes)
distrosRepoConfigs, err := loadAllRepositoriesFromFS(mergedFSes)
if len(distrosRepoConfigs) == 0 {
return nil, &NoReposLoadedError{confPaths}
return nil, &NoReposLoadedError{confPaths, confFSes}
}
return distrosRepoConfigs, err
}

func LoadAllRepositoriesFromFS(confPaths []fs.FS) (rpmmd.DistrosRepoConfigs, error) {
func loadAllRepositoriesFromFS(confPaths []fs.FS) (rpmmd.DistrosRepoConfigs, error) {
distrosRepoConfigs := rpmmd.DistrosRepoConfigs{}

for _, confPath := range confPaths {
Expand Down Expand Up @@ -89,9 +91,12 @@ func LoadAllRepositoriesFromFS(confPaths []fs.FS) (rpmmd.DistrosRepoConfigs, err
// If there are duplicate distro repositories definitions found in multiple paths, the first
// encounter is preferred. For this reason, the order of paths in the passed list should
// reflect the desired preference.
//
// Note that the confPaths must point directly to the directory with
// the json repo files.
func LoadRepositories(confPaths []string, distro string) (map[string][]rpmmd.RepoConfig, error) {
var repoConfigs map[string][]rpmmd.RepoConfig
path := "/repositories/" + distro + ".json"
path := distro + ".json"

for _, confPath := range confPaths {
var err error
Expand All @@ -109,7 +114,7 @@ func LoadRepositories(confPaths []string, distro string) (map[string][]rpmmd.Rep
}

if repoConfigs == nil {
return nil, &NoReposLoadedError{confPaths}
return nil, &NoReposLoadedError{confPaths, nil}
}

return repoConfigs, nil
Expand Down
8 changes: 4 additions & 4 deletions pkg/reporegistry/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (

func getConfPaths(t *testing.T) []string {
confPaths := []string{
"./test/confpaths/priority1",
"./test/confpaths/priority2",
"./test/confpaths/priority1/repositories",
"./test/confpaths/priority2/repositories",
}
var absConfPaths []string

Expand Down Expand Up @@ -280,7 +280,7 @@ func Test_LoadAllRepositories(t *testing.T) {

confPaths := getConfPaths(t)

distroReposMap, err := LoadAllRepositories(confPaths)
distroReposMap, err := loadAllRepositories(confPaths, nil)
assert.NotNil(t, distroReposMap)
assert.Nil(t, err)
assert.Equal(t, len(distroReposMap), len(expectedReposMap))
Expand All @@ -307,7 +307,7 @@ func TestLoadRepositoriesLogging(t *testing.T) {
logrus.AddHook(logHook)

confPaths := getConfPaths(t)
_, err := LoadAllRepositories(confPaths)
_, err := loadAllRepositories(confPaths, nil)
require.NoError(t, err)
needle := "Loaded repository configuration file: rhel-8.10.json"
assert.True(t, slices.ContainsFunc(logHook.AllEntries(), func(entry *logrus.Entry) bool {
Expand Down
Loading