Switch to glog for logging, bridge logging to glog.

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.
This commit is contained in:
Tim Hockin
2014-06-24 20:51:57 -07:00
parent 381ac4328c
commit 9f9e75f508
26 changed files with 251 additions and 177 deletions

View File

@@ -17,11 +17,11 @@ limitations under the License.
package config
import (
"log"
"sync"
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/golang/glog"
)
type Operation int
@@ -110,14 +110,14 @@ func NewServiceConfig() ServiceConfig {
}
func (impl *ServiceConfig) Run() {
log.Printf("Starting the config Run loop")
glog.Infof("Starting the config Run loop")
for {
select {
case source := <-impl.serviceNotifyChannel:
log.Printf("Got new service configuration from source %s", source)
glog.Infof("Got new service configuration from source %s", source)
impl.NotifyServiceUpdate()
case source := <-impl.endpointsNotifyChannel:
log.Printf("Got new endpoint configuration from source %s", source)
glog.Infof("Got new endpoint configuration from source %s", source)
impl.NotifyEndpointsUpdate()
case <-time.After(1 * time.Second):
}
@@ -132,24 +132,24 @@ func (impl *ServiceConfig) ServiceChannelListener(source string, listenChannel c
case update := <-listenChannel:
switch update.Op {
case ADD:
log.Printf("Adding new service from source %s : %v", source, update.Services)
glog.Infof("Adding new service from source %s : %v", source, update.Services)
for _, value := range update.Services {
serviceMap[value.ID] = value
}
case REMOVE:
log.Printf("Removing a service %v", update)
glog.Infof("Removing a service %v", update)
for _, value := range update.Services {
delete(serviceMap, value.ID)
}
case SET:
log.Printf("Setting services %v", update)
glog.Infof("Setting services %v", update)
// Clear the old map entries by just creating a new map
serviceMap = make(map[string]api.Service)
for _, value := range update.Services {
serviceMap[value.ID] = value
}
default:
log.Printf("Received invalid update type: %v", update)
glog.Infof("Received invalid update type: %v", update)
continue
}
impl.configLock.Lock()
@@ -167,25 +167,25 @@ func (impl *ServiceConfig) EndpointsChannelListener(source string, listenChannel
case update := <-listenChannel:
switch update.Op {
case ADD:
log.Printf("Adding a new endpoint %v", update)
glog.Infof("Adding a new endpoint %v", update)
for _, value := range update.Endpoints {
endpointMap[value.Name] = value
}
case REMOVE:
log.Printf("Removing an endpoint %v", update)
glog.Infof("Removing an endpoint %v", update)
for _, value := range update.Endpoints {
delete(endpointMap, value.Name)
}
case SET:
log.Printf("Setting services %v", update)
glog.Infof("Setting services %v", update)
// Clear the old map entries by just creating a new map
endpointMap = make(map[string]api.Endpoints)
for _, value := range update.Endpoints {
endpointMap[value.Name] = value
}
default:
log.Printf("Received invalid update type: %v", update)
glog.Infof("Received invalid update type: %v", update)
continue
}
impl.configLock.Lock()
@@ -280,7 +280,7 @@ func (impl *ServiceConfig) NotifyServiceUpdate() {
}
}
impl.configLock.RUnlock()
log.Printf("Unified configuration %+v", services)
glog.Infof("Unified configuration %+v", services)
impl.handlerLock.RLock()
handlers := impl.serviceHandlers
impl.handlerLock.RUnlock()
@@ -300,7 +300,7 @@ func (impl *ServiceConfig) NotifyEndpointsUpdate() {
}
}
impl.configLock.RUnlock()
log.Printf("Unified configuration %+v", endpoints)
glog.Infof("Unified configuration %+v", endpoints)
impl.handlerLock.RLock()
handlers := impl.endpointHandlers
impl.handlerLock.RUnlock()

View File

@@ -36,12 +36,12 @@ package config
import (
"encoding/json"
"fmt"
"log"
"strings"
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/coreos/go-etcd/etcd"
"github.com/golang/glog"
)
const RegistryRoot = "registry/services"
@@ -72,7 +72,7 @@ func (impl ConfigSourceEtcd) Run() {
if err == nil {
break
}
log.Printf("Failed to get any services: %v", err)
glog.Errorf("Failed to get any services: %v", err)
time.Sleep(2 * time.Second)
}
@@ -92,7 +92,7 @@ func (impl ConfigSourceEtcd) Run() {
for {
services, endpoints, err = impl.GetServices()
if err != nil {
log.Printf("ConfigSourceEtcd: Failed to get services: %v", err)
glog.Errorf("ConfigSourceEtcd: Failed to get services: %v", err)
} else {
if len(services) > 0 {
serviceUpdate := ServiceUpdate{Op: SET, Services: services}
@@ -112,7 +112,7 @@ func (impl ConfigSourceEtcd) Run() {
func (impl ConfigSourceEtcd) GetServices() ([]api.Service, []api.Endpoints, error) {
response, err := impl.client.Get(RegistryRoot+"/specs", true, false)
if err != nil {
log.Printf("Failed to get the key %s: %v", RegistryRoot, err)
glog.Errorf("Failed to get the key %s: %v", RegistryRoot, err)
return make([]api.Service, 0), make([]api.Endpoints, 0), err
}
if response.Node.Dir == true {
@@ -125,15 +125,15 @@ func (impl ConfigSourceEtcd) GetServices() ([]api.Service, []api.Endpoints, erro
var svc api.Service
err = json.Unmarshal([]byte(node.Value), &svc)
if err != nil {
log.Printf("Failed to load Service: %s (%#v)", node.Value, err)
glog.Errorf("Failed to load Service: %s (%#v)", node.Value, err)
continue
}
retServices[i] = svc
endpoints, err := impl.GetEndpoints(svc.ID)
if err != nil {
log.Printf("Couldn't get endpoints for %s : %v skipping", svc.ID, err)
glog.Errorf("Couldn't get endpoints for %s : %v skipping", svc.ID, err)
}
log.Printf("Got service: %s on localport %d mapping to: %s", svc.ID, svc.Port, endpoints)
glog.Infof("Got service: %s on localport %d mapping to: %s", svc.ID, svc.Port, endpoints)
retEndpoints[i] = endpoints
}
return retServices, retEndpoints, err
@@ -145,7 +145,7 @@ func (impl ConfigSourceEtcd) GetEndpoints(service string) (api.Endpoints, error)
key := fmt.Sprintf(RegistryRoot + "/endpoints/" + service)
response, err := impl.client.Get(key, true, false)
if err != nil {
log.Printf("Failed to get the key: %s %v", key, err)
glog.Errorf("Failed to get the key: %s %v", key, err)
return api.Endpoints{}, err
}
// Parse all the endpoint specifications in this value.
@@ -173,7 +173,7 @@ func ParseEndpoints(jsonString string) (api.Endpoints, error) {
}
func (impl ConfigSourceEtcd) WatchForChanges() {
log.Print("Setting up a watch for new services")
glog.Info("Setting up a watch for new services")
watchChannel := make(chan *etcd.Response)
go impl.client.Watch("/registry/services/", 0, true, watchChannel, nil)
for {
@@ -183,7 +183,7 @@ func (impl ConfigSourceEtcd) WatchForChanges() {
}
func (impl ConfigSourceEtcd) ProcessChange(response *etcd.Response) {
log.Printf("Processing a change in service configuration... %s", *response)
glog.Infof("Processing a change in service configuration... %s", *response)
// If it's a new service being added (signified by a localport being added)
// then process it as such
@@ -192,11 +192,11 @@ func (impl ConfigSourceEtcd) ProcessChange(response *etcd.Response) {
} else if response.Action == "set" {
service, err := EtcdResponseToService(response)
if err != nil {
log.Printf("Failed to parse %s Port: %s", response, err)
glog.Errorf("Failed to parse %s Port: %s", response, err)
return
}
log.Printf("New service added/updated: %#v", service)
glog.Infof("New service added/updated: %#v", service)
serviceUpdate := ServiceUpdate{Op: ADD, Services: []api.Service{*service}}
impl.serviceChannel <- serviceUpdate
return
@@ -204,22 +204,22 @@ func (impl ConfigSourceEtcd) ProcessChange(response *etcd.Response) {
if response.Action == "delete" {
parts := strings.Split(response.Node.Key[1:], "/")
if len(parts) == 4 {
log.Printf("Deleting service: %s", parts[3])
glog.Infof("Deleting service: %s", parts[3])
serviceUpdate := ServiceUpdate{Op: REMOVE, Services: []api.Service{{JSONBase: api.JSONBase{ID: parts[3]}}}}
impl.serviceChannel <- serviceUpdate
return
} else {
log.Printf("Unknown service delete: %#v", parts)
glog.Infof("Unknown service delete: %#v", parts)
}
}
}
func (impl ConfigSourceEtcd) ProcessEndpointResponse(response *etcd.Response) {
log.Printf("Processing a change in endpoint configuration... %s", *response)
glog.Infof("Processing a change in endpoint configuration... %s", *response)
var endpoints api.Endpoints
err := json.Unmarshal([]byte(response.Node.Value), &endpoints)
if err != nil {
log.Printf("Failed to parse service out of etcd key: %v : %+v", response.Node.Value, err)
glog.Errorf("Failed to parse service out of etcd key: %v : %+v", response.Node.Value, err)
return
}
endpointsUpdate := EndpointsUpdate{Op: ADD, Endpoints: []api.Endpoints{endpoints}}

View File

@@ -34,11 +34,11 @@ import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"reflect"
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/golang/glog"
)
// TODO: kill this struct.
@@ -68,7 +68,7 @@ func NewConfigSourceFile(filename string, serviceChannel chan ServiceUpdate, end
}
func (impl ConfigSourceFile) Run() {
log.Printf("Watching file %s", impl.filename)
glog.Infof("Watching file %s", impl.filename)
var lastData []byte
var lastServices []api.Service
var lastEndpoints []api.Endpoints
@@ -76,12 +76,12 @@ func (impl ConfigSourceFile) Run() {
for {
data, err := ioutil.ReadFile(impl.filename)
if err != nil {
log.Printf("Couldn't read file: %s : %v", impl.filename, err)
glog.Errorf("Couldn't read file: %s : %v", impl.filename, err)
} else {
var config ConfigFile
err = json.Unmarshal(data, &config)
if err != nil {
log.Printf("Couldn't unmarshal configuration from file : %s %v", data, err)
glog.Errorf("Couldn't unmarshal configuration from file : %s %v", data, err)
} else {
if !bytes.Equal(lastData, data) {
lastData = data

View File

@@ -19,11 +19,11 @@ package proxy
import (
"fmt"
"io"
"log"
"net"
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/golang/glog"
)
// Proxier is a simple proxy for tcp connections between a localhost:lport and services that provide
@@ -38,11 +38,11 @@ func NewProxier(loadBalancer LoadBalancer) *Proxier {
}
func CopyBytes(in, out *net.TCPConn) {
log.Printf("Copying from %v <-> %v <-> %v <-> %v",
glog.Infof("Copying from %v <-> %v <-> %v <-> %v",
in.RemoteAddr(), in.LocalAddr(), out.LocalAddr(), out.RemoteAddr())
_, err := io.Copy(in, out)
if err != nil && err != io.EOF {
log.Printf("I/O error: %v", err)
glog.Errorf("I/O error: %v", err)
}
in.CloseRead()
@@ -51,7 +51,7 @@ func CopyBytes(in, out *net.TCPConn) {
// Create a bidirectional byte shuffler. Copies bytes to/from each connection.
func ProxyConnection(in, out *net.TCPConn) {
log.Printf("Creating proxy between %v <-> %v <-> %v <-> %v",
glog.Infof("Creating proxy between %v <-> %v <-> %v <-> %v",
in.RemoteAddr(), in.LocalAddr(), out.LocalAddr(), out.RemoteAddr())
go CopyBytes(in, out)
go CopyBytes(out, in)
@@ -61,25 +61,25 @@ func (proxier Proxier) AcceptHandler(service string, listener net.Listener) {
for {
inConn, err := listener.Accept()
if err != nil {
log.Printf("Accept failed: %v", err)
glog.Errorf("Accept failed: %v", err)
continue
}
log.Printf("Accepted connection from: %v to %v", inConn.RemoteAddr(), inConn.LocalAddr())
glog.Infof("Accepted connection from: %v to %v", inConn.RemoteAddr(), inConn.LocalAddr())
// Figure out where this request should go.
endpoint, err := proxier.loadBalancer.LoadBalance(service, inConn.RemoteAddr())
if err != nil {
log.Printf("Couldn't find an endpoint for %s %v", service, err)
glog.Errorf("Couldn't find an endpoint for %s %v", service, err)
inConn.Close()
continue
}
log.Printf("Mapped service %s to endpoint %s", service, endpoint)
glog.Infof("Mapped service %s to endpoint %s", service, endpoint)
outConn, err := net.DialTimeout("tcp", endpoint, time.Duration(5)*time.Second)
// We basically need to take everything from inConn and send to outConn
// and anything coming from outConn needs to be sent to inConn.
if err != nil {
log.Printf("Dial failed: %v", err)
glog.Errorf("Dial failed: %v", err)
inConn.Close()
continue
}
@@ -112,22 +112,22 @@ func (proxier Proxier) addServiceOnUnusedPort(service string) (string, error) {
}
func (proxier Proxier) addServiceCommon(service string, l net.Listener) {
log.Printf("Listening for %s on %s", service, l.Addr().String())
glog.Infof("Listening for %s on %s", service, l.Addr().String())
// If that succeeds, start the accepting loop.
go proxier.AcceptHandler(service, l)
}
func (proxier Proxier) OnUpdate(services []api.Service) {
log.Printf("Received update notice: %+v", services)
glog.Infof("Received update notice: %+v", services)
for _, service := range services {
port, exists := proxier.serviceMap[service.ID]
if !exists || port != service.Port {
log.Printf("Adding a new service %s on port %d", service.ID, service.Port)
glog.Infof("Adding a new service %s on port %d", service.ID, service.Port)
err := proxier.AddService(service.ID, service.Port)
if err == nil {
proxier.serviceMap[service.ID] = service.Port
} else {
log.Printf("Failed to start listening for %s on %d", service.ID, service.Port)
glog.Infof("Failed to start listening for %s on %d", service.ID, service.Port)
}
}
}

View File

@@ -20,7 +20,6 @@ package proxy
import (
"errors"
"log"
"net"
"reflect"
"strconv"
@@ -28,6 +27,7 @@ import (
"sync"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/golang/glog"
)
type LoadBalancerRR struct {
@@ -86,7 +86,7 @@ func (impl LoadBalancerRR) OnUpdate(endpoints []api.Endpoints) {
for _, value := range endpoints {
existingEndpoints, exists := impl.endpointsMap[value.Name]
if !exists || !reflect.DeepEqual(value.Endpoints, existingEndpoints) {
log.Printf("LoadBalancerRR: Setting endpoints for %s to %+v", value.Name, value.Endpoints)
glog.Infof("LoadBalancerRR: Setting endpoints for %s to %+v", value.Name, value.Endpoints)
impl.endpointsMap[value.Name] = impl.FilterValidEndpoints(value.Endpoints)
// Start RR from the beginning if added or updated.
impl.rrIndex[value.Name] = 0
@@ -97,7 +97,7 @@ func (impl LoadBalancerRR) OnUpdate(endpoints []api.Endpoints) {
for key, value := range impl.endpointsMap {
_, exists := tmp[key]
if !exists {
log.Printf("LoadBalancerRR: Removing endpoints for %s -> %+v", key, value)
glog.Infof("LoadBalancerRR: Removing endpoints for %s -> %+v", key, value)
delete(impl.endpointsMap, key)
}
}