import json


def _make_pod_config(pod_name, image_name, container_port_list, cmd_list,
                    arg_list):
  """Creates a string containing the Pod defintion as required by the Kubernetes API"""
  body = {
      'kind': 'Pod',
      'apiVersion': 'v1',
      'metadata': {
          'name': pod_name,
          'labels': {'name': pod_name}
      },
      'spec': {
          'containers': [
              {
                  'name': pod_name,
                  'image': image_name,
                  'ports': []
              }
          ]
      }
  }
  # Populate the 'ports' list
  for port in container_port_list:
    port_entry = {'containerPort': port, 'protocol': 'TCP'}
    body['spec']['containers'][0]['ports'].append(port_entry)

  # Add the 'Command' and 'Args' attributes if they are passed.
  # Note:
  #  - 'Command' overrides the ENTRYPOINT in the Docker Image
  #  - 'Args' override the COMMAND in Docker image (yes, it is confusing!)
  if len(cmd_list) > 0:
    body['spec']['containers'][0]['command'] = cmd_list
  if len(arg_list) > 0:
    body['spec']['containers'][0]['args'] = arg_list
  return json.dumps(body)