log.go 2.6 KB

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