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 (
|
|
|
|
"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"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/tools"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
|
|
|
"github.com/coreos/go-etcd/etcd"
|
|
|
|
"github.com/golang/glog"
|
|
|
|
"gopkg.in/v1/yaml"
|
|
|
|
)
|
|
|
|
|
|
|
|
func EtcdKeyForHost(hostname string) string {
|
|
|
|
return path.Join("/", "registry", "hosts", hostname, "kubelet")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(lavalamp): Use a watcher interface instead of the etcd client directly
|
|
|
|
type SourceEtcd struct {
|
|
|
|
key string
|
|
|
|
client tools.EtcdClient
|
|
|
|
updates chan<- interface{}
|
|
|
|
|
2014-08-19 03:45:37 +00:00
|
|
|
interval time.Duration
|
|
|
|
timeout time.Duration
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +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-07-15 14:52:39 +00:00
|
|
|
config := &SourceEtcd{
|
|
|
|
key: key,
|
|
|
|
client: client,
|
|
|
|
updates: updates,
|
|
|
|
|
2014-08-19 03:45:37 +00:00
|
|
|
timeout: 1 * time.Minute,
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
|
|
|
glog.Infof("Watching etcd for %s", key)
|
2014-08-19 03:45:37 +00:00
|
|
|
go util.Forever(config.run, time.Second)
|
2014-07-15 14:52:39 +00:00
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// run loops forever looking for changes to a key in etcd.
|
2014-07-15 14:52:39 +00:00
|
|
|
func (s *SourceEtcd) run() {
|
|
|
|
index := uint64(0)
|
|
|
|
for {
|
2014-07-23 00:21:41 +00:00
|
|
|
nextIndex, err := s.fetchNextState(index)
|
2014-07-15 14:52:39 +00:00
|
|
|
if err != nil {
|
|
|
|
if !tools.IsEtcdNotFound(err) {
|
|
|
|
glog.Errorf("Unable to extract from the response (%s): %%v", s.key, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2014-07-23 00:21:41 +00:00
|
|
|
index = nextIndex
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetchNextState fetches the key (or waits for a change to a key) and then returns
|
2014-09-02 10:00:28 +00:00
|
|
|
// the nextIndex to read. It will watch no longer than s.waitDuration and then return.
|
2014-07-23 00:21:41 +00:00
|
|
|
func (s *SourceEtcd) fetchNextState(fromIndex uint64) (nextIndex uint64, err error) {
|
2014-07-15 14:52:39 +00:00
|
|
|
var response *etcd.Response
|
|
|
|
|
|
|
|
if fromIndex == 0 {
|
|
|
|
response, err = s.client.Get(s.key, true, false)
|
|
|
|
} else {
|
2014-08-19 03:45:37 +00:00
|
|
|
response, err = s.client.Watch(s.key, fromIndex, false, nil, stopChannel(s.timeout))
|
2014-07-15 14:52:39 +00:00
|
|
|
if tools.IsEtcdWatchStoppedByUser(err) {
|
|
|
|
return fromIndex, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
2014-07-23 00:21:41 +00:00
|
|
|
return fromIndex, err
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pods, err := responseToPods(response)
|
|
|
|
if err != nil {
|
|
|
|
glog.Infof("Response was in error: %#v", response)
|
|
|
|
return 0, fmt.Errorf("error parsing response: %#v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.Infof("Got state from etcd: %+v", pods)
|
|
|
|
s.updates <- kubelet.PodUpdate{pods, kubelet.SET}
|
|
|
|
|
2014-07-23 00:21:41 +00:00
|
|
|
return response.Node.ModifiedIndex + 1, nil
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// responseToPods takes an etcd Response object, and turns it into a structured list of containers.
|
|
|
|
// It returns a list of containers, or an error if one occurs.
|
|
|
|
func responseToPods(response *etcd.Response) ([]kubelet.Pod, error) {
|
|
|
|
pods := []kubelet.Pod{}
|
|
|
|
if response.Node == nil || len(response.Node.Value) == 0 {
|
|
|
|
return pods, fmt.Errorf("no nodes field: %v", response)
|
|
|
|
}
|
|
|
|
|
2014-07-23 01:53:41 +00:00
|
|
|
manifests := api.ContainerManifestList{}
|
2014-07-15 14:52:39 +00:00
|
|
|
if err := yaml.Unmarshal([]byte(response.Node.Value), &manifests); err != nil {
|
|
|
|
return pods, fmt.Errorf("could not unmarshal manifests: %v", err)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
pods = append(pods, kubelet.Pod{Name: name, Manifest: manifest})
|
|
|
|
}
|
|
|
|
return pods, nil
|
|
|
|
}
|
|
|
|
|
2014-08-19 03:45:37 +00:00
|
|
|
// stopChannel creates a channel that is closed after a duration for use with etcd client API.
|
|
|
|
// If until is 0, the channel will never close.
|
2014-07-15 14:52:39 +00:00
|
|
|
func stopChannel(until time.Duration) chan bool {
|
|
|
|
stop := make(chan bool)
|
2014-08-19 03:45:37 +00:00
|
|
|
if until == 0 {
|
|
|
|
return stop
|
|
|
|
}
|
2014-07-15 14:52:39 +00:00
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-time.After(until):
|
|
|
|
}
|
|
|
|
stop <- true
|
|
|
|
close(stop)
|
|
|
|
}()
|
|
|
|
return stop
|
|
|
|
}
|