mirror of
https://github.com/optim-enterprises-bv/kubernetes.git
synced 2025-12-03 14:53:52 +00:00
1) imported glog to third_party (previous commit) 2) add support for third_party/update.sh to update just one pkg 3) search-and-replace: s/log.Printf/glog.Infof/ s/log.Print/glog.Info/ s/log.Fatalf/glog.Fatalf/ s/log.Fatal/glog.Fatal/ 4) convert glog.Info.*, err into glog.Error* Adds some util interfaces to logging and calls them from each cmd, which will set the default log output to write to glog. Pass glog-wrapped Loggers to etcd for logging. Log files will go to /tmp - we should probably follow this up with a default log dir for each cmd. The glog lib is sort of weak in that it only flushes every 30 seconds, so we spin up our own flushing goroutine.
112 lines
3.2 KiB
Go
112 lines
3.2 KiB
Go
/*
|
|
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,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
// Reads the configuration from the file. Example file for two services [nodejs & mysql]
|
|
//{"Services": [
|
|
// {
|
|
// "Name":"nodejs",
|
|
// "Port":10000,
|
|
// "Endpoints":["10.240.180.168:8000", "10.240.254.199:8000", "10.240.62.150:8000"]
|
|
// },
|
|
// {
|
|
// "Name":"mysql",
|
|
// "Port":10001,
|
|
// "Endpoints":["10.240.180.168:9000", "10.240.254.199:9000", "10.240.62.150:9000"]
|
|
// }
|
|
//]
|
|
//}
|
|
package config
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"reflect"
|
|
"time"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
"github.com/golang/glog"
|
|
)
|
|
|
|
// TODO: kill this struct.
|
|
type ServiceJSON struct {
|
|
Name string
|
|
Port int
|
|
Endpoints []string
|
|
}
|
|
type ConfigFile struct {
|
|
Services []ServiceJSON
|
|
}
|
|
|
|
type ConfigSourceFile struct {
|
|
serviceChannel chan ServiceUpdate
|
|
endpointsChannel chan EndpointsUpdate
|
|
filename string
|
|
}
|
|
|
|
func NewConfigSourceFile(filename string, serviceChannel chan ServiceUpdate, endpointsChannel chan EndpointsUpdate) ConfigSourceFile {
|
|
config := ConfigSourceFile{
|
|
filename: filename,
|
|
serviceChannel: serviceChannel,
|
|
endpointsChannel: endpointsChannel,
|
|
}
|
|
go config.Run()
|
|
return config
|
|
}
|
|
|
|
func (impl ConfigSourceFile) Run() {
|
|
glog.Infof("Watching file %s", impl.filename)
|
|
var lastData []byte
|
|
var lastServices []api.Service
|
|
var lastEndpoints []api.Endpoints
|
|
|
|
for {
|
|
data, err := ioutil.ReadFile(impl.filename)
|
|
if err != nil {
|
|
glog.Errorf("Couldn't read file: %s : %v", impl.filename, err)
|
|
} else {
|
|
var config ConfigFile
|
|
err = json.Unmarshal(data, &config)
|
|
if err != nil {
|
|
glog.Errorf("Couldn't unmarshal configuration from file : %s %v", data, err)
|
|
} else {
|
|
if !bytes.Equal(lastData, data) {
|
|
lastData = data
|
|
// Ok, we have a valid configuration, send to channel for
|
|
// rejiggering.
|
|
newServices := make([]api.Service, len(config.Services))
|
|
newEndpoints := make([]api.Endpoints, len(config.Services))
|
|
for i, service := range config.Services {
|
|
newServices[i] = api.Service{JSONBase: api.JSONBase{ID: service.Name}, Port: service.Port}
|
|
newEndpoints[i] = api.Endpoints{Name: service.Name, Endpoints: service.Endpoints}
|
|
}
|
|
if !reflect.DeepEqual(lastServices, newServices) {
|
|
serviceUpdate := ServiceUpdate{Op: SET, Services: newServices}
|
|
impl.serviceChannel <- serviceUpdate
|
|
lastServices = newServices
|
|
}
|
|
if !reflect.DeepEqual(lastEndpoints, newEndpoints) {
|
|
endpointsUpdate := EndpointsUpdate{Op: SET, Endpoints: newEndpoints}
|
|
impl.endpointsChannel <- endpointsUpdate
|
|
lastEndpoints = newEndpoints
|
|
}
|
|
}
|
|
}
|
|
}
|
|
time.Sleep(5 * time.Second)
|
|
}
|
|
}
|