-
Notifications
You must be signed in to change notification settings - Fork 5
feat(runtime): add centralized runtime registration system #78
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
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 @@ | ||
| ../../skills/add-runtime |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| // Copyright 2026 Red Hat, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // Package runtimesetup provides centralized registration of all available runtime implementations. | ||
| package runtimesetup | ||
|
|
||
| import ( | ||
| "github.com/kortex-hub/kortex-cli/pkg/runtime" | ||
| "github.com/kortex-hub/kortex-cli/pkg/runtime/fake" | ||
| ) | ||
|
|
||
| // Registrar is an interface for types that can register runtimes. | ||
| // This is implemented by instances.Manager. | ||
| type Registrar interface { | ||
| RegisterRuntime(rt runtime.Runtime) error | ||
| } | ||
|
|
||
| // Available is an optional interface that runtimes can implement | ||
| // to report whether they are available in the current environment. | ||
| // | ||
| // This allows runtimes to check for: | ||
| // - Operating system compatibility | ||
| // - Required CLI tools or binaries | ||
| // - Configuration prerequisites | ||
| // - License or permission requirements | ||
| // | ||
| // Example implementation: | ||
| // | ||
| // type myRuntime struct {} | ||
| // | ||
| // func (r *myRuntime) Available() bool { | ||
| // // Check if required CLI tool exists | ||
| // _, err := exec.LookPath("my-tool") | ||
| // return err == nil | ||
| // } | ||
| type Available interface { | ||
| // Available returns true if the runtime is available in the current environment. | ||
| Available() bool | ||
| } | ||
|
|
||
| // runtimeFactory is a function that creates a new runtime instance. | ||
| type runtimeFactory func() runtime.Runtime | ||
|
|
||
| // availableRuntimes is the list of all runtimes that can be registered. | ||
| // Add new runtimes here to make them available for automatic registration. | ||
| var availableRuntimes = []runtimeFactory{ | ||
| fake.New, | ||
| } | ||
|
|
||
| // RegisterAll registers all available runtimes to the given registrar. | ||
| // It skips runtimes that implement the Available interface and report false. | ||
| // Returns an error if any runtime fails to register. | ||
| func RegisterAll(registrar Registrar) error { | ||
| return registerAllWithAvailable(registrar, availableRuntimes) | ||
| } | ||
|
|
||
| // registerAllWithAvailable registers the given runtimes to the registrar. | ||
| // It skips runtimes that implement the Available interface and report false. | ||
| // Returns an error if any runtime fails to register. | ||
| // This function is internal and used for testing with custom runtime lists. | ||
| func registerAllWithAvailable(registrar Registrar, factories []runtimeFactory) error { | ||
| for _, factory := range factories { | ||
| rt := factory() | ||
|
|
||
| // Skip runtimes that are not available in this environment | ||
| if avail, ok := rt.(Available); ok && !avail.Available() { | ||
| continue | ||
| } | ||
|
|
||
| if err := registrar.RegisterRuntime(rt); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
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,163 @@ | ||
| // Copyright 2026 Red Hat, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package runtimesetup | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/kortex-hub/kortex-cli/pkg/runtime" | ||
| ) | ||
|
|
||
| // fakeRegistrar is a test implementation of Registrar | ||
| type fakeRegistrar struct { | ||
| registered []runtime.Runtime | ||
| failNext bool | ||
| } | ||
|
|
||
| func (f *fakeRegistrar) RegisterRuntime(rt runtime.Runtime) error { | ||
| if f.failNext { | ||
| f.failNext = false | ||
| return runtime.ErrRuntimeNotFound // reusing an error for testing | ||
| } | ||
| f.registered = append(f.registered, rt) | ||
| return nil | ||
| } | ||
|
|
||
| // testRuntime is a simple test runtime implementation | ||
| type testRuntime struct { | ||
| runtimeType string | ||
| available bool | ||
| } | ||
|
|
||
| func (t *testRuntime) Type() string { return t.runtimeType } | ||
|
|
||
| func (t *testRuntime) Create(ctx context.Context, params runtime.CreateParams) (runtime.RuntimeInfo, error) { | ||
| return runtime.RuntimeInfo{}, fmt.Errorf("not implemented") | ||
| } | ||
|
|
||
| func (t *testRuntime) Start(ctx context.Context, id string) (runtime.RuntimeInfo, error) { | ||
| return runtime.RuntimeInfo{}, fmt.Errorf("not implemented") | ||
| } | ||
|
|
||
| func (t *testRuntime) Stop(ctx context.Context, id string) error { | ||
| return fmt.Errorf("not implemented") | ||
| } | ||
|
|
||
| func (t *testRuntime) Remove(ctx context.Context, id string) error { | ||
| return fmt.Errorf("not implemented") | ||
| } | ||
|
|
||
| func (t *testRuntime) Info(ctx context.Context, id string) (runtime.RuntimeInfo, error) { | ||
| return runtime.RuntimeInfo{}, fmt.Errorf("not implemented") | ||
| } | ||
|
|
||
| func (t *testRuntime) Available() bool { | ||
| return t.available | ||
| } | ||
|
|
||
| func TestRegisterAll(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| t.Run("registers all runtimes", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| registrar := &fakeRegistrar{} | ||
|
|
||
| // Create test runtimes | ||
| testFactories := []runtimeFactory{ | ||
| func() runtime.Runtime { return &testRuntime{runtimeType: "test1", available: true} }, | ||
| func() runtime.Runtime { return &testRuntime{runtimeType: "test2", available: true} }, | ||
| } | ||
|
|
||
| err := registerAllWithAvailable(registrar, testFactories) | ||
| if err != nil { | ||
| t.Fatalf("registerAllWithAvailable() failed: %v", err) | ||
| } | ||
|
|
||
| // We should have registered 2 test runtimes | ||
| if len(registrar.registered) != 2 { | ||
| t.Errorf("Expected 2 runtimes to be registered, got %d", len(registrar.registered)) | ||
| } | ||
|
|
||
| // Check that both types are present | ||
| types := make(map[string]bool) | ||
| for _, rt := range registrar.registered { | ||
| types[rt.Type()] = true | ||
| } | ||
|
|
||
| if !types["test1"] { | ||
| t.Error("Expected 'test1' runtime to be registered") | ||
| } | ||
| if !types["test2"] { | ||
| t.Error("Expected 'test2' runtime to be registered") | ||
| } | ||
| }) | ||
|
|
||
| t.Run("skips unavailable runtimes", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| registrar := &fakeRegistrar{} | ||
|
|
||
| // Create test runtimes with one unavailable | ||
| testFactories := []runtimeFactory{ | ||
| func() runtime.Runtime { return &testRuntime{runtimeType: "test1", available: true} }, | ||
| func() runtime.Runtime { return &testRuntime{runtimeType: "test2", available: false} }, | ||
| func() runtime.Runtime { return &testRuntime{runtimeType: "test3", available: true} }, | ||
| } | ||
|
|
||
| err := registerAllWithAvailable(registrar, testFactories) | ||
| if err != nil { | ||
| t.Fatalf("registerAllWithAvailable() failed: %v", err) | ||
| } | ||
|
|
||
| // We should have registered only 2 runtimes (test2 is unavailable) | ||
| if len(registrar.registered) != 2 { | ||
| t.Errorf("Expected 2 runtimes to be registered, got %d", len(registrar.registered)) | ||
| } | ||
|
|
||
| // Check that only available runtimes are present | ||
| types := make(map[string]bool) | ||
| for _, rt := range registrar.registered { | ||
| types[rt.Type()] = true | ||
| } | ||
|
|
||
| if !types["test1"] { | ||
| t.Error("Expected 'test1' runtime to be registered") | ||
| } | ||
| if types["test2"] { | ||
| t.Error("Did not expect 'test2' runtime to be registered (unavailable)") | ||
| } | ||
| if !types["test3"] { | ||
| t.Error("Expected 'test3' runtime to be registered") | ||
| } | ||
| }) | ||
|
|
||
| t.Run("returns error on registration failure", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| registrar := &fakeRegistrar{failNext: true} | ||
|
|
||
| testFactories := []runtimeFactory{ | ||
| func() runtime.Runtime { return &testRuntime{runtimeType: "test1", available: true} }, | ||
| } | ||
|
|
||
| err := registerAllWithAvailable(registrar, testFactories) | ||
| if err == nil { | ||
| t.Fatal("Expected error when registration fails, got nil") | ||
| } | ||
| }) | ||
| } |
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.