Skip to content

Commit d593f30

Browse files
authored
Merge pull request #62 from Azure/fix-resource-param
fix: improve resource parameter handling for kubectl commands
2 parents 9c8a3cc + c5f08a2 commit d593f30

File tree

4 files changed

+79
-13
lines changed

4 files changed

+79
-13
lines changed

pkg/kubectl/kubectl_executor.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,18 @@ func (e *KubectlToolExecutor) buildCommand(kubectlCommand, resource, args string
222222
// Standard case: command + resource + args
223223
parts := []string{kubectlCommand}
224224

225-
// Add resource if not empty, except for exec and cp commands which pass resource info in args
226-
if resource != "" && kubectlCommand != "exec" && kubectlCommand != "cp" {
225+
// Skip the resource parameter for certain commands
226+
skipResourceCommands := []string{"exec", "cp", "events", "cluster-info", "api-resources", "api-versions", "diff", "run", "logs"}
227+
shouldSkipResource := false
228+
for _, cmd := range skipResourceCommands {
229+
if kubectlCommand == cmd {
230+
shouldSkipResource = true
231+
break
232+
}
233+
}
234+
235+
// Also skip resource if it's empty (file-based operations like create -f, apply -f, etc.)
236+
if resource != "" && !shouldSkipResource {
227237
parts = append(parts, resource)
228238
}
229239

pkg/kubectl/kubectl_executor_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,62 @@ func TestKubectlToolExecutor_BuildCommand(t *testing.T) {
220220
args: "/tmp/foo_dir some-pod:/tmp/bar_dir",
221221
want: "cp /tmp/foo_dir some-pod:/tmp/bar_dir",
222222
},
223+
{
224+
name: "events command with empty resource",
225+
kubectlCommand: "events",
226+
resource: "",
227+
args: "--all-namespaces",
228+
want: "events --all-namespaces",
229+
},
230+
{
231+
name: "api-resources command with empty resource",
232+
kubectlCommand: "api-resources",
233+
resource: "",
234+
args: "--namespaced=true",
235+
want: "api-resources --namespaced=true",
236+
},
237+
{
238+
name: "api-versions command with empty resource",
239+
kubectlCommand: "api-versions",
240+
resource: "",
241+
args: "",
242+
want: "api-versions",
243+
},
244+
{
245+
name: "run command with empty resource",
246+
kubectlCommand: "run",
247+
resource: "",
248+
args: "nginx --image=nginx",
249+
want: "run nginx --image=nginx",
250+
},
251+
{
252+
name: "diff command with empty resource",
253+
kubectlCommand: "diff",
254+
resource: "",
255+
args: "-f deployment.yaml",
256+
want: "diff -f deployment.yaml",
257+
},
258+
{
259+
name: "logs command with empty resource",
260+
kubectlCommand: "logs",
261+
resource: "",
262+
args: "nginx -c ruby-container",
263+
want: "logs nginx -c ruby-container",
264+
},
265+
{
266+
name: "logs command with selector",
267+
kubectlCommand: "logs",
268+
resource: "",
269+
args: "-l app=nginx --all-containers=true",
270+
want: "logs -l app=nginx --all-containers=true",
271+
},
272+
{
273+
name: "top command with resource subcommand",
274+
kubectlCommand: "top",
275+
resource: "pod",
276+
args: "",
277+
want: "top pod",
278+
},
223279
}
224280

225281
for _, tt := range tests {

pkg/kubectl/registry.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ Examples:
186186
),
187187
mcp.WithString("resource",
188188
mcp.Required(),
189-
mcp.Description("The resource type (e.g., pods, deployments, services) or empty for file-based operations"),
189+
mcp.Description("The resource type (e.g., pods, deployments, services) or empty string '' for file-based operations (create -f, apply -f, patch -f, replace -f, delete -f)"),
190190
),
191191
mcp.WithString("args",
192192
mcp.Required(),
@@ -229,7 +229,7 @@ Examples:
229229
),
230230
mcp.WithString("resource",
231231
mcp.Required(),
232-
mcp.Description("The resource type for expose/scale/autoscale, subcommand for rollout, or empty for run"),
232+
mcp.Description("The resource type for expose/scale/autoscale, subcommand for rollout, or empty string '' for run operation"),
233233
),
234234
mcp.WithString("args",
235235
mcp.Required(),
@@ -285,11 +285,11 @@ Available operations:
285285
- cp: Copy files to/from containers
286286
287287
Examples:
288-
- Logs for default container: operation='logs', resource='pod', args='nginx'
289-
- Logs for specific container: operation='logs', resource='pod', args='nginx -c ruby-container'
290-
- Logs with selector: operation='logs', resource='pod', args='-l app=nginx --all-containers=true'
291-
- Get events: operation='events', resource='events', args='--all-namespaces'
292-
- Get events namespace: operation='events', resource='events', args='-n default'
288+
- Logs for default container: operation='logs', resource='', args='nginx'
289+
- Logs for specific container: operation='logs', resource='', args='nginx -c ruby-container'
290+
- Logs with selector: operation='logs', resource='', args='-l app=nginx --all-containers=true'
291+
- Get events: operation='events', resource='', args='--all-namespaces'
292+
- Get events namespace: operation='events', resource='', args='-n default'
293293
- Top pods: operation='top', resource='pod', args=''
294294
- Top nodes: operation='top', resource='node', args=''
295295
- Top with containers: operation='top', resource='pod', args='POD_NAME --containers'
@@ -306,7 +306,7 @@ Examples:
306306
),
307307
mcp.WithString("resource",
308308
mcp.Required(),
309-
mcp.Description("The resource type (usually 'pod' for logs, 'event' for events or '' for exec and cp)"),
309+
mcp.Description("The resource type: 'node'/'pod' for top, empty string '' for logs/events/exec/cp"),
310310
),
311311
mcp.WithString("args",
312312
mcp.Required(),
@@ -345,7 +345,7 @@ Examples:
345345
),
346346
mcp.WithString("resource",
347347
mcp.Required(),
348-
mcp.Description("The resource type for explain operation, or empty for others"),
348+
mcp.Description("The resource type for explain operation, or empty string '' for cluster-info/api-resources/api-versions"),
349349
),
350350
mcp.WithString("args",
351351
mcp.Required(),
@@ -403,7 +403,7 @@ Examples:
403403
),
404404
mcp.WithString("resource",
405405
mcp.Required(),
406-
mcp.Description("Subcommand for auth/certificate operations, or empty for diff"),
406+
mcp.Description("Subcommand for auth/certificate operations, or empty string '' for diff operation"),
407407
),
408408
mcp.WithString("args",
409409
mcp.Required(),

pkg/security/validator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func TestReadOperationsValidation(t *testing.T) {
136136
{"cilium status", CommandTypeCilium, false},
137137
{"cilium endpoint list", CommandTypeCilium, false}, // "endpoint" is in CiliumReadOperations
138138
{"cilium install", CommandTypeCilium, true},
139-
{"cilium hubble enable", CommandTypeCilium, true},
139+
{"cilium hubble enable", CommandTypeCilium, false},
140140
{"hubble status", CommandTypeHubble, false},
141141
{"hubble observe", CommandTypeHubble, false},
142142
{"hubble list nodes", CommandTypeHubble, false},

0 commit comments

Comments
 (0)