Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: node selector validation #2141

Merged
merged 1 commit into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -148,31 +148,46 @@ func DurationOrNone(value starlark.Value, attributeName string) *startosis_error
}

func StringMappingToString(value starlark.Value, attributeName string) *startosis_errors.InterpretationError {
labelsMap := map[string]string{}
labelsDict, ok := value.(*starlark.Dict)
if _, err := parseMapStringString(value, attributeName); err != nil {
return err
}
return nil
}

func ServiceLabelsValidator(value starlark.Value, attributeName string) *startosis_errors.InterpretationError {
labelsMap, interpretationErr := parseMapStringString(value, attributeName)
if interpretationErr != nil {
return interpretationErr
}
if err := service.ValidateServiceConfigLabels(labelsMap); err != nil {
return startosis_errors.WrapWithInterpretationError(err, "An error occurred validating service config labels '%+v'", labelsMap)
}
return nil
}

func parseMapStringString(value starlark.Value, attributeName string) (map[string]string, *startosis_errors.InterpretationError) {
stringMap := map[string]string{}
stringDict, ok := value.(*starlark.Dict)
if !ok {
return startosis_errors.NewInterpretationError("Attribute '%s' is expected to be a dictionary of strings, got '%s'", attributeName, reflect.TypeOf(value))
return nil, startosis_errors.NewInterpretationError("Attribute '%s' is expected to be a dictionary of strings, got '%s'", attributeName, reflect.TypeOf(value))
}
for _, labelKey := range labelsDict.Keys() {
labelValue, found, err := labelsDict.Get(labelKey)
for _, mapKey := range stringDict.Keys() {
mapValue, found, err := stringDict.Get(mapKey)
if err != nil {
return startosis_errors.WrapWithInterpretationError(err, "Unexpected error iterating on dictionary. Value associated to key '%v' could not be found", labelKey)
return nil, startosis_errors.WrapWithInterpretationError(err, "Unexpected error iterating on dictionary. Value associated to key '%v' could not be found", mapKey)
} else if !found {
return startosis_errors.NewInterpretationError("Unexpected error iterating on dictionary. Value associated to key '%v' could not be found", labelKey)
return nil, startosis_errors.NewInterpretationError("Unexpected error iterating on dictionary. Value associated to key '%v' could not be found", mapKey)
}

labelKeyStr, ok := labelKey.(starlark.String)
mapKeyStr, ok := mapKey.(starlark.String)
if !ok {
return startosis_errors.NewInterpretationError("Key in '%s' dictionary was expected to be a string, got '%s'", attributeName, reflect.TypeOf(labelKey))
return nil, startosis_errors.NewInterpretationError("Key in '%s' dictionary was expected to be a string, got '%s'", attributeName, reflect.TypeOf(mapKey))
}
labelValueStr, ok := labelValue.(starlark.String)
mapValueStr, ok := mapValue.(starlark.String)
if !ok {
return startosis_errors.NewInterpretationError("Value associated to key '%s' in dictionary '%s' was expected to be a string, got '%s'", labelKeyStr, attributeName, reflect.TypeOf(value))
return nil, startosis_errors.NewInterpretationError("Value associated to key '%s' in dictionary '%s' was expected to be a string, got '%s'", mapKeyStr, attributeName, reflect.TypeOf(value))
}
labelsMap[labelKeyStr.GoString()] = labelValueStr.GoString()
stringMap[mapKeyStr.GoString()] = mapValueStr.GoString()
}
if err := service.ValidateServiceConfigLabels(labelsMap); err != nil {
return startosis_errors.WrapWithInterpretationError(err, "An error occurred validating service config labels '%+v'", labelsMap)
}
return nil
return stringMap, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ var (
testServiceConfigLabelsKey2: testServiceConfigLabelsValue2,
}

testNodeSelectorKey1 = "disktype"
testNodeSelectorValue1 = "ssd"
testNodeSelectorKey1 = "k3s.io/hostname"
testNodeSelectorValue1 = "asrock-berlin-03"
testNodeSelectors = map[string]string{
testNodeSelectorKey1: testNodeSelectorValue1,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ func NewServiceConfigType() *kurtosis_type_constructor.KurtosisTypeConstructor {
IsOptional: true,
ZeroValueProvider: builtin_argument.ZeroValueProvider[*starlark.Dict],
Validator: func(value starlark.Value) *startosis_errors.InterpretationError {
return builtin_argument.StringMappingToString(value, LabelsAttr)

return builtin_argument.ServiceLabelsValidator(value, LabelsAttr)
},
},
{
Expand Down
Loading