Skip to content

Commit

Permalink
Add code to collect cluster endpoint data with cluster-resources coll…
Browse files Browse the repository at this point in the history
…ector (#1219)

added code to collect cluster endpoint data to cluster-resources collector
  • Loading branch information
drohnow authored Jun 13, 2023
1 parent 620fa75 commit a562887
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
42 changes: 42 additions & 0 deletions pkg/collect/cluster_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,13 @@ func (c *CollectClusterResources) Collect(progressChan chan<- interface{}) (Coll
output.SaveResult(c.BundlePath, path.Join(constants.CLUSTER_RESOURCES_DIR, fmt.Sprintf("%s.json", constants.CLUSTER_RESOURCES_CLUSTER_ROLE_BINDINGS)), bytes.NewBuffer(clusterRoleBindings))
output.SaveResult(c.BundlePath, path.Join(constants.CLUSTER_RESOURCES_DIR, fmt.Sprintf("%s-errors.json", constants.CLUSTER_RESOURCES_CLUSTER_ROLE_BINDINGS)), marshalErrors(clusterRoleBindingsErrors))

// endpoints
endpoints, endpointsErrors := endpoints(ctx, client, namespaceNames)
for k, v := range endpoints {
output.SaveResult(c.BundlePath, path.Join(constants.CLUSTER_RESOURCES_DIR, constants.CLUSTER_RESOURCES_ENDPOINTS, k), bytes.NewBuffer(v))
}
output.SaveResult(c.BundlePath, path.Join(constants.CLUSTER_RESOURCES_DIR, fmt.Sprintf("%s-errors.json", constants.CLUSTER_RESOURCES_ENDPOINTS)), marshalErrors(endpointsErrors))

return output, nil
}

Expand Down Expand Up @@ -1888,3 +1895,38 @@ func clusterRoleBindings(ctx context.Context, client *kubernetes.Clientset) ([]b
}
return b, nil
}

func endpoints(ctx context.Context, client *kubernetes.Clientset, namespaces []string) (map[string][]byte, map[string]string) {
endpointsByNamespace := make(map[string][]byte)
errorsByNamespace := make(map[string]string)

for _, namespace := range namespaces {
endpoints, err := client.CoreV1().Endpoints(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
errorsByNamespace[namespace] = err.Error()
continue
}

gvk, err := apiutil.GVKForObject(endpoints, scheme.Scheme)
if err == nil {
endpoints.GetObjectKind().SetGroupVersionKind(gvk)
}

for i, o := range endpoints.Items {
gvk, err := apiutil.GVKForObject(&o, scheme.Scheme)
if err == nil {
endpoints.Items[i].GetObjectKind().SetGroupVersionKind(gvk)
}
}

b, err := json.MarshalIndent(endpoints, "", " ")
if err != nil {
errorsByNamespace[namespace] = err.Error()
continue
}

endpointsByNamespace[namespace+".json"] = b
}

return endpointsByNamespace, errorsByNamespace
}
1 change: 1 addition & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const (
CLUSTER_RESOURCES_CLUSTER_ROLES = "clusterroles"
CLUSTER_RESOURCES_CLUSTER_ROLE_BINDINGS = "clusterrolebindings"
CLUSTER_RESOURCES_PRIORITY_CLASS = "priorityclasses"
CLUSTER_RESOURCES_ENDPOINTS = "endpoints"

// Custom exit codes
EXIT_CODE_CATCH_ALL = 1
Expand Down

0 comments on commit a562887

Please sign in to comment.