diff --git a/cmd/resolutioncli/client.go b/cmd/resolutioncli/client.go index 434282d1e..93d0697c9 100644 --- a/cmd/resolutioncli/client.go +++ b/cmd/resolutioncli/client.go @@ -20,15 +20,22 @@ import ( "context" "github.com/operator-framework/operator-registry/alpha/action" + "github.com/operator-framework/operator-registry/alpha/declcfg" "github.com/operator-framework/operator-controller/internal/catalogmetadata" + "github.com/operator-framework/operator-controller/internal/catalogmetadata/client" + "github.com/operator-framework/operator-controller/internal/controllers" ) type indexRefClient struct { - renderer action.Render - bundlesCache []*catalogmetadata.Bundle + renderer action.Render + bundlesCache []*catalogmetadata.Bundle + channelsCache []*catalogmetadata.Channel + packagesCache []*catalogmetadata.Package } +var _ controllers.CatalogProvider = &indexRefClient{} + func newIndexRefClient(indexRef string) *indexRefClient { return &indexRefClient{ renderer: action.Render{ @@ -38,47 +45,81 @@ func newIndexRefClient(indexRef string) *indexRefClient { } } -func (c *indexRefClient) Bundles(ctx context.Context) ([]*catalogmetadata.Bundle, error) { - if c.bundlesCache == nil { +func (c *indexRefClient) CatalogContents(ctx context.Context) (*client.Contents, error) { + if c.bundlesCache == nil || c.channelsCache == nil || c.packagesCache == nil { cfg, err := c.renderer.Run(ctx) if err != nil { return nil, err } var ( - channels []*catalogmetadata.Channel - bundles []*catalogmetadata.Bundle - deprecations []*catalogmetadata.Deprecation + channels []*catalogmetadata.Channel + bundles []*catalogmetadata.Bundle + packages []*catalogmetadata.Package ) + // TODO: update fake catalog name string to be catalog name once we support multiple catalogs in CLI + catalogName := "offline-catalog" + + for i := range cfg.Packages { + packages = append(packages, &catalogmetadata.Package{ + Package: cfg.Packages[i], + Catalog: catalogName, + }) + } + for i := range cfg.Channels { channels = append(channels, &catalogmetadata.Channel{ Channel: cfg.Channels[i], + Catalog: catalogName, }) } for i := range cfg.Bundles { bundles = append(bundles, &catalogmetadata.Bundle{ - Bundle: cfg.Bundles[i], + Bundle: cfg.Bundles[i], + Catalog: catalogName, }) } - for i := range cfg.Deprecations { - deprecations = append(deprecations, &catalogmetadata.Deprecation{ - Deprecation: cfg.Deprecations[i], - }) + for _, deprecation := range cfg.Deprecations { + for _, entry := range deprecation.Entries { + switch entry.Reference.Schema { + case declcfg.SchemaPackage: + for _, pkg := range packages { + if pkg.Name == deprecation.Package { + pkg.Deprecation = &declcfg.DeprecationEntry{ + Reference: entry.Reference, + Message: entry.Message, + } + } + } + case declcfg.SchemaChannel: + for _, channel := range channels { + if channel.Package == deprecation.Package && channel.Name == entry.Reference.Name { + channel.Deprecation = &declcfg.DeprecationEntry{ + Reference: entry.Reference, + Message: entry.Message, + } + } + } + case declcfg.SchemaBundle: + for _, bundle := range bundles { + if bundle.Package == deprecation.Package && bundle.Name == entry.Reference.Name { + bundle.Deprecation = &declcfg.DeprecationEntry{ + Reference: entry.Reference, + Message: entry.Message, + } + } + } + } + } } - // TODO: update fake catalog name string to be catalog name once we support multiple catalogs in CLI - // catalogName := "offline-catalog" - - // bundles, err = client.PopulateExtraFields(catalogName, channels, bundles, deprecations) - // if err != nil { - // return nil, err - // } - c.bundlesCache = bundles + c.channelsCache = channels + c.packagesCache = packages } - return c.bundlesCache, nil + return &client.Contents{}, nil } diff --git a/cmd/resolutioncli/main.go b/cmd/resolutioncli/main.go index 6fbe62af8..c46872acd 100644 --- a/cmd/resolutioncli/main.go +++ b/cmd/resolutioncli/main.go @@ -36,7 +36,6 @@ import ( ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1" "github.com/operator-framework/operator-controller/internal/catalogmetadata" - "github.com/operator-framework/operator-controller/internal/catalogmetadata/client" "github.com/operator-framework/operator-controller/internal/controllers" olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables" ) @@ -151,7 +150,7 @@ func run(ctx context.Context, packageName, packageChannel, packageVersionRange, cl := clientBuilder.Build() catalogClient := newIndexRefClient(indexRef) - allBundles, err := catalogClient.Bundles(ctx) + contents, err := catalogClient.CatalogContents(ctx) if err != nil { return err } @@ -163,7 +162,7 @@ func run(ctx context.Context, packageName, packageChannel, packageVersionRange, if err := cl.List(ctx, &bundleDeploymentList); err != nil { return err } - variables, err := controllers.GenerateVariables(&client.Contents{Bundles: allBundles}, clusterExtensionList.Items, bundleDeploymentList.Items) + variables, err := controllers.GenerateVariables(contents, clusterExtensionList.Items, bundleDeploymentList.Items) if err != nil { return err } diff --git a/internal/resolution/variablesources/installed_package.go b/internal/resolution/variablesources/installed_package.go index a92d7e297..6b3bbca07 100644 --- a/internal/resolution/variablesources/installed_package.go +++ b/internal/resolution/variablesources/installed_package.go @@ -12,7 +12,6 @@ import ( ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1" "github.com/operator-framework/operator-controller/internal/catalogmetadata" "github.com/operator-framework/operator-controller/internal/catalogmetadata/filter" - catalogfilter "github.com/operator-framework/operator-controller/internal/catalogmetadata/filter" catalogsort "github.com/operator-framework/operator-controller/internal/catalogmetadata/sort" olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables" "github.com/operator-framework/operator-controller/pkg/features" @@ -62,9 +61,9 @@ func MakeInstalledPackageVariables( bundleImage := sourceImage.Ref // find corresponding bundle for the installed content - resultSet := catalogfilter.Filter(allBundles, catalogfilter.And( - catalogfilter.WithPackageName(clusterExtension.Spec.PackageName), - catalogfilter.WithImage(bundleImage), + resultSet := filter.Filter(allBundles, filter.And( + filter.WithPackageName(clusterExtension.Spec.PackageName), + filter.WithImage(bundleImage), )) if len(resultSet) == 0 { return nil, fmt.Errorf("bundle with image %q for package %q not found in available catalogs but is currently installed via BundleDeployment %q", bundleImage, clusterExtension.Spec.PackageName, bundleDeployment.Name) @@ -110,9 +109,9 @@ type successorsFunc func(allBundles []*catalogmetadata.Bundle, installedBundle * func legacySemanticsSuccessors(allBundles []*catalogmetadata.Bundle, installedBundle *catalogmetadata.Bundle, channels []*catalogmetadata.Channel) ([]*catalogmetadata.Bundle, error) { // find the bundles that replace the bundle provided // TODO: this algorithm does not yet consider skips and skipRange - upgradeEdges := catalogfilter.Filter(allBundles, catalogfilter.And( - catalogfilter.WithPackageName(installedBundle.Package), - catalogfilter.Replaces(installedBundle.Name, channels), + upgradeEdges := filter.Filter(allBundles, filter.And( + filter.WithPackageName(installedBundle.Package), + filter.Replaces(installedBundle.Name, channels), )) sort.SliceStable(upgradeEdges, func(i, j int) bool { return catalogsort.ByVersion(upgradeEdges[i], upgradeEdges[j]) @@ -138,9 +137,9 @@ func semverSuccessors(allBundles []*catalogmetadata.Bundle, installedBundle *cat return nil, err } - upgradeEdges := catalogfilter.Filter(allBundles, catalogfilter.And( - catalogfilter.WithPackageName(installedBundle.Package), - catalogfilter.InMastermindsSemverRange(wantedVersionRangeConstraint), + upgradeEdges := filter.Filter(allBundles, filter.And( + filter.WithPackageName(installedBundle.Package), + filter.InMastermindsSemverRange(wantedVersionRangeConstraint), )) sort.SliceStable(upgradeEdges, func(i, j int) bool { return catalogsort.ByVersion(upgradeEdges[i], upgradeEdges[j]) diff --git a/internal/resolution/variablesources/required_package.go b/internal/resolution/variablesources/required_package.go index 47665bcc3..2d7041411 100644 --- a/internal/resolution/variablesources/required_package.go +++ b/internal/resolution/variablesources/required_package.go @@ -9,7 +9,6 @@ import ( ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1" "github.com/operator-framework/operator-controller/internal/catalogmetadata" "github.com/operator-framework/operator-controller/internal/catalogmetadata/filter" - catalogfilter "github.com/operator-framework/operator-controller/internal/catalogmetadata/filter" catalogsort "github.com/operator-framework/operator-controller/internal/catalogmetadata/sort" olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables" ) @@ -36,12 +35,12 @@ func MakeRequiredPackageVariables(allBundles []*catalogmetadata.Bundle, allChann }, ) - predicates := []catalogfilter.Predicate[catalogmetadata.Bundle]{ - catalogfilter.WithPackageName(packageName), + predicates := []filter.Predicate[catalogmetadata.Bundle]{ + filter.WithPackageName(packageName), } if channelName != "" { - predicates = append(predicates, catalogfilter.InChannel(channelName, channels)) + predicates = append(predicates, filter.InChannel(channelName, channels)) } if versionRange != "" { @@ -49,10 +48,10 @@ func MakeRequiredPackageVariables(allBundles []*catalogmetadata.Bundle, allChann if err != nil { return nil, fmt.Errorf("invalid version range %q: %w", versionRange, err) } - predicates = append(predicates, catalogfilter.InMastermindsSemverRange(vr)) + predicates = append(predicates, filter.InMastermindsSemverRange(vr)) } - resultSet := catalogfilter.Filter(allBundles, catalogfilter.And(predicates...)) + resultSet := filter.Filter(allBundles, filter.And(predicates...)) if len(resultSet) == 0 { if versionRange != "" && channelName != "" { return nil, fmt.Errorf("no package %q matching version %q found in channel %q", packageName, versionRange, channelName)