Skip to content

Commit ca69051

Browse files
committed
refactor(pod log):refactor for container valiate, little cleanup
bug(pod log):TestCheckLogLocation should point out pod name modify container if statement fix typo
1 parent 6c1080b commit ca69051

File tree

2 files changed

+45
-42
lines changed

2 files changed

+45
-42
lines changed

pkg/registry/core/pod/strategy.go

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func getPod(getter ResourceGetter, ctx context.Context, name string) (*api.Pod,
250250
return pod, nil
251251
}
252252

253-
// returns primary IP for a Pod
253+
// getPodIP returns primary IP for a Pod
254254
func getPodIP(pod *api.Pod) string {
255255
if pod == nil {
256256
return ""
@@ -327,25 +327,9 @@ func LogLocation(
327327
// Try to figure out a container
328328
// If a container was provided, it must be valid
329329
container := opts.Container
330-
if len(container) == 0 {
331-
switch len(pod.Spec.Containers) {
332-
case 1:
333-
container = pod.Spec.Containers[0].Name
334-
case 0:
335-
return nil, nil, errors.NewBadRequest(fmt.Sprintf("a container name must be specified for pod %s", name))
336-
default:
337-
containerNames := getContainerNames(pod.Spec.Containers)
338-
initContainerNames := getContainerNames(pod.Spec.InitContainers)
339-
err := fmt.Sprintf("a container name must be specified for pod %s, choose one of: [%s]", name, containerNames)
340-
if len(initContainerNames) > 0 {
341-
err += fmt.Sprintf(" or one of the init containers: [%s]", initContainerNames)
342-
}
343-
return nil, nil, errors.NewBadRequest(err)
344-
}
345-
} else {
346-
if !podHasContainerWithName(pod, container) {
347-
return nil, nil, errors.NewBadRequest(fmt.Sprintf("container %s is not valid for pod %s", container, name))
348-
}
330+
container, err = validateContainer(container, pod)
331+
if err != nil {
332+
return nil, nil, err
349333
}
350334
nodeName := types.NodeName(pod.Spec.NodeName)
351335
if len(nodeName) == 0 {
@@ -488,26 +472,11 @@ func streamLocation(
488472

489473
// Try to figure out a container
490474
// If a container was provided, it must be valid
491-
if container == "" {
492-
switch len(pod.Spec.Containers) {
493-
case 1:
494-
container = pod.Spec.Containers[0].Name
495-
case 0:
496-
return nil, nil, errors.NewBadRequest(fmt.Sprintf("a container name must be specified for pod %s", name))
497-
default:
498-
containerNames := getContainerNames(pod.Spec.Containers)
499-
initContainerNames := getContainerNames(pod.Spec.InitContainers)
500-
err := fmt.Sprintf("a container name must be specified for pod %s, choose one of: [%s]", name, containerNames)
501-
if len(initContainerNames) > 0 {
502-
err += fmt.Sprintf(" or one of the init containers: [%s]", initContainerNames)
503-
}
504-
return nil, nil, errors.NewBadRequest(err)
505-
}
506-
} else {
507-
if !podHasContainerWithName(pod, container) {
508-
return nil, nil, errors.NewBadRequest(fmt.Sprintf("container %s is not valid for pod %s", container, name))
509-
}
475+
container, err = validateContainer(container, pod)
476+
if err != nil {
477+
return nil, nil, err
510478
}
479+
511480
nodeName := types.NodeName(pod.Spec.NodeName)
512481
if len(nodeName) == 0 {
513482
// If pod has not been assigned a host, return an empty location
@@ -564,3 +533,29 @@ func PortForwardLocation(
564533
}
565534
return loc, nodeInfo.Transport, nil
566535
}
536+
537+
// validateContainer validate container is valid for pod, return valid container
538+
func validateContainer(container string, pod *api.Pod) (string, error) {
539+
if len(container) == 0 {
540+
switch len(pod.Spec.Containers) {
541+
case 1:
542+
container = pod.Spec.Containers[0].Name
543+
case 0:
544+
return "", errors.NewBadRequest(fmt.Sprintf("a container name must be specified for pod %s", pod.Name))
545+
default:
546+
containerNames := getContainerNames(pod.Spec.Containers)
547+
initContainerNames := getContainerNames(pod.Spec.InitContainers)
548+
err := fmt.Sprintf("a container name must be specified for pod %s, choose one of: [%s]", pod.Name, containerNames)
549+
if len(initContainerNames) > 0 {
550+
err += fmt.Sprintf(" or one of the init containers: [%s]", initContainerNames)
551+
}
552+
return "", errors.NewBadRequest(err)
553+
}
554+
} else {
555+
if !podHasContainerWithName(pod, container) {
556+
return "", errors.NewBadRequest(fmt.Sprintf("container %s is not valid for pod %s", container, pod.Name))
557+
}
558+
}
559+
560+
return container, nil
561+
}

pkg/registry/core/pod/strategy_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ func (g mockPodGetter) Get(context.Context, string, *metav1.GetOptions) (runtime
320320

321321
func TestCheckLogLocation(t *testing.T) {
322322
ctx := genericapirequest.NewDefaultContext()
323+
fakePodName := "test"
323324
tcs := []struct {
324325
name string
325326
in *api.Pod
@@ -330,6 +331,7 @@ func TestCheckLogLocation(t *testing.T) {
330331
{
331332
name: "simple",
332333
in: &api.Pod{
334+
ObjectMeta: metav1.ObjectMeta{Name: fakePodName},
333335
Spec: api.PodSpec{
334336
Containers: []api.Container{
335337
{Name: "mycontainer"},
@@ -345,6 +347,7 @@ func TestCheckLogLocation(t *testing.T) {
345347
{
346348
name: "insecure",
347349
in: &api.Pod{
350+
ObjectMeta: metav1.ObjectMeta{Name: fakePodName},
348351
Spec: api.PodSpec{
349352
Containers: []api.Container{
350353
{Name: "mycontainer"},
@@ -362,8 +365,9 @@ func TestCheckLogLocation(t *testing.T) {
362365
{
363366
name: "missing container",
364367
in: &api.Pod{
365-
Spec: api.PodSpec{},
366-
Status: api.PodStatus{},
368+
ObjectMeta: metav1.ObjectMeta{Name: fakePodName},
369+
Spec: api.PodSpec{},
370+
Status: api.PodStatus{},
367371
},
368372
opts: &api.PodLogOptions{},
369373
expectedErr: errors.NewBadRequest("a container name must be specified for pod test"),
@@ -372,6 +376,7 @@ func TestCheckLogLocation(t *testing.T) {
372376
{
373377
name: "choice of two containers",
374378
in: &api.Pod{
379+
ObjectMeta: metav1.ObjectMeta{Name: fakePodName},
375380
Spec: api.PodSpec{
376381
Containers: []api.Container{
377382
{Name: "container1"},
@@ -387,6 +392,7 @@ func TestCheckLogLocation(t *testing.T) {
387392
{
388393
name: "initcontainers",
389394
in: &api.Pod{
395+
ObjectMeta: metav1.ObjectMeta{Name: fakePodName},
390396
Spec: api.PodSpec{
391397
Containers: []api.Container{
392398
{Name: "container1"},
@@ -405,6 +411,7 @@ func TestCheckLogLocation(t *testing.T) {
405411
{
406412
name: "bad container",
407413
in: &api.Pod{
414+
ObjectMeta: metav1.ObjectMeta{Name: fakePodName},
408415
Spec: api.PodSpec{
409416
Containers: []api.Container{
410417
{Name: "container1"},
@@ -422,6 +429,7 @@ func TestCheckLogLocation(t *testing.T) {
422429
{
423430
name: "good with two containers",
424431
in: &api.Pod{
432+
ObjectMeta: metav1.ObjectMeta{Name: fakePodName},
425433
Spec: api.PodSpec{
426434
Containers: []api.Container{
427435
{Name: "container1"},
@@ -446,7 +454,7 @@ func TestCheckLogLocation(t *testing.T) {
446454
InsecureSkipTLSVerifyTransport: fakeInsecureRoundTripper,
447455
}}
448456

449-
_, actualTransport, err := LogLocation(getter, connectionGetter, ctx, "test", tc.opts)
457+
_, actualTransport, err := LogLocation(getter, connectionGetter, ctx, fakePodName, tc.opts)
450458
if !reflect.DeepEqual(err, tc.expectedErr) {
451459
t.Errorf("expected %v, got %v", tc.expectedErr, err)
452460
}

0 commit comments

Comments
 (0)