Skip to content

Commit

Permalink
Add k8s.io/apimachinery/pkg/util/sets (#462)
Browse files Browse the repository at this point in the history
Switches from `map` of empty structs to using
sets package from apimachinery utils.

Signed-off-by: Mikalai Radchuk <[email protected]>
  • Loading branch information
m1kola authored Oct 16, 2023
1 parent e001a0f commit 7161a7b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
7 changes: 4 additions & 3 deletions internal/resolution/variablesources/bundle_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/operator-framework/deppy/pkg/deppy"
"github.com/operator-framework/deppy/pkg/deppy/input"
rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
"k8s.io/apimachinery/pkg/util/sets"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -36,14 +37,14 @@ func (o *BundleDeploymentVariableSource) GetVariables(ctx context.Context) ([]de
return nil, err
}

processed := map[string]struct{}{}
processed := sets.Set[string]{}
for _, bundleDeployment := range bundleDeployments.Items {
sourceImage := bundleDeployment.Spec.Template.Spec.Source.Image
if sourceImage != nil && sourceImage.Ref != "" {
if _, ok := processed[sourceImage.Ref]; ok {
if processed.Has(sourceImage.Ref) {
continue
}
processed[sourceImage.Ref] = struct{}{}
processed.Insert(sourceImage.Ref)
ips, err := NewInstalledPackageVariableSource(o.catalogClient, bundleDeployment.Spec.Template.Spec.Source.Image.Ref)
if err != nil {
return nil, err
Expand Down
13 changes: 7 additions & 6 deletions internal/resolution/variablesources/bundles_and_dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/operator-framework/deppy/pkg/deppy"
"github.com/operator-framework/deppy/pkg/deppy/input"
"k8s.io/apimachinery/pkg/util/sets"

"github.com/operator-framework/operator-controller/internal/catalogmetadata"
catalogfilter "github.com/operator-framework/operator-controller/internal/catalogmetadata/filter"
Expand Down Expand Up @@ -57,7 +58,7 @@ func (b *BundlesAndDepsVariableSource) GetVariables(ctx context.Context) ([]depp
}

// build bundle and dependency variables
visited := map[deppy.Identifier]struct{}{}
visited := sets.Set[deppy.Identifier]{}
for len(bundleQueue) > 0 {
// pop head of queue
var head *catalogmetadata.Bundle
Expand All @@ -66,10 +67,10 @@ func (b *BundlesAndDepsVariableSource) GetVariables(ctx context.Context) ([]depp
id := olmvariables.BundleVariableID(head)

// ignore bundles that have already been processed
if _, ok := visited[id]; ok {
if visited.Has(id) {
continue
}
visited[id] = struct{}{}
visited.Insert(id)

// get bundle dependencies
dependencies, err := b.filterBundleDependencies(allBundles, head)
Expand All @@ -89,7 +90,7 @@ func (b *BundlesAndDepsVariableSource) GetVariables(ctx context.Context) ([]depp

func (b *BundlesAndDepsVariableSource) filterBundleDependencies(allBundles []*catalogmetadata.Bundle, bundle *catalogmetadata.Bundle) ([]*catalogmetadata.Bundle, error) {
var dependencies []*catalogmetadata.Bundle
added := map[deppy.Identifier]struct{}{}
added := sets.Set[deppy.Identifier]{}

// gather required package dependencies
// todo(perdasilva): disambiguate between not found and actual errors
Expand All @@ -102,9 +103,9 @@ func (b *BundlesAndDepsVariableSource) filterBundleDependencies(allBundles []*ca
for i := 0; i < len(packageDependencyBundles); i++ {
bundle := packageDependencyBundles[i]
id := olmvariables.BundleVariableID(bundle)
if _, ok := added[id]; !ok {
if !added.Has(id) {
dependencies = append(dependencies, bundle)
added[id] = struct{}{}
added.Insert(id)
}
}
}
Expand Down

0 comments on commit 7161a7b

Please sign in to comment.