Skip to content

Commit

Permalink
Switch to glog for logging, bridge logging to glog.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
thockin committed Jun 25, 2014
1 parent 381ac43 commit 9f9e75f
Show file tree
Hide file tree
Showing 26 changed files with 251 additions and 177 deletions.
14 changes: 8 additions & 6 deletions cmd/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ package main

import (
"flag"
"log"
"net"
"strconv"

"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider"
"github.com/GoogleCloudPlatform/kubernetes/pkg/master"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/golang/glog"
)

var (
Expand All @@ -44,9 +44,11 @@ func init() {

func main() {
flag.Parse()
util.InitLogs()
defer util.FlushLogs()

if len(machineList) == 0 {
log.Fatal("No machines specified!")
glog.Fatal("No machines specified!")
}

var cloud cloudprovider.Interface
Expand All @@ -55,13 +57,13 @@ func main() {
var err error
cloud, err = cloudprovider.NewGCECloud()
if err != nil {
log.Fatal("Couldn't connect to GCE cloud: %#v", err)
glog.Fatal("Couldn't connect to GCE cloud: %#v", err)
}
default:
if len(*cloudProvider) > 0 {
log.Printf("Unknown cloud provider: %s", *cloudProvider)
glog.Infof("Unknown cloud provider: %s", *cloudProvider)
} else {
log.Print("No cloud provider specified.")
glog.Info("No cloud provider specified.")
}
}

Expand All @@ -72,5 +74,5 @@ func main() {
m = master.NewMemoryServer(machineList, cloud)
}

log.Fatal(m.Run(net.JoinHostPort(*address, strconv.Itoa(int(*port))), *apiPrefix))
glog.Fatal(m.Run(net.JoinHostPort(*address, strconv.Itoa(int(*port))), *apiPrefix))
}
43 changes: 23 additions & 20 deletions cmd/cloudcfg/cloudcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
"strconv"
Expand All @@ -29,6 +28,8 @@ import (

kube_client "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudcfg"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/golang/glog"
)

const APP_VERSION = "0.1"
Expand Down Expand Up @@ -66,21 +67,21 @@ func usage() {
flag.PrintDefaults()
}

// Reads & parses config file. On error, calls log.Fatal().
// Reads & parses config file. On error, calls glog.Fatal().
func readConfig(storage string) []byte {
if len(*config) == 0 {
log.Fatal("Need config file (-c)")
glog.Fatal("Need config file (-c)")
}
data, err := ioutil.ReadFile(*config)
if err != nil {
log.Fatalf("Unable to read %v: %#v\n", *config, err)
glog.Fatalf("Unable to read %v: %#v\n", *config, err)
}
data, err = cloudcfg.ToWireFormat(data, storage)
if err != nil {
log.Fatalf("Error parsing %v as an object for %v: %#v\n", *config, storage, err)
glog.Fatalf("Error parsing %v as an object for %v: %#v\n", *config, storage, err)
}
if *verbose {
log.Printf("Parsed config file successfully; sending:\n%v\n", string(data))
glog.Infof("Parsed config file successfully; sending:\n%v\n", string(data))
}
return data
}
Expand All @@ -92,6 +93,8 @@ func main() {
}

flag.Parse() // Scan the arguments list
util.InitLogs()
defer util.FlushLogs()

if *versionFlag {
fmt.Println("Version:", APP_VERSION)
Expand All @@ -101,7 +104,7 @@ func main() {
secure := true
parsedUrl, err := url.Parse(*httpServer)
if err != nil {
log.Fatalf("Unable to parse %v as a URL\n", err)
glog.Fatalf("Unable to parse %v as a URL\n", err)
}
if parsedUrl.Scheme != "" && parsedUrl.Scheme != "https" {
secure = false
Expand All @@ -111,14 +114,14 @@ func main() {
if secure {
auth, err = cloudcfg.LoadAuthInfo(*authConfig)
if err != nil {
log.Fatalf("Error loading auth: %#v", err)
glog.Fatalf("Error loading auth: %#v", err)
}
}

if *proxy {
log.Println("Starting to serve on localhost:8001")
glog.Info("Starting to serve on localhost:8001")
server := cloudcfg.NewProxyServer(*www, *httpServer, auth)
log.Fatal(server.Serve())
glog.Fatal(server.Serve())
}

if len(flag.Args()) < 1 {
Expand All @@ -129,15 +132,15 @@ func main() {

matchFound := executeAPIRequest(method, auth) || executeControllerRequest(method, auth)
if matchFound == false {
log.Fatalf("Unknown command %s", method)
glog.Fatalf("Unknown command %s", method)
}
}

// Attempts to execute an API request
func executeAPIRequest(method string, auth *kube_client.AuthInfo) bool {
parseStorage := func() string {
if len(flag.Args()) != 2 {
log.Fatal("usage: cloudcfg [OPTIONS] get|list|create|update|delete <url>")
glog.Fatal("usage: cloudcfg [OPTIONS] get|list|create|update|delete <url>")
}
return strings.Trim(flag.Arg(1), "/")
}
Expand Down Expand Up @@ -165,7 +168,7 @@ func executeAPIRequest(method string, auth *kube_client.AuthInfo) bool {
}
obj, err := r.Do().Get()
if err != nil {
log.Fatalf("Got request error: %v\n", err)
glog.Fatalf("Got request error: %v\n", err)
return false
}

Expand All @@ -179,7 +182,7 @@ func executeAPIRequest(method string, auth *kube_client.AuthInfo) bool {
}

if err = printer.PrintObj(obj, os.Stdout); err != nil {
log.Fatalf("Failed to print: %#v\nRaw received object:\n%#v\n", err, obj)
glog.Fatalf("Failed to print: %#v\nRaw received object:\n%#v\n", err, obj)
}
fmt.Print("\n")

Expand All @@ -190,7 +193,7 @@ func executeAPIRequest(method string, auth *kube_client.AuthInfo) bool {
func executeControllerRequest(method string, auth *kube_client.AuthInfo) bool {
parseController := func() string {
if len(flag.Args()) != 2 {
log.Fatal("usage: cloudcfg [OPTIONS] stop|rm|rollingupdate <controller>")
glog.Fatal("usage: cloudcfg [OPTIONS] stop|rm|rollingupdate <controller>")
}
return flag.Arg(1)
}
Expand All @@ -207,31 +210,31 @@ func executeControllerRequest(method string, auth *kube_client.AuthInfo) bool {
err = cloudcfg.Update(parseController(), c, *updatePeriod)
case "run":
if len(flag.Args()) != 4 {
log.Fatal("usage: cloudcfg [OPTIONS] run <image> <replicas> <controller>")
glog.Fatal("usage: cloudcfg [OPTIONS] run <image> <replicas> <controller>")
}
image := flag.Arg(1)
replicas, err := strconv.Atoi(flag.Arg(2))
name := flag.Arg(3)
if err != nil {
log.Fatalf("Error parsing replicas: %#v", err)
glog.Fatalf("Error parsing replicas: %#v", err)
}
err = cloudcfg.RunController(image, name, replicas, c, *portSpec, *servicePort)
case "resize":
args := flag.Args()
if len(args) < 3 {
log.Fatal("usage: cloudcfg resize <controller> <replicas>")
glog.Fatal("usage: cloudcfg resize <controller> <replicas>")
}
name := args[1]
replicas, err := strconv.Atoi(args[2])
if err != nil {
log.Fatalf("Error parsing replicas: %#v", err)
glog.Fatalf("Error parsing replicas: %#v", err)
}
err = cloudcfg.ResizeController(name, replicas, c)
default:
return false
}
if err != nil {
log.Fatalf("Error: %#v", err)
glog.Fatalf("Error: %#v", err)
}
return true
}
10 changes: 6 additions & 4 deletions cmd/controller-manager/controller-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ package main

import (
"flag"
"log"
"os"
"time"

"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/controller"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/coreos/go-etcd/etcd"
"github.com/golang/glog"
)

var (
Expand All @@ -39,13 +39,15 @@ var (

func main() {
flag.Parse()
util.InitLogs()
defer util.FlushLogs()

if len(*etcd_servers) == 0 || len(*master) == 0 {
log.Fatal("usage: controller-manager -etcd_servers <servers> -master <master>")
glog.Fatal("usage: controller-manager -etcd_servers <servers> -master <master>")
}

// Set up logger for etcd client
etcd.SetLogger(log.New(os.Stderr, "etcd ", log.LstdFlags))
etcd.SetLogger(util.NewLogger("etcd "))

controllerManager := controller.MakeReplicationManager(
etcd.NewClient([]string{*etcd_servers}),
Expand Down
24 changes: 14 additions & 10 deletions cmd/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"time"
Expand All @@ -31,14 +30,19 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/controller"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet"
"github.com/GoogleCloudPlatform/kubernetes/pkg/master"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/coreos/go-etcd/etcd"
"github.com/golang/glog"
)

func main() {
util.InitLogs()
defer util.FlushLogs()

manifestUrl := ServeCachedManifestFile()
// Setup
servers := []string{"http://localhost:4001"}
log.Printf("Creating etcd client pointing to %v", servers)
glog.Infof("Creating etcd client pointing to %v", servers)
machineList := []string{"localhost", "machine"}

// Master
Expand Down Expand Up @@ -75,30 +79,30 @@ func main() {
go otherKubelet.RunKubelet("", "", servers[0], "localhost", 0)

// Ok. we're good to go.
log.Printf("API Server started on %s", apiserver.URL)
glog.Infof("API Server started on %s", apiserver.URL)
// Wait for the synchronization threads to come up.
time.Sleep(time.Second * 10)

kubeClient := client.New(apiserver.URL, nil)
data, err := ioutil.ReadFile("api/examples/controller.json")
if err != nil {
log.Fatalf("Unexpected error: %#v", err)
glog.Fatalf("Unexpected error: %#v", err)
}
var controllerRequest api.ReplicationController
if err = json.Unmarshal(data, &controllerRequest); err != nil {
log.Fatalf("Unexpected error: %#v", err)
glog.Fatalf("Unexpected error: %#v", err)
}

if _, err = kubeClient.CreateReplicationController(controllerRequest); err != nil {
log.Fatalf("Unexpected error: %#v", err)
glog.Fatalf("Unexpected error: %#v", err)
}
// Give the controllers some time to actually create the pods
time.Sleep(time.Second * 10)

// Validate that they're truly up.
pods, err := kubeClient.ListPods(nil)
if err != nil || len(pods.Items) != 2 {
log.Fatal("FAILED")
glog.Fatal("FAILED")
}

// Check that kubelet tried to make the pods.
Expand All @@ -120,9 +124,9 @@ func main() {
// We expect 5: 2 net containers + 2 pods from the replication controller +
// 1 net container + 2 pods from the URL.
if len(createdPods) != 7 {
log.Fatalf("Unexpected list of created pods: %#v\n", createdPods)
glog.Fatalf("Unexpected list of created pods: %#v\n", createdPods)
}
log.Printf("OK")
glog.Infof("OK")
}

// Serve a file for kubelet to read.
Expand All @@ -132,7 +136,7 @@ func ServeCachedManifestFile() (servingAddress string) {
w.Write([]byte(testManifestFile))
return
}
log.Fatalf("Got request: %#v\n", r)
glog.Fatalf("Got request: %#v\n", r)
http.NotFound(w, r)
}))
return server.URL + "/manifest"
Expand Down
12 changes: 7 additions & 5 deletions cmd/kubelet/kubelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ package main

import (
"flag"
"log"
"math/rand"
"os"
"os/exec"
"time"

"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/coreos/go-etcd/etcd"
"github.com/fsouza/go-dockerclient"
"github.com/golang/glog"
)

var (
Expand All @@ -49,15 +49,17 @@ const dockerBinary = "/usr/bin/docker"

func main() {
flag.Parse()
util.InitLogs()
defer util.FlushLogs()
rand.Seed(time.Now().UTC().UnixNano())

// Set up logger for etcd client
etcd.SetLogger(log.New(os.Stderr, "etcd ", log.LstdFlags))
etcd.SetLogger(util.NewLogger("etcd "))

endpoint := "unix:///var/run/docker.sock"
dockerClient, err := docker.NewClient(endpoint)
if err != nil {
log.Fatal("Couldn't connnect to docker.")
glog.Fatal("Couldn't connnect to docker.")
}

hostname := []byte(*hostnameOverride)
Expand All @@ -66,7 +68,7 @@ func main() {
// want the FQDN, and this is the easiest way to get it.
hostname, err = exec.Command("hostname", "-f").Output()
if err != nil {
log.Fatalf("Couldn't determine hostname: %v", err)
glog.Fatalf("Couldn't determine hostname: %v", err)
}
}

Expand Down
Loading

0 comments on commit 9f9e75f

Please sign in to comment.