log.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. api "k8s.io/kubernetes/pkg/apis/core"
  23. "k8s.io/kubernetes/pkg/apis/core/validation"
  24. "k8s.io/kubernetes/pkg/kubelet/client"
  25. "k8s.io/kubernetes/pkg/registry/core/pod"
  26. )
  27. // LogREST implements the log endpoint for a Pod
  28. type LogREST struct {
  29. KubeletConn client.ConnectionInfoGetter
  30. Store *genericregistry.Store
  31. }
  32. // LogREST implements GetterWithOptions
  33. var _ = rest.GetterWithOptions(&LogREST{})
  34. // New creates a new Pod log options object
  35. func (r *LogREST) New() runtime.Object {
  36. // TODO - return a resource that represents a log
  37. return &api.Pod{}
  38. }
  39. // LogREST implements StorageMetadata
  40. func (r *LogREST) ProducesMIMETypes(verb string) []string {
  41. // Since the default list does not include "plain/text", we need to
  42. // explicitly override ProducesMIMETypes, so that it gets added to
  43. // the "produces" section for pods/{name}/log
  44. return []string{
  45. "text/plain",
  46. }
  47. }
  48. // LogREST implements StorageMetadata, return string as the generating object
  49. func (r *LogREST) ProducesObject(verb string) interface{} {
  50. return ""
  51. }
  52. // Get retrieves a runtime.Object that will stream the contents of the pod log
  53. func (r *LogREST) Get(ctx context.Context, name string, opts runtime.Object) (runtime.Object, error) {
  54. logOpts, ok := opts.(*api.PodLogOptions)
  55. if !ok {
  56. return nil, fmt.Errorf("invalid options object: %#v", opts)
  57. }
  58. if errs := validation.ValidatePodLogOptions(logOpts); len(errs) > 0 {
  59. return nil, errors.NewInvalid(api.Kind("PodLogOptions"), name, errs)
  60. }
  61. location, transport, err := pod.LogLocation(r.Store, r.KubeletConn, ctx, name, logOpts)
  62. if err != nil {
  63. return nil, err
  64. }
  65. return &genericrest.LocationStreamer{
  66. Location: location,
  67. Transport: transport,
  68. ContentType: "text/plain",
  69. Flush: logOpts.Follow,
  70. ResponseChecker: genericrest.NewGenericHttpResponseChecker(api.Resource("pods/log"), name),
  71. RedirectChecker: genericrest.PreventRedirects,
  72. }, nil
  73. }
  74. // NewGetOptions creates a new options object
  75. func (r *LogREST) NewGetOptions() (runtime.Object, bool, string) {
  76. return &api.PodLogOptions{}, false, ""
  77. }
  78. // OverrideMetricsVerb override the GET verb to CONNECT for pod log resource
  79. func (r *LogREST) OverrideMetricsVerb(oldVerb string) (newVerb string) {
  80. newVerb = oldVerb
  81. if oldVerb == "GET" {
  82. newVerb = "CONNECT"
  83. }
  84. return
  85. }