import os.path
from ansible.module_utils.dl import Installer
from ansible.module_utils.common_info import ContainerRuntimeType
class NpuExporterInstaller(Installer):
component_name = 'npu-exporter'
def get_modified_yaml_contents(self):
lines = self._get_yaml_contents()
replace_line = ""
offset = -1
node_type = self.container_runtime_type.get(self.node_name)
if not node_type:
self.module.fail_json("[ASCEND][ERROR] failed to find container runtime type for node: {}"
"in dict: {}.".format(self.node_name, self.container_runtime_type))
for index, line in enumerate(lines):
"""
Check if the line contains the default docker container mode parameter
NPU Exporter's YAML defaults to "-containerMode=docker".
If node's container runtime is docker: no modification needed
If container runtime is containerd: modify -containerMode, -containerd and -endpoint parameters
"""
if "-containerMode=docker" in line:
if node_type == ContainerRuntimeType.CONTAINERD:
replace_line = line.replace("-containerMode=docker",
"-containerMode=containerd -containerd=/run/containerd/containerd.sock "
"-endpoint=/run/containerd/containerd.sock")
offset = index
break
if replace_line:
lines[offset] = replace_line
return lines
def create_pull_secret(self):
create_namespace_cmd = 'kubectl create namespace npu-exporter'
self.module.run_command(create_namespace_cmd)
if __name__ == '__main__':
NpuExporterInstaller().run()