|
| 1 | +package resources |
| 2 | + |
| 3 | +import ( |
| 4 | + "slices" |
| 5 | + "sort" |
| 6 | + |
| 7 | + "go.mondoo.com/cnquery/v11/llx" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + // https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/ |
| 12 | + K8sApplicationName = "app.kubernetes.io/name" |
| 13 | + K8sApplicationInstance = "app.kubernetes.io/instance" |
| 14 | + K8sApplicationVersion = "app.kubernetes.io/version" |
| 15 | + K8sApplicationComponent = "app.kubernetes.io/component" |
| 16 | + K8sApplicationPartOf = "app.kubernetes.io/part-of" |
| 17 | + K8sApplicationManagedBy = "app.kubernetes.io/managed-by" |
| 18 | +) |
| 19 | + |
| 20 | +func (k *mqlK8s) apps() ([]interface{}, error) { |
| 21 | + type k8sapp struct { |
| 22 | + name string |
| 23 | + version string |
| 24 | + instance string |
| 25 | + components []string |
| 26 | + partOf string |
| 27 | + managedBy string |
| 28 | + } |
| 29 | + |
| 30 | + apps := map[string]k8sapp{} |
| 31 | + |
| 32 | + // fetch deployment resources |
| 33 | + deployments := k.GetDeployments() |
| 34 | + if deployments.Error != nil { |
| 35 | + return nil, deployments.Error |
| 36 | + } |
| 37 | + |
| 38 | + for i := range deployments.Data { |
| 39 | + d := deployments.Data[i].(*mqlK8sDeployment) |
| 40 | + labels := d.GetLabels().Data |
| 41 | + name, nameOk := labels[K8sApplicationName] |
| 42 | + instance, instanceOK := labels[K8sApplicationInstance] |
| 43 | + version, versionOK := labels[K8sApplicationVersion] |
| 44 | + component, componentOK := labels[K8sApplicationComponent] |
| 45 | + partOf, partOfOK := labels[K8sApplicationPartOf] |
| 46 | + managedBy, managedByOK := labels[K8sApplicationManagedBy] |
| 47 | + |
| 48 | + if !nameOk { |
| 49 | + continue |
| 50 | + } |
| 51 | + |
| 52 | + app := k8sapp{ |
| 53 | + name: name.(string), |
| 54 | + } |
| 55 | + if instanceOK { |
| 56 | + app.instance = instance.(string) |
| 57 | + } |
| 58 | + if versionOK { |
| 59 | + app.version = version.(string) |
| 60 | + } |
| 61 | + if componentOK { |
| 62 | + app.components = []string{component.(string)} |
| 63 | + } |
| 64 | + if partOfOK { |
| 65 | + app.partOf = partOf.(string) |
| 66 | + } |
| 67 | + if managedByOK { |
| 68 | + app.managedBy = managedBy.(string) |
| 69 | + } |
| 70 | + |
| 71 | + key := app.name + app.instance |
| 72 | + if existing, ok := apps[key]; ok { |
| 73 | + // if the app already exists, we need to merge the components |
| 74 | + components := append(existing.components, app.components...) |
| 75 | + sort.Strings(components) |
| 76 | + components = slices.Compact(components) |
| 77 | + existing.components = components |
| 78 | + apps[app.name+app.instance] = existing |
| 79 | + } else { |
| 80 | + apps[app.name+app.instance] = app |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // // fetch daemonset resources |
| 85 | + // daemonsets := k.GetDaemonsets() |
| 86 | + // if daemonsets.Error != nil { |
| 87 | + // return nil, daemonsets.Error |
| 88 | + // } |
| 89 | + |
| 90 | + // return k8s app list |
| 91 | + appList := []interface{}{} |
| 92 | + for _, app := range apps { |
| 93 | + r, err := CreateResource(k.MqlRuntime, "k8s.app", map[string]*llx.RawData{ |
| 94 | + "__id": llx.StringData("app"), |
| 95 | + "name": llx.StringData(app.name), |
| 96 | + "version": llx.StringData(app.version), |
| 97 | + "instance": llx.StringData(app.instance), |
| 98 | + "managedBy": llx.StringData(app.managedBy), |
| 99 | + }) |
| 100 | + if err != nil { |
| 101 | + return nil, err |
| 102 | + } |
| 103 | + |
| 104 | + appList = append(appList, r) |
| 105 | + } |
| 106 | + |
| 107 | + return appList, nil |
| 108 | +} |
0 commit comments