Skip to content

Commit

Permalink
dnfjson: support passing and receiving modules
Browse files Browse the repository at this point in the history
This commit allows for passing on module specifications to the
depsolver. When module specifications *are* passed then the depsolve
result will contain additional information about these modules. We also
store that data for use in pipelines later on.

Signed-off-by: Simon de Vlieger <[email protected]>
  • Loading branch information
supakeen committed Dec 16, 2024
1 parent 4c9b590 commit c5121bb
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 9 deletions.
51 changes: 43 additions & 8 deletions pkg/dnfjson/dnfjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ type Solver struct {
type DepsolveResult struct {
Packages []rpmmd.PackageSpec
Repos []rpmmd.RepoConfig
Modules []rpmmd.ModuleConfig
SBOM *sbom.Document
Solver string
}
Expand Down Expand Up @@ -231,7 +232,7 @@ func (s *Solver) Depsolve(pkgSets []rpmmd.PackageSet, sbomType sbom.StandardType
return nil, fmt.Errorf("decoding depsolve result failed: %w", err)
}

packages, repos := result.toRPMMD(rhsmMap)
packages, repos, modules := result.toRPMMD(rhsmMap)

var sbomDoc *sbom.Document
if sbomType != sbom.StandardTypeNone {
Expand All @@ -243,6 +244,7 @@ func (s *Solver) Depsolve(pkgSets []rpmmd.PackageSet, sbomType sbom.StandardType

return &DepsolveResult{
Packages: packages,
Modules: modules,
Repos: repos,
SBOM: sbomDoc,
Solver: result.Solver,
Expand Down Expand Up @@ -415,6 +417,17 @@ type repoConfig struct {
repoHash string
}

type moduleConfigModule struct {
Data string `json:"data"`
Path string `json:"path"`
}

type moduleConfig struct {
Name string `json:"name"`
ModuleFile moduleConfigModule `json:"module-file"`
FailsafeFile moduleConfigModule `json:"failsafe-file"`
}

// use the hash calculated by the `rpmmd.RepoConfig.Hash()`
// function rather than re-implementing the same code
func (r *repoConfig) Hash() string {
Expand Down Expand Up @@ -454,9 +467,10 @@ func (s *Solver) makeDepsolveRequest(pkgSets []rpmmd.PackageSet, sbomType sbom.S
transactions := make([]transactionArgs, len(pkgSets))
for dsIdx, pkgSet := range pkgSets {
transactions[dsIdx] = transactionArgs{
PackageSpecs: pkgSet.Include,
ExcludeSpecs: pkgSet.Exclude,
InstallWeakDeps: pkgSet.InstallWeakDeps,
PackageSpecs: pkgSet.Include,
ExcludeSpecs: pkgSet.Exclude,
ModuleEnableSpecs: pkgSet.EnabledModules,
InstallWeakDeps: pkgSet.InstallWeakDeps,
}

for _, jobRepo := range pkgSet.Repositories {
Expand Down Expand Up @@ -568,9 +582,10 @@ func (s *Solver) makeSearchRequest(repos []rpmmd.RepoConfig, packages []string)
// convert internal a list of PackageSpecs and map of repoConfig to the rpmmd
// equivalents and attach key and subscription information based on the
// repository configs.
func (result depsolveResult) toRPMMD(rhsm map[string]bool) ([]rpmmd.PackageSpec, []rpmmd.RepoConfig) {
func (result depsolveResult) toRPMMD(rhsm map[string]bool) ([]rpmmd.PackageSpec, []rpmmd.RepoConfig, []rpmmd.ModuleConfig) {
pkgs := result.Packages
repos := result.Repos
modules := result.Modules
rpmDependencies := make([]rpmmd.PackageSpec, len(pkgs))
for i, dep := range pkgs {
repo, ok := repos[dep.RepoID]
Expand Down Expand Up @@ -624,7 +639,23 @@ func (result depsolveResult) toRPMMD(rhsm map[string]bool) ([]rpmmd.PackageSpec,
SSLClientCert: repo.SSLClientCert,
})
}
return rpmDependencies, repoConfigs

var moduleConfigs []rpmmd.ModuleConfig
for moduleName := range modules {
module := modules[moduleName]

moduleConfigs = append(moduleConfigs, rpmmd.ModuleConfig{
Name: moduleName,

ModuleFilePath: module.ModuleFile.Path,
ModuleFileData: module.ModuleFile.Data,

FailsafeFilePath: module.FailsafeFile.Path,
FailsafeFileData: module.FailsafeFile.Data,
})
}

return rpmDependencies, repoConfigs, moduleConfigs
}

// Request command and arguments for dnf-json
Expand Down Expand Up @@ -712,6 +743,9 @@ type transactionArgs struct {
// Packages to exclude from results
ExcludeSpecs []string `json:"exclude-specs"`

// Modules to enable
ModuleEnableSpecs []string `json:"module-enable-specs"`

// IDs of repositories to use for this depsolve
RepoIDs []string `json:"repo-ids"`

Expand All @@ -722,8 +756,9 @@ type transactionArgs struct {
type packageSpecs []PackageSpec

type depsolveResult struct {
Packages packageSpecs `json:"packages"`
Repos map[string]repoConfig `json:"repos"`
Packages packageSpecs `json:"packages"`
Repos map[string]repoConfig `json:"repos"`
Modules map[string]moduleConfig `json:"modules"`

// (optional) contains the solver used, e.g. "dnf5"
Solver string `json:"solver,omitempty"`
Expand Down
18 changes: 17 additions & 1 deletion pkg/rpmmd/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ type RepoConfig struct {
SSLClientCert string `json:"sslclientcert,omitempty"`
}

// A module config contains the configuration file for the module to put
// in `/etc/dnf/modules.d/{Name}` and the failsafe file to put in
// `/var/lib/dnf/modulefailsafe`.
type ModuleConfig struct {
Name string

ModuleFilePath string
ModuleFileData string

FailsafeFilePath string
FailsafeFileData string
}

// Hash calculates an ID string that uniquely represents a repository
// configuration. The Name and ImageTypeTags fields are not considered in the
// calculation.
Expand Down Expand Up @@ -137,12 +150,15 @@ func (pkg Package) ToPackageInfo() PackageInfo {

// The inputs to depsolve, a set of packages to include and a set of packages
// to exclude. The Repositories are used when depsolving this package set in
// addition to the base repositories.
// addition to the base repositories. When modules are passed they are enabled
// before the rest of the depsolve as well. Note that we only expose enabled
// modules and not installation modules.
type PackageSet struct {
Include []string
Exclude []string
Repositories []RepoConfig
InstallWeakDeps bool
EnabledModules []string
}

// Append the Include and Exclude package list from another PackageSet and
Expand Down

0 comments on commit c5121bb

Please sign in to comment.