13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
+ import { generateKubernetesSelectorFilter } from '../utils/labelSelectorParser' ;
17
+ import { Entity } from '@backstage/catalog-model' ;
16
18
17
- export const dynatraceQueries : Record < string , string | undefined > = {
18
- 'kubernetes-deployments' : `
19
+ export enum DynatraceQueryKeys {
20
+ KUBERNETES_DEPLOYMENTS = 'kubernetes-deployments' ,
21
+ }
22
+
23
+ interface ApiConfig {
24
+ environmentName : string ;
25
+ environmentUrl : string ;
26
+ }
27
+
28
+ export const isValidDynatraceQueryKey = (
29
+ key : string ,
30
+ ) : key is DynatraceQueryKeys => key in dynatraceQueries ;
31
+
32
+ export const dynatraceQueries : Record <
33
+ DynatraceQueryKeys ,
34
+ ( entity : Entity , apiConfig : ApiConfig ) => string
35
+ > = {
36
+ [ DynatraceQueryKeys . KUBERNETES_DEPLOYMENTS ] : ( entity , apiConfig ) => {
37
+ const labelSelector =
38
+ entity . metadata . annotations ?. [ 'backstage.io/kubernetes-label-selector' ] ;
39
+ const kubernetesId =
40
+ entity . metadata . annotations ?. [ 'backstage.io/kubernetes-id' ] ;
41
+ const namespace =
42
+ entity . metadata . annotations ?. [ 'backstage.io/kubernetes-namespace' ] ;
43
+
44
+ const filterLabel = labelSelector
45
+ ? generateKubernetesSelectorFilter ( labelSelector )
46
+ : '' ;
47
+ // if a label filter is given, the id is ignored
48
+ const filterKubernetesId =
49
+ filterLabel || ! kubernetesId
50
+ ? ''
51
+ : `| filter workload.labels[\`backstage.io/kubernetes-id\`] == "${ kubernetesId } "` ;
52
+ const filterNamespace = namespace
53
+ ? `| filter Namespace == "${ namespace } "`
54
+ : '' ;
55
+
56
+ if ( ! filterKubernetesId && ! filterLabel ) {
57
+ throw new Error (
58
+ 'One of the component annotations is required: "backstage.io/kubernetes-id" or "backstage.io/kubernetes-label-selector"' ,
59
+ ) ;
60
+ }
61
+
62
+ return `
19
63
fetch dt.entity.cloud_application, from: -10m
20
64
| fields id,
21
65
name = entity.name,
@@ -25,19 +69,22 @@ export const dynatraceQueries: Record<string, string | undefined> = {
25
69
namespace.id = belongs_to[dt.entity.cloud_application_namespace]
26
70
| sort upper(name) asc
27
71
| lookup [fetch dt.entity.cloud_application_instance, from: -10m | fields matchedId = instance_of[dt.entity.cloud_application], podVersion = cloudApplicationLabels[\`app.kubernetes.io/version\`]], sourceField:id, lookupField:matchedId, fields:{podVersion}
28
- | fieldsAdd Workload = record({type="link", text=name, url=concat("\${ environmentUrl}/ui/apps/dynatrace.kubernetes/resources/pod?entityId=", id)})
72
+ | fieldsAdd Workload = record({type="link", text=name, url=concat("${ apiConfig . environmentUrl } /ui/apps/dynatrace.kubernetes/resources/pod?entityId=", id)})
29
73
| lookup [fetch dt.entity.kubernetes_cluster, from: -10m | fields id, Cluster = entity.name],sourceField:cluster.id, lookupField:id, fields:{Cluster}
30
74
| lookup [fetch dt.entity.cloud_application_namespace, from: -10m | fields id, Namespace = entity.name], sourceField:namespace.id, lookupField:id, fields:{Namespace}
31
75
| lookup [fetch events, from: -30m | filter event.kind == "DAVIS_PROBLEM" | fieldsAdd affected_entity_id = affected_entity_ids[0] | summarize collectDistinct(event.status), by:{display_id, affected_entity_id}, alias:problem_status | filter NOT in(problem_status, "CLOSED") | summarize Problems = count(), by:{affected_entity_id}], sourceField:id, lookupField:affected_entity_id, fields:{Problems}
32
76
| fieldsAdd Problems=coalesce(Problems,0)
33
77
| lookup [ fetch events, from: -30m | filter event.kind=="SECURITY_EVENT" | filter event.category=="VULNERABILITY_MANAGEMENT" | filter event.provider=="Dynatrace" | filter event.type=="VULNERABILITY_STATE_REPORT_EVENT" | filter in(vulnerability.stack,{"CODE_LIBRARY","SOFTWARE","CONTAINER_ORCHESTRATION"}) | filter event.level=="ENTITY" | summarize { workloadId=arrayFirst(takeFirst(related_entities.kubernetes_workloads.ids)), vulnerability.stack=takeFirst(vulnerability.stack)}, by: {vulnerability.id, affected_entity.id} | summarize { Vulnerabilities=count() }, by: {workloadId}], sourceField:id, lookupField:workloadId, fields:{Vulnerabilities}
34
78
| fieldsAdd Vulnerabilities=coalesce(Vulnerabilities,0)
35
- | filter workload.labels[\`backstage.io/component\`] == "\${componentNamespace}.\${componentName}"
79
+ ${ filterKubernetesId }
80
+ ${ filterNamespace }
81
+ ${ filterLabel }
36
82
| fieldsAdd Logs = record({type="link", text="Show logs", url=concat(
37
- "\${ environmentUrl}",
83
+ "${ apiConfig . environmentUrl } ",
38
84
"/ui/apps/dynatrace.notebooks/intent/view-query#%7B%22dt.query%22%3A%22fetch%20logs%20%7C%20filter%20matchesValue(dt.entity.cloud_application%2C%5C%22",
39
85
id,
40
86
"%5C%22)%20%7C%20sort%20timestamp%20desc%22%2C%22title%22%3A%22Logs%22%7D")})
41
87
| fieldsRemove id, deploymentVersion, podVersion, name, workload.labels, cluster.id, namespace.id
42
- | fieldsAdd Environment = "\${environmentName}"` ,
88
+ | fieldsAdd Environment = "${ apiConfig . environmentName } "` ;
89
+ } ,
43
90
} ;
0 commit comments