Skip to content

Commit

Permalink
fix: optimize namespace listing to resolve N+1 problem
Browse files Browse the repository at this point in the history
  • Loading branch information
012xx committed Feb 17, 2025
1 parent 8fda699 commit f02d359
Showing 1 changed file with 24 additions and 38 deletions.
62 changes: 24 additions & 38 deletions cmd/kubectl-accurate/sub/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,59 +57,45 @@ func (o *listOptions) Fill(streams genericiooptions.IOStreams, config *genericcl
}

func (o *listOptions) Run(ctx context.Context) error {
nsList := &corev1.NamespaceList{}
if err := o.client.List(ctx, nsList); err != nil {
return fmt.Errorf("failed to list namespaces from Kubernetes API: %w", err)
allNamespaces := &corev1.NamespaceList{}
if err := o.client.List(ctx, allNamespaces); err != nil {
return fmt.Errorf("failed to list all namespaces: %w", err)
}

nsMap := make(map[string][]corev1.Namespace)
for _, ns := range nsList.Items {
parent := ""
if ns.Labels != nil {
if p, exists := ns.Labels[constants.LabelParent]; exists {
parent = p
}
nsMap := make(map[string]*corev1.Namespace)
childMap := make(map[string][]*corev1.Namespace)

for i := range allNamespaces.Items {
ns := &allNamespaces.Items[i]
nsMap[ns.Name] = ns
if parent, ok := ns.Labels[constants.LabelParent]; ok {
childMap[parent] = append(childMap[parent], ns)
}
nsMap[parent] = append(nsMap[parent], ns)
}

if o.root != "" {
if _, exists := nsMap[o.root]; !exists {
return fmt.Errorf("specified root namespace '%s' not found", o.root)
var roots []*corev1.Namespace
for _, ns := range allNamespaces.Items {
if ns.Labels[constants.LabelType] == constants.NSTypeRoot {
roots = append(roots, &ns)
}
return o.showNSTree(ctx, o.root, nsMap, 0)
}

rootNamespaces, exists := nsMap[""]
if !exists || len(rootNamespaces) == 0 {
return fmt.Errorf("no root namespaces found; ensure namespaces have correct labels")
for _, root := range roots {
o.showNSRecursive(root, childMap, 0)
}

for _, ns := range rootNamespaces {
if err := o.showNSTree(ctx, ns.Name, nsMap, 0); err != nil {
return fmt.Errorf("failed to display namespace tree for '%s': %w", ns.Name, err)
}
}
return nil
}

func (o *listOptions) showNSTree(ctx context.Context, name string, nsMap map[string][]corev1.Namespace, level int) error {
if name == "" {
return fmt.Errorf("invalid namespace name: empty string")
}

fmt.Fprintf(o.streams.Out, "%*s%s\n", level, "", name)

children, exists := nsMap[name]
if !exists || len(children) == 0 {
return nil
func (o *listOptions) showNSRecursive(ns *corev1.Namespace, childMap map[string][]*corev1.Namespace, level int) {
subMark := " "
if _, ok := ns.Labels[constants.LabelParent]; ok {
subMark = "⮡"
}
fmt.Fprintf(o.streams.Out, "%*s%s%s\n", level, "", subMark, ns.Name)

level += indent
for _, child := range children {
if err := o.showNSTree(ctx, child.Name, nsMap, level); err != nil {
return fmt.Errorf("failed to process child namespace '%s': %w", child.Name, err)
}
for _, child := range childMap[ns.Name] {
o.showNSRecursive(child, childMap, level)
}
return nil
}

0 comments on commit f02d359

Please sign in to comment.