aws / aws/aws-cdk

(aws-eks): kubectl objectName is required by CDK, but not always by `kubectl get`

Open
#34,148 2 comments 0 reactions 0 assignees View on GitHub
@aws-cdk/aws-eks @aws-cdk/aws-eks-v2-alpha bug effort/medium p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

I am using `KubernetesObjectValue` to retrieve some values from my cluster. Via cloudshell I can run this query successfully :

```
kubectl get targetgroupbindings --namespace mynamespace
```

There is no object name in this command, which works fine.

I try to add this to CDK (in my case in java) using

```java
KubernetesObjectValueProps.builder()
.cluster(cluster)
.objectNamespace("mynamespace")
.objectType("targetgroupbindings")
.jsonPath(".items[0].metadata.name")
.build()
```

I get an exception `java.lang.NullPointerException: objectName is required`.

Supplying an empty `objectName` (i.e. `.objectName("")` prevents the NullPointerException, but triggers an error deploy-time:

> Received response status [FAILED] from custom resource. Message returned: Error: Timeout waiting for output from kubectl command: ['get', '-n', 'mynamespace', 'targetgroupbindings', '', "-o=jsonpath='{.items[0].metadata.name}'"] (last_error=b'error: resource name may not be empty\n') Logs: /aws/lambda/ at invokeUserFunction (/var/task/framework.js:2:6) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async onEvent (/var/task/framework.js:1:369) at async Runtime.handler (/var/task/cfn-response.js:1:1837) (RequestId: )

Relevant source code is possibly [here](https://github.com/aws/aws-cdk/blob/main/packages/%40aws-cdk/aws-eks-v2-alpha/lib/kubectl-handler/get/__init__.py#L44):
```python
output = wait_for_output(['get', '-n', object_namespace, object_type, object_name, "-o=jsonpath='{{{0}}}'".format(json_path)], int(timeout_seconds))
```

It seems like an implementation flaw to require a property that the underlying API does not always require.

Is there a workaround?
- Can I supply a "trick" value so that the resulting command does not contain an object name?
- Am I using the wrong construct? Is there another construct for more general `kubectl get` commands?
- A way to supply the full desired command manually in a lower-level way?

### Regression Issue

- [ ] Select this option if this issue appears to be a regression.

### Last Known Working CDK Version

_No response_

### Expected Behavior

ObjectName is not required when calling commands that do not require it (such as `kubectl get targetgroupbindings`)

### Current Behavior

ObjectName is required when calling commands that do not require it (such as `kubectl get targetgroupbindings`)

### Reproduction Steps

- Have a kubernetes cluster
- Attempt to retrieve a value that does not require an object name

### Possible Solution

_No response_

### Additional Information/Context

_No response_

### CDK CLI Version

2.1005.0 (build be378de)

### Framework Version

_No response_

### Node.js Version

v22.14.0

### OS

Mac OS 15.3.2 (24D81)

### Language

Java

### Language Version

openjdk version "23" 2024-09-17

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start with packages/@aws-cdk/aws-eks-v2-alpha/lib/kubectl-handler/get/__init__.py and trace how KubernetesObjectValueProps passes objectName into the kubectl command. Compare the failing command with kubectl get targetgroupbindings, then verify that a supported name-less query produces the expected jsonPath output without an empty resource-name argument.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, java, kubernetes, python, typescript
Domain
cloud, devops, infrastructure
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.