예제 #1
0
 def testOK(self):
     project_id = 'test-project'
     cluster_location = 'us-central1-a'
     cluster_name = 'test-cluster'
     gke_resource = '//container.googleapis.com/projects/test-project/locations/us-central1-a/clusters/test-cluster'
     gke_uri = 'https://container.googleapis.com/v1/projects/test-project/locations/us-central1-a/clusters/test-cluster'
     expect_resource, expect_uri = api_util.GetGKEURIAndResourceName(
         project_id, cluster_location, cluster_name)
     self.assertEqual(gke_resource, expect_resource)
     self.assertEqual(gke_uri, expect_uri)
예제 #2
0
    def GetKubeconfigAndContext(self, flags, temp_kubeconfig_dir):
        """Gets the kubeconfig, cluster context and resource link from arguments and defaults.

    Args:
      flags: the flags passed to the enclosing command. It must include
        kubeconfig and context.
      temp_kubeconfig_dir: a TemporaryDirectoryObject.

    Returns:
      the kubeconfig filepath and context name

    Raises:
      calliope_exceptions.MinimumArgumentException: if a kubeconfig file cannot
        be deduced from the command line flags or environment
      exceptions.Error: if the context does not exist in the deduced kubeconfig
        file
    """
        # Parsing flags to get the name and location of the GKE cluster to register
        if flags.gke_uri or flags.gke_cluster:
            cluster_project = None
            if flags.gke_uri:
                cluster_project, location, name = _ParseGKEURI(flags.gke_uri)
            else:
                cluster_project = properties.VALUES.core.project.GetOrFail()
                location, name = _ParseGKECluster(flags.gke_cluster)

            self.gke_cluster_self_link, self.gke_cluster_uri = api_util.GetGKEURIAndResourceName(
                cluster_project, location, name)
            return _GetGKEKubeconfig(cluster_project, location, name,
                                     temp_kubeconfig_dir), None

        # We need to support in-cluster configuration so that gcloud can run from
        # a container on the Cluster we are registering. KUBERNETES_SERICE_PORT
        # and KUBERNETES_SERVICE_HOST environment variables are set in a kubernetes
        # cluster automatically, which can be used by kubectl to talk to
        # the API server.
        if not flags.kubeconfig and encoding.GetEncodedValue(
                os.environ,
                'KUBERNETES_SERVICE_PORT') and encoding.GetEncodedValue(
                    os.environ, 'KUBERNETES_SERVICE_HOST'):
            return None, None

        kubeconfig_file = (flags.kubeconfig or encoding.GetEncodedValue(
            os.environ, 'KUBECONFIG') or '~/.kube/config')

        kubeconfig = files.ExpandHomeDir(kubeconfig_file)
        if not kubeconfig:
            raise calliope_exceptions.MinimumArgumentException(
                ['--kubeconfig'],
                'Please specify --kubeconfig, set the $KUBECONFIG environment '
                'variable, or ensure that $HOME/.kube/config exists')
        kc = kconfig.Kubeconfig.LoadFromFile(kubeconfig)

        context_name = flags.context

        if context_name not in kc.contexts:
            raise exceptions.Error(
                'context [{}] does not exist in kubeconfig [{}]'.format(
                    context_name, kubeconfig))

        return kubeconfig, context_name