-
Notifications
You must be signed in to change notification settings - Fork 260
detect template type from input, but respect explicit specification #1844
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 3 commits into
operator-framework:master
from
grokspawn:template-autodetect
Jan 15, 2026
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
|
|
||
| "github.com/operator-framework/operator-registry/alpha/declcfg" | ||
| ) | ||
|
|
||
| // BundleRenderer defines the function signature for rendering a string containing a bundle image/path/file into a DeclarativeConfig fragment | ||
| // It's provided as a discrete type to allow for easy mocking in tests as well as facilitating variable | ||
| // restrictions on reference types | ||
| type BundleRenderer func(context.Context, string) (*declcfg.DeclarativeConfig, error) | ||
|
|
||
| // Template defines the common interface for all template types | ||
| type Template interface { | ||
| // RenderBundle renders a bundle image reference into a DeclarativeConfig fragment. | ||
| // This function is used to render a single bundle image reference by a template instance, | ||
| // and is provided to the template on construction. | ||
| // This is typically used in the call to Render the template to DeclarativeConfig, and | ||
| // needs to be configurable to handle different bundle image formats and configurations. | ||
| RenderBundle(ctx context.Context, imageRef string) (*declcfg.DeclarativeConfig, error) | ||
| // Render processes the raw template yaml/json input and returns an expanded DeclarativeConfig | ||
| // in the case where expansion fails, it returns an error | ||
| Render(ctx context.Context, reader io.Reader) (*declcfg.DeclarativeConfig, error) | ||
| // Schema returns the schema identifier for this template type | ||
| Schema() string | ||
| } | ||
|
|
||
| // Factory creates template instances based on schema | ||
| type Factory interface { | ||
| // CreateTemplate creates a new template instance with the given RenderBundle function | ||
| CreateTemplate(renderBundle BundleRenderer) Template | ||
| // Schema returns the schema identifier this factory handles | ||
| Schema() string | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| package template | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "slices" | ||
| "strings" | ||
| "sync" | ||
| "text/tabwriter" | ||
|
|
||
| "github.com/operator-framework/operator-registry/alpha/template/api" | ||
| "github.com/operator-framework/operator-registry/alpha/template/basic" | ||
| "github.com/operator-framework/operator-registry/alpha/template/semver" | ||
| ) | ||
|
|
||
| // Re-export api types for backward compatibility | ||
| type ( | ||
| BundleRenderer = api.BundleRenderer | ||
| Template = api.Template | ||
| Factory = api.Factory | ||
| ) | ||
|
|
||
| type Registry interface { | ||
| Register(factory Factory) | ||
| GetSupportedTypes() []string | ||
| HasType(templateType string) bool | ||
| HasSchema(schema string) bool | ||
| CreateTemplateBySchema(reader io.Reader, renderBundle BundleRenderer) (Template, io.Reader, error) | ||
| CreateTemplateByType(templateType string, renderBundle BundleRenderer) (Template, error) | ||
| GetSupportedSchemas() []string | ||
| HelpText() string | ||
| } | ||
|
|
||
| // registry maintains a mapping of schema identifiers to template factories | ||
| type registry struct { | ||
| mu sync.RWMutex | ||
| factories map[string]Factory | ||
| } | ||
|
|
||
| // NewRegistry creates a new registry and registers all built-in template factories. | ||
| func NewRegistry() Registry { | ||
| r := ®istry{ | ||
| factories: make(map[string]Factory), | ||
| } | ||
| r.Register(&basic.Factory{}) | ||
| r.Register(&semver.Factory{}) | ||
| return r | ||
| } | ||
|
|
||
| func (r *registry) HelpText() string { | ||
| var help strings.Builder | ||
| supportedTypes := r.GetSupportedTypes() | ||
| help.WriteString("\n") | ||
| tabber := tabwriter.NewWriter(&help, 0, 0, 1, ' ', 0) | ||
| for _, item := range supportedTypes { | ||
| fmt.Fprintf(tabber, " - %s\n", item) | ||
| } | ||
| tabber.Flush() | ||
| return help.String() | ||
| } | ||
|
|
||
| // Register adds a template factory to the registry | ||
| func (r *registry) Register(factory Factory) { | ||
| r.mu.Lock() | ||
| defer r.mu.Unlock() | ||
| r.factories[factory.Schema()] = factory | ||
| } | ||
|
|
||
| // CreateTemplateBySchema creates a template instance based on the schema found in the input | ||
| // and returns a reader that can be used to render the template. The returned reader includes | ||
| // both the data consumed during schema detection and the remaining unconsumed data. | ||
| func (r *registry) CreateTemplateBySchema(reader io.Reader, renderBundle BundleRenderer) (Template, io.Reader, error) { | ||
| schema, replayReader, err := detectSchema(reader) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| r.mu.RLock() | ||
| factory, exists := r.factories[schema] | ||
| defer r.mu.RUnlock() | ||
| if !exists { | ||
| return nil, nil, &UnknownSchemaError{Schema: schema} | ||
| } | ||
|
|
||
| return factory.CreateTemplate(renderBundle), replayReader, nil | ||
| } | ||
|
|
||
| func (r *registry) CreateTemplateByType(templateType string, renderBundle BundleRenderer) (Template, error) { | ||
| r.mu.RLock() | ||
| factory, exists := r.factories[templateType] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI, this change introduces a regression in Issue opened: |
||
| defer r.mu.RUnlock() | ||
| if !exists { | ||
| return nil, &UnknownSchemaError{Schema: templateType} | ||
| } | ||
|
|
||
| return factory.CreateTemplate(renderBundle), nil | ||
| } | ||
|
|
||
| // GetSupportedSchemas returns all supported schema identifiers | ||
| func (r *registry) GetSupportedSchemas() []string { | ||
| r.mu.RLock() | ||
| defer r.mu.RUnlock() | ||
| schemas := make([]string, 0, len(r.factories)) | ||
| for schema := range r.factories { | ||
| schemas = append(schemas, schema) | ||
| } | ||
| slices.Sort(schemas) | ||
| return schemas | ||
| } | ||
|
|
||
| // GetSupportedTypes returns all supported template types | ||
| // TODO: in future, might store the type separately from the schema | ||
| // right now it's just the last part of the schema string | ||
| func (r *registry) GetSupportedTypes() []string { | ||
| r.mu.RLock() | ||
| defer r.mu.RUnlock() | ||
| types := make([]string, 0, len(r.factories)) | ||
| for schema := range r.factories { | ||
| types = append(types, schema[strings.LastIndex(schema, ".")+1:]) | ||
| } | ||
| slices.Sort(types) | ||
| return types | ||
| } | ||
|
|
||
| func (r *registry) HasSchema(schema string) bool { | ||
| r.mu.RLock() | ||
| defer r.mu.RUnlock() | ||
| _, exists := r.factories[schema] | ||
| return exists | ||
| } | ||
|
|
||
| func (r *registry) HasType(templateType string) bool { | ||
| types := r.GetSupportedTypes() | ||
| return slices.Contains(types, templateType) | ||
| } | ||
|
|
||
| // UnknownSchemaError is returned when a schema is not recognized | ||
| type UnknownSchemaError struct { | ||
| Schema string | ||
| } | ||
|
|
||
| func (e *UnknownSchemaError) Error() string { | ||
| return "unknown template schema: " + e.Schema | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does it need to be public?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's used by the template converter to be able to make a basic template from a full FBC representation, so it does need to be public.
I'm sure that I could pivot this away, but the purpose of this PR was to change the functional flow of the CLI commands to be able to simplify them (not needing to always specify the template type) and I could follow up with such things in later efforts.