From d4e3258d53a6fc01382ed799b9409b096edd526c Mon Sep 17 00:00:00 2001 From: Robert Brennan Date: Fri, 20 Dec 2019 20:14:49 +0000 Subject: [PATCH] simplify GetSupportedControllerFromString --- pkg/config/supportedcontrollers.go | 20 +++++++++----------- pkg/config/supportedcontrollers_test.go | 23 ++++++++--------------- pkg/webhook/validator.go | 8 ++++---- 3 files changed, 21 insertions(+), 30 deletions(-) diff --git a/pkg/config/supportedcontrollers.go b/pkg/config/supportedcontrollers.go index 2efc20ac..808a9538 100644 --- a/pkg/config/supportedcontrollers.go +++ b/pkg/config/supportedcontrollers.go @@ -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 diff --git a/pkg/config/supportedcontrollers_test.go b/pkg/config/supportedcontrollers_test.go index 0aeeec2f..694f4ccf 100644 --- a/pkg/config/supportedcontrollers_test.go +++ b/pkg/config/supportedcontrollers_test.go @@ -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)) } } diff --git a/pkg/webhook/validator.go b/pkg/webhook/validator.go index b37d73af..60e7f842 100644 --- a/pkg/webhook/validator.go +++ b/pkg/webhook/validator.go @@ -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