def _LoadClusterDetails(self):
        """Get the current cluster and its connection info from the kubeconfig.

    Yields:
      A tuple of (endpoint, ca_certs), where endpoint is the ip address
      of the GKE master, and ca_certs is the absolute path of a temporary file
      (lasting the life of the python process) holding the ca_certs to connect
      to the GKE cluster.
    Raises:
      flags.KubeconfigError: if the config file has missing keys or values.
    """
        try:
            if self.context:
                curr_ctx_name = self.context
            else:
                curr_ctx_name = self.config['current-context']
            curr_ctx = next((c for c in self.config['contexts']
                             if c['name'] == curr_ctx_name), None)
            if not curr_ctx:
                raise flags.KubeconfigError(
                    'Count not find context [{}] in kubeconfig.'.format(
                        curr_ctx_name))

            self.cluster = next(
                (c for c in self.config['clusters']
                 if c['name'] == curr_ctx['context']['cluster']), None)
            if not self.cluster:
                raise flags.KubeconfigError(
                    'Could not find cluster [{}] specified by context [{}] in '
                    'kubeconfig.'.format(curr_ctx['context']['cluster'],
                                         curr_ctx_name))

            ca_data = self.cluster['cluster']['certificate-authority-data']
            endpoint = self.cluster['cluster']['server'].replace(
                'https://', '')
        except KeyError as e:
            raise flags.KubeconfigError(
                'Missing key `{}` in kubeconfig.'.format(e.args[0]))

        fd, filename = tempfile.mkstemp()
        os.close(fd)
        files.WriteBinaryFileContents(filename,
                                      base64.b64decode(ca_data),
                                      private=True)
        try:
            yield endpoint, filename
        finally:
            os.remove(filename)
Esempio n. 2
0
  def _LoadClusterDetails(self):
    """Get the current cluster and its connection info from the kubeconfig.

    Yields:
      A tuple of (endpoint, ca_certs), where endpoint is the ip address
      of the GKE main, and ca_certs is the absolute path of a temporary file
      (lasting the life of the python process) holding the ca_certs to connect
      to the GKE cluster.
    Raises:
      flags.KubeconfigError: if the config file has missing keys or values.
    """
    try:
      curr_ctx = self.kubeconfig.contexts[self.kubeconfig.current_context]
      self.cluster = self.kubeconfig.clusters[curr_ctx['context']['cluster']]
      ca_data = self.cluster['cluster']['certificate-authority-data']
      parsed_server = urlparse.urlparse(self.cluster['cluster']['server'])
      endpoint = parsed_server.hostname
    except KeyError as e:
      raise flags.KubeconfigError('Missing key `{}` in kubeconfig.'.format(
          e.args[0]))

    fd, filename = tempfile.mkstemp()
    os.close(fd)
    files.WriteBinaryFileContents(
        filename, base64.b64decode(ca_data), private=True)
    try:
      yield endpoint, filename
    finally:
      os.remove(filename)
Esempio n. 3
0
    def _LoadClusterDetails(self):
        """Get the current cluster and its connection info from the kubeconfig.

    Yields:
      None.
    Raises:
      flags.KubeconfigError: if the config file has missing keys or values.
    """
        try:
            self.curr_ctx = self.kubeconfig.contexts[
                self.kubeconfig.current_context]
            self.cluster = self.kubeconfig.clusters[self.curr_ctx['context']
                                                    ['cluster']]
            self.ca_certs = self.cluster['cluster'].get(
                'certificate-authority', None)
            if not self.ca_certs:
                self.ca_data = self.cluster['cluster'].get(
                    'certificate-authority-data', None)

            parsed_server = urlparse.urlparse(
                self.cluster['cluster']['server'])
            self.raw_hostname = parsed_server.hostname
            self.user = self.kubeconfig.users[self.curr_ctx['context']['user']]
            self.client_key = self.user['user'].get('client-key', None)
            self.client_key_data = None
            self.client_cert_data = None
            if not self.client_key:
                self.client_key_data = self.user['user'].get(
                    'client-key-data', None)
            self.client_cert = self.user['user'].get('client-certificate',
                                                     None)
            if not self.client_cert:
                self.client_cert_data = self.user['user'].get(
                    'client-certificate-data', None)
        except KeyError as e:
            raise flags.KubeconfigError(
                'Missing key `{}` in kubeconfig.'.format(e.args[0]))
        with self._WriteDataIfNoFile(self.ca_certs, self.ca_data) as ca_certs, \
            self._WriteDataIfNoFile(self.client_key, self.client_key_data) as client_key, \
            self._WriteDataIfNoFile(self.client_cert, self.client_cert_data) as client_cert:
            self.ca_certs = ca_certs
            self.client_key = client_key
            self.client_cert = client_cert
            if self.client_cert:
                # The "domain" we're connecting to is different between PY2 and PY3
                # (TLS address checking is handled differently) so compensate for that
                # here.p
                if six.PY2:
                    self.client_cert_domain = 'kubernetes.default'
                else:
                    self.client_cert_domain = self.raw_hostname
            yield
    def _LoadClusterDetails(self):
        """Get the current cluster and its connection info from the kubeconfig.

    Yields:
      None.
    Raises:
      flags.KubeconfigError: if the config file has missing keys or values.
    """
        try:
            self.curr_ctx = self.kubeconfig.contexts[
                self.kubeconfig.current_context]
            self.cluster = self.kubeconfig.clusters[self.curr_ctx['context']
                                                    ['cluster']]
            self.ca_certs = self.cluster['cluster'].get(
                'certificate-authority', None)
            if not self.ca_certs:
                self.ca_data = self.cluster['cluster'].get(
                    'certificate-authority-data', None)

            parsed_server = urlparse.urlparse(
                self.cluster['cluster']['server'])
            self.raw_hostname = parsed_server.hostname
            self.user = self.kubeconfig.users[self.curr_ctx['context']['user']]
            self.client_key = self.user.get('client-key', None)
            if not self.client_key:
                self.client_key_data = self.user['user'].get(
                    'client-key-data', None)
            self.client_cert = self.user['user'].get('client-certificate',
                                                     None)
            if not self.client_cert:
                self.client_cert_data = self.user['user'].get(
                    'client-certificate-data', None)
        except KeyError as e:
            raise flags.KubeconfigError(
                'Missing key `{}` in kubeconfig.'.format(e.args[0]))
        with self._WriteDataIfNoFile(self.ca_certs, self.ca_data) as ca_certs, \
            self._WriteDataIfNoFile(self.client_key, self.client_key_data) as client_key, \
            self._WriteDataIfNoFile(self.client_cert, self.client_cert_data) as client_cert:
            self.ca_certs = ca_certs
            self.client_key = client_key
            self.client_cert = client_cert
            if self.client_cert:
                self.client_cert_domain = 'kubernetes.default'
            yield