feat: add operations for kubeconfig - #15
Conversation
📝 WalkthroughWalkthroughAdds 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
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
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
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 }
6f832ce to
97fa9ed
Compare
There was a problem hiding this comment.
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.
Signed-off-by: Matthias Wessendorf <mwessend@redhat.com>
97fa9ed to
140a323
Compare
This PR adds operations for for checking on
kubeconfigSummary by CodeRabbit
New Features
Documentation
Tests