Skip to content

Commit

Permalink
Merge branch 'master' into jmedved
Browse files Browse the repository at this point in the history
  • Loading branch information
jmedved committed Aug 24, 2017
2 parents e9baddf + 60b676e commit 0bc5124
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 62 deletions.
31 changes: 16 additions & 15 deletions config/plugin_config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config

import (
"flag"
"github.com/namsral/flag"
)

// PluginConfig is API for plugins to access configuration.
Expand All @@ -23,25 +23,16 @@ type PluginConfig interface {
//
// It tries to lookup `plugin + "-config"` in flags.
func ForPlugin(pluginName string) PluginConfig {
plugCfg := pluginName + "-config"
flg := flag.CommandLine.Lookup(plugCfg)
if flg != nil {
val := flg.Value.String()
if val != "" {
plugCfg = val
}
}

return &pluginConfig{configFile: plugCfg}
return &pluginConfig{pluginName: pluginName}
}

type pluginConfig struct {
configFile string
pluginName string
}

// GetValue binds the configuration to config method argument
func (p *pluginConfig) GetValue(config interface{}) (found bool, err error) {
err = ParseConfigFromYamlFile(p.configFile, config) //TODO switch to Viper
err = ParseConfigFromYamlFile(p.GetConfigName(), config) //TODO switch to Viper
if err != nil {
return false, err
}
Expand All @@ -51,5 +42,15 @@ func (p *pluginConfig) GetValue(config interface{}) (found bool, err error) {

// GetConfigName - see description in PluginConfig.GetConfigName
func (p *pluginConfig) GetConfigName() string {
return p.configFile
}
plugCfg := p.pluginName + "-config"
flg := flag.CommandLine.Lookup(plugCfg)
if flg != nil {
val := flg.Value.String()

if val != "" {
plugCfg = val
}
}

return plugCfg
}
5 changes: 5 additions & 0 deletions db/keyval/etcdv3/plugin_impl_etcdv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func (p *Plugin) Init() (err error) {
// Retrieve config
var cfg Config
_, err = p.PluginConfig.GetValue(&cfg)
// need to be strict about config presence for ETCD
//if !found {
// p.Log.Info("etcd config not found ", p.PluginConfig.GetConfigName(), " - skip loading this plugin")
// return nil
//}
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion docs/guidelines/CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Flags & Environment variables

1. Ligato source code uses golang flag package to define & parse command
1. Ligato source code uses [flag](github.com/namsral/flag) package to define & parse command
line flags and/or environment variables. Plan is to incorporate
[Viper](https://github.com/spf13/viper)
that is backward compatible with golang flag package.
Expand Down
2 changes: 1 addition & 1 deletion examples/kafka_broker/asyncproducer/asyncproducer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package main

import (
"flag"
"github.com/namsral/flag"
"fmt"
"os"

Expand Down
2 changes: 1 addition & 1 deletion examples/kafka_broker/consumer/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package main

import (
"flag"
"github.com/namsral/flag"
"fmt"
"os"
"os/signal"
Expand Down
2 changes: 1 addition & 1 deletion examples/kafka_broker/syncproducer/syncproducer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package main

import (
"flag"
"github.com/namsral/flag"
"fmt"
"os"
"strings"
Expand Down
15 changes: 6 additions & 9 deletions flavors/etcdkafka/etcd_kafka_flavor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package etcdkafka

import (
"flag"
"github.com/namsral/flag"

"github.com/ligato/cn-infra/core"
"github.com/ligato/cn-infra/datasync/kvdbsync"
Expand All @@ -24,15 +24,12 @@ import (
"github.com/ligato/cn-infra/messaging/kafka"
)

// flags for location of configuration files
var (
etcdv3DefaultConfig string
kafkaDefaultConfig string
)

// defines etcd & kafka flags // TODO switch to viper to avoid global configuration
func init() {
flag.StringVar(&etcdv3DefaultConfig, "etcdv3-config", "", "Location of the Etcd configuration file; also set via 'ETCDV3_CONFIG' env variable.")
flag.StringVar(&kafkaDefaultConfig, "kafka-config", "", "Location of the Kafka configuration file; also set via 'KAFKA_CONFIG' env variable.")
flag.String("etcdv3-config", "etcd.conf",
"Location of the Etcd configuration file; also set via 'ETCDV3_CONFIG' env variable.")
flag.String("kafka-config", "kafka.conf",
"Location of the Kafka configuration file; also set via 'KAFKA_CONFIG' env variable.")
}

// FlavorEtcdKafka glues together FlavorRPC plugins with:
Expand Down
25 changes: 0 additions & 25 deletions logging/logrus/log_msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,6 @@ import (
"github.com/onsi/gomega"
)

/*TODO change test to WithField
func TestEntryWithError(t *testing.T) {
gomega.RegisterTestingT(t)
defer func() {
ErrorKey = "error"
}()
err := fmt.Errorf("kaboom at layer %d", 4711)
gomega.Expect(err).To(gomega.BeEquivalentTo(WithError(err).Entry.Data["error"]))
logger := NewLogger("testLogger")
logger.std.Out = &bytes.Buffer{}
entry := NewEntry(logger)
gomega.Expect(err).To(gomega.BeEquivalentTo(entry.WithError(err).Entry.Data["error"]))
ErrorKey = "err"
gomega.Expect(err).To(gomega.BeEquivalentTo(entry.WithError(err).Entry.Data["err"]))
}*/

func TestEntryPanicln(t *testing.T) {
gomega.RegisterTestingT(t)

Expand Down
14 changes: 10 additions & 4 deletions logging/logrus/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,15 @@ func (logger *Logger) StandardLogger() *lg.Logger {
func (logger *Logger) GetLineInfo(depth int) string {
_, f, l, ok := runtime.Caller(depth)
if !ok {
f = "???"
l = 0
return ""
}
if f == "<autogenerated>" {
_, f, l, ok = runtime.Caller(depth + 1)
if !ok {
return ""
}
}

base := path.Base(f)
dir := path.Dir(f)
folders := strings.Split(dir, "/")
Expand Down Expand Up @@ -309,14 +315,14 @@ func (logger *Logger) withFields(fields Fields, depth ...int) *LogMsg {
for k, v := range fields {
f[k] = v
}
/*TODO

if _, ok := f[tagKey]; !ok {
f[tagKey] = logger.GetTag()
}
if _, ok := f[locKey]; !ok {
f[locKey] = logger.GetLineInfo(d)
}
*/

entry := logger.std.WithFields(f)
return &LogMsg{
logger: logger,
Expand Down
4 changes: 1 addition & 3 deletions logging/logrus/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ func TestDefaultFieldsAreNotPrefixed(t *testing.T) {
})
}

/*TODO Lukas
func TestDoubleLoggingDoesntPrefixPreviousFields(t *testing.T) {

var buffer bytes.Buffer
Expand Down Expand Up @@ -259,9 +258,8 @@ func TestDoubleLoggingDoesntPrefixPreviousFields(t *testing.T) {
gomega.Expect(fields["msg"]).To(gomega.BeEquivalentTo("omg it is!"))
gomega.Expect(fields["context"]).To(gomega.BeEquivalentTo("eating raw fish"))
gomega.Expect(fields["fields.msg"]).To(gomega.BeNil(), "should not have prefixed previous `msg` entry")
}*/
}

/*TODO*/
func TestGetSetLevelRace(t *testing.T) {
logger := NewLogger("testLogger")

Expand Down
8 changes: 6 additions & 2 deletions messaging/kafka/plugin_impl_kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

// Plugin provides API for interaction with kafka brokers.
type Plugin struct {
Deps // inject
Deps // inject
subscription chan (*client.ConsumerMessage)
mx *mux.Multiplexer
consumer *client.Consumer
Expand All @@ -53,7 +53,11 @@ func (p *Plugin) Init() (err error) {

// Get config data
config := &mux.Config{}
p.PluginConfig.GetValue(config)
found, err := p.PluginConfig.GetValue(config)
if !found {
p.Log.Info("kafka config not found ", p.PluginConfig.GetConfigName(), " - skip loading this plugin")
return nil //skip loading the plugin
}
if err != nil {
return err
}
Expand Down

0 comments on commit 0bc5124

Please sign in to comment.