add readonly tag (#190)

This commit is contained in:
RoyUP9 2021-08-10 09:51:35 +03:00 committed by GitHub
parent ca897dd3c7
commit c53b2148d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 10 deletions

View File

@ -21,6 +21,8 @@ import (
const ( const (
Separator = "=" Separator = "="
SetCommandName = "set" SetCommandName = "set"
FieldNameTag = "yaml"
ReadonlyTag = "readonly"
) )
var allowedSetFlags = []string{ var allowedSetFlags = []string{
@ -71,8 +73,8 @@ func GetConfigWithDefaults() (string, error) {
return "", err return "", err
} }
// TODO: change to generic solution configElem := reflect.ValueOf(&defaultConf).Elem()
defaultConf.AgentImage = "" setZeroForReadonlyFields(configElem)
return uiUtils.PrettyYaml(defaultConf) return uiUtils.PrettyYaml(defaultConf)
} }
@ -110,16 +112,14 @@ func initFlag(f *pflag.Flag) {
} }
if f.Name == SetCommandName { if f.Name == SetCommandName {
mergeSetFlag(sliceValue.GetSlice()) mergeSetFlag(configElem, sliceValue.GetSlice())
return return
} }
mergeFlagValues(configElem, f.Name, sliceValue.GetSlice()) mergeFlagValues(configElem, f.Name, sliceValue.GetSlice())
} }
func mergeSetFlag(setValues []string) { func mergeSetFlag(configElem reflect.Value, setValues []string) {
configElem := reflect.ValueOf(&Config).Elem()
for _, setValue := range setValues { for _, setValue := range setValues {
if !strings.Contains(setValue, Separator) { if !strings.Contains(setValue, Separator) {
Log.Warningf(uiUtils.Warning, fmt.Sprintf("Ignoring set argument %s (set argument format: <flag name>=<flag value>)", setValue)) Log.Warningf(uiUtils.Warning, fmt.Sprintf("Ignoring set argument %s (set argument format: <flag name>=<flag value>)", setValue))
@ -150,7 +150,7 @@ func mergeFlagValue(currentElem reflect.Value, flagKey string, flagValue string)
continue continue
} }
if currentField.Tag.Get("yaml") != flagKey { if getFieldNameByTag(currentField) != flagKey {
continue continue
} }
@ -176,7 +176,7 @@ func mergeFlagValues(currentElem reflect.Value, flagKey string, flagValues []str
continue continue
} }
if currentField.Tag.Get("yaml") != flagKey { if getFieldNameByTag(currentField) != flagKey {
continue 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) { func getParsedValue(kind reflect.Kind, value string) (reflect.Value, error) {
switch kind { switch kind {
case reflect.String: 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") 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))
}
}
}

View File

@ -19,7 +19,7 @@ type ConfigStruct struct {
Fetch configStructs.FetchConfig `yaml:"fetch"` Fetch configStructs.FetchConfig `yaml:"fetch"`
Version configStructs.VersionConfig `yaml:"version"` Version configStructs.VersionConfig `yaml:"version"`
View configStructs.ViewConfig `yaml:"view"` 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"` MizuResourcesNamespace string `yaml:"mizu-resources-namespace" default:"mizu"`
Telemetry bool `yaml:"telemetry" default:"true"` Telemetry bool `yaml:"telemetry" default:"true"`
DumpLogs bool `yaml:"dump-logs" default:"false"` DumpLogs bool `yaml:"dump-logs" default:"false"`

View File

@ -34,7 +34,7 @@ type TapConfig struct {
Analysis bool `yaml:"analysis" default:"false"` Analysis bool `yaml:"analysis" default:"false"`
AllNamespaces bool `yaml:"all-namespaces" default:"false"` AllNamespaces bool `yaml:"all-namespaces" default:"false"`
PlainTextFilterRegexes []string `yaml:"regex-masking"` 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"` DisableRedaction bool `yaml:"no-redact" default:"false"`
HumanMaxEntriesDBSize string `yaml:"max-entries-db-size" default:"200MB"` HumanMaxEntriesDBSize string `yaml:"max-entries-db-size" default:"200MB"`
Direction string `yaml:"direction" default:"in"` Direction string `yaml:"direction" default:"in"`

39
cli/mizu/config_test.go Normal file
View File

@ -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)
}
}
}