Skip to content

Commit

Permalink
deduplicate results right away
Browse files Browse the repository at this point in the history
  • Loading branch information
baderbuddy committed Mar 25, 2020
1 parent aa0658b commit 5769acf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 28 deletions.
27 changes: 1 addition & 26 deletions pkg/validator/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,6 @@ func ValidateController(conf *conf.Configuration, controller controller.GenericC
return result, nil
}

// Because the controllers with an Owner take on the name of the Owner, this eliminates any duplicates.
// In cases like CronJobs older children can hang around, so this takes the most recent.
func deduplicateControllers(controllerResults []ControllerResult) []ControllerResult {
controllerMap := make(map[string][]ControllerResult)
for _, controller := range controllerResults {
key := controller.Namespace + "/" + controller.Kind + "/" + controller.Name
controllerMap[key] = append(controllerMap[key], controller)
}
results := make([]ControllerResult, 0)
for _, controllers := range controllerMap {
if len(controllers) == 1 {
results = append(results, controllers[0])
} else {
latestController := controllers[0]
for _, controller := range controllers[1:] {
if controller.CreatedTime.After(latestController.CreatedTime) {
latestController = controller
}
}
results = append(results, latestController)
}
}
return results
}

// ValidateControllers validates that each deployment conforms to the Polaris config,
// builds a list of ResourceResults organized by namespace.
func ValidateControllers(config *conf.Configuration, kubeResources *kube.ResourceProvider) ([]ControllerResult, error) {
Expand All @@ -88,7 +63,7 @@ func ValidateControllers(config *conf.Configuration, kubeResources *kube.Resourc
results = append(results, result)
}

return deduplicateControllers(results), nil
return results, nil
}

func hasExemptionAnnotation(ctrl controller.GenericController) bool {
Expand Down
21 changes: 19 additions & 2 deletions pkg/validator/controllers/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,25 @@ func LoadControllers(pods []kubeAPICoreV1.Pod, dynamicClientPointer *dynamic.Int
for _, pod := range pods {
interfaces = append(interfaces, NewGenericPodController(pod, dynamicClientPointer, restMapperPointer))
}
// TODO DeDupe
return interfaces
return deduplicateControllers(interfaces)
}

// Because the controllers with an Owner take on the name of the Owner, this eliminates any duplicates.
// In cases like CronJobs older children can hang around, so this takes the most recent.
func deduplicateControllers(controllers []GenericController) []GenericController {
controllerMap := make(map[string]GenericController)
for _, controller := range controllers {
key := controller.GetNamespace() + "/" + controller.GetKindString() + "/" + controller.Name
oldController, ok := controllerMap[key]
if !ok || controller.CreatedTime.After(oldController.CreatedTime) {
controllerMap[key] = controller
}
}
results := make([]GenericController, 0)
for _, controller := range controllerMap {
results = append(results, controller)
}
return results
}

// NewGenericPodController builds a new controller interface for anytype of Pod
Expand Down

0 comments on commit 5769acf

Please sign in to comment.