Files
kubernetes/pkg/proxy/config/file.go
Clayton Coleman 4e56dafecc Introduce some default log verbosity control
Move a lot of common error logging into better buckets:

glog.Errorf() - Always an error
glog.Warningf() - Something unexpected, but probably not an error
glog.V(0) - Generally useful for this to ALWAYS be visible
            to an operator
            * Programmer errors
            * Logging extra info about a panic
            * CLI argument handling
glog.V(1) - A reasonable default log level if you don't want
            verbosity
            * Information about config (listening on X, watching Y)
            * Errors that repeat frequently that relate to conditions
              that can be corrected (pod detected as unhealthy)
glog.V(2) - Useful steady state information about the service
            * Logging HTTP requests and their exit code
            * System state changing (killing pod)
            * Controller state change events (starting pods)
            * Scheduler log messages
glog.V(3) - Extended information about changes
            * More info about system state changes
glog.V(4) - Debug level verbosity (for now)
            * Logging in particularly thorny parts of code where
              you may want to come back later and check it
2014-09-25 16:30:14 -04:00

131 lines
3.9 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"
"fmt"
"io/ioutil"
"reflect"
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/golang/glog"
)
// serviceConfig is a deserialized form of the config file format which ConfigSourceFile accepts.
type serviceConfig struct {
Services []struct {
Name string `json: "name"`
Port int `json: "port"`
Endpoints []string `json: "endpoints"`
} `json: "service"`
}
// ConfigSourceFile periodically reads service configurations in JSON from a file, and sends the services and endpoints defined in the file to the specified channels.
type ConfigSourceFile struct {
serviceChannel chan ServiceUpdate
endpointsChannel chan EndpointsUpdate
filename string
}
// NewConfigSourceFile creates a new ConfigSourceFile and let it immediately runs the created ConfigSourceFile in a goroutine.
func NewConfigSourceFile(filename string, serviceChannel chan ServiceUpdate, endpointsChannel chan EndpointsUpdate) ConfigSourceFile {
config := ConfigSourceFile{
filename: filename,
serviceChannel: serviceChannel,
endpointsChannel: endpointsChannel,
}
go config.Run()
return config
}
// Run begins watching the config file.
func (s ConfigSourceFile) Run() {
glog.V(1).Infof("Watching file %s", s.filename)
var lastData []byte
var lastServices []api.Service
var lastEndpoints []api.Endpoints
sleep := 5 * time.Second
// Used to avoid spamming the error log file, makes error logging edge triggered.
hadSuccess := true
for {
data, err := ioutil.ReadFile(s.filename)
if err != nil {
msg := fmt.Sprintf("Couldn't read file: %s : %v", s.filename, err)
if hadSuccess {
glog.Error(msg)
} else {
glog.V(1).Info(msg)
}
hadSuccess = false
time.Sleep(sleep)
continue
}
hadSuccess = true
if bytes.Equal(lastData, data) {
time.Sleep(sleep)
continue
}
lastData = data
config := &serviceConfig{}
if err = json.Unmarshal(data, config); err != nil {
glog.Errorf("Couldn't unmarshal configuration from file : %s %v", data, err)
continue
}
// 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{JSONBase: api.JSONBase{ID: service.Name}, Endpoints: service.Endpoints}
}
if !reflect.DeepEqual(lastServices, newServices) {
serviceUpdate := ServiceUpdate{Op: SET, Services: newServices}
s.serviceChannel <- serviceUpdate
lastServices = newServices
}
if !reflect.DeepEqual(lastEndpoints, newEndpoints) {
endpointsUpdate := EndpointsUpdate{Op: SET, Endpoints: newEndpoints}
s.endpointsChannel <- endpointsUpdate
lastEndpoints = newEndpoints
}
time.Sleep(sleep)
}
}