forked from containers/kubernetes-mcp-server
-
Notifications
You must be signed in to change notification settings - Fork 32
NE-2447: Implement inspect_route tool #147
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
Open
bentito
wants to merge
5
commits into
openshift:main
Choose a base branch
from
bentito:feat/NE-2447-inspect-route
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
12a1bd0
NE-2447: Implement inspect_route tool
bentito af6aa96
Register inspect_route tool in toolset
bentito 62f6441
Fix lint and test errors by refactoring mocks
bentito e8f5c9c
Address PR comments: Use DynamicClient and update description
bentito 0d73d2e
Refactor netedge tests to use testify/suite
bentito 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package netedge | ||
|
|
||
| import ( | ||
| "github.com/containers/kubernetes-mcp-server/pkg/api" | ||
| "k8s.io/client-go/dynamic" | ||
| "k8s.io/client-go/rest" | ||
| ) | ||
|
|
||
| // Mock implementations | ||
| type mockToolCallRequest struct { | ||
| args map[string]interface{} | ||
| } | ||
|
|
||
| func (m *mockToolCallRequest) GetArguments() map[string]interface{} { | ||
| return m.args | ||
| } | ||
|
|
||
| func (m *mockToolCallRequest) GetName() string { | ||
| return "mock_tool" | ||
| } | ||
|
|
||
| type mockKubernetesClient struct { | ||
| api.KubernetesClient | ||
| restConfig *rest.Config | ||
| dynamicClient dynamic.Interface | ||
| } | ||
|
|
||
| func (m *mockKubernetesClient) RESTConfig() *rest.Config { | ||
| return m.restConfig | ||
| } | ||
|
|
||
| func (m *mockKubernetesClient) DynamicClient() dynamic.Interface { | ||
| return m.dynamicClient | ||
| } |
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,73 @@ | ||
| package netedge | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
|
|
||
| "github.com/containers/kubernetes-mcp-server/pkg/api" | ||
| "github.com/google/jsonschema-go/jsonschema" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| "k8s.io/utils/ptr" | ||
| ) | ||
|
|
||
| func initRoutes() []api.ServerTool { | ||
| return []api.ServerTool{ | ||
| { | ||
| Tool: api.Tool{ | ||
| Name: "inspect_route", | ||
| Description: "Inspect an OpenShift Route to view its full configuration and status.", | ||
| InputSchema: &jsonschema.Schema{ | ||
| Type: "object", | ||
| Properties: map[string]*jsonschema.Schema{ | ||
| "namespace": { | ||
| Type: "string", | ||
| Description: "Route namespace", | ||
| }, | ||
| "route": { | ||
| Type: "string", | ||
| Description: "Route name", | ||
| }, | ||
| }, | ||
| Required: []string{"namespace", "route"}, | ||
| }, | ||
| Annotations: api.ToolAnnotations{ | ||
| Title: "Inspect Route", | ||
| ReadOnlyHint: ptr.To(true), | ||
| DestructiveHint: ptr.To(false), | ||
| OpenWorldHint: ptr.To(true), | ||
| }, | ||
| }, | ||
| Handler: inspectRoute, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func inspectRoute(params api.ToolHandlerParams) (*api.ToolCallResult, error) { | ||
| namespace, err := api.RequiredString(params, "namespace") | ||
| if err != nil { | ||
| return api.NewToolCallResult("", err), nil | ||
| } | ||
| routeName, err := api.RequiredString(params, "route") | ||
| if err != nil { | ||
| return api.NewToolCallResult("", err), nil | ||
| } | ||
|
|
||
| gvr := schema.GroupVersionResource{ | ||
| Group: "route.openshift.io", | ||
| Version: "v1", | ||
| Resource: "routes", | ||
| } | ||
|
|
||
| route, err := params.DynamicClient().Resource(gvr).Namespace(namespace).Get(params.Context, routeName, metav1.GetOptions{}) | ||
| if err != nil { | ||
| return api.NewToolCallResult("", fmt.Errorf("failed to get route %s/%s: %w", namespace, routeName, err)), nil | ||
| } | ||
|
|
||
| data, err := json.MarshalIndent(route.Object, "", " ") | ||
| if err != nil { | ||
| return api.NewToolCallResult("", fmt.Errorf("failed to marshal route: %w", err)), nil | ||
| } | ||
|
|
||
| return api.NewToolCallResult(string(data), nil), 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,99 @@ | ||
| package netedge | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
|
|
||
| "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/client-go/dynamic/fake" | ||
| ) | ||
|
|
||
| func (s *NetEdgeTestSuite) TestInspectRoute() { | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| namespace string | ||
| route string | ||
| existingObjs []runtime.Object | ||
| expectedError string | ||
| validate func(result string) | ||
| }{ | ||
| { | ||
| name: "successful retrieval", | ||
| namespace: "default", | ||
| route: "my-route", | ||
| existingObjs: []runtime.Object{ | ||
| &unstructured.Unstructured{ | ||
| Object: map[string]interface{}{ | ||
| "apiVersion": "route.openshift.io/v1", | ||
| "kind": "Route", | ||
| "metadata": map[string]interface{}{ | ||
| "name": "my-route", | ||
| "namespace": "default", | ||
| }, | ||
| "spec": map[string]interface{}{ | ||
| "host": "example.com", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| validate: func(result string) { | ||
| var r map[string]interface{} | ||
| err := json.Unmarshal([]byte(result), &r) | ||
| s.Require().NoError(err) | ||
| s.Assert().Equal("my-route", r["metadata"].(map[string]interface{})["name"]) | ||
| s.Assert().Equal("example.com", r["spec"].(map[string]interface{})["host"]) | ||
| }, | ||
| }, | ||
| { | ||
| name: "route not found", | ||
| namespace: "default", | ||
| route: "missing", | ||
| existingObjs: []runtime.Object{}, | ||
| expectedError: "failed to get route", | ||
| }, | ||
| { | ||
| name: "missing arguments", | ||
| namespace: "", | ||
| route: "", | ||
| expectedError: "parameter required", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| s.Run(tt.name, func() { | ||
| // Create fake dynamic client | ||
| scheme := runtime.NewScheme() | ||
| dynClient := fake.NewSimpleDynamicClient(scheme, tt.existingObjs...) | ||
|
|
||
| // Create mock params | ||
| args := make(map[string]any) | ||
| if tt.namespace != "" { | ||
| args["namespace"] = tt.namespace | ||
| } | ||
| if tt.route != "" { | ||
| args["route"] = tt.route | ||
| } | ||
|
|
||
| // Set args using suite helper | ||
| s.SetArgs(args) | ||
| s.SetDynamicClient(dynClient) | ||
|
|
||
| result, err := inspectRoute(s.params) | ||
|
|
||
| if tt.expectedError != "" { | ||
| s.Assert().NoError(err) | ||
| s.Require().NotNil(result) | ||
| s.Require().Error(result.Error) | ||
| s.Assert().Contains(result.Error.Error(), tt.expectedError) | ||
| } else { | ||
| s.Assert().NoError(err) | ||
| s.Require().NotNil(result) | ||
| s.Assert().NoError(result.Error) | ||
| if tt.validate != nil { | ||
| tt.validate(result.Content) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } |
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,42 @@ | ||
| package netedge | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/containers/kubernetes-mcp-server/pkg/api" | ||
| "github.com/stretchr/testify/suite" | ||
| "k8s.io/client-go/dynamic" | ||
| "k8s.io/client-go/rest" | ||
| ) | ||
|
|
||
| type NetEdgeTestSuite struct { | ||
| suite.Suite | ||
| params api.ToolHandlerParams | ||
| mockReq *mockToolCallRequest | ||
| mockClient *mockKubernetesClient | ||
| } | ||
|
|
||
| func (s *NetEdgeTestSuite) SetupTest() { | ||
| s.mockReq = &mockToolCallRequest{args: make(map[string]interface{})} | ||
| s.mockClient = &mockKubernetesClient{ | ||
| restConfig: &rest.Config{}, | ||
| } | ||
| s.params = api.ToolHandlerParams{ | ||
| Context: context.Background(), | ||
| ToolCallRequest: s.mockReq, | ||
| KubernetesClient: s.mockClient, | ||
| } | ||
| } | ||
|
|
||
| func (s *NetEdgeTestSuite) SetArgs(args map[string]interface{}) { | ||
| s.mockReq.args = args | ||
| } | ||
|
|
||
| func (s *NetEdgeTestSuite) SetDynamicClient(dynClient dynamic.Interface) { | ||
| s.mockClient.dynamicClient = dynClient | ||
| } | ||
|
|
||
| func TestNetEdgeSuite(t *testing.T) { | ||
| suite.Run(t, new(NetEdgeTestSuite)) | ||
| } |
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
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.
could we do grouping via
testify/suite?e.g. similar to requested here: #131 (comment) ?
I guess we can also do that in a follow up PR, for all
netedgetests.