Skip to content

feat: add operations for kubeconfig - #15

Merged
Cali0707 merged 1 commit into
mcpchecker:mainfrom
matzew:add_extension_config
Feb 3, 2026
Merged

feat: add operations for kubeconfig#15
Cali0707 merged 1 commit into
mcpchecker:mainfrom
matzew:add_extension_config

Conversation

@matzew

@matzew matzew commented Feb 3, 2026

Copy link
Copy Markdown
Contributor

This PR adds operations for for checking on kubeconfig

Summary by CodeRabbit

  • New Features

    • Added three kubeconfig operations: list contexts (with details), get current context, and view kubeconfig as YAML (optional minify).
  • Documentation

    • Updated operation docs with usage examples, input schemas, and output definitions for the new kubeconfig operations.
  • Tests

    • Added unit tests covering listing, current-context retrieval, and view-config behaviors across success and error scenarios.

@coderabbitai

coderabbitai Bot commented Feb 3, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

Adds kubeconfig inspection features to the Kubernetes extension: three new operations (kubernetes.listContexts, kubernetes.getCurrentContext, kubernetes.viewConfig), corresponding ResourceClient methods and dynamicClientAdapter field, extension handlers, unit tests, mock hooks, and README docs updates.

Changes

Cohort / File(s) Summary
Interface & Client Layer
pkg/extension/client.go
Extended ResourceClient with ListContexts, GetCurrentContext, ViewConfig; added kubeconfigPath to dynamicClientAdapter and implemented these methods (load kubeconfig, enumerate contexts, return YAML, support minify).
Extension Init & Ops Registry
pkg/extension/extension.go, pkg/extension/operations.go
Set kubeconfigPath when constructing dynamicClientAdapter; registered three new operations (listContexts, getCurrentContext, viewConfig) with input schemas and handlers.
Kubeconfig Handlers
pkg/extension/kubeconfig.go
Added public ContextInfo type and three Extension handlers: handleListContexts, handleGetCurrentContext, handleViewConfig (validate client, call client methods, log, return structured outputs).
Tests & Mocks
pkg/extension/kubeconfig_test.go, pkg/extension/mock_client_test.go
New unit tests for each handler covering success and failure scenarios; extended mockClient with hookable ListContexts, GetCurrentContext, ViewConfig methods and function fields.
Documentation
README.md
Added three Kubernetes extension operations to the Operations table with YAML usage examples, inputs, and outputs.

Sequence Diagram(s)

sequenceDiagram
    participant User as "User"
    participant Ext as "Extension"
    participant Handler as "Handler"
    participant Client as "ResourceClient"
    participant Kube as "Kubeconfig (file/API)"

    User->>Ext: invoke operation (listContexts / getCurrentContext / viewConfig)
    Ext->>Handler: route to handler
    Handler->>Handler: validate client, parse args
    Handler->>Client: call ListContexts / GetCurrentContext / ViewConfig
    Client->>Kube: load kubeconfig (kubeconfigPath) & perform operation
    Kube-->>Client: return contexts / current context / YAML
    Client-->>Handler: return result
    Handler-->>Ext: build OperationResult (outputs)
    Ext-->>User: return success/failure
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • Cali0707

Poem

🐰 I hop through kubeconfigs by moonlight,

I list the contexts, point the right,
The current name I proudly sing,
I show the YAML, trimmed or big—
A tiny rabbit, Kubernetes spring.

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and concisely summarizes the main change: adding three new kubeconfig-related operations (listContexts, getCurrentContext, viewConfig) to the extension.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

🤖 Fix all issues with AI agents
In `@pkg/extension/client.go`:
- Line 127: The code currently force-casts
NewNonInteractiveDeferredLoadingClientConfig(...) to
*clientcmd.DirectClientConfig which can panic; instead stop the unsafe type
assertion and assign the result to the clientcmd.ClientConfig interface (i.e.,
treat the return of NewNonInteractiveDeferredLoadingClientConfig as a
clientcmd.ClientConfig). Update the variable declaration/assignment for config
so it uses the clientcmd.ClientConfig type and remove the
(*clientcmd.DirectClientConfig) cast; ensure callers that relied on
DirectClientConfig methods are adjusted to use the interface methods or perform
a safe type switch where absolutely necessary (referencing
NewNonInteractiveDeferredLoadingClientConfig and the config variable).
- Around line 120-141: The minify branch in dynamicClientAdapter.ViewConfig
loads the full config but never calls clientcmd.MinifyConfig, so it doesn't
strip unused contexts/clusters/users; fix by calling
clientcmd.MinifyConfig(&rawConfig) (pass the address of the returned api.Config)
immediately after obtaining rawConfig from config.RawConfig(), then proceed to
clientcmd.Write on the minified config so the returned YAML is actually
minified.

