Skip to content

Commit

Permalink
feat: Extend ObjectLabelSelector to handle all label selector formats
Browse files Browse the repository at this point in the history
Currently `ObjectLabelSelector` is only able to support label
selectors of `map[string]string` type that is used by the
`corev1.Service` API.

This change allows `ObjectLabelSelector` to also support all label
selector formats used in Kubernetes (eg. `[]metav1.LabelSelector` &
`[]metav1.LabelSelectorRequirement`).

Signed-off-by: Justin Toh <[email protected]>
  • Loading branch information
tohjustin committed Oct 2, 2021
1 parent 6cbe11d commit 4516759
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions pkg/cmd/lineage/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ type ObjectLabelSelector struct {
Group string
Kind string
Namespace string
Selector map[string]string
Selector labels.Selector
}

// Key converts the ObjectLabelSelector into a ObjectLabelSelectorKey.
func (o *ObjectLabelSelector) Key() ObjectLabelSelectorKey {
k := fmt.Sprintf("%s/%s/%s/%s", o.Group, o.Kind, o.Namespace, labels.Set(o.Selector).String())
k := fmt.Sprintf("%s\\%s\\%s\\%s", o.Group, o.Kind, o.Namespace, o.Selector)
return ObjectLabelSelectorKey(k)
}

Expand All @@ -49,7 +49,7 @@ type ObjectReference struct {

// Key converts the ObjectReference into a ObjectReferenceKey.
func (o *ObjectReference) Key() ObjectReferenceKey {
k := fmt.Sprintf("%s/%s/%s/%s", o.Group, o.Kind, o.Namespace, o.Name)
k := fmt.Sprintf("%s\\%s\\%s\\%s", o.Group, o.Kind, o.Namespace, o.Name)
return ObjectReferenceKey(k)
}

Expand Down Expand Up @@ -84,7 +84,7 @@ type RelationshipMap struct {
DependentsByLabelSelector map[ObjectLabelSelectorKey]RelationshipSet
DependentsByRef map[ObjectReferenceKey]RelationshipSet
DependentsByUID map[types.UID]RelationshipSet
ObjectLabelSelectorByKey map[ObjectLabelSelectorKey]ObjectLabelSelector
ObjectLabelSelectors map[ObjectLabelSelectorKey]ObjectLabelSelector
}

func newRelationshipMap() RelationshipMap {
Expand All @@ -95,7 +95,7 @@ func newRelationshipMap() RelationshipMap {
DependentsByLabelSelector: map[ObjectLabelSelectorKey]RelationshipSet{},
DependentsByRef: map[ObjectReferenceKey]RelationshipSet{},
DependentsByUID: map[types.UID]RelationshipSet{},
ObjectLabelSelectorByKey: map[ObjectLabelSelectorKey]ObjectLabelSelector{},
ObjectLabelSelectors: map[ObjectLabelSelectorKey]ObjectLabelSelector{},
}
}

Expand All @@ -112,7 +112,7 @@ func (m *RelationshipMap) AddDependencyByLabelSelector(o ObjectLabelSelector, r
m.DependenciesByLabelSelector[k] = RelationshipSet{}
}
m.DependenciesByLabelSelector[k][r] = struct{}{}
m.ObjectLabelSelectorByKey[k] = o
m.ObjectLabelSelectors[k] = o
}

func (m *RelationshipMap) AddDependencyByUID(uid types.UID, r Relationship) {
Expand All @@ -135,7 +135,7 @@ func (m *RelationshipMap) AddDependentByLabelSelector(o ObjectLabelSelector, r R
m.DependentsByLabelSelector[k] = RelationshipSet{}
}
m.DependentsByLabelSelector[k][r] = struct{}{}
m.ObjectLabelSelectorByKey[k] = o
m.ObjectLabelSelectors[k] = o
}

func (m *RelationshipMap) AddDependentByUID(uid types.UID, r Relationship) {
Expand Down Expand Up @@ -269,14 +269,10 @@ func resolveDependents(objects []unstructuredv1.Unstructured, rootUID types.UID)
}

resolveSelectorToNodes := func(o ObjectLabelSelector) []*Node {
selector, err := labels.ValidatedSelectorFromSet(labels.Set(o.Selector))
if err != nil {
return nil
}
var result []*Node
for _, n := range globalMapByUID {
if n.Group == o.Group && n.Kind == o.Kind && n.Namespace == o.Namespace {
if ok := selector.Matches(labels.Set(n.GetLabels())); ok {
if ok := o.Selector.Matches(labels.Set(n.GetLabels())); ok {
result = append(result, n)
}
}
Expand All @@ -299,17 +295,17 @@ func resolveDependents(objects []unstructuredv1.Unstructured, rootUID types.UID)
}
}
for k, rset := range rmap.DependenciesByLabelSelector {
if sel, ok := rmap.ObjectLabelSelectorByKey[k]; ok {
for _, n := range resolveSelectorToNodes(sel) {
if ols, ok := rmap.ObjectLabelSelectors[k]; ok {
for _, n := range resolveSelectorToNodes(ols) {
for r := range rset {
n.AddDependent(node.UID, r)
}
}
}
}
for k, rset := range rmap.DependentsByLabelSelector {
if sel, ok := rmap.ObjectLabelSelectorByKey[k]; ok {
for _, n := range resolveSelectorToNodes(sel) {
if ols, ok := rmap.ObjectLabelSelectors[k]; ok {
for _, n := range resolveSelectorToNodes(ols) {
for r := range rset {
node.AddDependent(n.UID, r)
}
Expand Down Expand Up @@ -873,13 +869,17 @@ func getServiceRelationships(n *Node) (*RelationshipMap, error) {
return nil, err
}

var sel ObjectLabelSelector
var ols ObjectLabelSelector
ns := svc.Namespace
result := newRelationshipMap()

// RelationshipServiceSelector
sel = ObjectLabelSelector{Kind: "Pod", Namespace: ns, Selector: svc.Spec.Selector}
result.AddDependencyByLabelSelector(sel, RelationshipService)
selector, err := labels.ValidatedSelectorFromSet(labels.Set(svc.Spec.Selector))
if err != nil {
return nil, err
}
ols = ObjectLabelSelector{Kind: "Pod", Namespace: ns, Selector: selector}
result.AddDependencyByLabelSelector(ols, RelationshipService)

return &result, nil
}
Expand Down

0 comments on commit 4516759

Please sign in to comment.