<h2>API for retrieving information on a Kubernetes resource on-demand</h2><br/><div style="overflow-x:auto"><p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">When CNO is deployed, a scripted API can be used to send a request to the informer to get the information on a specific Kubernetes resource.</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">The script name is sn_acc_visibility.K8sInformerGetResourceApi and it is open to all application scopes, but will reject calls from users who do not have the discovery_admin role.</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"><img src="/sys_attachment.do?sys_id=cc52b125930b4650101833527cba1014" width="674" height="282" /></p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">The caller should call the script function getResources which accepts two parameters:</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p> <ul style="list-style-position: inside;"><li style="font-size: 12pt; font-family: 'Times New Roman', serif;">Informer – the informer name as seen in the Name field (e.g. k8s_informer_05a96879-9ea0-4e2d-bc19-cd99cc8f983c)</li><li style="font-size: 12pt; font-family: 'Times New Roman', serif;">Resources – array of resource requests. Each resource request has the following fields:</li></ul> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif; padding-left: 80px;"><strong>url</strong>: this is the URL of the request resource in the Kubernetes API server. The URL does not include the protocol and host name. Example: apis/apps/v1/namespaces/servicenow/deployments/k8s-informer-cno</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif; padding-left: 80px;"> </p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif; padding-left: 80px;"><strong>jsonPath</strong>: this is an optional parameter allowing the caller to specify which part in the resource JSON we would like to send to the instance. Example (<span style="font-size: 10.0pt;">"</span>.spec<span style="font-size: 10.0pt;">"</span>)</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif; padding-left: 80px;"> </p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif; padding-left: 80px;"><strong>targetUrl</strong>: this is the URL on the instance to which the informer will post the result.</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">Example of an API call:</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p> <p style="line-height: 10pt; background: rgb(255, 255, 254); margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"><span style="font-size: 7.5pt; font-family: Menlo; color: blue;">var</span><span style="font-size: 7.5pt; font-family: Menlo; color: black;"> requests = [ { url: </span><span style="font-size: 7.5pt; font-family: Menlo; color: #a31515;">'apis/apps/v1/namespaces/servicenow/deployments/k8s-informer-cno'</span><span style="font-size: 7.5pt; font-family: Menlo; color: black;">, </span></p> <p style="line-height: 10pt; background: rgb(255, 255, 254); margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"><span style="font-size: 7.5pt; font-family: Menlo; color: black;"> targetUrl: </span><span style="font-size: 7.5pt; font-family: Menlo; color: #a31515;">'api/sn_acc_visibility/informer/resource?cluster_name=my_cluster'</span><span style="font-size: 7.5pt; font-family: Menlo; color: black;">, </span></p> <p style="line-height: 10pt; background: rgb(255, 255, 254); margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"><span style="font-size: 7.5pt; font-family: Menlo; color: black;"> jsonPath: </span><span style="font-size: 7.5pt; font-family: Menlo; color: #a31515;">'.spec'</span><span style="font-size: 7.5pt; font-family: Menlo; color: black;"> </span></p> <p style="line-height: 10pt; background: rgb(255, 255, 254); margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"><span style="font-size: 7.5pt; font-family: Menlo; color: black;"> }</span></p> <p style="line-height: 10pt; background: rgb(255, 255, 254); margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"><span style="font-size: 7.5pt; font-family: Menlo; color: black;"> ];</span></p> <p style="line-height: 10pt; background: rgb(255, 255, 254); margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p> <p style="line-height: 10pt; background: rgb(255, 255, 254); margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"><span style="font-size: 7.5pt; font-family: Menlo; color: blue;">new</span><span style="font-size: 7.5pt; font-family: Menlo; color: black;"> sn_acc_visibility.</span><span style="font-size: 7.5pt; font-family: Menlo; color: teal;">K8sInformerGetResourceApi</span><span style="font-size: 7.5pt; font-family: Menlo; color: black;">().getResources(</span><span style="font-size: 7.5pt; font-family: Menlo; color: #a31515;">'k8s_informer_05a96879-9ea0-4e2d-bc19-cd99cc8f983c'</span><span style="font-size: 7.5pt; font-family: Menlo; color: black;">,requests);</span></p> <h1 style="margin: 18pt 0cm 4pt; break-after: avoid; font-size: 20pt; font-family: 'Aptos Display', sans-serif; color: rgb(15, 71, 97); font-weight: normal;">The Happy Path</h1> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">On the instance, when a request is processed and a command is sent to the informer, the syslog will show:</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"><span style="font-size: 10.0pt;">K8sInformer[K8sInformerGetResourceApi] K8sInformerGetResourceApi message sent to informer k8s_informer_47685711-e83e-4169-b84b-5ce610713375</span></p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">When the informer receives the request and processes it successfully, the log messages report on that. As an example:</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"><span style="font-size: 10.0pt;">2024/05/05 13:02:40 Accepted command: get_resource=[{"url":"apis/apps/v1/namespaces/trivy-system/deployments/trivy-operator","targetUrl":"/api/sn_acc_visibility/informer/resource?cluster_name=my_cluster"}]</span></p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"><span style="font-size: 10.0pt;">2024/05/05 13:02:41 Payload size 6278 posted to https://cnotal5.service-now.com/api/sn_acc_visibility/informer/resource?cluster_name=my_cluster</span></p> <h1 style="margin: 18pt 0cm 4pt; break-after: avoid; font-size: 20pt; font-family: 'Aptos Display', sans-serif; color: rgb(15, 71, 97); font-weight: normal;">Error Conditions Detected On the Instance Side</h1> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">The calling user does not have the discovery_admin role</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">Action taken: Exception thrown and shown in the system logs</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">One of the requests does not have the mandatory attributes url and targetUrl</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">Action taken: Exception thrown and shown in the system logs</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">Requests input argument is not provided</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">Action taken: Exception thrown and shown in the system logs</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">The number of requests exceeds the maximum allowed (10)</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">Action taken: Exception thrown and shown in the system logs</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">The informer name is not found</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">Action taken: Reported in syslog</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">The informer name is found, but the informer is not in the Up state</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">Action taken: Reported to syslog</p> <h1 style="margin: 18pt 0cm 4pt; break-after: avoid; font-size: 20pt; font-family: 'Aptos Display', sans-serif; color: rgb(15, 71, 97); font-weight: normal;">Error Conditions Detected On the Informer Side</h1> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">All errors are reported in the informer's log.</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">The number of requests in a single command exceeded the maximum allowed.</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">The informer will drop request beyond the declared limit.</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">(the maximum is defined in Helm value maxGetResourceRequests with default of 10).</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">The size of the payload of a single request exceeds the maximum allowed.</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">The informer will report to the log and ignore the request</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">(the size is defined in Helm value getResourceMaxBytes with the default of 5242800).</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">Missing url or targetUrl in a request</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">The informer will drop the invalid requests</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">Failure to get the resource from Kubernetes API server</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">The informer will drop the request and report to the log</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">The payload extracted using the given JSON path resulted in empty payload</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">The informer will drop the request and report to the log</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">Failure to complete the HTTP post call to the instance</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">The informer will retry two times by default (controlled by Helm value httpRestApiPostRetryCount)</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">Http Post call was successful but the status code was >= 400</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">Error will be reported to the informer log</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p> <h1 style="margin: 18pt 0cm 4pt; break-after: avoid; font-size: 20pt; font-family: 'Aptos Display', sans-serif; color: rgb(15, 71, 97); font-weight: normal;">The Instance Side Scripted Rest API</h1> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;">The URL provided in the get_resource request should correspond to a scripted rest API endpoint. The associated script should look like this:</p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p> <p style="line-height: 10pt; background: rgb(255, 255, 254); margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"><span style="font-size: 7.5pt; font-family: Menlo; color: black;">(</span><span style="font-size: 7.5pt; font-family: Menlo; color: blue;">function</span><span style="font-size: 7.5pt; font-family: Menlo; color: black;"> process(</span><span style="font-size: 7.5pt; font-family: Menlo; color: green;">/*RESTAPIRequest*/</span><span style="font-size: 7.5pt; font-family: Menlo; color: black;"> request, </span><span style="font-size: 7.5pt; font-family: Menlo; color: green;">/*RESTAPIResponse*/</span><span style="font-size: 7.5pt; font-family: Menlo; color: black;"> response) {<!-- --></span></p> <p style="line-height: 10pt; background: rgb(255, 255, 254); margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p> <p style="line-height: 10pt; background: rgb(255, 255, 254); margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"><span style="font-size: 7.5pt; font-family: Menlo; color: black;"> </span><span style="font-size: 7.5pt; font-family: Menlo; color: green;">// The resource payload will be in request.body.dataString</span></p> <p style="line-height: 10pt; background: rgb(255, 255, 254); margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"><span style="font-size: 7.5pt; font-family: Menlo; color: black;"> </span><span style="font-size: 7.5pt; font-family: Menlo; color: blue;">var</span><span style="font-size: 7.5pt; font-family: Menlo; color: black;"> reqStr = request.body.dataString;</span></p> <p style="line-height: 10pt; background: rgb(255, 255, 254); margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p> <p style="line-height: 10pt; background: rgb(255, 255, 254); margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"><span style="font-size: 7.5pt; font-family: Menlo; color: black;"> </span><span style="font-size: 7.5pt; font-family: Menlo; color: green;">// The request may come with query parameters</span></p> <p style="line-height: 10pt; background: rgb(255, 255, 254); margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"><span style="font-size: 7.5pt; font-family: Menlo; color: black;"> </span><span style="font-size: 7.5pt; font-family: Menlo; color: blue;">if</span><span style="font-size: 7.5pt; font-family: Menlo; color: black;"> (request.queryParams)</span></p> <p style="line-height: 10pt; background: rgb(255, 255, 254); margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"><span style="font-size: 7.5pt; font-family: Menlo; color: black;"> gs.info(</span><span style="font-size: 7.5pt; font-family: Menlo; color: #a31515;">'Informer request params: '</span><span style="font-size: 7.5pt; font-family: Menlo; color: black;"> + </span><span style="font-size: 7.5pt; font-family: Menlo; color: teal;">JSON</span><span style="font-size: 7.5pt; font-family: Menlo; color: black;">.stringify(request.queryParams));</span></p> <p style="line-height: 10pt; background: rgb(255, 255, 254); margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"><span style="font-size: 7.5pt; font-family: Menlo; color: black;"> </span></p> <p style="line-height: 10pt; background: rgb(255, 255, 254); margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"><span style="font-size: 7.5pt; font-family: Menlo; color: black;"> </span><span style="font-size: 7.5pt; font-family: Menlo; color: green;">// Put your logic here</span></p> <p style="line-height: 10pt; background: rgb(255, 255, 254); margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"><span style="font-size: 7.5pt; font-family: Menlo; color: black;"> gs.info(</span><span style="font-size: 7.5pt; font-family: Menlo; color: #a31515;">'Uploaded from informer: '</span><span style="font-size: 7.5pt; font-family: Menlo; color: black;"> + reqStr);</span></p> <p style="line-height: 10pt; background: rgb(255, 255, 254); margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p> <p style="line-height: 10pt; background: rgb(255, 255, 254); margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"><span style="font-size: 7.5pt; font-family: Menlo; color: black;">})(request, response);</span></p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p> <p style="margin: 0cm; font-size: 12pt; font-family: 'Times New Roman', serif;"> </p></div>