In `@pkg/extension/kubeconfig.go`:
- Around line 40-50: The loop variable `ctx` in the contexts iteration is
shadowing the function parameter `ctx context.Context`, causing e.LogInfo to
receive a ContextInfo instead of the context.Context; rename the loop variable
(e.g., to `ci`, `ctxInfo`, or `c`) in the for _, ... loop that iterates over
`contexts` (and update any references like `ctx.IsCurrent` and `ctx.Name` to the
new name) so that the function parameter `ctx` remains the context passed to
e.LogInfo(ctx, ...).
- Line 54: The loop in the contextNames logic is shadowing the function
parameter named ctx by declaring for _, ctx := range contexts; rename the loop
variable (e.g., to c or ctxInfo) and update all references inside that loop (any
uses of ctx within the loop body) to the new name to avoid variable shadowing
and preserve the outer ctx parameter.
- Around line 52-56: Remove the unused contextNames slice: delete the
declaration "contextNames := make([]string, 0, len(contexts))" and the for-loop
that appends ctx.Name (referencing contextNames and contexts) so no unused
variable remains; if you intended to return context names instead, modify the
function's output type to include a contexts field and populate that with the
extracted names rather than leaving contextNames unused.
🧹 Nitpick comments (1)
pkg/extension/client.go (1)

91-109: Non-deterministic context ordering due to map iteration.

Iterating over config.Contexts (a Go map) yields non-deterministic order. If consistent ordering is expected by consumers (e.g., for display or testing), consider sorting by name.

♻️ Proposed fix to ensure consistent ordering
+import "sort"
+
 func (a *dynamicClientAdapter) ListContexts(ctx context.Context) ([]ContextInfo, error) {
 	config, err := clientcmd.LoadFromFile(a.kubeconfigPath)
 	if err != nil {
 		return nil, fmt.Errorf("failed to load kubeconfig: %w", err)
 	}
 
 	var contexts []ContextInfo
 	for name, context := range config.Contexts {
 		contexts = append(contexts, ContextInfo{
 			Name:      name,
 			Cluster:   context.Cluster,
 			User:      context.AuthInfo,
 			Namespace: context.Namespace,
 			IsCurrent: name == config.CurrentContext,
 		})
 	}
 
+	sort.Slice(contexts, func(i, j int) bool {
+		return contexts[i].Name < contexts[j].Name
+	})
+
 	return contexts, nil
 }

Comment thread pkg/extension/client.go Outdated
Comment thread pkg/extension/client.go Outdated
Comment thread pkg/extension/kubeconfig.go Outdated
Comment thread pkg/extension/kubeconfig.go Outdated
Comment thread pkg/extension/kubeconfig.go Outdated
@matzew
matzew force-pushed the add_extension_config branch from 6f832ce to 97fa9ed Compare February 3, 2026 12:49

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@pkg/extension/client.go`:
- Around line 137-169: The minify branch currently drops referenced dependencies
silently; update the minify logic so that after resolving currentContext and
currentCtx you validate that rawConfig.Clusters[currentCtx.Cluster] and
rawConfig.AuthInfos[currentCtx.AuthInfo] exist and return explicit errors (e.g.,
fmt.Errorf("referenced cluster %q not found", currentCtx.Cluster) /
fmt.Errorf("referenced authInfo %q not found", currentCtx.AuthInfo)) instead of
silently omitting them; modify the block that sets minifiedConfig.Clusters and
minifiedConfig.AuthInfos to fail fast when those lookups return false so the
resulting clientcmdapi.NewConfig() always contains the declared dependencies.

Comment thread pkg/extension/client.go
Signed-off-by: Matthias Wessendorf <mwessend@redhat.com>
@matzew
matzew force-pushed the add_extension_config branch from 97fa9ed to 140a323 Compare February 3, 2026 12:58
@Cali0707
Cali0707 merged commit 0a8609c into mcpchecker:main Feb 3, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants