simplify GetSupportedControllerFromString

This commit is contained in:
Robert Brennan
2019-12-20 20:14:49 +00:00
parent b4e3f78b72
commit d4e3258d53
3 changed files with 21 additions and 30 deletions

View File

@@ -84,9 +84,9 @@ func (s *SupportedController) UnmarshalJSON(b []byte) error {
return err
}
*s, err = GetSupportedControllerFromString(j)
if err != nil {
return err
*s = GetSupportedControllerFromString(j)
if *s == Unsupported {
return fmt.Errorf("Unsupported controller kind: %s", j)
}
return nil
}
@@ -124,21 +124,19 @@ func (s SupportedController) ListSupportedAPIVersions() []runtime.Object {
}
// GetSupportedControllerFromString fuzzy matches a string with a SupportedController Enum
func GetSupportedControllerFromString(str string) (SupportedController, error) {
func GetSupportedControllerFromString(str string) SupportedController {
lowerStr := strings.ToLower(str)
controller, keyFound := stringLookupForSupportedControllers[lowerStr]
if !keyFound || controller == Unsupported {
return 0, fmt.Errorf("Value ('%v') in configuration was not found in Supported Controllers: (%v)", str, strings.Join(ControllerStrings, ","))
if !keyFound {
controller = Unsupported
}
return controller, nil
return controller
}
// CheckIfKindIsConfiguredForValidation takes a kind (in string format) and checks if Polaris is configured to scan this type of controller
func (c Configuration) CheckIfKindIsConfiguredForValidation(kind string) bool {
controller, err := GetSupportedControllerFromString(kind)
// if no errors then we found the kind in supported controller types
if err == nil {
// see if the kind exists in the controllers to scan config
controller := GetSupportedControllerFromString(kind)
if controller != Unsupported {
for _, controllerToScan := range c.ControllersToScan {
if controller == controllerToScan {
return true

View File

@@ -51,19 +51,17 @@ func TestUnmarshalSupportedControllers(t *testing.T) {
func TestMarshalSupportedControllers(t *testing.T) {
for idx, controllerString := range ControllerStrings {
controllerType, err := GetSupportedControllerFromString(controllerString)
controllerType := GetSupportedControllerFromString(controllerString)
if idx == 0 {
if err == nil {
t.Errorf("Expected first element (%s) to fail as a non-valid supported controller. Reserved for 'Unsupported'", controllerString)
}
} else if err != nil {
t.Errorf("Unable to take the configured string (%s) and convert into Enum; Error: (%s)", controllerString, err)
assert.Equal(t, SupportedController(0), controllerType)
} else {
assert.NotEqual(t, SupportedController(0), controllerType)
}
object := checkMarshal{
Controllers: []SupportedController{controllerType},
}
_, err = json.Marshal(object)
_, err := json.Marshal(object)
if idx == 0 {
if err == nil {
t.Errorf("Expected (%s) to throw an error. Reserving the first element in the enum to be an invalid config", controllerString)
@@ -77,10 +75,8 @@ func TestMarshalSupportedControllers(t *testing.T) {
func TestCheckIfControllerKindIsConfiguredForValidation(t *testing.T) {
config := Configuration{}
for _, controllerString := range ControllerStrings[1:] {
controllerEnum, err := GetSupportedControllerFromString(controllerString)
if err != nil {
t.Errorf("Expected controller string (%s) to be convertable into enum: (%s)", controllerString, err)
}
controllerEnum := GetSupportedControllerFromString(controllerString)
assert.NotEqual(t, SupportedController(0), controllerEnum)
config.ControllersToScan = append(config.ControllersToScan, controllerEnum)
}
@@ -116,10 +112,7 @@ func TestGetSupportedControllerFromString(t *testing.T) {
}
for inputString, expectedType := range fixture {
resolvedType, err := GetSupportedControllerFromString(inputString)
if expectedType == Unsupported && err == nil {
t.Errorf("Expected (%s) to resolve to an unsupported type and throw an error.", inputString)
}
resolvedType := GetSupportedControllerFromString(inputString)
assert.Equal(t, expectedType, resolvedType, fmt.Sprintf("Expected (%s) to return (%s) controller type.", inputString, expectedType))
}
}

View File

@@ -103,11 +103,11 @@ func (v *Validator) Handle(ctx context.Context, req types.Request) types.Respons
}
// We should never hit this case unless something is misconfiured in CheckIfKindIsConfiguredForValidation
controllerType, err := config.GetSupportedControllerFromString(req.AdmissionRequest.Kind.Kind)
if err != nil {
msg := fmt.Errorf("Unexpected error occurred. Expected Kind to be a supported type (%s)", req.AdmissionRequest.Kind.Kind)
controllerType := config.GetSupportedControllerFromString(req.AdmissionRequest.Kind.Kind)
if controllerType == config.Unsupported {
msg := fmt.Errorf("Expected Kind (%s) to be a supported type", req.AdmissionRequest.Kind.Kind)
logrus.Error(msg)
return admission.ErrorResponse(http.StatusInternalServerError, err)
return admission.ErrorResponse(http.StatusInternalServerError, msg)
}
// For each type, perform the scan