66 "github.com/mark3labs/mcp-go/mcp"
77 "github.com/mark3labs/mcp-go/server"
88 "github.com/spf13/afero"
9+ corev1 "k8s.io/api/core/v1"
10+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
911 "k8s.io/client-go/kubernetes"
1012 "k8s.io/client-go/rest"
1113 "k8s.io/client-go/tools/clientcmd"
@@ -55,6 +57,8 @@ func TestMain(m *testing.M) {
5557 BinaryAssetsDirectory : filepath .Join (envTestDir , "k8s" , versionDir ),
5658 }
5759 envTestRestConfig , _ = envTest .Start ()
60+ kc , _ := kubernetes .NewForConfig (envTestRestConfig )
61+ createTestData (context .Background (), kc )
5862
5963 // Test!
6064 code := m .Run ()
@@ -111,6 +115,7 @@ func testCase(t *testing.T, test func(c *mcpContext)) {
111115 test (mcpCtx )
112116}
113117
118+ // withKubeConfig sets up a fake kubeconfig in the temp directory based on the provided rest.Config
114119func (c * mcpContext ) withKubeConfig (rc * rest.Config ) * api.Config {
115120 fakeConfig := api .NewConfig ()
116121 fakeConfig .CurrentContext = "fake-context"
@@ -132,10 +137,12 @@ func (c *mcpContext) withKubeConfig(rc *rest.Config) *api.Config {
132137 return fakeConfig
133138}
134139
140+ // withEnvTest sets up the environment for kubeconfig to be used with envTest
135141func (c * mcpContext ) withEnvTest () {
136142 c .withKubeConfig (envTestRestConfig )
137143}
138144
145+ // newKubernetesClient creates a new Kubernetes client with the current kubeconfig
139146func (c * mcpContext ) newKubernetesClient () * kubernetes.Clientset {
140147 c .withEnvTest ()
141148 pathOptions := clientcmd .NewDefaultPathOptions ()
@@ -147,9 +154,44 @@ func (c *mcpContext) newKubernetesClient() *kubernetes.Clientset {
147154 return kubernetesClient
148155}
149156
157+ // callTool helper function to call a tool by name with arguments
150158func (c * mcpContext ) callTool (name string , args map [string ]interface {}) (* mcp.CallToolResult , error ) {
151159 callToolRequest := mcp.CallToolRequest {}
152160 callToolRequest .Params .Name = name
153161 callToolRequest .Params .Arguments = args
154162 return c .mcpClient .CallTool (c .ctx , callToolRequest )
155163}
164+
165+ func createTestData (ctx context.Context , kc * kubernetes.Clientset ) {
166+ _ , _ = kc .CoreV1 ().Namespaces ().
167+ Create (ctx , & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : "ns-1" }}, metav1.CreateOptions {})
168+ _ , _ = kc .CoreV1 ().Namespaces ().
169+ Create (ctx , & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : "ns-2" }}, metav1.CreateOptions {})
170+ _ , _ = kc .CoreV1 ().Pods ("default" ).
171+ Create (ctx , & corev1.Pod {
172+ ObjectMeta : metav1.ObjectMeta {Name : "a-pod-in-default" },
173+ Spec : corev1.PodSpec {
174+ Containers : []corev1.Container {
175+ {Name : "nginx" , Image : "nginx" },
176+ },
177+ },
178+ }, metav1.CreateOptions {})
179+ _ , _ = kc .CoreV1 ().Pods ("ns-1" ).
180+ Create (ctx , & corev1.Pod {
181+ ObjectMeta : metav1.ObjectMeta {Name : "a-pod-in-ns-1" },
182+ Spec : corev1.PodSpec {
183+ Containers : []corev1.Container {
184+ {Name : "nginx" , Image : "nginx" },
185+ },
186+ },
187+ }, metav1.CreateOptions {})
188+ _ , _ = kc .CoreV1 ().Pods ("ns-2" ).
189+ Create (ctx , & corev1.Pod {
190+ ObjectMeta : metav1.ObjectMeta {Name : "a-pod-in-ns-2" },
191+ Spec : corev1.PodSpec {
192+ Containers : []corev1.Container {
193+ {Name : "nginx" , Image : "nginx" },
194+ },
195+ },
196+ }, metav1.CreateOptions {})
197+ }
0 commit comments