Skip to content

Commit

Permalink
refactor: optimize namespace listing and tree display logic
Browse files Browse the repository at this point in the history
  • Loading branch information
012xx committed Feb 17, 2025
1 parent 0d94bbd commit 8fda699
Showing 1 changed file with 36 additions and 22 deletions.
58 changes: 36 additions & 22 deletions cmd/kubectl-accurate/sub/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,44 +57,58 @@ 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)
}

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[parent] = append(nsMap[parent], ns)
}

if o.root != "" {
return o.showNS(ctx, o.root, 0)
if _, exists := nsMap[o.root]; !exists {
return fmt.Errorf("specified root namespace '%s' not found", o.root)
}
return o.showNSTree(ctx, o.root, nsMap, 0)
}

roots := &corev1.NamespaceList{}
if err := o.client.List(ctx, roots, client.MatchingLabels{constants.LabelType: constants.NSTypeRoot}); err != nil {
return fmt.Errorf("failed to list the root namespaces: %w", err)
rootNamespaces, exists := nsMap[""]
if !exists || len(rootNamespaces) == 0 {
return fmt.Errorf("no root namespaces found; ensure namespaces have correct labels")
}

for _, ns := range roots.Items {
if err := o.showNS(ctx, ns.Name, 0); err != nil {
return err
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) showNS(ctx context.Context, name string, level int) error {
ns := &corev1.Namespace{}
if err := o.client.Get(ctx, client.ObjectKey{Name: name}, ns); err != nil {
return fmt.Errorf("failed to get namespace %s: %w", name, err)
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")
}

subMark := " "
if _, ok := ns.Labels[constants.LabelParent]; ok {
subMark = "⮡"
}
fmt.Fprintf(o.streams.Out, "%*s%s%s\n", level, "", subMark, name)
fmt.Fprintf(o.streams.Out, "%*s%s\n", level, "", name)

children := &corev1.NamespaceList{}
if err := o.client.List(ctx, children, client.MatchingLabels{constants.LabelParent: name}); err != nil {
return fmt.Errorf("failed to list the children of %s: %w", name, err)
children, exists := nsMap[name]
if !exists || len(children) == 0 {
return nil
}

level += indent
for _, child := range children.Items {
if err := o.showNS(ctx, child.Name, level); err != nil {
return err
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)
}
}
return nil
Expand Down

0 comments on commit 8fda699

Please sign in to comment.