From c53b2148d1912d4090473718e5751e6fe0d28bc0 Mon Sep 17 00:00:00 2001 From: RoyUP9 <87927115+RoyUP9@users.noreply.github.com> Date: Tue, 10 Aug 2021 09:51:35 +0300 Subject: [PATCH] add readonly tag (#190) --- cli/mizu/config.go | 36 ++++++++++++++++++++------ cli/mizu/configStruct.go | 2 +- cli/mizu/configStructs/tapConfig.go | 2 +- cli/mizu/config_test.go | 39 +++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 cli/mizu/config_test.go diff --git a/cli/mizu/config.go b/cli/mizu/config.go index e8f4dc537..bb6b9d65a 100644 --- a/cli/mizu/config.go +++ b/cli/mizu/config.go @@ -21,6 +21,8 @@ import ( const ( Separator = "=" SetCommandName = "set" + FieldNameTag = "yaml" + ReadonlyTag = "readonly" ) var allowedSetFlags = []string{ @@ -71,8 +73,8 @@ func GetConfigWithDefaults() (string, error) { return "", err } - // TODO: change to generic solution - defaultConf.AgentImage = "" + configElem := reflect.ValueOf(&defaultConf).Elem() + setZeroForReadonlyFields(configElem) return uiUtils.PrettyYaml(defaultConf) } @@ -110,16 +112,14 @@ func initFlag(f *pflag.Flag) { } if f.Name == SetCommandName { - mergeSetFlag(sliceValue.GetSlice()) + mergeSetFlag(configElem, sliceValue.GetSlice()) return } mergeFlagValues(configElem, f.Name, sliceValue.GetSlice()) } -func mergeSetFlag(setValues []string) { - configElem := reflect.ValueOf(&Config).Elem() - +func mergeSetFlag(configElem reflect.Value, setValues []string) { for _, setValue := range setValues { if !strings.Contains(setValue, Separator) { Log.Warningf(uiUtils.Warning, fmt.Sprintf("Ignoring set argument %s (set argument format: =)", setValue)) @@ -150,7 +150,7 @@ func mergeFlagValue(currentElem reflect.Value, flagKey string, flagValue string) continue } - if currentField.Tag.Get("yaml") != flagKey { + if getFieldNameByTag(currentField) != flagKey { continue } @@ -176,7 +176,7 @@ func mergeFlagValues(currentElem reflect.Value, flagKey string, flagValues []str continue } - if currentField.Tag.Get("yaml") != flagKey { + if getFieldNameByTag(currentField) != flagKey { continue } @@ -197,6 +197,10 @@ func mergeFlagValues(currentElem reflect.Value, flagKey string, flagValues []str } } +func getFieldNameByTag(field reflect.StructField) string { + return strings.Split(field.Tag.Get(FieldNameTag), ",")[0] +} + func getParsedValue(kind reflect.Kind, value string) (reflect.Value, error) { switch kind { case reflect.String: @@ -282,3 +286,19 @@ func getParsedValue(kind reflect.Kind, value string) (reflect.Value, error) { return reflect.ValueOf(nil), errors.New("value to parse does not match type") } + +func setZeroForReadonlyFields(currentElem reflect.Value) { + for i := 0; i < currentElem.NumField(); i++ { + currentField := currentElem.Type().Field(i) + currentFieldByName := currentElem.FieldByName(currentField.Name) + + if currentField.Type.Kind() == reflect.Struct { + setZeroForReadonlyFields(currentFieldByName) + continue + } + + if _, ok := currentField.Tag.Lookup(ReadonlyTag); ok { + currentFieldByName.Set(reflect.Zero(currentField.Type)) + } + } +} diff --git a/cli/mizu/configStruct.go b/cli/mizu/configStruct.go index cd7d9bad3..a4e81bb90 100644 --- a/cli/mizu/configStruct.go +++ b/cli/mizu/configStruct.go @@ -19,7 +19,7 @@ type ConfigStruct struct { Fetch configStructs.FetchConfig `yaml:"fetch"` Version configStructs.VersionConfig `yaml:"version"` View configStructs.ViewConfig `yaml:"view"` - AgentImage string `yaml:"agent-image,omitempty"` + AgentImage string `yaml:"agent-image,omitempty" readonly:""` MizuResourcesNamespace string `yaml:"mizu-resources-namespace" default:"mizu"` Telemetry bool `yaml:"telemetry" default:"true"` DumpLogs bool `yaml:"dump-logs" default:"false"` diff --git a/cli/mizu/configStructs/tapConfig.go b/cli/mizu/configStructs/tapConfig.go index 553b1bf8c..16403ca57 100644 --- a/cli/mizu/configStructs/tapConfig.go +++ b/cli/mizu/configStructs/tapConfig.go @@ -34,7 +34,7 @@ type TapConfig struct { Analysis bool `yaml:"analysis" default:"false"` AllNamespaces bool `yaml:"all-namespaces" default:"false"` PlainTextFilterRegexes []string `yaml:"regex-masking"` - HealthChecksUserAgentHeaders []string `yaml:"ignored-user-agents" default:"[]"` + HealthChecksUserAgentHeaders []string `yaml:"ignored-user-agents"` DisableRedaction bool `yaml:"no-redact" default:"false"` HumanMaxEntriesDBSize string `yaml:"max-entries-db-size" default:"200MB"` Direction string `yaml:"direction" default:"in"` diff --git a/cli/mizu/config_test.go b/cli/mizu/config_test.go new file mode 100644 index 000000000..f1f9edb8a --- /dev/null +++ b/cli/mizu/config_test.go @@ -0,0 +1,39 @@ +package mizu_test + +import ( + "github.com/up9inc/mizu/cli/mizu" + "reflect" + "strings" + "testing" +) + +func TestConfigWriteIgnoresReadonlyFields(t *testing.T) { + var readonlyFields []string + + configElem := reflect.ValueOf(&mizu.ConfigStruct{}).Elem() + getFieldsWithReadonlyTag(configElem, &readonlyFields) + + config, _ := mizu.GetConfigWithDefaults() + for _, readonlyField := range readonlyFields { + if strings.Contains(config, readonlyField) { + t.Errorf("unexpected result - readonly field: %v, config: %v", readonlyField, config) + } + } +} + +func getFieldsWithReadonlyTag(currentElem reflect.Value, readonlyFields *[]string) { + for i := 0; i < currentElem.NumField(); i++ { + currentField := currentElem.Type().Field(i) + currentFieldByName := currentElem.FieldByName(currentField.Name) + + if currentField.Type.Kind() == reflect.Struct { + getFieldsWithReadonlyTag(currentFieldByName, readonlyFields) + continue + } + + if _, ok := currentField.Tag.Lookup(mizu.ReadonlyTag); ok { + fieldNameByTag := strings.Split(currentField.Tag.Get(mizu.FieldNameTag), ",")[0] + *readonlyFields = append(*readonlyFields, fieldNameByTag) + } + } +}