Skip to content

Commit

Permalink
chore: Collect endpointslices resources (#1636)
Browse files Browse the repository at this point in the history
Signed-off-by: Evans Mungai <[email protected]>
  • Loading branch information
banjoh authored Oct 3, 2024
1 parent f58f025 commit 0240a63
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
43 changes: 43 additions & 0 deletions pkg/collect/cluster_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,13 @@ func (c *CollectClusterResources) Collect(progressChan chan<- interface{}) (Coll
}
output.SaveResult(c.BundlePath, path.Join(constants.CLUSTER_RESOURCES_DIR, fmt.Sprintf("%s-errors.json", constants.CLUSTER_RESOURCES_ENDPOINTS)), marshalErrors(endpointsErrors))

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

// Service Accounts
servicesAccounts, servicesAccountsErrors := serviceAccounts(ctx, client, namespaceNames)
for k, v := range servicesAccounts {
Expand Down Expand Up @@ -1983,6 +1990,42 @@ func endpoints(ctx context.Context, client *kubernetes.Clientset, namespaces []s
return endpointsByNamespace, errorsByNamespace
}

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

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

// TODO: Can we DRY this? We repeat this pattern a lot
gvk, err := apiutil.GVKForObject(objs, scheme.Scheme)
if err == nil {
objs.GetObjectKind().SetGroupVersionKind(gvk)
}

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

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

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

return objsByNamespace, errorsByNamespace
}

func serviceAccounts(ctx context.Context, client kubernetes.Interface, namespaces []string) (map[string][]byte, map[string]string) {
serviceAccountsByNamespace := make(map[string][]byte)
errorsByNamespace := make(map[string]string)
Expand Down
1 change: 1 addition & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const (
CLUSTER_RESOURCES_CLUSTER_ROLE_BINDINGS = "clusterrolebindings"
CLUSTER_RESOURCES_PRIORITY_CLASS = "priorityclasses"
CLUSTER_RESOURCES_ENDPOINTS = "endpoints"
CLUSTER_RESOURCES_ENDPOINTSICES = "endpointslices"
CLUSTER_RESOURCES_SERVICE_ACCOUNTS = "serviceaccounts"
CLUSTER_RESOURCES_LEASES = "leases"
CLUSTER_RESOURCES_VOLUME_ATTACHMENTS = "volumeattachments"
Expand Down

0 comments on commit 0240a63

Please sign in to comment.