2014-07-15 14:52:39 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2014-07-15 14:52:39 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
2014-08-13 00:21:39 +00:00
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
2014-07-15 14:52:39 +00:00
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// Reads the pod configuration from an HTTP GET response.
|
2014-07-15 14:52:39 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2014-09-17 17:43:44 +00:00
|
|
|
"bytes"
|
2014-07-15 14:52:39 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
2014-10-08 19:56:02 +00:00
|
|
|
|
2014-07-15 14:52:39 +00:00
|
|
|
"github.com/golang/glog"
|
|
|
|
)
|
|
|
|
|
2014-11-21 21:47:21 +00:00
|
|
|
type sourceURL struct {
|
2015-03-23 03:06:12 +00:00
|
|
|
url string
|
2015-06-18 18:27:55 +00:00
|
|
|
hostname string
|
2015-03-23 03:06:12 +00:00
|
|
|
updates chan<- interface{}
|
|
|
|
data []byte
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
|
|
|
|
2015-06-18 18:27:55 +00:00
|
|
|
func NewSourceURL(url, hostname string, period time.Duration, updates chan<- interface{}) {
|
2014-11-21 21:47:21 +00:00
|
|
|
config := &sourceURL{
|
2015-03-23 03:06:12 +00:00
|
|
|
url: url,
|
2015-06-18 18:27:55 +00:00
|
|
|
hostname: hostname,
|
2015-03-23 03:06:12 +00:00
|
|
|
updates: updates,
|
|
|
|
data: nil,
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
2014-09-18 10:46:14 +00:00
|
|
|
glog.V(1).Infof("Watching URL %s", url)
|
2014-07-15 14:52:39 +00:00
|
|
|
go util.Forever(config.run, period)
|
|
|
|
}
|
|
|
|
|
2014-11-21 21:47:21 +00:00
|
|
|
func (s *sourceURL) run() {
|
2014-07-15 14:52:39 +00:00
|
|
|
if err := s.extractFromURL(); err != nil {
|
2014-11-20 10:00:36 +00:00
|
|
|
glog.Errorf("Failed to read URL: %v", err)
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-23 03:06:12 +00:00
|
|
|
func (s *sourceURL) applyDefaults(pod *api.Pod) error {
|
2015-06-18 18:27:55 +00:00
|
|
|
return applyDefaults(pod, s.url, false, s.hostname)
|
2015-03-23 03:06:12 +00:00
|
|
|
}
|
|
|
|
|
2014-11-21 21:47:21 +00:00
|
|
|
func (s *sourceURL) extractFromURL() error {
|
2014-07-15 14:52:39 +00:00
|
|
|
resp, err := http.Get(s.url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
data, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-09-09 17:20:58 +00:00
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return fmt.Errorf("%v: %v", s.url, resp.Status)
|
|
|
|
}
|
2014-07-15 14:52:39 +00:00
|
|
|
if len(data) == 0 {
|
2015-01-09 07:01:07 +00:00
|
|
|
// Emit an update with an empty PodList to allow HTTPSource to be marked as seen
|
2015-04-03 22:51:50 +00:00
|
|
|
s.updates <- kubelet.PodUpdate{[]*api.Pod{}, kubelet.SET, kubelet.HTTPSource}
|
2014-07-15 14:52:39 +00:00
|
|
|
return fmt.Errorf("zero-length data received from %v", s.url)
|
|
|
|
}
|
2015-05-22 15:20:27 +00:00
|
|
|
// Short circuit if the data has not changed since the last time it was read.
|
2014-09-17 17:43:44 +00:00
|
|
|
if bytes.Compare(data, s.data) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
s.data = data
|
2014-07-15 14:52:39 +00:00
|
|
|
|
2015-03-18 14:27:44 +00:00
|
|
|
// First try as it is a single pod.
|
2015-03-23 03:06:12 +00:00
|
|
|
parsed, pod, singlePodErr := tryDecodeSinglePod(data, s.applyDefaults)
|
2015-03-18 14:27:44 +00:00
|
|
|
if parsed {
|
|
|
|
if singlePodErr != nil {
|
|
|
|
// It parsed but could not be used.
|
|
|
|
return singlePodErr
|
|
|
|
}
|
2015-04-03 22:51:50 +00:00
|
|
|
s.updates <- kubelet.PodUpdate{[]*api.Pod{pod}, kubelet.SET, kubelet.HTTPSource}
|
2015-03-18 14:27:44 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// That didn't work, so try a list of pods.
|
2015-04-03 22:51:50 +00:00
|
|
|
parsed, podList, multiPodErr := tryDecodePodList(data, s.applyDefaults)
|
2015-03-18 14:27:44 +00:00
|
|
|
if parsed {
|
|
|
|
if multiPodErr != nil {
|
|
|
|
// It parsed but could not be used.
|
|
|
|
return multiPodErr
|
|
|
|
}
|
2015-04-03 22:51:50 +00:00
|
|
|
pods := make([]*api.Pod, 0)
|
|
|
|
for i := range podList.Items {
|
|
|
|
pods = append(pods, &podList.Items[i])
|
|
|
|
}
|
|
|
|
s.updates <- kubelet.PodUpdate{pods, kubelet.SET, kubelet.HTTPSource}
|
2015-03-18 14:27:44 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-22 15:20:27 +00:00
|
|
|
return fmt.Errorf("%v: received '%v', but couldn't parse as "+
|
2015-03-18 14:27:44 +00:00
|
|
|
"single (%v) or multiple pods (%v).\n",
|
2015-05-22 15:20:27 +00:00
|
|
|
s.url, string(data), singlePodErr, multiPodErr)
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|