2014-07-15 14:52:39 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
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-07-23 01:53:41 +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 etcd using the Kubernetes etcd schema.
|
2014-07-15 14:52:39 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2014-08-28 23:24:17 +00:00
|
|
|
"errors"
|
2014-07-15 14:52:39 +00:00
|
|
|
"path"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-09-11 17:02:53 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
|
2014-07-15 14:52:39 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/tools"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
2014-08-28 23:24:17 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
2014-07-15 14:52:39 +00:00
|
|
|
"github.com/golang/glog"
|
|
|
|
)
|
|
|
|
|
|
|
|
func EtcdKeyForHost(hostname string) string {
|
2014-10-14 21:31:11 +00:00
|
|
|
return path.Join("/", "registry", "nodes", hostname, "boundpods")
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
|
|
|
|
2014-11-21 21:47:21 +00:00
|
|
|
type sourceEtcd struct {
|
2014-07-15 14:52:39 +00:00
|
|
|
key string
|
2014-08-28 23:24:17 +00:00
|
|
|
helper tools.EtcdHelper
|
2014-07-15 14:52:39 +00:00
|
|
|
updates chan<- interface{}
|
|
|
|
}
|
|
|
|
|
2014-08-28 23:24:17 +00:00
|
|
|
// NewSourceEtcd creates a config source that watches and pulls from a key in etcd
|
2014-11-21 21:47:21 +00:00
|
|
|
func NewSourceEtcd(key string, client tools.EtcdClient, updates chan<- interface{}) {
|
2014-08-28 23:24:17 +00:00
|
|
|
helper := tools.EtcdHelper{
|
|
|
|
client,
|
2014-09-16 19:56:26 +00:00
|
|
|
latest.Codec,
|
2014-10-07 20:51:28 +00:00
|
|
|
tools.RuntimeVersionAdapter{latest.ResourceVersioner},
|
2014-08-28 23:24:17 +00:00
|
|
|
}
|
2014-11-21 21:47:21 +00:00
|
|
|
source := &sourceEtcd{
|
2014-07-15 14:52:39 +00:00
|
|
|
key: key,
|
2014-08-28 23:24:17 +00:00
|
|
|
helper: helper,
|
2014-07-15 14:52:39 +00:00
|
|
|
updates: updates,
|
|
|
|
}
|
2014-09-18 10:46:14 +00:00
|
|
|
glog.V(1).Infof("Watching etcd for %s", key)
|
2014-08-28 23:24:17 +00:00
|
|
|
go util.Forever(source.run, time.Second)
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
|
|
|
|
2014-11-21 21:47:21 +00:00
|
|
|
func (s *sourceEtcd) run() {
|
2015-01-09 07:01:07 +00:00
|
|
|
boundPods := api.BoundPods{}
|
|
|
|
err := s.helper.ExtractToList(s.key, &boundPods)
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("etcd failed to retrieve the value for the key %q. Error: %v", s.key, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Push update. Maybe an empty PodList to allow EtcdSource to be marked as seen
|
|
|
|
s.updates <- kubelet.PodUpdate{boundPods.Items, kubelet.SET, kubelet.EtcdSource}
|
|
|
|
index, _ := s.helper.ResourceVersioner.ResourceVersion(&boundPods)
|
|
|
|
watching := s.helper.Watch(s.key, index)
|
2014-07-15 14:52:39 +00:00
|
|
|
for {
|
2014-08-28 23:24:17 +00:00
|
|
|
select {
|
|
|
|
case event, ok := <-watching.ResultChan():
|
|
|
|
if !ok {
|
|
|
|
return
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
2014-09-23 00:37:12 +00:00
|
|
|
if event.Type == watch.Error {
|
2014-10-14 23:30:13 +00:00
|
|
|
glog.Infof("Watch closed (%#v). Reopening.", event.Object)
|
2014-09-23 00:37:12 +00:00
|
|
|
watching.Stop()
|
|
|
|
return
|
|
|
|
}
|
2014-08-28 23:24:17 +00:00
|
|
|
pods, err := eventToPods(event)
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("Failed to parse result from etcd watch: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
2014-07-15 14:52:39 +00:00
|
|
|
|
2014-09-18 10:46:14 +00:00
|
|
|
glog.V(4).Infof("Received state from etcd watch: %+v", pods)
|
2014-12-17 05:11:27 +00:00
|
|
|
s.updates <- kubelet.PodUpdate{pods, kubelet.SET, kubelet.EtcdSource}
|
2014-08-28 23:24:17 +00:00
|
|
|
}
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-28 23:24:17 +00:00
|
|
|
// eventToPods takes a watch.Event object, and turns it into a structured list of pods.
|
2014-07-15 14:52:39 +00:00
|
|
|
// It returns a list of containers, or an error if one occurs.
|
2014-10-08 19:56:02 +00:00
|
|
|
func eventToPods(ev watch.Event) ([]api.BoundPod, error) {
|
|
|
|
pods := []api.BoundPod{}
|
2015-01-09 07:01:07 +00:00
|
|
|
if ev.Object == nil {
|
|
|
|
return pods, nil
|
|
|
|
}
|
2014-10-08 19:56:02 +00:00
|
|
|
boundPods, ok := ev.Object.(*api.BoundPods)
|
2014-08-28 23:24:17 +00:00
|
|
|
if !ok {
|
2014-10-08 19:56:02 +00:00
|
|
|
return pods, errors.New("unable to parse response as BoundPods")
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 06:49:37 +00:00
|
|
|
for _, pod := range boundPods.Items {
|
2014-10-08 19:56:02 +00:00
|
|
|
// Backwards compatibility with old api servers
|
2015-01-05 01:30:30 +00:00
|
|
|
// TODO: Remove this after 1.0 release.
|
2014-10-08 19:56:02 +00:00
|
|
|
if len(pod.Namespace) == 0 {
|
|
|
|
pod.Namespace = api.NamespaceDefault
|
|
|
|
}
|
|
|
|
pods = append(pods, pod)
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
2014-08-28 23:24:17 +00:00
|
|
|
|
2014-07-15 14:52:39 +00:00
|
|
|
return pods, nil
|
|
|
|
}
|