Skip to content

Commit

Permalink
simplify GetSupportedControllerFromString
Browse files Browse the repository at this point in the history
  • Loading branch information
rbren committed Dec 23, 2019
1 parent 3304285 commit d0dc7f4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 30 deletions.
20 changes: 9 additions & 11 deletions pkg/config/supportedcontrollers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
23 changes: 8 additions & 15 deletions pkg/config/supportedcontrollers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}

Expand Down Expand Up @@ -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))
}
}
8 changes: 4 additions & 4 deletions pkg/webhook/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d0dc7f4

Please sign in to comment.