def GetConnectionContext(args, product=flags.Product.RUN, release_track=base.ReleaseTrack.GA, version_override=None, platform=None): """Gets the regional, kubeconfig, or GKE connection context. Args: args: Namespace, the args namespace. product: Which product is requesting connection context. release_track: Release track of the command being run. version_override: If specified, the given api version will be used no matter the other parameters. platform: 'gke', 'kubernetes', or 'managed'. If not specified, the value of the --platform flag will be used instead. Raises: ArgumentError if region or cluster is not specified. Returns: A GKE or regional ConnectionInfo object. """ if platform is None: platform = platforms.GetPlatform() if platform == platforms.PLATFORM_KUBERNETES: kubeconfig = flags.GetKubeconfig(getattr(args, 'kubeconfig', None)) api_name = _GetApiName(product, release_track, is_cluster=True) api_version = _GetApiVersion( product, release_track, is_cluster=True, version_override=version_override) return KubeconfigConnectionContext(kubeconfig, api_name, api_version, args.context) if platform == platforms.PLATFORM_GKE: cluster_ref = args.CONCEPTS.cluster.Parse() if not cluster_ref: raise serverless_exceptions.ArgumentError( 'You must specify a cluster in a given location. ' 'Either use the `--cluster` and `--cluster-location` flags ' 'or set the run/cluster and run/cluster_location properties.') api_name = _GetApiName(product, release_track, is_cluster=True) api_version = _GetApiVersion( product, release_track, is_cluster=True, version_override=version_override) return GKEConnectionContext(cluster_ref, api_name, api_version) if platform == platforms.PLATFORM_MANAGED: region = flags.GetRegion(args, prompt=True) if not region: raise serverless_exceptions.ArgumentError( 'You must specify a region. Either use the `--region` flag ' 'or set the run/region property.') api_name = _GetApiName(product, release_track) api_version = _GetApiVersion( product, release_track, version_override=version_override) return _RegionalConnectionContext(region, api_name, api_version)
def EventsConnectionContext(args): """Returns the appropriate cluster connection context based on args. Unless the user has configured cluster connectivity options, calling this will result in the user being prompted to select a GKE cluster. Args: args: A parsed argument context Returns: googlecloudsdk.command_lib.run.connection_context.ConnectionInfo Raises: flags.ConfigurationError when the user has not specified a cluster connection method and can't be prompted. """ api_name = _CLUSTER_EVENTS_API_NAME api_version = _CLUSTER_EVENTS_API_VERSION connection = flags.ClusterConnectionMethod(args) if connection == flags.CONNECTION_KUBECONFIG: kubeconfig_path, context = flags.KubeconfigPathAndContext(args) kubeconfig = run_flags.GetKubeconfig(kubeconfig_path) return run_context.KubeconfigConnectionContext( kubeconfig, api_name, api_version, context) elif connection == flags.CONNECTION_GKE: cluster_ref = flags.ParseClusterRefOrPromptUser(args) return run_context.GKEConnectionContext(cluster_ref, api_name, api_version) else: raise exceptions.Error('Unable to determine cluster connection method')
def testGetFromEnvVar(self): self._SetUpEnvTest() path = '/some/path1' self.StartDictPatch(os.environ, {'KUBECONFIG': path}) self.assertEqual(self.expected_configs[path], flags.GetKubeconfig(self.args)) kubeconfig.Kubeconfig.LoadFromFile.assert_called_once_with(path)
def GetConnectionContext(args): """Gets the regional, kubeconfig, or GKE connection context. Args: args: Namespace, the args namespace. Raises: ArgumentError if region or cluster is not specified. Returns: A GKE or regional ConnectionInfo object. """ if flags.IsKubernetes(args): kubeconfig = flags.GetKubeconfig(args) return _KubeconfigConnectionContext(kubeconfig, args.context) if flags.IsGKE(args): cluster_ref = args.CONCEPTS.cluster.Parse() if not cluster_ref: raise flags.ArgumentError( 'You must specify a cluster in a given location. ' 'Either use the `--cluster` and `--cluster-location` flags ' 'or set the run/cluster and run/cluster_location properties.') return _GKEConnectionContext(cluster_ref) if flags.IsManaged(args): region = flags.GetRegion(args, prompt=True) if not region: raise flags.ArgumentError( 'You must specify a region. Either use the `--region` flag ' 'or set the run/region property.') return _RegionalConnectionContext(region)
def testGetFromDefault(self): expected_config = kubeconfig.Kubeconfig.Default() expected_config.SetCurrentContext('context') self.StartObjectPatch( kubeconfig.Kubeconfig, 'LoadFromFile', return_value=expected_config) self.assertEqual(expected_config, flags.GetKubeconfig(self.args)) path_args, _ = kubeconfig.Kubeconfig.LoadFromFile.call_args self.assertTrue(path_args[0].endswith('/.kube/config'))
def testGetFromArgs(self): expected_config = kubeconfig.Kubeconfig.Default() expected_config.SetCurrentContext('context') self.StartObjectPatch( kubeconfig.Kubeconfig, 'LoadFromFile', return_value=expected_config) path = '/some/path' self.args.kubeconfig = path self.assertEqual(expected_config, flags.GetKubeconfig(self.args)) kubeconfig.Kubeconfig.LoadFromFile.assert_called_once_with(path)
def testGetFromEnvVarPartiallyInvalidPath(self): self._SetUpEnvTest() path = '/some/path1' self.StartDictPatch(os.environ, { 'KUBECONFIG': '/invalid/path{sep}{path}'.format(sep=os.pathsep, path=path) }) self.assertEqual(self.expected_configs[path], flags.GetKubeconfig(self.args)) kubeconfig.Kubeconfig.LoadFromFile.assert_any_call('/invalid/path') kubeconfig.Kubeconfig.LoadFromFile.assert_any_call(path) self.assertEqual(2, kubeconfig.Kubeconfig.LoadFromFile.call_count)
def testGetFromEnvVarMerge(self): self._SetUpEnvTest() path_1 = '/some/path1' path_2 = '/some/path2' self.StartDictPatch( os.environ, { 'KUBECONFIG': '{path_1}{sep}{path_2}'.format( path_1=path_1, sep=os.pathsep, path_2=path_2) }) expected_config = self.expected_configs[path_1] expected_config.Merge(self.expected_configs[path_2]) self.assertEqual(expected_config, flags.GetKubeconfig(self.args)) kubeconfig.Kubeconfig.LoadFromFile.assert_any_call(path_1) kubeconfig.Kubeconfig.LoadFromFile.assert_any_call(path_2) self.assertEqual(2, kubeconfig.Kubeconfig.LoadFromFile.call_count)
def GetConnectionContext(args, product=Product.RUN): """Gets the regional, kubeconfig, or GKE connection context. Args: args: Namespace, the args namespace. product: Which product is requesting connection context. Raises: ArgumentError if region or cluster is not specified. Returns: A GKE or regional ConnectionInfo object. """ if flags.GetPlatform() == flags.PLATFORM_KUBERNETES: kubeconfig = flags.GetKubeconfig(args) return _KubeconfigConnectionContext(kubeconfig, _CLUSTER_API_VERSION, args.context) if flags.GetPlatform() == flags.PLATFORM_GKE: cluster_ref = args.CONCEPTS.cluster.Parse() if not cluster_ref: raise flags.ArgumentError( 'You must specify a cluster in a given location. ' 'Either use the `--cluster` and `--cluster-location` flags ' 'or set the run/cluster and run/cluster_location properties.') return _GKEConnectionContext(cluster_ref, _CLUSTER_API_VERSION) if flags.GetPlatform() == flags.PLATFORM_MANAGED: region = flags.GetRegion(args, prompt=True) if not region: raise flags.ArgumentError( 'You must specify a region. Either use the `--region` flag ' 'or set the run/region property.') if product == Product.RUN: version = global_methods.SERVERLESS_API_VERSION elif product == Product.EVENTS: version = _EVENTS_API_VERSION else: raise ValueError('Unrecognized product: ' + six.u(product)) return _RegionalConnectionContext(region, version)
def GetConnectionContext(args, track=base.ReleaseTrack.BETA): """Gets the regional, kubeconfig, or GKE connection context. Args: args: Namespace, the args namespace. track: ReleaseTrack, the release track. Raises: ArgumentError if region or cluster is not specified. Returns: A GKE or regional ConnectionInfo object. """ if flags.IsKubernetes(args): kubeconfig = flags.GetKubeconfig(args) return _KubeconfigConnectionContext(kubeconfig, args.context) if flags.IsGKE(args): cluster_ref = args.CONCEPTS.cluster.Parse() if not cluster_ref: raise flags.ArgumentError( 'You must specify a cluster in a given location. ' 'Either use the `--cluster` and `--cluster-location` flags ' 'or set the run/cluster and run/cluster_location properties.') return _GKEConnectionContext(cluster_ref) if flags.IsManaged(args): region = flags.GetRegion(args, prompt=True) if not region: raise flags.ArgumentError( 'You must specify a region. Either use the `--region` flag ' 'or set the run/region property.') if track == base.ReleaseTrack.ALPHA: version = 'v1' else: version = global_methods.SERVERLESS_API_VERSION return _RegionalConnectionContext(region, version)
def testGetFromEnvVarInvalidPath(self): self._SetUpEnvTest() self.StartDictPatch(os.environ, {'KUBECONFIG': '/invalid/path'}) with self.assertRaises(flags.KubeconfigError): flags.GetKubeconfig(self.args)