sunguroku 5 лет назад
Родитель
Сommit
c4a6ef9ba0
2 измененных файлов с 25 добавлено и 9 удалено
  1. 13 5
      internal/helm/grapher/object.go
  2. 12 4
      internal/helm/grapher/relation.go

+ 13 - 5
internal/helm/grapher/object.go

@@ -16,25 +16,33 @@ func ParseObjs(objs []map[string]interface{}) []Object {
 	objArr := []Object{}
 
 	for i, obj := range objs {
-		kind := getField(obj, "kind").(string)
-		name := getField(obj, "metadata", "name").(string)
-
+		kind := getField(obj, "kind")
+		name := getField(obj, "metadata", "name")
 		namespace := getField(obj, "metadata", "namespace")
 
 		if namespace == nil {
 			namespace = "default"
 		}
 
+		if kind == nil {
+			kind = ""
+		}
+
+		if name == nil {
+			name = ""
+		}
+
 		// First add the object that appears on the YAML
 		parsedObj := Object{
 			ID:        i,
-			Kind:      kind,
-			Name:      name,
+			Kind:      kind.(string),
+			Name:      name.(string),
 			Namespace: namespace.(string),
 			RawYAML:   obj,
 			Relations: Relations{
 				ControlRels: []ControlRel{},
 				LabelRels:   []LabelRel{},
+				SpecRels:    []SpecRel{},
 			},
 		}
 		objArr = append(objArr, parsedObj)

+ 12 - 4
internal/helm/grapher/relation.go

@@ -65,15 +65,24 @@ func (parsed *ParsedObjs) GetControlRel() {
 	children := []Object{}
 	for i, obj := range parsed.Objects {
 		yaml := obj.RawYAML
+		kind := getField(yaml, "kind")
 
-		switch kind := getField(yaml, "kind").(string); kind {
+		if kind == nil {
+			kind = ""
+		}
+
+		switch kind.(string) {
 		// Parse for all possible controller types
 		case "Deployment", "StatefulSet", "ReplicaSet", "DaemonSet", "Job":
 			rs := getField(yaml, "spec", "replicas")
 
 			if rs != nil && rs.(int) > 0 {
 				// Add Pods for controller objects
-				template := getField(yaml, "spec", "template").(map[string]interface{})
+				template := getField(yaml, "spec", "template")
+				if template == nil {
+					continue
+				}
+
 				for j := 0; j < rs.(int); j++ {
 					cid := len(parsed.Objects) + len(children)
 					crel := ControlRel{
@@ -89,7 +98,7 @@ func (parsed *ParsedObjs) GetControlRel() {
 						Kind:      "Pod",
 						Name:      obj.Name + "-" + strconv.Itoa(j), // tentative name pre-deploy
 						Namespace: obj.Namespace,
-						RawYAML:   template,
+						RawYAML:   template.(map[string]interface{}),
 						Relations: Relations{
 							ControlRels: []ControlRel{
 								crel,
@@ -291,7 +300,6 @@ func (parsed *ParsedObjs) findRBACTargets(parentID int, yaml map[string]interfac
 			if tr["namespace"] != nil && o.Kind == tr["kind"] && o.Name == tr["name"] &&
 				(o.Namespace == tr["namespace"] || o.Namespace == "default") {
 
-				fmt.Println("subject", o.Name, tr["kind"])
 				// Add bidirectional link from children as well.
 				parsed.Objects[i].Relations.SpecRels = append(parsed.Objects[i].Relations.SpecRels, newrel)
 				targets = append(targets, o.ID)