-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added: generic get and list tools for any k8s resource
- Loading branch information
Showing
25 changed files
with
722 additions
and
507 deletions.
There are no files selected for viewing
This file contains 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 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,30 @@ | ||
package deployment | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/strowk/foxy-contexts/pkg/mcp" | ||
"github.com/strowk/mcp-k8s-go/internal/content" | ||
"github.com/strowk/mcp-k8s-go/internal/utils" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
func GetDeployment(clientset kubernetes.Interface, namespace string, name string) *mcp.CallToolResult { | ||
deployment, err := clientset.AppsV1().Deployments(namespace).Get(context.Background(), name, metav1.GetOptions{}) | ||
if err != nil { | ||
return utils.ErrResponse(err) | ||
} | ||
utils.SanitizeObjectMeta(&deployment.ObjectMeta) | ||
|
||
c, err := content.NewJsonContent(deployment) | ||
if err != nil { | ||
return utils.ErrResponse(err) | ||
} | ||
return &mcp.CallToolResult{ | ||
Meta: map[string]interface{}{}, | ||
Content: []interface{}{c}, | ||
IsError: utils.Ptr(false), | ||
} | ||
} |
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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,84 @@ | ||
package deployment | ||
|
||
import ( | ||
"context" | ||
"sort" | ||
"time" | ||
|
||
"github.com/strowk/foxy-contexts/pkg/mcp" | ||
"github.com/strowk/mcp-k8s-go/internal/content" | ||
"github.com/strowk/mcp-k8s-go/internal/utils" | ||
|
||
appsv1 "k8s.io/api/apps/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
// DeploymentInList provides a structured representation of Deployment information | ||
type DeploymentInList struct { | ||
Name string `json:"name"` | ||
Namespace string `json:"namespace"` | ||
Age string `json:"age"` | ||
DesiredReplicas int `json:"desired_replicas"` | ||
ReadyReplicas int `json:"ready_replicas"` | ||
UpdatedReplicas int `json:"updated_replicas"` | ||
AvailableReplicas int `json:"available_replicas"` | ||
CreatedAt string `json:"created_at"` | ||
} | ||
|
||
func ListDeployments(clientset kubernetes.Interface, namespace string) *mcp.CallToolResult { | ||
var deployments *appsv1.DeploymentList | ||
var err error | ||
if namespace == "" { | ||
// List Deployments from all namespaces | ||
deployments, err = clientset.AppsV1().Deployments(metav1.NamespaceAll).List(context.Background(), metav1.ListOptions{}) | ||
} else { | ||
// List Deployments from specific namespace | ||
deployments, err = clientset.AppsV1().Deployments(namespace).List(context.Background(), metav1.ListOptions{}) | ||
} | ||
|
||
if err != nil { | ||
return utils.ErrResponse(err) | ||
} | ||
|
||
sort.Slice(deployments.Items, func(i, j int) bool { | ||
// Sort by namespace, then by name | ||
if deployments.Items[i].Namespace == deployments.Items[j].Namespace { | ||
return deployments.Items[i].Name < deployments.Items[j].Name | ||
} | ||
return deployments.Items[i].Namespace < deployments.Items[j].Namespace | ||
}) | ||
|
||
var contents []interface{} = make([]interface{}, len(deployments.Items)) | ||
for i, deployment := range deployments.Items { | ||
// Calculate age | ||
age := time.Since(deployment.CreationTimestamp.Time) | ||
|
||
// Extract deployment status information | ||
desiredReplicas := int(*(deployment.Spec.Replicas)) | ||
readyReplicas := deployment.Status.ReadyReplicas | ||
updatedReplicas := deployment.Status.UpdatedReplicas | ||
availableReplicas := deployment.Status.AvailableReplicas | ||
|
||
content, err := content.NewJsonContent(DeploymentInList{ | ||
Name: deployment.Name, | ||
Namespace: deployment.Namespace, | ||
Age: utils.FormatAge(age), | ||
DesiredReplicas: desiredReplicas, | ||
ReadyReplicas: int(readyReplicas), | ||
UpdatedReplicas: int(updatedReplicas), | ||
AvailableReplicas: int(availableReplicas), | ||
CreatedAt: deployment.CreationTimestamp.Format(time.RFC3339), | ||
}) | ||
if err != nil { | ||
return utils.ErrResponse(err) | ||
} | ||
contents[i] = content | ||
} | ||
|
||
return &mcp.CallToolResult{ | ||
Meta: map[string]interface{}{}, | ||
Content: contents, | ||
IsError: utils.Ptr(false), | ||
} | ||
} |
This file contains 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
Oops, something went wrong.