Skip to content

Commit bd10420

Browse files
authored
Merge pull request kubernetes#81678 from verb/debug-list
Add ephemeral containers to streamLocation name suggestions
2 parents 1aa2163 + cc32702 commit bd10420

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

pkg/registry/core/pod/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ go_test(
4747
"//pkg/api/testing:go_default_library",
4848
"//pkg/apis/core:go_default_library",
4949
"//pkg/apis/core/install:go_default_library",
50+
"//pkg/features:go_default_library",
5051
"//pkg/kubelet/client:go_default_library",
5152
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",
5253
"//staging/src/k8s.io/apimachinery/pkg/api/resource:go_default_library",
@@ -56,7 +57,9 @@ go_test(
5657
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
5758
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
5859
"//staging/src/k8s.io/apiserver/pkg/endpoints/request:go_default_library",
60+
"//staging/src/k8s.io/apiserver/pkg/util/feature:go_default_library",
5961
"//staging/src/k8s.io/client-go/tools/cache:go_default_library",
62+
"//staging/src/k8s.io/component-base/featuregate/testing:go_default_library",
6063
],
6164
)
6265

pkg/registry/core/pod/strategy.go

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -312,15 +312,6 @@ func ResourceLocation(ctx context.Context, getter ResourceGetter, rt http.RoundT
312312
return loc, rt, nil
313313
}
314314

315-
// getContainerNames returns a formatted string containing the container names
316-
func getContainerNames(containers []api.Container) string {
317-
names := []string{}
318-
for _, c := range containers {
319-
names = append(names, c.Name)
320-
}
321-
return strings.Join(names, " ")
322-
}
323-
324315
// LogLocation returns the log URL for a pod container. If opts.Container is blank
325316
// and only one container is present in the pod, that container is used.
326317
func LogLocation(
@@ -553,13 +544,13 @@ func validateContainer(container string, pod *api.Pod) (string, error) {
553544
case 0:
554545
return "", errors.NewBadRequest(fmt.Sprintf("a container name must be specified for pod %s", pod.Name))
555546
default:
556-
containerNames := getContainerNames(pod.Spec.Containers)
557-
initContainerNames := getContainerNames(pod.Spec.InitContainers)
558-
err := fmt.Sprintf("a container name must be specified for pod %s, choose one of: [%s]", pod.Name, containerNames)
559-
if len(initContainerNames) > 0 {
560-
err += fmt.Sprintf(" or one of the init containers: [%s]", initContainerNames)
561-
}
562-
return "", errors.NewBadRequest(err)
547+
var containerNames []string
548+
podutil.VisitContainers(&pod.Spec, func(c *api.Container) bool {
549+
containerNames = append(containerNames, c.Name)
550+
return true
551+
})
552+
errStr := fmt.Sprintf("a container name must be specified for pod %s, choose one of: %s", pod.Name, containerNames)
553+
return "", errors.NewBadRequest(errStr)
563554
}
564555
} else {
565556
if !podHasContainerWithName(pod, container) {

pkg/registry/core/pod/strategy_test.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ import (
3232
"k8s.io/apimachinery/pkg/runtime"
3333
"k8s.io/apimachinery/pkg/types"
3434
genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
35+
utilfeature "k8s.io/apiserver/pkg/util/feature"
3536
"k8s.io/client-go/tools/cache"
37+
featuregatetesting "k8s.io/component-base/featuregate/testing"
3638
apitesting "k8s.io/kubernetes/pkg/api/testing"
3739
api "k8s.io/kubernetes/pkg/apis/core"
40+
"k8s.io/kubernetes/pkg/features"
3841
"k8s.io/kubernetes/pkg/kubelet/client"
3942

4043
// ensure types are installed
@@ -321,6 +324,7 @@ func (g mockPodGetter) Get(context.Context, string, *metav1.GetOptions) (runtime
321324
}
322325

323326
func TestCheckLogLocation(t *testing.T) {
327+
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.EphemeralContainers, true)()
324328
ctx := genericapirequest.NewDefaultContext()
325329
fakePodName := "test"
326330
tcs := []struct {
@@ -407,7 +411,28 @@ func TestCheckLogLocation(t *testing.T) {
407411
Status: api.PodStatus{},
408412
},
409413
opts: &api.PodLogOptions{},
410-
expectedErr: errors.NewBadRequest("a container name must be specified for pod test, choose one of: [container1 container2] or one of the init containers: [initcontainer1]"),
414+
expectedErr: errors.NewBadRequest("a container name must be specified for pod test, choose one of: [initcontainer1 container1 container2]"),
415+
expectedTransport: nil,
416+
},
417+
{
418+
in: &api.Pod{
419+
ObjectMeta: metav1.ObjectMeta{Name: fakePodName},
420+
Spec: api.PodSpec{
421+
Containers: []api.Container{
422+
{Name: "container1"},
423+
{Name: "container2"},
424+
},
425+
InitContainers: []api.Container{
426+
{Name: "initcontainer1"},
427+
},
428+
EphemeralContainers: []api.EphemeralContainer{
429+
{EphemeralContainerCommon: api.EphemeralContainerCommon{Name: "debugger"}},
430+
},
431+
},
432+
Status: api.PodStatus{},
433+
},
434+
opts: &api.PodLogOptions{},
435+
expectedErr: errors.NewBadRequest("a container name must be specified for pod test, choose one of: [initcontainer1 container1 container2 debugger]"),
411436
expectedTransport: nil,
412437
},
413438
{
@@ -458,10 +483,10 @@ func TestCheckLogLocation(t *testing.T) {
458483

459484
_, actualTransport, err := LogLocation(ctx, getter, connectionGetter, fakePodName, tc.opts)
460485
if !reflect.DeepEqual(err, tc.expectedErr) {
461-
t.Errorf("expected %v, got %v", tc.expectedErr, err)
486+
t.Errorf("expected %q, got %q", tc.expectedErr, err)
462487
}
463488
if actualTransport != tc.expectedTransport {
464-
t.Errorf("expected %v, got %v", tc.expectedTransport, actualTransport)
489+
t.Errorf("expected %q, got %q", tc.expectedTransport, actualTransport)
465490
}
466491
})
467492
}

0 commit comments

Comments
 (0)