-
Notifications
You must be signed in to change notification settings - Fork 67
🌱 Refactor NewBundleFS test utility to builder pattern #2237
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
Merged
openshift-merge-bot
merged 1 commit into
operator-framework:main
from
perdasilva:refactor-new-bundle-fs
Oct 2, 2025
+308
−92
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 0 additions & 64 deletions
64
internal/operator-controller/rukpak/util/testing/bundlefs.go
This file was deleted.
Oops, something went wrong.
145 changes: 145 additions & 0 deletions
145
internal/operator-controller/rukpak/util/testing/bundlefs/bundlefs.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package bundlefs | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"strings" | ||
"testing/fstest" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/yaml" | ||
|
||
"github.com/operator-framework/api/pkg/operators/v1alpha1" | ||
"github.com/operator-framework/operator-registry/alpha/property" | ||
|
||
"github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/bundle/source" | ||
registry "github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/operator-registry" | ||
) | ||
|
||
const ( | ||
BundlePathAnnotations = "metadata/annotations.yaml" | ||
BundlePathProperties = "metadata/properties.yaml" | ||
BundlePathManifests = "manifests" | ||
) | ||
|
||
// BundleFSBuilder builds a registry+v1 bundle filesystem | ||
type BundleFSBuilder interface { | ||
WithPackageName(packageName string) BundleFSBuilder | ||
WithChannels(channels ...string) BundleFSBuilder | ||
WithDefaultChannel(channel string) BundleFSBuilder | ||
WithBundleProperty(propertyType string, value string) BundleFSBuilder | ||
WithBundleResource(resourceName string, resource client.Object) BundleFSBuilder | ||
WithCSV(csv v1alpha1.ClusterServiceVersion) BundleFSBuilder | ||
Build() fstest.MapFS | ||
} | ||
|
||
// bundleFSBuilder builds a registry+v1 bundle filesystem | ||
type bundleFSBuilder struct { | ||
annotations *registry.Annotations | ||
properties []property.Property | ||
resources map[string]client.Object | ||
} | ||
|
||
func Builder() BundleFSBuilder { | ||
return &bundleFSBuilder{} | ||
} | ||
|
||
// WithPackageName is an option for NewBundleFS used to set the package name annotation in the | ||
// bundle filesystem metadata/annotations.yaml file | ||
func (b *bundleFSBuilder) WithPackageName(packageName string) BundleFSBuilder { | ||
if b.annotations == nil { | ||
b.annotations = ®istry.Annotations{} | ||
} | ||
b.annotations.PackageName = packageName | ||
return b | ||
} | ||
|
||
// WithChannels is an option for NewBundleFS used to set the channels annotation in the | ||
// bundle filesystem metadata/annotations.yaml file | ||
func (b *bundleFSBuilder) WithChannels(channels ...string) BundleFSBuilder { | ||
if b.annotations == nil { | ||
b.annotations = ®istry.Annotations{} | ||
} | ||
b.annotations.Channels = strings.Join(channels, ",") | ||
return b | ||
} | ||
|
||
// WithDefaultChannel is an option for NewBundleFS used to set the channel annotation in the | ||
// bundle filesystem metadata/annotations.yaml file | ||
func (b *bundleFSBuilder) WithDefaultChannel(channel string) BundleFSBuilder { | ||
if b.annotations == nil { | ||
b.annotations = ®istry.Annotations{} | ||
} | ||
b.annotations.DefaultChannelName = channel | ||
return b | ||
} | ||
|
||
// WithBundleProperty is an options for NewBundleFS used to add a property to the list of properties | ||
// in the bundle filesystem metadata/properties.yaml file | ||
func (b *bundleFSBuilder) WithBundleProperty(propertyType string, value string) BundleFSBuilder { | ||
b.properties = append(b.properties, property.Property{ | ||
Type: propertyType, | ||
Value: []byte(`"` + value + `"`), | ||
}) | ||
return b | ||
} | ||
|
||
// WithBundleResource is an option for NewBundleFS use to add the yaml representation of resource to the | ||
// path manifests/<resourceName>.yaml on the bundles filesystem | ||
func (b *bundleFSBuilder) WithBundleResource(resourceName string, resource client.Object) BundleFSBuilder { | ||
if b.resources == nil { | ||
b.resources = make(map[string]client.Object) | ||
} | ||
b.resources[resourceName] = resource | ||
return b | ||
} | ||
|
||
// WithCSV is an optiona for NewBundleFS used to add the yaml representation of csv to the | ||
// path manifests/csv.yaml on the bundle filesystem | ||
func (b *bundleFSBuilder) WithCSV(csv v1alpha1.ClusterServiceVersion) BundleFSBuilder { | ||
if b.resources == nil { | ||
b.resources = make(map[string]client.Object) | ||
} | ||
b.resources["csv.yaml"] = &csv | ||
return b | ||
} | ||
|
||
// Build creates a registry+v1 bundle filesystem with the applied options | ||
// By default, an empty registry+v1 bundle filesystem will be returned | ||
func (b *bundleFSBuilder) Build() fstest.MapFS { | ||
bundleFS := fstest.MapFS{} | ||
|
||
// Add annotations metadata | ||
if b.annotations != nil { | ||
annotationsYml, err := yaml.Marshal(registry.AnnotationsFile{ | ||
Annotations: *b.annotations, | ||
}) | ||
if err != nil { | ||
panic(fmt.Errorf("error building bundle fs: %w", err)) | ||
} | ||
bundleFS[BundlePathAnnotations] = &fstest.MapFile{Data: annotationsYml} | ||
} | ||
|
||
// Add property metadata | ||
if len(b.properties) > 0 { | ||
propertiesYml, err := yaml.Marshal(source.RegistryV1Properties{ | ||
Properties: b.properties, | ||
}) | ||
if err != nil { | ||
panic(fmt.Errorf("error building bundle fs: %w", err)) | ||
} | ||
bundleFS[BundlePathProperties] = &fstest.MapFile{Data: propertiesYml} | ||
} | ||
|
||
// Add resources | ||
for name, obj := range b.resources { | ||
resourcePath := filepath.Join(BundlePathManifests, name) | ||
resourceYml, err := yaml.Marshal(obj) | ||
if err != nil { | ||
panic(fmt.Errorf("error building bundle fs: %w", err)) | ||
} | ||
bundleFS[resourcePath] = &fstest.MapFile{Data: resourceYml} | ||
} | ||
|
||
return bundleFS | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.