|
楼主 |
发表于 2022-10-17 11:23:33
|
显示全部楼层
本帖最后由 python鱼new 于 2022-10-17 11:34 编辑
我在看一个项目,看懂了,它在最后调用了if get_google_credentials:
- class KubeConfigLoader(object):
- def __init__(self, config_dict, active_context=None,
- get_google_credentials=None,
- config_base_path="",
- config_persister=None,
- temp_file_path=None):
- if config_dict is None:
- raise ConfigException(
- 'Invalid kube-config. '
- 'Expected config_dict to not be None.')
- elif isinstance(config_dict, ConfigNode):
- self._config = config_dict
- else:
- self._config = ConfigNode('kube-config', config_dict)
- self._current_context = None
- self._user = None
- self._cluster = None
- self.set_active_context(active_context)
- self._config_base_path = config_base_path
- self._config_persister = config_persister
- self._temp_file_path = temp_file_path
- def _refresh_credentials_with_cmd_path():
- config = self._user['auth-provider']['config']
- cmd = config['cmd-path']
- if len(cmd) == 0:
- raise ConfigException(
- 'missing access token cmd '
- '(cmd-path is an empty string in your kubeconfig file)')
- if 'scopes' in config and config['scopes'] != "":
- raise ConfigException(
- 'scopes can only be used '
- 'when kubectl is using a gcp service account key')
- args = []
- if 'cmd-args' in config:
- args = config['cmd-args'].split()
- else:
- fields = config['cmd-path'].split()
- cmd = fields[0]
- args = fields[1:]
- commandTokenSource = CommandTokenSource(
- cmd, args,
- config.safe_get('token-key'),
- config.safe_get('expiry-key'))
- return commandTokenSource.token()
- def _refresh_credentials():
- # Refresh credentials using cmd-path
- if ('auth-provider' in self._user and
- 'config' in self._user['auth-provider'] and
- 'cmd-path' in self._user['auth-provider']['config']):
- return _refresh_credentials_with_cmd_path()
- credentials, project_id = google.auth.default(scopes=[
- 'https://www.googleapis.com/auth/cloud-platform',
- 'https://www.googleapis.com/auth/userinfo.email'
- ])
- request = google.auth.transport.requests.Request()
- credentials.refresh(request)
- return credentials
- if get_google_credentials:
- self._get_google_credentials = get_google_credentials
- else:
- self._get_google_credentials = _refresh_credentials
复制代码 |
|