Skip to content

Commit

Permalink
Merge pull request #17 from spreadshirt/fix-default-container
Browse files Browse the repository at this point in the history
Select the default container if one exists
  • Loading branch information
heyLu authored May 12, 2023
2 parents 2277e54 + db5802b commit f21c143
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
9 changes: 7 additions & 2 deletions cmd/kubectl-request/kubectl-request.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,17 @@ func (ac *accessCommand) Request(cmd *cobra.Command, args []string) error {

}

// default to first container if none is set (like `kubectl exec`)
// default to default or first container if none is set (like `kubectl exec`)
if ac.execOptions.Container == "" {
if len(selectedPod.Spec.Containers) == 0 {
return fmt.Errorf("no containers in pod %q", selectedPod.Name)
}
ac.execOptions.Container = selectedPod.Spec.Containers[0].Name

if selectedPod.ObjectMeta.Annotations["kubectl.kubernetes.io/default-container"] != "" {
ac.execOptions.Container = selectedPod.ObjectMeta.Annotations["kubectl.kubernetes.io/default-container"]
} else {
ac.execOptions.Container = selectedPod.Spec.Containers[0].Name
}
}

accessRequestsClient, err := accessrequestsclientv1.NewForConfig(config)
Expand Down
11 changes: 10 additions & 1 deletion dev/nginx-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@ spec:
app: nginx
template:
metadata:
annotations:
kubectl.kubernetes.io/default-container: nginx
labels:
app: nginx
spec:
containers:
# sleeps forever, used to test that default-container annotations is respected
- name: sleeper
image: busybox
command:
- /bin/sh
- -c
- 'sleep infinity'
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
- containerPort: 80

0 comments on commit f21c143

Please sign in to comment.