[CNI] zap logger migration for store package (#2231)
* zap logger migration for store package
This commit is contained in:
Родитель
3c6bb62ff3
Коммит
49431986ee
|
@ -111,7 +111,7 @@ func getMetadata(th *telemetryHandle) {
|
|||
}
|
||||
|
||||
// Save metadata retrieved from wireserver to a file
|
||||
kvs, err := store.NewJsonFileStore(metadataFile, lockclient)
|
||||
kvs, err := store.NewJsonFileStore(metadataFile, lockclient, nil)
|
||||
if err != nil {
|
||||
debugLog("[AppInsights] Error initializing kvs store: %v", err)
|
||||
return
|
||||
|
|
|
@ -25,6 +25,7 @@ import (
|
|||
)
|
||||
|
||||
var logger = log.CNILogger.With(zap.String("component", "cni-plugin"))
|
||||
var storeLogger = log.CNILogger.With(zap.String("component", "cni-store"))
|
||||
|
||||
var errEmptyContent = errors.New("read content is zero bytes")
|
||||
|
||||
|
@ -188,7 +189,7 @@ func (plugin *Plugin) InitializeKeyValueStore(config *common.PluginConfig) error
|
|||
return errors.Wrap(err, "error creating new filelock")
|
||||
}
|
||||
|
||||
plugin.Store, err = store.NewJsonFileStore(platform.CNIRuntimePath+plugin.Name+".json", lockclient)
|
||||
plugin.Store, err = store.NewJsonFileStore(platform.CNIRuntimePath+plugin.Name+".json", lockclient, storeLogger)
|
||||
if err != nil {
|
||||
logger.Error("Failed to create store", zap.Error(err))
|
||||
return err
|
||||
|
|
|
@ -167,7 +167,7 @@ func main() {
|
|||
|
||||
// Create the key value store.
|
||||
storeFileName := storeFileLocation + name + ".json"
|
||||
config.Store, err = store.NewJsonFileStore(storeFileName, lockclient)
|
||||
config.Store, err = store.NewJsonFileStore(storeFileName, lockclient, nil)
|
||||
if err != nil {
|
||||
log.Errorf("Failed to create store file: %s, due to error %v\n", storeFileName, err)
|
||||
return
|
||||
|
|
|
@ -150,7 +150,7 @@ func main() {
|
|||
return
|
||||
}
|
||||
|
||||
config.Store, err = store.NewJsonFileStore(platform.CNIRuntimePath+pluginName+".json", lockclient)
|
||||
config.Store, err = store.NewJsonFileStore(platform.CNIRuntimePath+pluginName+".json", lockclient, nil)
|
||||
if err != nil {
|
||||
fmt.Printf("[monitor] Failed to create store: %v\n", err)
|
||||
return
|
||||
|
|
|
@ -1671,7 +1671,7 @@ func startService() error {
|
|||
config := common.ServiceConfig{}
|
||||
|
||||
// Create the key value fileStore.
|
||||
fileStore, err := store.NewJsonFileStore(cnsJsonFileName, processlock.NewMockFileLock(false))
|
||||
fileStore, err := store.NewJsonFileStore(cnsJsonFileName, processlock.NewMockFileLock(false), nil)
|
||||
if err != nil {
|
||||
logger.Errorf("Failed to create store file: %s, due to error %v\n", cnsJsonFileName, err)
|
||||
return err
|
||||
|
|
|
@ -641,7 +641,7 @@ func main() {
|
|||
|
||||
// Create the key value store.
|
||||
storeFileName := storeFileLocation + name + ".json"
|
||||
config.Store, err = store.NewJsonFileStore(storeFileName, lockclient)
|
||||
config.Store, err = store.NewJsonFileStore(storeFileName, lockclient, nil)
|
||||
if err != nil {
|
||||
logger.Errorf("Failed to create store file: %s, due to error %v\n", storeFileName, err)
|
||||
return
|
||||
|
@ -664,7 +664,7 @@ func main() {
|
|||
}
|
||||
// Create the key value store.
|
||||
storeFileName := endpointStoreLocation + endpointStoreName + ".json"
|
||||
endpointStateStore, err = store.NewJsonFileStore(storeFileName, endpointStoreLock)
|
||||
endpointStateStore, err = store.NewJsonFileStore(storeFileName, endpointStoreLock, nil)
|
||||
if err != nil {
|
||||
logger.Errorf("Failed to create endpoint state store file: %s, due to error %v\n", storeFileName, err)
|
||||
return
|
||||
|
@ -903,7 +903,7 @@ func main() {
|
|||
|
||||
// Create the key value store.
|
||||
pluginStoreFile := storeFileLocation + pluginName + ".json"
|
||||
pluginConfig.Store, err = store.NewJsonFileStore(pluginStoreFile, lockclientCnm)
|
||||
pluginConfig.Store, err = store.NewJsonFileStore(pluginStoreFile, lockclientCnm, nil)
|
||||
if err != nil {
|
||||
logger.Errorf("Failed to create plugin store file %s, due to error : %v\n", pluginStoreFile, err)
|
||||
return
|
||||
|
|
|
@ -16,6 +16,7 @@ import (
|
|||
"github.com/Azure/azure-container-networking/platform"
|
||||
"github.com/Azure/azure-container-networking/processlock"
|
||||
"github.com/pkg/errors"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -35,12 +36,13 @@ type jsonFileStore struct {
|
|||
inSync bool
|
||||
processLock processlock.Interface
|
||||
sync.Mutex
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
// NewJsonFileStore creates a new jsonFileStore object, accessed as a KeyValueStore.
|
||||
//
|
||||
//nolint:revive // ignoring name change
|
||||
func NewJsonFileStore(fileName string, lockclient processlock.Interface) (KeyValueStore, error) {
|
||||
func NewJsonFileStore(fileName string, lockclient processlock.Interface, logger *zap.Logger) (KeyValueStore, error) {
|
||||
if fileName == "" {
|
||||
return &jsonFileStore{}, errors.New("need to pass in a json file path")
|
||||
}
|
||||
|
@ -48,6 +50,7 @@ func NewJsonFileStore(fileName string, lockclient processlock.Interface) (KeyVal
|
|||
fileName: fileName,
|
||||
processLock: lockclient,
|
||||
data: make(map[string]*json.RawMessage),
|
||||
logger: logger,
|
||||
}
|
||||
|
||||
return kvs, nil
|
||||
|
@ -83,7 +86,12 @@ func (kvs *jsonFileStore) Read(key string, value interface{}) error {
|
|||
}
|
||||
|
||||
if len(b) == 0 {
|
||||
if kvs.logger != nil {
|
||||
kvs.logger.Info("Unable to read empty file", zap.String("fileName", kvs.fileName))
|
||||
} else {
|
||||
log.Printf("Unable to read file %s, was empty", kvs.fileName)
|
||||
}
|
||||
|
||||
return ErrStoreEmpty
|
||||
}
|
||||
|
||||
|
@ -184,7 +192,12 @@ func (kvs *jsonFileStore) Lock(timeout time.Duration) error {
|
|||
afterTime := time.After(timeout)
|
||||
status := make(chan error)
|
||||
|
||||
if kvs.logger != nil {
|
||||
kvs.logger.Info("Acquiring process lock")
|
||||
} else {
|
||||
log.Printf("Acquiring process lock")
|
||||
}
|
||||
|
||||
go kvs.lockUtil(status)
|
||||
|
||||
var err error
|
||||
|
@ -198,7 +211,12 @@ func (kvs *jsonFileStore) Lock(timeout time.Duration) error {
|
|||
return errors.Wrap(err, "processLock acquire error")
|
||||
}
|
||||
|
||||
if kvs.logger != nil {
|
||||
kvs.logger.Info("Acquired process lock with timeout value of", zap.Any("timeout", timeout))
|
||||
} else {
|
||||
log.Printf("Acquired process lock with timeout value of %v", timeout)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -212,7 +230,12 @@ func (kvs *jsonFileStore) Unlock() error {
|
|||
return errors.Wrap(err, "unlock error")
|
||||
}
|
||||
|
||||
if kvs.logger != nil {
|
||||
kvs.logger.Info("Released process lock")
|
||||
} else {
|
||||
log.Printf("Released process lock")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -223,7 +246,12 @@ func (kvs *jsonFileStore) GetModificationTime() (time.Time, error) {
|
|||
|
||||
info, err := os.Stat(kvs.fileName)
|
||||
if err != nil {
|
||||
if kvs.logger != nil {
|
||||
kvs.logger.Info("os.stat() for file", zap.String("fileName", kvs.fileName), zap.Error(err))
|
||||
} else {
|
||||
log.Printf("os.stat() for file %v failed: %v", kvs.fileName, err)
|
||||
}
|
||||
|
||||
return time.Time{}.UTC(), err
|
||||
}
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/Azure/azure-container-networking/cni/log"
|
||||
"github.com/Azure/azure-container-networking/processlock"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
@ -49,7 +50,7 @@ func TestKeyValuePairsAreReinstantiatedFromJSONFile(t *testing.T) {
|
|||
defer os.Remove(testFileName)
|
||||
|
||||
// Create the store, initialized using the JSON file.
|
||||
kvs, err := NewJsonFileStore(testFileName, processlock.NewMockFileLock(false))
|
||||
kvs, err := NewJsonFileStore(testFileName, processlock.NewMockFileLock(false), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create KeyValueStore %v\n", err)
|
||||
}
|
||||
|
@ -74,7 +75,7 @@ func TestKeyValuePairsArePersistedToJSONFile(t *testing.T) {
|
|||
var actualPair string
|
||||
|
||||
// Create the store.
|
||||
kvs, err := NewJsonFileStore(testFileName, processlock.NewMockFileLock(false))
|
||||
kvs, err := NewJsonFileStore(testFileName, processlock.NewMockFileLock(false), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create KeyValueStore %v\n", err)
|
||||
}
|
||||
|
@ -119,8 +120,11 @@ func TestKeyValuePairsAreWrittenAndReadCorrectly(t *testing.T) {
|
|||
anotherValue := testType1{"any", 14}
|
||||
var readValue testType1
|
||||
|
||||
// Test when passing zap logger obj to NewJsonFileStore
|
||||
logger := log.CNILogger
|
||||
|
||||
// Create the store.
|
||||
kvs, err := NewJsonFileStore(testFileName, processlock.NewMockFileLock(false))
|
||||
kvs, err := NewJsonFileStore(testFileName, processlock.NewMockFileLock(false), logger)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create KeyValueStore %v\n", err)
|
||||
}
|
||||
|
@ -155,12 +159,12 @@ func TestKeyValuePairsAreWrittenAndReadCorrectly(t *testing.T) {
|
|||
|
||||
// test case for testing newjsonfilestore idempotent
|
||||
func TestNewJsonFileStoreIdempotent(t *testing.T) {
|
||||
_, err := NewJsonFileStore(testLockFileName, processlock.NewMockFileLock(false))
|
||||
_, err := NewJsonFileStore(testLockFileName, processlock.NewMockFileLock(false), nil)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to initialize store: %v", err)
|
||||
}
|
||||
|
||||
_, err = NewJsonFileStore(testLockFileName, processlock.NewMockFileLock(false))
|
||||
_, err = NewJsonFileStore(testLockFileName, processlock.NewMockFileLock(false), nil)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to initialize same store second time: %v", err)
|
||||
}
|
||||
|
@ -177,7 +181,7 @@ func TestLock(t *testing.T) {
|
|||
{
|
||||
name: "Acquire Lock happy path",
|
||||
store: func() KeyValueStore {
|
||||
st, _ := NewJsonFileStore(testFileName, processlock.NewMockFileLock(false))
|
||||
st, _ := NewJsonFileStore(testFileName, processlock.NewMockFileLock(false), nil)
|
||||
return st
|
||||
}(),
|
||||
timeoutms: 10000,
|
||||
|
@ -186,7 +190,7 @@ func TestLock(t *testing.T) {
|
|||
{
|
||||
name: "Acquire Lock Fail",
|
||||
store: func() KeyValueStore {
|
||||
st, _ := NewJsonFileStore(testFileName, processlock.NewMockFileLock(true))
|
||||
st, _ := NewJsonFileStore(testFileName, processlock.NewMockFileLock(true), nil)
|
||||
return st
|
||||
}(),
|
||||
timeoutms: 10000,
|
||||
|
@ -196,7 +200,7 @@ func TestLock(t *testing.T) {
|
|||
{
|
||||
name: "Acquire Lock timeout error",
|
||||
store: func() KeyValueStore {
|
||||
st, _ := NewJsonFileStore(testFileName, processlock.NewMockFileLock(false))
|
||||
st, _ := NewJsonFileStore(testFileName, processlock.NewMockFileLock(false), nil)
|
||||
return st
|
||||
}(),
|
||||
timeoutms: 0,
|
||||
|
@ -223,12 +227,12 @@ func TestLock(t *testing.T) {
|
|||
|
||||
// test case for testing newjsonfilestore idempotent
|
||||
func TestFileName(t *testing.T) {
|
||||
_, err := NewJsonFileStore("", processlock.NewMockFileLock(false))
|
||||
_, err := NewJsonFileStore("", processlock.NewMockFileLock(false), nil)
|
||||
if err == nil {
|
||||
t.Errorf("This should have failed for empty file name")
|
||||
}
|
||||
|
||||
_, err = NewJsonFileStore("test.json", processlock.NewMockFileLock(false))
|
||||
_, err = NewJsonFileStore("test.json", processlock.NewMockFileLock(false), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("This should not fail for a non-empty file %v", err)
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче