Skip to content

Commit

Permalink
feat: Support CSINode relationships
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Toh <tohjustin@hotmail.com>
  • Loading branch information
tohjustin committed Oct 11, 2021
1 parent b01b1a1 commit 5d7684b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ List of supported relationships used for discovering dependent objects:
- Kubernetes
- [ClusterRole References](https://kubernetes.io/docs/reference/kubernetes-api/authorization-resources/cluster-role-v1/), [ClusterRoleBinding References](https://kubernetes.io/docs/reference/kubernetes-api/authorization-resources/cluster-role-binding-v1/) & [RoleBinding References](https://kubernetes.io/docs/reference/kubernetes-api/authorization-resources/role-binding-v1/)
- [Controller References](https://github.com/kubernetes/community/blob/master/contributors/design-proposals/api-machinery/controller-ref.md) & [Owner References](https://kubernetes.io/docs/concepts/overview/working-with-objects/owners-dependents/)
- [CSINode References](https://kubernetes.io/docs/reference/kubernetes-api/config-and-storage-resources/csi-node-v1/)
- [Event References](https://kubernetes.io/docs/reference/kubernetes-api/cluster-resources/event-v1/)
- [Ingress References](https://kubernetes.io/docs/reference/kubernetes-api/service-resources/ingress-v1/) & [IngressClass Reference](https://kubernetes.io/docs/reference/kubernetes-api/service-resources/ingress-class-v1/)
- [MutatingWebhookConfiguration References](https://kubernetes.io/docs/reference/kubernetes-api/extend-resources/mutating-webhook-configuration-v1/) & [ValidatingWebhookConfiguration References](https://kubernetes.io/docs/reference/kubernetes-api/extend-resources/validating-webhook-configuration-v1/)
Expand Down
7 changes: 7 additions & 0 deletions internal/graph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,13 @@ func ResolveDependents(m meta.RESTMapper, objects []unstructuredv1.Unstructured,
klog.V(4).Infof("Failed to get relationships for rolebinding named \"%s\" in namespace \"%s\": %s: %s", node.Name, node.Namespace, err)
continue
}
// Populate dependents based on CSINode relationships
case node.Group == "storage.k8s.io" && node.Kind == "CSINode":
rmap, err = getCSINodeRelationships(node)
if err != nil {
klog.V(4).Infof("Failed to get relationships for csinode named \"%s\": %s: %s", node.Name, err)
continue
}
default:
continue
}
Expand Down
25 changes: 25 additions & 0 deletions internal/graph/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
networkingv1 "k8s.io/api/networking/v1"
rbacv1 "k8s.io/api/rbac/v1"
storagev1 "k8s.io/api/storage/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -20,6 +21,9 @@ const (
RelationshipRoleBindingSubject Relationship = "RoleBindingSubject"
RelationshipRoleBindingRole Relationship = "RoleBindingRole"

// Kubernetes CSINode relationships.
RelationshipCSINodeDriver Relationship = "CSINodeDriver"

// Kubernetes Event relationships.
RelationshipEventRegarding Relationship = "EventRegarding"
RelationshipEventRelated Relationship = "EventRelated"
Expand Down Expand Up @@ -114,6 +118,27 @@ func getClusterRoleBindingRelationships(n *Node) (*RelationshipMap, error) {
return &result, nil
}

// getCSINodeRelationships returns a map of relationships that this CSINode has
// with other objects, based on what was referenced in its manifest.
func getCSINodeRelationships(n *Node) (*RelationshipMap, error) {
var csin storagev1.CSINode
err := runtime.DefaultUnstructuredConverter.FromUnstructured(n.UnstructuredContent(), &csin)
if err != nil {
return nil, err
}

var ref ObjectReference
result := newRelationshipMap()

// RelationshipCSINodeDriver
for _, d := range csin.Spec.Drivers {
ref = ObjectReference{Group: "storage.k8s.io", Kind: "CSIDriver", Name: d.Name}
result.AddDependentByKey(ref.Key(), RelationshipCSINodeDriver)
}

return &result, nil
}

// getEventRelationships returns a map of relationships that this Event has with
// other objects, based on what was referenced in its manifest.
//nolint:unparam
Expand Down

0 comments on commit 5d7684b

Please sign in to comment.