Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def run_test(args, test_case): # pylint: disable=too-many-branches,too-many-statements
"""Run a test."""
util.load_kube_config()
api_client = k8s_client.ApiClient()
t = test_util.TestCase()
t.class_name = "tfjob_test"
namespace, name, env = ks_util.setup_ks_app(args)
t.name = os.path.basename(name)
try: # pylint: disable=too-many-nested-blocks
util.run(["ks", "apply", env, "-c", args.component], cwd=args.app_dir)
logging.info("Created job %s in namespaces %s", name, namespace)
logging.info("Wait for conditions Failed")
results = tf_job_client.wait_for_condition(
api_client, namespace, name, ["Succeeded", "Failed"],
status_callback=tf_job_client.log_status)
def run_simple_tfjob(self, component):
api_client = k8s_client.ApiClient()
# Setup the ksonnet app
ks_util.setup_ks_app(self.app_dir, self.env, self.namespace, component,
self.params)
# Create the TF job
ks_cmd = ks_util.get_ksonnet_cmd(self.app_dir)
util.run([ks_cmd, "apply", self.env, "-c", component], cwd=self.app_dir)
logging.info("Created job %s in namespaces %s", self.name, self.namespace)
# Wait for the job to either be in Running state or a terminal state
logging.info("Wait for conditions Running, Succeeded, or Failed")
results = tf_job_client.wait_for_condition(
api_client,
self.namespace,
self.name, ["Running", "Succeeded", "Failed"],
def create_k8s_client(_):
# We need to load the kube config so that we can have credentials to
# talk to the APIServer.
util.load_kube_config(persist_config=False)
# Create an API client object to talk to the K8s master.
api_client = k8s_client.ApiClient()
return api_client
def k8s_raw_client(self):
configure = client.Configuration()
configure.verify_ssl = False
configure.host = f"{self._bcs_server_host}{self.context['server_address_path']}".rstrip('/')
configure.api_key = {"authorization": f"Bearer {self.context['user_token']}"}
api_client = client.ApiClient(configure)
return api_client
def get_client():
client = kubernetes.client.ApiClient(config=config)
return client
def admissionregistartion_api_client_from_config(config):
if config is None:
k8sconfig.load_incluster_config()
return k8sclient.AdmissionregistrationV1beta1Api()
else:
client = k8sclient.ApiClient(configuration=config)
return k8sclient.AdmissionregistrationV1beta1Api(api_client=client)
def _wait_for_deployment_ready(
self, kube_config: kubernetes.client.Configuration, app_name: str):
"""Wait for the deployment of Django app to get ready.
Args:
kube_config: A kubernetes configuration which has access to the
given cluster.
app_name: Name of the Django app.
"""
api_client = kubernetes.client.ApiClient(kube_config)
api = kubernetes.client.ExtensionsV1beta1Api(api_client)
label_selector = '='.join(['app', app_name])
self._try_get_ready_replicas(api, label_selector)
if self.params.get('username') and self.params.get('password') and self.params.get('host'):
auth_method = 'self.params'
elif self.params.get('api_key') and self.params.get('host'):
auth_method = 'self.params'
elif self.params.get('kubeconfig') or self.params.get('context'):
auth_method = 'file'
else:
auth_method = 'default'
# First try to do incluster config, then kubeconfig
# TODO: Re-evaluate at some point (can be hard to force file)
if auth_method == 'default':
try:
kubernetes.config.load_incluster_config()
return DynamicClient(kubernetes.client.ApiClient())
except kubernetes.config.ConfigException:
return DynamicClient(self.client_from_kubeconfig(self.params.get('kubeconfig'), self.params.get('context')))
if auth_method == 'file':
return DynamicClient(self.client_from_kubeconfig(self.params.get('kubeconfig'), self.params.get('context')))
if auth_method == 'params':
return DynamicClient(kubernetes.client.ApiClient(configuration))
def get_simple_client(id_token):
api_client = client.ApiClient(get_k8s_configuration())
api_client.set_default_header("Authorization", "Bearer " + id_token)
return api_client