def use_named(self, instance_name):
        # Check to make sure kubectl exists
        try:
            subprocess.check_output(['kubectl'])
        except OSError:
            raise base_environment.VitessEnvironmentError(
                'kubectl not found, please install by visiting kubernetes.io or '
                'running gcloud components update kubectl if using compute engine.'
            )

        vtctld_ip = kubernetes_components.get_forwarded_ip(
            'vtctld', instance_name)
        self.vtctl_addr = '%s:15999' % vtctld_ip

        self.vtctl_helper = vtctl_helper.VtctlHelper('grpc', self.vtctl_addr)
        self.cluster_name = instance_name

        keyspaces = self.vtctl_helper.execute_vtctl_command(['GetKeyspaces'])
        self.mobs = filter(None, keyspaces.split('\n'))
        self.keyspaces = self.mobs

        if not self.keyspaces:
            raise base_environment.VitessEnvironmentError(
                'Invalid environment, no keyspaces found')

        self.num_shards = []
        self.shards = []

        for keyspace in self.keyspaces:
            shards = json.loads(
                self.vtctl_helper.execute_vtctl_command(
                    ['FindAllShardsInKeyspace', keyspace]))
            self.shards.append(shards)
            self.num_shards.append(len(shards))

        # This assumes that all keyspaces use the same set of cells
        self.cells = json.loads(
            self.vtctl_helper.execute_vtctl_command([
                'GetShard',
                '%s/%s' % (self.keyspaces[0], self.shards[0].keys()[0])
            ]))['cells']

        self.primary_cells = self.cells
        self.replica_instances = []
        self.rdonly_instances = []

        # This assumes that all cells are equivalent for k8s environments.
        all_tablets_in_a_cell = self.vtctl_helper.execute_vtctl_command(
            ['ListAllTablets', self.cells[0]])
        all_tablets_in_a_cell = [
            x.split(' ')
            for x in filter(None, all_tablets_in_a_cell.split('\n'))
        ]

        for index, keyspace in enumerate(self.keyspaces):
            keyspace_tablets_in_cell = [
                tablet for tablet in all_tablets_in_a_cell
                if tablet[1] == keyspace
            ]
            replica_tablets_in_cell = [
                tablet for tablet in keyspace_tablets_in_cell
                if tablet[3] == 'master' or tablet[3] == 'replica'
            ]
            replica_instances = len(
                replica_tablets_in_cell) / self.num_shards[index]
            self.replica_instances.append(replica_instances)
            self.rdonly_instances.append((len(keyspace_tablets_in_cell) /
                                          self.num_shards[index]) -
                                         replica_instances)

        # Converts keyspace name and alias to number of instances
        self.keyspace_alias_to_num_instances_dict = {}
        for index, keyspace in enumerate(self.keyspaces):
            self.keyspace_alias_to_num_instances_dict[keyspace] = {
                'replica': int(self.replica_instances[index]),
                'rdonly': int(self.rdonly_instances[index])
            }

        self.vtgate_addrs = {}
        for cell in self.cells:
            vtgate_ip = kubernetes_components.get_forwarded_ip(
                'vtgate-%s' % cell, instance_name)
            self.vtgate_addrs[cell] = '%s:15991' % vtgate_ip
        super(K8sEnvironment, self).use_named(instance_name)
Esempio n. 2
0
  def use_named(self, instance_name):
    # Check to make sure kubectl exists
    try:
      subprocess.check_output(['kubectl'])
    except OSError:
      raise base_environment.VitessEnvironmentError(
          'kubectl not found, please install by visiting kubernetes.io or '
          'running gcloud components update kubectl if using compute engine.')

    vtctld_ip = kubernetes_components.get_forwarded_ip(
        'vtctld', instance_name)
    self.vtctl_addr = '%s:15999' % vtctld_ip

    self.vtctl_helper = vtctl_helper.VtctlHelper('grpc', self.vtctl_addr)
    self.cluster_name = instance_name

    keyspaces = self.vtctl_helper.execute_vtctl_command(['GetKeyspaces'])
    self.mobs = filter(None, keyspaces.split('\n'))
    self.keyspaces = self.mobs

    if not self.keyspaces:
      raise base_environment.VitessEnvironmentError(
          'Invalid environment, no keyspaces found')

    self.num_shards = []
    self.shards = []

    for keyspace in self.keyspaces:
      shards = json.loads(self.vtctl_helper.execute_vtctl_command(
          ['FindAllShardsInKeyspace', keyspace]))
      self.shards.append(shards)
      self.num_shards.append(len(shards))

    # This assumes that all keyspaces use the same set of cells
    self.cells = json.loads(self.vtctl_helper.execute_vtctl_command(
        ['GetShard', '%s/%s' % (self.keyspaces[0], self.shards[0].keys()[0])]
        ))['cells']

    self.primary_cells = self.cells
    self.replica_instances = []
    self.rdonly_instances = []

    # This assumes that all cells are equivalent for k8s environments.
    all_tablets_in_a_cell = self.vtctl_helper.execute_vtctl_command(
        ['ListAllTablets', self.cells[0]])
    all_tablets_in_a_cell = [x.split(' ') for x in
                             filter(None, all_tablets_in_a_cell.split('\n'))]

    for index, keyspace in enumerate(self.keyspaces):
      keyspace_tablets_in_cell = [
          tablet for tablet in all_tablets_in_a_cell if tablet[1] == keyspace]
      replica_tablets_in_cell = [
          tablet for tablet in keyspace_tablets_in_cell
          if tablet[3] == 'master' or tablet[3] == 'replica']
      replica_instances = len(replica_tablets_in_cell) / self.num_shards[index]
      self.replica_instances.append(replica_instances)
      self.rdonly_instances.append(
          (len(keyspace_tablets_in_cell) / self.num_shards[index]) -
          replica_instances)

    # Converts keyspace name and alias to number of instances
    self.keyspace_alias_to_num_instances_dict = {}
    for index, keyspace in enumerate(self.keyspaces):
      self.keyspace_alias_to_num_instances_dict[keyspace] = {
          'replica': int(self.replica_instances[index]),
          'rdonly': int(self.rdonly_instances[index])
      }

    self.vtgate_addrs = {}
    for cell in self.cells:
      vtgate_ip = kubernetes_components.get_forwarded_ip(
          'vtgate-%s' % cell, instance_name)
      self.vtgate_addrs[cell] = '%s:15991' % vtgate_ip
    super(K8sEnvironment, self).use_named(instance_name)