Skip to content

Commit

Permalink
Add an source to all log statements
Browse files Browse the repository at this point in the history
This includes a `logger` as a data members for all objects,
which sets a default "source" field on every log statement
so that it's easy to track where log messages are coming from.

This is particularly useful as the controller for Agones
gets more complicated.
  • Loading branch information
markmandel committed Feb 23, 2018
1 parent 4409235 commit d99b932
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 125 deletions.
33 changes: 16 additions & 17 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
package main

import (
"strings"
"time"
"os"
"path/filepath"
"strings"
"time"

"agones.dev/agones/pkg"
"agones.dev/agones/pkg/client/clientset/versioned"
Expand All @@ -28,7 +28,6 @@ import (
"agones.dev/agones/pkg/util/runtime"
"agones.dev/agones/pkg/util/signals"
"agones.dev/agones/pkg/util/webhooks"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
extclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
Expand All @@ -46,15 +45,15 @@ const (
keyFileFlag = "key-file"
)

func init() {
logrus.SetFormatter(&logrus.JSONFormatter{})
}
var (
logger = runtime.NewLoggerWithSource("main")
)

// main starts the operator for the gameserver CRD
func main() {
exec, err := os.Executable()
if err != nil {
logrus.WithError(err).Fatal("Could not get executable path")
logger.WithError(err).Fatal("Could not get executable path")
}

base := filepath.Dir(exec)
Expand Down Expand Up @@ -87,7 +86,7 @@ func main() {
keyFile := viper.GetString(keyFileFlag)
certFile := viper.GetString(certFileFlag)

logrus.WithField(sidecarFlag, sidecarImage).
logger.WithField(sidecarFlag, sidecarImage).
WithField("minPort", minPort).
WithField("maxPort", maxPort).
WithField(keyFileFlag, keyFile).
Expand All @@ -96,29 +95,29 @@ func main() {
WithField("Version", pkg.Version).Info("starting gameServer operator...")

if minPort <= 0 || maxPort <= 0 {
logrus.Fatal("Min Port and Max Port values are required.")
logger.Fatal("Min Port and Max Port values are required.")
} else if maxPort < minPort {
logrus.Fatal("Max Port cannot be set less that the Min Port")
logger.Fatal("Max Port cannot be set less that the Min Port")
}

config, err := rest.InClusterConfig()
if err != nil {
logrus.WithError(err).Fatal("Could not create in cluster config")
logger.WithError(err).Fatal("Could not create in cluster config")
}

kubeClient, err := kubernetes.NewForConfig(config)
if err != nil {
logrus.WithError(err).Fatal("Could not create the kubernetes clientset")
logger.WithError(err).Fatal("Could not create the kubernetes clientset")
}

extClient, err := extclientset.NewForConfig(config)
if err != nil {
logrus.WithError(err).Fatal("Could not create the api extension clientset")
logger.WithError(err).Fatal("Could not create the api extension clientset")
}

agonesClient, err := versioned.NewForConfig(config)
if err != nil {
logrus.WithError(err).Fatal("Could not create the agones api clientset")
logger.WithError(err).Fatal("Could not create the agones api clientset")
}

agonesInformerFactory := externalversions.NewSharedInformerFactory(agonesClient, 30*time.Second)
Expand All @@ -134,14 +133,14 @@ func main() {

go func() {
if err := wh.Run(stop); err != nil { // nolint: vetshadow
logrus.WithError(err).Fatal("could not run webhook server")
logger.WithError(err).Fatal("could not run webhook server")
}
}()

err = c.Run(2, stop)
if err != nil {
logrus.WithError(err).Fatal("Could not run gameserver controller")
logger.WithError(err).Fatal("Could not run gameserver controller")
}

logrus.Info("Shut down gameserver controller")
logger.Info("Shut down gameserver controller")
}
21 changes: 10 additions & 11 deletions cmd/sdk-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"agones.dev/agones/pkg/gameservers"
"agones.dev/agones/pkg/sdk"
"agones.dev/agones/pkg/util/runtime"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"golang.org/x/net/context"
Expand All @@ -51,9 +50,9 @@ const (
healthFailureThresholdFlag = "health-failure-threshold"
)

func init() {
logrus.SetFormatter(&logrus.JSONFormatter{})
}
var (
logger = runtime.NewLoggerWithSource("main")
)

func main() {
viper.SetDefault(localFlag, false)
Expand Down Expand Up @@ -92,15 +91,15 @@ func main() {
healthInitialDelay := time.Duration(viper.GetInt64(healthInitialDelayFlag)) * time.Second
healthFailureThreshold := viper.GetInt64(healthFailureThresholdFlag)

logrus.WithField(localFlag, isLocal).WithField("version", pkg.Version).
logger.WithField(localFlag, isLocal).WithField("version", pkg.Version).
WithField("port", port).WithField(addressFlag, address).
WithField(healthDisabledFlag, healthDisabled).WithField(healthTimeoutFlag, healthTimeout).
WithField(healthFailureThresholdFlag, healthFailureThreshold).
WithField(healthInitialDelayFlag, healthInitialDelay).Info("Starting sdk sidecar")

lis, err := net.Listen("tcp", fmt.Sprintf("%s:%d", address, port))
if err != nil {
logrus.WithField("port", port).WithField("address", address).Fatalf("Could not listen on port")
logger.WithField("port", port).WithField("address", address).Fatalf("Could not listen on port")
}
grpcServer := grpc.NewServer()

Expand All @@ -109,24 +108,24 @@ func main() {
} else {
config, err := rest.InClusterConfig()
if err != nil {
logrus.WithError(err).Fatal("Could not create in cluster config")
logger.WithError(err).Fatal("Could not create in cluster config")
}

kubeClient, err := kubernetes.NewForConfig(config)
if err != nil {
logrus.WithError(err).Fatal("Could not create the kubernetes clientset")
logger.WithError(err).Fatal("Could not create the kubernetes clientset")
}

agonesClient, err := versioned.NewForConfig(config)
if err != nil {
logrus.WithError(err).Fatalf("Could not create the agones api clientset")
logger.WithError(err).Fatalf("Could not create the agones api clientset")
}

var s *gameservers.SDKServer
s, err = gameservers.NewSDKServer(viper.GetString(gameServerNameEnv), viper.GetString(podNamespaceEnv),
healthDisabled, healthTimeout, healthFailureThreshold, healthInitialDelay, kubeClient, agonesClient)
if err != nil {
logrus.WithError(err).Fatalf("Could not start sidecar")
logger.WithError(err).Fatalf("Could not start sidecar")
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand All @@ -137,6 +136,6 @@ func main() {

err = grpcServer.Serve(lis)
if err != nil {
logrus.WithError(err).Error("Could not serve grpc server")
logger.WithError(err).Error("Could not serve grpc server")
}
}
2 changes: 0 additions & 2 deletions pkg/apis/stable/v1alpha1/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"testing"

"agones.dev/agones/pkg/apis/stable"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -218,7 +217,6 @@ func TestGameServerPod(t *testing.T) {
assert.Equal(t, fixture.Spec.HostPort, pod.Spec.Containers[0].Ports[0].HostPort)
assert.Equal(t, fixture.Spec.ContainerPort, pod.Spec.Containers[0].Ports[0].ContainerPort)
assert.Equal(t, corev1.Protocol("UDP"), pod.Spec.Containers[0].Ports[0].Protocol)
logrus.SetFormatter(&logrus.JSONFormatter{})
assert.True(t, metav1.IsControlledBy(pod, fixture))

sidecar := corev1.Container{Name: "sidecar", Image: "container/sidecar"}
Expand Down
Loading

0 comments on commit d99b932

Please sign in to comment.