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-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 file or a directory of files.
|
2014-07-15 14:52:39 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2015-01-05 01:30:30 +00:00
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
2014-07-15 14:52:39 +00:00
|
|
|
"fmt"
|
2015-01-05 01:30:30 +00:00
|
|
|
"hash/adler32"
|
2014-07-15 14:52:39 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
|
2014-10-08 19:56:02 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-07-15 14:52:39 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet"
|
2015-01-14 23:22:21 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/types"
|
2014-07-15 14:52:39 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
2014-10-08 19:56:02 +00:00
|
|
|
|
2014-12-01 04:12:37 +00:00
|
|
|
"github.com/ghodss/yaml"
|
2014-07-15 14:52:39 +00:00
|
|
|
"github.com/golang/glog"
|
|
|
|
)
|
|
|
|
|
2014-11-21 21:47:21 +00:00
|
|
|
type sourceFile struct {
|
2014-07-15 14:52:39 +00:00
|
|
|
path string
|
|
|
|
updates chan<- interface{}
|
|
|
|
}
|
|
|
|
|
2014-11-21 21:47:21 +00:00
|
|
|
func NewSourceFile(path string, period time.Duration, updates chan<- interface{}) {
|
|
|
|
config := &sourceFile{
|
2014-07-15 14:52:39 +00:00
|
|
|
path: path,
|
|
|
|
updates: updates,
|
|
|
|
}
|
2014-10-19 21:07:15 +00:00
|
|
|
glog.V(1).Infof("Watching path %q", path)
|
2014-07-15 14:52:39 +00:00
|
|
|
go util.Forever(config.run, period)
|
|
|
|
}
|
|
|
|
|
2014-11-21 21:47:21 +00:00
|
|
|
func (s *sourceFile) run() {
|
2014-07-15 14:52:39 +00:00
|
|
|
if err := s.extractFromPath(); err != nil {
|
2014-10-19 21:07:15 +00:00
|
|
|
glog.Errorf("Unable to read config path %q: %v", s.path, err)
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-21 21:47:21 +00:00
|
|
|
func (s *sourceFile) extractFromPath() error {
|
2014-07-15 14:52:39 +00:00
|
|
|
path := s.path
|
|
|
|
statInfo, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
2014-10-19 21:07:15 +00:00
|
|
|
return err
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
2015-01-09 07:01:07 +00:00
|
|
|
// Emit an update with an empty PodList to allow FileSource to be marked as seen
|
|
|
|
s.updates <- kubelet.PodUpdate{[]api.BoundPod{}, kubelet.SET, kubelet.FileSource}
|
2014-10-19 21:07:15 +00:00
|
|
|
return fmt.Errorf("path does not exist, ignoring")
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case statInfo.Mode().IsDir():
|
|
|
|
pods, err := extractFromDir(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-17 05:11:27 +00:00
|
|
|
s.updates <- kubelet.PodUpdate{pods, kubelet.SET, kubelet.FileSource}
|
2014-07-15 14:52:39 +00:00
|
|
|
|
|
|
|
case statInfo.Mode().IsRegular():
|
|
|
|
pod, err := extractFromFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-17 05:11:27 +00:00
|
|
|
s.updates <- kubelet.PodUpdate{[]api.BoundPod{pod}, kubelet.SET, kubelet.FileSource}
|
2014-07-15 14:52:39 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("path is not a directory or file")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-19 21:07:15 +00:00
|
|
|
// Get as many pod configs as we can from a directory. Return an error iff something
|
|
|
|
// prevented us from reading anything at all. Do not return an error if only some files
|
|
|
|
// were problematic.
|
2014-10-08 19:56:02 +00:00
|
|
|
func extractFromDir(name string) ([]api.BoundPod, error) {
|
2014-10-19 21:07:15 +00:00
|
|
|
dirents, err := filepath.Glob(filepath.Join(name, "[^.]*"))
|
2014-07-15 14:52:39 +00:00
|
|
|
if err != nil {
|
2014-10-19 21:07:15 +00:00
|
|
|
return nil, fmt.Errorf("glob failed: %v", err)
|
2014-10-08 19:56:02 +00:00
|
|
|
}
|
2014-12-01 04:12:37 +00:00
|
|
|
|
|
|
|
pods := make([]api.BoundPod, 0)
|
2014-10-19 21:07:15 +00:00
|
|
|
if len(dirents) == 0 {
|
2014-12-01 04:12:37 +00:00
|
|
|
return pods, nil
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
|
|
|
|
2014-10-19 21:07:15 +00:00
|
|
|
sort.Strings(dirents)
|
|
|
|
for _, path := range dirents {
|
|
|
|
statInfo, err := os.Stat(path)
|
2014-07-15 14:52:39 +00:00
|
|
|
if err != nil {
|
2014-10-19 21:07:15 +00:00
|
|
|
glog.V(1).Infof("Can't get metadata for %q: %v", path, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case statInfo.Mode().IsDir():
|
|
|
|
glog.V(1).Infof("Not recursing into config path %q", path)
|
|
|
|
case statInfo.Mode().IsRegular():
|
|
|
|
pod, err := extractFromFile(path)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(1).Infof("Can't process config file %q: %v", path, err)
|
|
|
|
} else {
|
|
|
|
pods = append(pods, pod)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
glog.V(1).Infof("Config path %q is not a directory or file: %v", path, statInfo.Mode())
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return pods, nil
|
|
|
|
}
|
|
|
|
|
2014-10-19 21:07:15 +00:00
|
|
|
func extractFromFile(filename string) (api.BoundPod, error) {
|
2014-10-08 19:56:02 +00:00
|
|
|
var pod api.BoundPod
|
2014-07-15 14:52:39 +00:00
|
|
|
|
2014-10-19 21:07:15 +00:00
|
|
|
glog.V(3).Infof("Reading config file %q", filename)
|
|
|
|
file, err := os.Open(filename)
|
2014-07-15 14:52:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return pod, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
|
|
|
return pod, err
|
|
|
|
}
|
|
|
|
|
2014-10-08 19:56:02 +00:00
|
|
|
manifest := &api.ContainerManifest{}
|
|
|
|
// TODO: use api.Scheme.DecodeInto
|
|
|
|
if err := yaml.Unmarshal(data, manifest); err != nil {
|
2014-10-19 21:07:15 +00:00
|
|
|
return pod, fmt.Errorf("can't unmarshal file %q: %v", filename, err)
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
|
|
|
|
2014-10-08 19:56:02 +00:00
|
|
|
if err := api.Scheme.Convert(manifest, &pod); err != nil {
|
2014-10-19 21:07:15 +00:00
|
|
|
return pod, fmt.Errorf("can't convert pod from file %q: %v", filename, err)
|
2014-10-08 19:56:02 +00:00
|
|
|
}
|
|
|
|
|
2015-01-05 01:30:30 +00:00
|
|
|
hostname, err := os.Hostname() //TODO: kubelet name would be better
|
|
|
|
if err != nil {
|
|
|
|
return pod, err
|
|
|
|
}
|
|
|
|
|
2014-10-08 19:56:02 +00:00
|
|
|
if len(pod.UID) == 0 {
|
2015-01-05 01:30:30 +00:00
|
|
|
hasher := md5.New()
|
|
|
|
fmt.Fprintf(hasher, "host:%s", hostname)
|
|
|
|
fmt.Fprintf(hasher, "file:%s", filename)
|
|
|
|
util.DeepHashObject(hasher, pod)
|
2015-01-14 23:22:21 +00:00
|
|
|
pod.UID = types.UID(hex.EncodeToString(hasher.Sum(nil)[0:]))
|
2015-01-05 01:30:30 +00:00
|
|
|
glog.V(5).Infof("Generated UID %q for pod %q from file %s", pod.UID, pod.Name, filename)
|
2014-10-08 19:56:02 +00:00
|
|
|
}
|
2015-01-22 19:55:04 +00:00
|
|
|
// This is required for backward compatibility, and should be removed once we
|
|
|
|
// completely deprecate ContainerManifest.
|
|
|
|
if len(pod.Name) == 0 {
|
|
|
|
pod.Name = string(pod.UID)
|
|
|
|
glog.V(5).Infof("Generated Name %q for UID %q from file %s", pod.Name, pod.UID, filename)
|
|
|
|
}
|
2014-10-08 19:56:02 +00:00
|
|
|
if len(pod.Namespace) == 0 {
|
2015-01-05 01:30:30 +00:00
|
|
|
hasher := adler32.New()
|
|
|
|
fmt.Fprint(hasher, filename)
|
|
|
|
// TODO: file-<sum>.hostname would be better, if DNS subdomains
|
|
|
|
// are allowed for namespace (some places only allow DNS
|
|
|
|
// labels).
|
|
|
|
pod.Namespace = fmt.Sprintf("file-%08x-%s", hasher.Sum32(), hostname)
|
|
|
|
glog.V(5).Infof("Generated namespace %q for pod %q from file %s", pod.Namespace, pod.Name, filename)
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
2015-01-05 19:03:51 +00:00
|
|
|
// TODO(dchen1107): BoundPod is not type of runtime.Object. Once we allow kubelet talks
|
|
|
|
// about Pod directly, we can use SelfLinker defined in package: latest
|
|
|
|
// Currently just simply follow the same format in resthandler.go
|
|
|
|
pod.ObjectMeta.SelfLink = fmt.Sprintf("/api/v1beta2/pods/%s?namespace=%s",
|
|
|
|
pod.Name, pod.Namespace)
|
2014-07-15 14:52:39 +00:00
|
|
|
|
2014-10-19 21:07:15 +00:00
|
|
|
if glog.V(4) {
|
|
|
|
glog.Infof("Got pod from file %q: %#v", filename, pod)
|
|
|
|
} else {
|
2014-10-22 17:02:02 +00:00
|
|
|
glog.V(1).Infof("Got pod from file %q: %s.%s (%s)", filename, pod.Namespace, pod.Name, pod.UID)
|
2014-10-19 21:07:15 +00:00
|
|
|
}
|
2014-07-15 14:52:39 +00:00
|
|
|
return pod, nil
|
|
|
|
}
|