logsforobject.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package polymorphichelpers
  14. import (
  15. "errors"
  16. "fmt"
  17. "os"
  18. "sort"
  19. "time"
  20. corev1 "k8s.io/api/core/v1"
  21. "k8s.io/apimachinery/pkg/runtime"
  22. "k8s.io/cli-runtime/pkg/genericclioptions"
  23. corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
  24. "k8s.io/client-go/rest"
  25. "k8s.io/kubernetes/pkg/kubectl/util/podutils"
  26. )
  27. func logsForObject(restClientGetter genericclioptions.RESTClientGetter, object, options runtime.Object, timeout time.Duration, allContainers bool) ([]rest.ResponseWrapper, error) {
  28. clientConfig, err := restClientGetter.ToRESTConfig()
  29. if err != nil {
  30. return nil, err
  31. }
  32. clientset, err := corev1client.NewForConfig(clientConfig)
  33. if err != nil {
  34. return nil, err
  35. }
  36. return logsForObjectWithClient(clientset, object, options, timeout, allContainers)
  37. }
  38. // TODO: remove internal clientset once all callers use external versions
  39. // this is split for easy test-ability
  40. func logsForObjectWithClient(clientset corev1client.CoreV1Interface, object, options runtime.Object, timeout time.Duration, allContainers bool) ([]rest.ResponseWrapper, error) {
  41. opts, ok := options.(*corev1.PodLogOptions)
  42. if !ok {
  43. return nil, errors.New("provided options object is not a PodLogOptions")
  44. }
  45. switch t := object.(type) {
  46. case *corev1.PodList:
  47. ret := []rest.ResponseWrapper{}
  48. for i := range t.Items {
  49. currRet, err := logsForObjectWithClient(clientset, &t.Items[i], options, timeout, allContainers)
  50. if err != nil {
  51. return nil, err
  52. }
  53. ret = append(ret, currRet...)
  54. }
  55. return ret, nil
  56. case *corev1.Pod:
  57. // if allContainers is true, then we're going to locate all containers and then iterate through them. At that point, "allContainers" is false
  58. if !allContainers {
  59. return []rest.ResponseWrapper{clientset.Pods(t.Namespace).GetLogs(t.Name, opts)}, nil
  60. }
  61. ret := []rest.ResponseWrapper{}
  62. for _, c := range t.Spec.InitContainers {
  63. currOpts := opts.DeepCopy()
  64. currOpts.Container = c.Name
  65. currRet, err := logsForObjectWithClient(clientset, t, currOpts, timeout, false)
  66. if err != nil {
  67. return nil, err
  68. }
  69. ret = append(ret, currRet...)
  70. }
  71. for _, c := range t.Spec.Containers {
  72. currOpts := opts.DeepCopy()
  73. currOpts.Container = c.Name
  74. currRet, err := logsForObjectWithClient(clientset, t, currOpts, timeout, false)
  75. if err != nil {
  76. return nil, err
  77. }
  78. ret = append(ret, currRet...)
  79. }
  80. return ret, nil
  81. }
  82. namespace, selector, err := SelectorsForObject(object)
  83. if err != nil {
  84. return nil, fmt.Errorf("cannot get the logs from %T: %v", object, err)
  85. }
  86. sortBy := func(pods []*corev1.Pod) sort.Interface { return podutils.ByLogging(pods) }
  87. pod, numPods, err := GetFirstPod(clientset, namespace, selector.String(), timeout, sortBy)
  88. if err != nil {
  89. return nil, err
  90. }
  91. if numPods > 1 {
  92. fmt.Fprintf(os.Stderr, "Found %v pods, using pod/%v\n", numPods, pod.Name)
  93. }
  94. return logsForObjectWithClient(clientset, pod, options, timeout, allContainers)
  95. }