Skip to content

Commit

Permalink
Log new configuration
Browse files Browse the repository at this point in the history
Log new configuration if it's enabled.
  • Loading branch information
kislaykishore committed Aug 9, 2024
1 parent 3f6cd41 commit b21695a
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 21 deletions.
12 changes: 10 additions & 2 deletions cfg/config_util.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Google LLC
// Copyright 2024 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.
Expand All @@ -14,7 +14,11 @@

package cfg

import "runtime"
import (
"os"
"runtime"
"strings"
)

func DefaultMaxParallelDownloads() int {
return max(16, 2*runtime.NumCPU())
Expand All @@ -23,3 +27,7 @@ func DefaultMaxParallelDownloads() int {
func IsFileCacheEnabled(mountConfig *Config) bool {
return mountConfig.FileCache.MaxSizeMb != 0 && string(mountConfig.CacheDir) != ""
}

func IsNewConfigEnabled() bool {
return strings.ToLower(os.Getenv("ENABLE_GCSFUSE_VIPER_CONFIG")) == "true"
}
4 changes: 2 additions & 2 deletions cfg/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func (o *Octal) UnmarshalText(text []byte) error {
return nil
}

func (o *Octal) String() string {
return fmt.Sprintf("%o", *o)
func (o Octal) MarshalText() (text []byte, err error) {
return []byte(strconv.FormatInt(int64(o), 8)), nil
}

// Protocol is the datatype that specifies the type of connection: http1/http2/grpc.
Expand Down
38 changes: 27 additions & 11 deletions cmd/legacy_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,30 @@ func getUserAgent(appName string, config string) string {
}
}

func logNewConfiguration(newConfig *cfg.Config) {
cfgStr, err := util.YAMLStringify(newConfig)
if err != nil {
logger.Warnf("failed to stringify configuration: %v", err)
return
}
logger.Infof("GCSFuse mount config: %s", cfgStr)
}
func logLegacyConfiguration(flags *flagStorage, mountConfig *config.MountConfig) {
flagsStringified, err := util.JSONStringify(*flags)
if err != nil {
logger.Warnf("failed to stringify cli flags: %v", err)
} else {
logger.Infof("GCSFuse mount command flags: %s", flagsStringified)
}

mountConfigStringified, err := util.JSONStringify(*mountConfig)
if err != nil {
logger.Warnf("failed to stringify config-file: %v", err)
} else {
logger.Infof("GCSFuse mount config flags: %s", mountConfigStringified)
}
}

func getConfigForUserAgent(mountConfig *cfg.Config) string {
// Minimum configuration details created in a bitset fashion. Right now, its restricted only to File Cache Settings.
isFileCacheEnabled := "0"
Expand Down Expand Up @@ -297,18 +321,10 @@ func runCLIApp(c *cli.Context) (err error) {
// if these are already being logged into a log-file, otherwise
// there will be duplicate logs for these in both places (stdout and log-file).
if newConfig.Foreground || newConfig.Logging.FilePath == "" {
flagsStringified, err := util.Stringify(*flags)
if err != nil {
logger.Warnf("failed to stringify cli flags: %v", err)
} else {
logger.Infof("GCSFuse mount command flags: %s", flagsStringified)
}

mountConfigStringified, err := util.Stringify(*mountConfig)
if err != nil {
logger.Warnf("failed to stringify config-file: %v", err)
if cfg.IsNewConfigEnabled() {
logNewConfiguration(newConfig)
} else {
logger.Infof("GCSFuse mount config flags: %s", mountConfigStringified)
logLegacyConfiguration(flags, mountConfig)
}
}

Expand Down
18 changes: 15 additions & 3 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"path/filepath"
"strings"
"time"

"gopkg.in/yaml.v3"
)

const (
Expand Down Expand Up @@ -75,12 +77,22 @@ func GetResolvedPath(filePath string) (resolvedPath string, err error) {
}
}

// Stringify marshals an object (only exported attribute) to a JSON string. If marshalling fails, it returns an empty string.
func Stringify(input any) (string, error) {
// JSONStringify marshals an object (only exported attribute) to a JSON string. If marshalling fails, it returns an empty string.
func JSONStringify(input any) (string, error) {
inputBytes, err := json.Marshal(input)

if err != nil {
return "", fmt.Errorf("error in Stringify %w", err)
return "", fmt.Errorf("error in JSONStringify %w", err)
}
return string(inputBytes), nil
}

// YAMLStringify marshals an object to a YAML string. If marshalling fails, it returns an error.
func YAMLStringify(input any) (string, error) {
inputBytes, err := yaml.Marshal(input)

if err != nil {
return "", fmt.Errorf("error in YAMLStringify %w", err)
}
return string(inputBytes), nil
}
Expand Down
41 changes: 39 additions & 2 deletions internal/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,43 @@ func (ts *UtilTest) ResolveWhenParentProcDirEnvSetAndAbsoluteFilePath() {
assert.Equal(ts.T(), "/var/dir/test.txt", resolvedPath)
}

func TestYAMLStringify(t *testing.T) {
tests := []struct {
name string
obj any
expected string
}{
{
name: "Test Map",
obj: map[string]int{
"1": 1,
"2": 2,
"3": 3,
},
expected: "\"1\": 1\n\"2\": 2\n\"3\": 3\n",
},
{
name: "Test Nested Map",
obj: map[string]any{
"1": 1,
"2": map[string]int{
"3": 1,
},
},
expected: "\"1\": 1\n\"2\":\n \"3\": 1\n",
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
s, err := YAMLStringify(tc.obj)

if assert.NoError(t, err) {
assert.Equal(t, tc.expected, s)
}
})
}
}
func (ts *UtilTest) TestStringifyShouldReturnAllFieldsPassedInCustomObjectAsMarshalledString() {
sampleMap := map[string]int{
"1": 1,
Expand All @@ -163,7 +200,7 @@ func (ts *UtilTest) TestStringifyShouldReturnAllFieldsPassedInCustomObjectAsMars
NestedValue: sampleNestedValue,
}

actual, _ := Stringify(customObject)
actual, _ := JSONStringify(customObject)

expected := "{\"Value\":\"test_value\",\"NestedValue\":{\"SomeField\":10,\"SomeOther\":{\"1\":1,\"2\":2,\"3\":3}}}"
assert.Equal(ts.T(), expected, actual)
Expand All @@ -174,7 +211,7 @@ func (ts *UtilTest) TestStringifyShouldReturnEmptyStringWhenMarshalErrorsOut() {
value: "example",
}

actual, _ := Stringify(customInstance)
actual, _ := JSONStringify(customInstance)

expected := ""
assert.Equal(ts.T(), expected, actual)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func main() {
defer logPanic()
// Make logging output better.
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds)
if strings.ToLower(os.Getenv("ENABLE_GCSFUSE_VIPER_CONFIG")) == "true" {
if cfg.IsNewConfigEnabled() {
// TODO: implement the mount logic instead of simply returning nil.
rootCmd, err := cmd.NewRootCmd(func(*cfg.Config, string, string) error { return nil })
if err != nil {
Expand Down

0 comments on commit b21695a

Please sign in to comment.