github.com/nginxinc/kubernetes-ingress@v1.12.5/tests/suite/kube_config_utils.py (about)

     1  """Describe methods to work with kubeconfig file."""
     2  import pytest
     3  import yaml
     4  
     5  
     6  def get_current_context_name(kube_config) -> str:
     7      """
     8      Get current-context from kubeconfig.
     9  
    10      :param kube_config: absolute path to kubeconfig
    11      :return: str
    12      """
    13      with open(kube_config) as conf:
    14          dep = yaml.safe_load(conf)
    15          return dep['current-context']
    16  
    17  
    18  def ensure_context_in_config(kube_config, context_name) -> None:
    19      """
    20      Verify that kubeconfig contains specific context and fail if it doesn't.
    21  
    22      :param kube_config: absolute path to kubeconfig
    23      :param context_name: context name to verify
    24      :return:
    25      """
    26      with open(kube_config) as conf:
    27          dep = yaml.safe_load(conf)
    28          for contexts in dep['contexts']:
    29              if contexts['name'] == context_name:
    30                  return
    31      pytest.fail(f"Failed to find context '{context_name}' in the kubeconfig file: {kube_config}")