github.com/SUSE/skuba@v1.4.17/ci/infra/testrunner/kubectl/kubectl.py (about)

     1  from utils.utils import (Utils)
     2  from time import sleep
     3  
     4  class Kubectl:
     5  
     6      def __init__(self, conf):
     7          self.conf = conf
     8          self.binpath = conf.kubectl.binpath
     9          self.kubeconfig = conf.kubectl.kubeconfig
    10          self.utils = Utils(self.conf)
    11  
    12      def run_kubectl(self, command, stdin=None):
    13          shell_cmd = f'{self.binpath} --kubeconfig={self.kubeconfig} {command}'
    14          try:
    15              return self.utils.runshellcommand(shell_cmd, stdin=stdin)
    16          except Exception as ex:
    17              raise Exception("Error executing cmd {}".format(shell_cmd)) from ex
    18  
    19      def get_num_nodes_by_role(self, role):
    20          """ Returns the number of nodes by role (master/worker)"""
    21          if role not in ("master", "worker"):
    22              raise ValueError("Invalid role {}".format(role))
    23  
    24          nodes = self.get_node_names_by_role(role)
    25          return len(nodes)
    26  
    27      def get_node_names_by_role(self, role):
    28          """Returns a list of node names for a given role
    29          Uses selectors to get the nodes. Master nodes have the node-role.kubernetes.io/master="" label, while other
    30          nodes (workers) dont even have the label.
    31          """
    32  
    33          if role not in ("master", "worker"):
    34              raise ValueError("Invalid role {}".format(role))
    35  
    36          roles = {
    37              "master": "==",
    38              "worker": "!="
    39          }
    40          command = f"get nodes --selector=node-role.kubernetes.io/master{roles.get(role)}"" -o jsonpath='{.items[*].metadata.name}'"
    41          return self.run_kubectl(command).split()
    42  
    43      def inhibit_kured(self):
    44          max_attempt = 18
    45          current_attempt = 0
    46          while current_attempt <= max_attempt:
    47              try:
    48                  self.run_kubectl("-n kube-system annotate ds kured weave.works/kured-node-lock='{\"nodeID\":\"manual\"}'")
    49                  break
    50              except Exception:
    51                  current_attempt += 1
    52                  sleep(10)