Skip to content

Commit d80eba6

Browse files
Metrics plugin update
When collecting pods in a namespace for an app instance, we want to ensure that the namespace is indeed for the Kind whose app instance is being passed to the kubectl metrics command. Without this Kind name check, kubeplus may gets wrong Pods if app instances for two different kinds are given the same name.
1 parent 4865996 commit d80eba6

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

plugins/crmetrics.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,29 @@ def parse_pod_details(self, out, instance):
4545
pod_info['Namespace'] = instance
4646
pod_list.append(pod_info)
4747
return pod_list
48-
48+
49+
def get_service_name_from_ns(self, instanceName, orgPath):
50+
service_name_in_annotation = ""
51+
get_ns_cm = "kubectl get ns " + instanceName + " " + orgPath + " -o json "
52+
out_a, err_a = self.run_command(get_ns_cm)
53+
out_a = out_a.strip()
54+
err_a = err_a.strip()
55+
if "NotFound" not in err_a:
56+
json_obj = json.loads(out_a)
57+
helm_release_name = json_obj["metadata"]["annotations"]["meta.helm.sh/release-name"]
58+
parts = helm_release_name.split("-" + instanceName)
59+
service_name_in_annotation = parts[0].strip()
60+
service_name_in_annotation = service_name_in_annotation.replace('"','')
61+
return service_name_in_annotation
62+
4963
def get_pods_in_ns(self, kind, instance, kubeconfig):
5064
pod_list = []
5165
labelSelector = "partof=" + kind + "-" + instance
5266
labelSelector = labelSelector.lower()
67+
service_name_in_annotation = self.get_service_name_from_ns(instance, kubeconfig)
68+
service_name_in_annotation = service_name_in_annotation.lower()
69+
if service_name_in_annotation != kind.lower():
70+
return pod_list
5371
cmd = "kubectl get pods -n " + instance + " " + kubeconfig
5472
#print(cmd)
5573
out = ''
@@ -1146,16 +1164,17 @@ def get_metrics_cr(self, custom_resource, custom_res_instance, namespace, follow
11461164
millis = int(round(time.time() * 1000))
11471165
timeInMillis = str(millis)
11481166
metricsToReturn = ''
1149-
cpuMetrics = 'cpu{custom_resource="'+custom_res_instance+'"} ' + str(cpu) + ' ' + timeInMillis
1150-
memoryMetrics = 'memory{custom_resource="'+custom_res_instance+'"} ' + str(memory) + ' ' + timeInMillis
1151-
storageMetrics = 'storage{custom_resource="'+custom_res_instance+'"} ' + str(total_storage) + ' ' + timeInMillis
1152-
networkReceiveBytes = 'network_receive_bytes_total{custom_resource="'+custom_res_instance+'"} ' + str(networkReceiveBytesTotal) + ' ' + timeInMillis
1153-
networkTransmitBytes = 'network_transmit_bytes_total{custom_resource="'+custom_res_instance+'"} ' + str(networkTransmitBytesTotal) + ' ' + timeInMillis
1154-
numOfPods = 'pods{custom_resource="'+custom_res_instance+'"} ' + str(num_of_pods) + ' ' + timeInMillis
1155-
numOfContainers = 'containers{custom_resource="'+custom_res_instance+'"} ' + str(num_of_containers) + ' ' + timeInMillis
1156-
numOfNotRunningPods = 'not_running_pods{custom_resource="'+custom_res_instance+'"} ' + str(num_of_not_running_pods) + ' ' + timeInMillis
1157-
1158-
oomEvents = 'oom_events{custom_resource="'+custom_res_instance+'"} ' + str(oom_events) + ' ' + timeInMillis
1167+
fq_instance = custom_resource.lower() + "-" + custom_res_instance.lower()
1168+
cpuMetrics = 'cpu{custom_resource="'+fq_instance+'"} ' + str(cpu) + ' ' + timeInMillis
1169+
memoryMetrics = 'memory{custom_resource="'+fq_instance+'"} ' + str(memory) + ' ' + timeInMillis
1170+
storageMetrics = 'storage{custom_resource="'+fq_instance+'"} ' + str(total_storage) + ' ' + timeInMillis
1171+
networkReceiveBytes = 'network_receive_bytes_total{custom_resource="'+fq_instance+'"} ' + str(networkReceiveBytesTotal) + ' ' + timeInMillis
1172+
networkTransmitBytes = 'network_transmit_bytes_total{custom_resource="'+fq_instance+'"} ' + str(networkTransmitBytesTotal) + ' ' + timeInMillis
1173+
numOfPods = 'pods{custom_resource="'+fq_instance+'"} ' + str(num_of_pods) + ' ' + timeInMillis
1174+
numOfContainers = 'containers{custom_resource="'+fq_instance+'"} ' + str(num_of_containers) + ' ' + timeInMillis
1175+
numOfNotRunningPods = 'not_running_pods{custom_resource="'+fq_instance+'"} ' + str(num_of_not_running_pods) + ' ' + timeInMillis
1176+
1177+
oomEvents = 'oom_events{custom_resource="'+fq_instance+'"} ' + str(oom_events) + ' ' + timeInMillis
11591178

11601179
metricsToReturn = cpuMetrics + "\n" + memoryMetrics + "\n" + storageMetrics + "\n" + numOfPods + "\n" + numOfContainers + "\n" + networkReceiveBytes + "\n" + networkTransmitBytes + "\n" + numOfNotRunningPods + "\n" + oomEvents
11611180
print(metricsToReturn)

0 commit comments

Comments
 (0)