http.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. // Reads the pod configuration from an HTTP GET response.
  14. package config
  15. import (
  16. "bytes"
  17. "fmt"
  18. "io/ioutil"
  19. "net/http"
  20. "time"
  21. "k8s.io/api/core/v1"
  22. "k8s.io/apimachinery/pkg/util/wait"
  23. api "k8s.io/kubernetes/pkg/apis/core"
  24. kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
  25. "k8s.io/apimachinery/pkg/types"
  26. "k8s.io/klog"
  27. )
  28. type sourceURL struct {
  29. url string
  30. header http.Header
  31. nodeName types.NodeName
  32. updates chan<- interface{}
  33. data []byte
  34. failureLogs int
  35. client *http.Client
  36. }
  37. func NewSourceURL(url string, header http.Header, nodeName types.NodeName, period time.Duration, updates chan<- interface{}) {
  38. config := &sourceURL{
  39. url: url,
  40. header: header,
  41. nodeName: nodeName,
  42. updates: updates,
  43. data: nil,
  44. // Timing out requests leads to retries. This client is only used to
  45. // read the manifest URL passed to kubelet.
  46. client: &http.Client{Timeout: 10 * time.Second},
  47. }
  48. klog.V(1).Infof("Watching URL %s", url)
  49. go wait.Until(config.run, period, wait.NeverStop)
  50. }
  51. func (s *sourceURL) run() {
  52. if err := s.extractFromURL(); err != nil {
  53. // Don't log this multiple times per minute. The first few entries should be
  54. // enough to get the point across.
  55. if s.failureLogs < 3 {
  56. klog.Warningf("Failed to read pods from URL: %v", err)
  57. } else if s.failureLogs == 3 {
  58. klog.Warningf("Failed to read pods from URL. Dropping verbosity of this message to V(4): %v", err)
  59. } else {
  60. klog.V(4).Infof("Failed to read pods from URL: %v", err)
  61. }
  62. s.failureLogs++
  63. } else {
  64. if s.failureLogs > 0 {
  65. klog.Info("Successfully read pods from URL.")
  66. s.failureLogs = 0
  67. }
  68. }
  69. }
  70. func (s *sourceURL) applyDefaults(pod *api.Pod) error {
  71. return applyDefaults(pod, s.url, false, s.nodeName)
  72. }
  73. func (s *sourceURL) extractFromURL() error {
  74. req, err := http.NewRequest("GET", s.url, nil)
  75. if err != nil {
  76. return err
  77. }
  78. req.Header = s.header
  79. resp, err := s.client.Do(req)
  80. if err != nil {
  81. return err
  82. }
  83. defer resp.Body.Close()
  84. data, err := ioutil.ReadAll(resp.Body)
  85. if err != nil {
  86. return err
  87. }
  88. if resp.StatusCode != http.StatusOK {
  89. return fmt.Errorf("%v: %v", s.url, resp.Status)
  90. }
  91. if len(data) == 0 {
  92. // Emit an update with an empty PodList to allow HTTPSource to be marked as seen
  93. s.updates <- kubetypes.PodUpdate{Pods: []*v1.Pod{}, Op: kubetypes.SET, Source: kubetypes.HTTPSource}
  94. return fmt.Errorf("zero-length data received from %v", s.url)
  95. }
  96. // Short circuit if the data has not changed since the last time it was read.
  97. if bytes.Compare(data, s.data) == 0 {
  98. return nil
  99. }
  100. s.data = data
  101. // First try as it is a single pod.
  102. parsed, pod, singlePodErr := tryDecodeSinglePod(data, s.applyDefaults)
  103. if parsed {
  104. if singlePodErr != nil {
  105. // It parsed but could not be used.
  106. return singlePodErr
  107. }
  108. s.updates <- kubetypes.PodUpdate{Pods: []*v1.Pod{pod}, Op: kubetypes.SET, Source: kubetypes.HTTPSource}
  109. return nil
  110. }
  111. // That didn't work, so try a list of pods.
  112. parsed, podList, multiPodErr := tryDecodePodList(data, s.applyDefaults)
  113. if parsed {
  114. if multiPodErr != nil {
  115. // It parsed but could not be used.
  116. return multiPodErr
  117. }
  118. pods := make([]*v1.Pod, 0)
  119. for i := range podList.Items {
  120. pods = append(pods, &podList.Items[i])
  121. }
  122. s.updates <- kubetypes.PodUpdate{Pods: pods, Op: kubetypes.SET, Source: kubetypes.HTTPSource}
  123. return nil
  124. }
  125. return fmt.Errorf("%v: received '%v', but couldn't parse as "+
  126. "single (%v) or multiple pods (%v).\n",
  127. s.url, string(data), singlePodErr, multiPodErr)
  128. }