log.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. Copyright 2014 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 rest
  14. import (
  15. "context"
  16. "fmt"
  17. "k8s.io/apimachinery/pkg/api/errors"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. genericregistry "k8s.io/apiserver/pkg/registry/generic/registry"
  20. genericrest "k8s.io/apiserver/pkg/registry/generic/rest"
  21. "k8s.io/apiserver/pkg/registry/rest"
  22. utilfeature "k8s.io/apiserver/pkg/util/feature"
  23. api "k8s.io/kubernetes/pkg/apis/core"
  24. "k8s.io/kubernetes/pkg/apis/core/validation"
  25. "k8s.io/kubernetes/pkg/features"
  26. "k8s.io/kubernetes/pkg/kubelet/client"
  27. "k8s.io/kubernetes/pkg/registry/core/pod"
  28. // ensure types are installed
  29. _ "k8s.io/kubernetes/pkg/apis/core/install"
  30. )
  31. // LogREST implements the log endpoint for a Pod
  32. type LogREST struct {
  33. KubeletConn client.ConnectionInfoGetter
  34. Store *genericregistry.Store
  35. }
  36. // LogREST implements GetterWithOptions
  37. var _ = rest.GetterWithOptions(&LogREST{})
  38. // New creates a new Pod log options object
  39. func (r *LogREST) New() runtime.Object {
  40. // TODO - return a resource that represents a log
  41. return &api.Pod{}
  42. }
  43. // ProducesMIMETypes returns a list of the MIME types the specified HTTP verb (GET, POST, DELETE,
  44. // PATCH) can respond with.
  45. func (r *LogREST) ProducesMIMETypes(verb string) []string {
  46. // Since the default list does not include "plain/text", we need to
  47. // explicitly override ProducesMIMETypes, so that it gets added to
  48. // the "produces" section for pods/{name}/log
  49. return []string{
  50. "text/plain",
  51. }
  52. }
  53. // ProducesObject returns an object the specified HTTP verb respond with. It will overwrite storage object if
  54. // it is not nil. Only the type of the return object matters, the value will be ignored.
  55. func (r *LogREST) ProducesObject(verb string) interface{} {
  56. return ""
  57. }
  58. // Get retrieves a runtime.Object that will stream the contents of the pod log
  59. func (r *LogREST) Get(ctx context.Context, name string, opts runtime.Object) (runtime.Object, error) {
  60. logOpts, ok := opts.(*api.PodLogOptions)
  61. if !ok {
  62. return nil, fmt.Errorf("invalid options object: %#v", opts)
  63. }
  64. if !utilfeature.DefaultFeatureGate.Enabled(features.AllowInsecureBackendProxy) {
  65. logOpts.InsecureSkipTLSVerifyBackend = false
  66. }
  67. if errs := validation.ValidatePodLogOptions(logOpts); len(errs) > 0 {
  68. return nil, errors.NewInvalid(api.Kind("PodLogOptions"), name, errs)
  69. }
  70. location, transport, err := pod.LogLocation(ctx, r.Store, r.KubeletConn, name, logOpts)
  71. if err != nil {
  72. return nil, err
  73. }
  74. return &genericrest.LocationStreamer{
  75. Location: location,
  76. Transport: transport,
  77. ContentType: "text/plain",
  78. Flush: logOpts.Follow,
  79. ResponseChecker: genericrest.NewGenericHttpResponseChecker(api.Resource("pods/log"), name),
  80. RedirectChecker: genericrest.PreventRedirects,
  81. }, nil
  82. }
  83. // NewGetOptions creates a new options object
  84. func (r *LogREST) NewGetOptions() (runtime.Object, bool, string) {
  85. return &api.PodLogOptions{}, false, ""
  86. }
  87. // OverrideMetricsVerb override the GET verb to CONNECT for pod log resource
  88. func (r *LogREST) OverrideMetricsVerb(oldVerb string) (newVerb string) {
  89. newVerb = oldVerb
  90. if oldVerb == "GET" {
  91. newVerb = "CONNECT"
  92. }
  93. return
  94. }