Skip to content

Commit

Permalink
merge the list of resources from custom checks and the generated cont…
Browse files Browse the repository at this point in the history
…roller list before deduplicating them (#727)

Co-authored-by: Robert Brennan <accounts@rbren.io>
  • Loading branch information
staerion and rbren authored Apr 7, 2022
1 parent 1841b74 commit fd16fb9
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions pkg/kube/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ func CreateResourceProviderFromAPI(ctx context.Context, kube kubernetes.Interfac
}
}

var kubernetesResources []GenericResource
for _, kind := range additionalKinds {
groupKind := parseGroupKind(maybeTransformKindIntoGroupKind(string(kind)))
mapping, err := (restMapper).RESTMapping(groupKind)
Expand All @@ -319,7 +320,7 @@ func CreateResourceProviderFromAPI(ctx context.Context, kube kubernetes.Interfac
if err != nil {
return nil, err
}
provider.Resources.addResource(res)
kubernetesResources = append(kubernetesResources, res)
}
}

Expand All @@ -330,9 +331,12 @@ func CreateResourceProviderFromAPI(ctx context.Context, kube kubernetes.Interfac
logrus.Errorf("Error loading controllers from pods: %v", err)
return nil, err
}
// resources loaded from custom checks can also contain controllers and thus would be added twice to the provider
kubernetesResources = deduplicateControllers(append(kubernetesResources, controllers...))

provider.Nodes = nodes.Items
provider.Namespaces = namespaces.Items
provider.Resources.addResources(controllers)
provider.Resources.addResources(kubernetesResources)
return &provider, nil
}

Expand All @@ -355,14 +359,14 @@ func LoadControllers(ctx context.Context, pods []corev1.Pod, dynamicClientPointe
}
interfaces = append(interfaces, workload)
}
return deduplicateControllers(interfaces), nil
return interfaces, 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(inputControllers []GenericResource) []GenericResource {
func deduplicateControllers(inputResources []GenericResource) []GenericResource {
controllerMap := make(map[string]GenericResource)
for _, controller := range inputControllers {
for _, controller := range inputResources {
key := controller.ObjectMeta.GetNamespace() + "/" + controller.Kind + "/" + controller.ObjectMeta.GetName()
oldController, ok := controllerMap[key]
if !ok || controller.ObjectMeta.GetCreationTimestamp().Time.After(oldController.ObjectMeta.GetCreationTimestamp().Time) {
Expand Down

0 comments on commit fd16fb9

Please sign in to comment.