...

Source file src/sigs.k8s.io/cli-utils/pkg/object/graph/edge.go

Documentation: sigs.k8s.io/cli-utils/pkg/object/graph

     1  // Copyright 2022 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package graph
     5  
     6  import (
     7  	"sort"
     8  
     9  	"sigs.k8s.io/cli-utils/pkg/object"
    10  )
    11  
    12  // Edge encapsulates a pair of vertices describing a
    13  // directed edge.
    14  type Edge struct {
    15  	From object.ObjMetadata
    16  	To   object.ObjMetadata
    17  }
    18  
    19  // SortableEdges sorts a list of edges alphanumerically by From and then To.
    20  type SortableEdges []Edge
    21  
    22  var _ sort.Interface = SortableEdges{}
    23  
    24  func (a SortableEdges) Len() int      { return len(a) }
    25  func (a SortableEdges) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
    26  func (a SortableEdges) Less(i, j int) bool {
    27  	if a[i].From != a[j].From {
    28  		return metaIsLessThan(a[i].From, a[j].From)
    29  	}
    30  	return metaIsLessThan(a[i].To, a[j].To)
    31  }
    32  
    33  func metaIsLessThan(i, j object.ObjMetadata) bool {
    34  	if i.GroupKind.Group != j.GroupKind.Group {
    35  		return i.GroupKind.Group < j.GroupKind.Group
    36  	}
    37  	if i.GroupKind.Kind != j.GroupKind.Kind {
    38  		return i.GroupKind.Kind < j.GroupKind.Kind
    39  	}
    40  	if i.Namespace != j.Namespace {
    41  		return i.Namespace < j.Namespace
    42  	}
    43  	return i.Name < j.Name
    44  }
    45  

View as plain text