forked from containers/kubernetes-mcp-server
-
Notifications
You must be signed in to change notification settings - Fork 43
NE-2448: Add get_service_endpoints tool for NIDS troubleshooting #144
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
35336dd
NE-2448: implement get_service_endpoints tool
bentito a23c0d4
NE-2448: refactor to use EndpointSlice API
bentito b48a81b
NE-2448: isolate tests by using local scheme instead of global kubern…
bentito 4880422
Refactor to use DynamicClient and testify/suite
bentito c6f7aff
feat(netedge): highlight key fields and output yaml in get_service_en…
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,124 @@ | ||
| package netedge | ||
|
|
||
| import ( | ||
| "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/apis/meta/v1/unstructured" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| "k8s.io/utils/ptr" | ||
| "sigs.k8s.io/yaml" | ||
| ) | ||
|
|
||
| func initEndpoints() []api.ServerTool { | ||
| return []api.ServerTool{ | ||
| { | ||
| Tool: api.Tool{ | ||
| Name: "get_service_endpoints", | ||
| Description: "Return EndpointSlice objects for a Service to verify backend pod availability.", | ||
| InputSchema: &jsonschema.Schema{ | ||
| Type: "object", | ||
| Properties: map[string]*jsonschema.Schema{ | ||
| "namespace": { | ||
| Type: "string", | ||
| Description: "Service namespace", | ||
| }, | ||
| "service": { | ||
| Type: "string", | ||
| Description: "Service name", | ||
| }, | ||
| }, | ||
| Required: []string{"namespace", "service"}, | ||
| }, | ||
| Annotations: api.ToolAnnotations{ | ||
| Title: "Get Service Endpoints", | ||
| ReadOnlyHint: ptr.To(true), | ||
| DestructiveHint: ptr.To(false), | ||
| OpenWorldHint: ptr.To(true), | ||
| }, | ||
| }, | ||
| Handler: getServiceEndpoints, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func getServiceEndpoints(params api.ToolHandlerParams) (*api.ToolCallResult, error) { | ||
| namespace, err := api.RequiredString(params, "namespace") | ||
| if err != nil { | ||
| return api.NewToolCallResult("", err), nil | ||
| } | ||
| serviceName, err := api.RequiredString(params, "service") | ||
| if err != nil { | ||
| return api.NewToolCallResult("", err), nil | ||
| } | ||
|
|
||
| gvr := schema.GroupVersionResource{ | ||
| Group: "discovery.k8s.io", | ||
| Version: "v1", | ||
| Resource: "endpointslices", | ||
| } | ||
|
|
||
| // EndpointSlices are linked to a service via the "kubernetes.io/service-name" label | ||
| labelSelector := "kubernetes.io/service-name=" + serviceName | ||
|
|
||
| list, err := params.DynamicClient().Resource(gvr).Namespace(namespace).List(params.Context, metav1.ListOptions{ | ||
| LabelSelector: labelSelector, | ||
| }) | ||
| if err != nil { | ||
| return api.NewToolCallResult("", fmt.Errorf("failed to list EndpointSlices for service %s/%s: %w", namespace, serviceName, err)), nil | ||
| } | ||
|
|
||
| if len(list.Items) == 0 { | ||
| return api.NewToolCallResult("", fmt.Errorf("no EndpointSlices found for service %s/%s", namespace, serviceName)), nil | ||
| } | ||
|
|
||
| // Extract KeyFields from EndpointSlices | ||
| var keyFields []map[string]interface{} | ||
| for _, eps := range list.Items { | ||
| kf := map[string]interface{}{ | ||
| "Name": eps.GetName(), | ||
| "Namespace": eps.GetNamespace(), | ||
| } | ||
|
|
||
| if endpoints, found, err := unstructured.NestedSlice(eps.Object, "endpoints"); found && err == nil { | ||
| var addresses []string | ||
| var nodeNames []string | ||
| for _, epRaw := range endpoints { | ||
| if ep, ok := epRaw.(map[string]interface{}); ok { | ||
| if addrs, ok := ep["addresses"].([]interface{}); ok { | ||
| for _, a := range addrs { | ||
| if addrStr, ok := a.(string); ok { | ||
| addresses = append(addresses, addrStr) | ||
| } | ||
| } | ||
| } | ||
| if nodeName, ok := ep["nodeName"].(string); ok { | ||
| nodeNames = append(nodeNames, nodeName) | ||
| } | ||
| } | ||
| } | ||
| kf["Addresses"] = addresses | ||
| kf["NodeNames"] = nodeNames | ||
| } | ||
|
|
||
| if ports, found, err := unstructured.NestedSlice(eps.Object, "ports"); found && err == nil { | ||
| kf["Ports"] = ports | ||
| } | ||
|
|
||
| keyFields = append(keyFields, kf) | ||
| } | ||
|
|
||
| resultObj := map[string]interface{}{ | ||
| "KeyFields": keyFields, | ||
| "RawEndpointSlices": list.Items, | ||
| } | ||
|
|
||
| data, err := yaml.Marshal(resultObj) | ||
| if err != nil { | ||
| return api.NewToolCallResult("", fmt.Errorf("failed to marshal endpoint slices as yaml: %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,126 @@ | ||
| package netedge | ||
|
|
||
| import ( | ||
| discoveryv1 "k8s.io/api/discovery/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/client-go/dynamic/fake" | ||
| clientgoscheme "k8s.io/client-go/kubernetes/scheme" | ||
| "k8s.io/utils/ptr" | ||
| "sigs.k8s.io/yaml" | ||
| ) | ||
|
|
||
| func (s *NetEdgeTestSuite) TestGetServiceEndpoints() { | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| namespace string | ||
| service string | ||
| existingObjs []runtime.Object | ||
| expectedError string | ||
| validate func(result string) | ||
| }{ | ||
| { | ||
| name: "successful retrieval", | ||
| namespace: "default", | ||
| service: "myservice", | ||
| existingObjs: []runtime.Object{ | ||
| &discoveryv1.EndpointSlice{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "myservice-1", | ||
| Namespace: "default", | ||
| Labels: map[string]string{ | ||
| "kubernetes.io/service-name": "myservice", | ||
| }, | ||
| }, | ||
| Endpoints: []discoveryv1.Endpoint{ | ||
| { | ||
| Addresses: []string{"1.2.3.4"}, | ||
| }, | ||
| }, | ||
| Ports: []discoveryv1.EndpointPort{ | ||
| { | ||
| Port: ptr.To(int32(80)), | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| validate: func(result string) { | ||
| var r map[string]interface{} | ||
| err := yaml.Unmarshal([]byte(result), &r) | ||
| s.Require().NoError(err) | ||
|
|
||
| raw := r["RawEndpointSlices"].([]interface{}) | ||
| s.Assert().Len(raw, 1) | ||
|
|
||
| keyFields := r["KeyFields"].([]interface{}) | ||
| s.Assert().Len(keyFields, 1) | ||
|
|
||
| firstRaw := raw[0].(map[string]interface{}) | ||
| firstKF := keyFields[0].(map[string]interface{}) | ||
|
|
||
| s.Assert().Equal("myservice-1", firstRaw["metadata"].(map[string]interface{})["name"]) | ||
| s.Assert().Equal("myservice-1", firstKF["Name"]) | ||
| }, | ||
| }, | ||
| { | ||
| name: "endpoints not found", | ||
| namespace: "default", | ||
| service: "missing", | ||
| existingObjs: []runtime.Object{}, | ||
| expectedError: "no EndpointSlices found", | ||
| }, | ||
| { | ||
| name: "missing arguments", | ||
| namespace: "", | ||
| service: "", | ||
| expectedError: "parameter required", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| s.Run(tt.name, func() { | ||
| // Create fake dynamic client | ||
| scheme := runtime.NewScheme() | ||
| err := clientgoscheme.AddToScheme(scheme) | ||
| s.Require().NoError(err) | ||
| err = discoveryv1.AddToScheme(scheme) | ||
| s.Require().NoError(err) | ||
| dynClient := fake.NewSimpleDynamicClient(scheme, tt.existingObjs...) | ||
|
|
||
| // Create mock params | ||
| args := make(map[string]any) | ||
| if tt.namespace != "" { | ||
| args["namespace"] = tt.namespace | ||
| } | ||
| if tt.service != "" { | ||
| args["service"] = tt.service | ||
| } | ||
|
|
||
| s.SetArgs(args) | ||
| s.SetDynamicClient(dynClient) | ||
|
|
||
| result, err := getServiceEndpoints(s.params) | ||
|
|
||
| // If the handler returns an error in ToolCallResult (which is mostly what it does for logic errors), | ||
| // result.Error will be set. `err` return is usually nil unless panic/protocol error. | ||
|
|
||
| // However, our handler returns `api.NewToolCallResult("", err)`. | ||
| // So we check result.Error. | ||
|
|
||
| if tt.expectedError != "" { | ||
| s.Assert().NoError(err) // The handler doesn't return Go error | ||
| 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,66 @@ | ||
| 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" | ||
| ) | ||
|
|
||
| // mockKubernetesClient implements api.KubernetesClient for testing | ||
| 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 | ||
| } | ||
|
|
||
| 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 | ||
| s.params.KubernetesClient = s.mockClient | ||
| } | ||
|
|
||
| func TestNetEdgeSuite(t *testing.T) { | ||
| suite.Run(t, new(NetEdgeTestSuite)) | ||
| } | ||
|
|
||
| type mockToolCallRequest struct { | ||
| args map[string]any | ||
| } | ||
|
|
||
| func (m *mockToolCallRequest) GetArguments() map[string]any { | ||
| return m.args | ||
| } |
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.