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
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-08-29 19:15:30 +00:00
|
|
|
_ "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1"
|
2014-07-15 14:52:39 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet"
|
2014-08-28 23:24:17 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2014-07-15 14:52:39 +00:00
|
|
|
"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 {
|
|
|
|
return path.Join("/", "registry", "hosts", hostname, "kubelet")
|
|
|
|
}
|
|
|
|
|
|
|
|
type SourceEtcd struct {
|
|
|
|
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-08-19 03:45:37 +00:00
|
|
|
func NewSourceEtcd(key string, client tools.EtcdClient, updates chan<- interface{}) *SourceEtcd {
|
2014-08-28 23:24:17 +00:00
|
|
|
helper := tools.EtcdHelper{
|
|
|
|
client,
|
2014-09-06 01:47:09 +00:00
|
|
|
runtime.DefaultCodec,
|
|
|
|
runtime.DefaultResourceVersioner,
|
2014-08-28 23:24:17 +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,
|
|
|
|
}
|
|
|
|
glog.Infof("Watching etcd for %s", key)
|
2014-08-28 23:24:17 +00:00
|
|
|
go util.Forever(source.run, time.Second)
|
|
|
|
return source
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SourceEtcd) run() {
|
2014-08-28 23:24:17 +00:00
|
|
|
watching, err := s.helper.Watch(s.key, 0)
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("Failed to initialize etcd watch: %v", err)
|
|
|
|
return
|
|
|
|
}
|
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-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-08-28 23:24:17 +00:00
|
|
|
glog.Infof("Received state from etcd watch: %+v", pods)
|
|
|
|
s.updates <- kubelet.PodUpdate{pods, kubelet.SET}
|
|
|
|
}
|
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-08-28 23:24:17 +00:00
|
|
|
func eventToPods(ev watch.Event) ([]kubelet.Pod, error) {
|
2014-07-15 14:52:39 +00:00
|
|
|
pods := []kubelet.Pod{}
|
2014-08-28 23:24:17 +00:00
|
|
|
manifests, ok := ev.Object.(*api.ContainerManifestList)
|
|
|
|
if !ok {
|
|
|
|
return pods, errors.New("unable to parse response as ContainerManifestList")
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
|
|
|
|
2014-07-23 01:53:41 +00:00
|
|
|
for i, manifest := range manifests.Items {
|
2014-07-15 14:52:39 +00:00
|
|
|
name := manifest.ID
|
|
|
|
if name == "" {
|
2014-08-03 20:55:34 +00:00
|
|
|
name = fmt.Sprintf("%d", i+1)
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
2014-09-05 09:49:11 +00:00
|
|
|
pods = append(pods, kubelet.Pod{
|
|
|
|
Name: name,
|
|
|
|
Manifest: manifest})
|
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
|
|
|
|
}
|
|
|
|
|
2014-08-28 23:24:17 +00:00
|
|
|
func makeContainerKey(machine string) string {
|
|
|
|
return "/registry/hosts/" + machine + "/kubelet"
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|