Example #1
0
 def __init__(self,
              registry=None,
              image_name=constants.DEFAULT_IMAGE_NAME,
              context_source=None,
              preprocessor=None,
              push=True,
              base_image=constants.DEFAULT_BASE_IMAGE,
              pod_spec_mutators=None,
              namespace=None,
              dockerfile_path=None,
              cleanup=False,
              executable_path_prefix=None):
     super().__init__(registry=registry,
                      image_name=image_name,
                      push=push,
                      preprocessor=preprocessor,
                      base_image=base_image,
                      dockerfile_path=dockerfile_path)
     self.manager = KubeManager()
     if context_source is None:
         raise RuntimeError("context_source is not specified")
     self.context_source = context_source
     self.pod_spec_mutators = pod_spec_mutators or []
     self.namespace = namespace or utils.get_default_target_namespace()
     self.cleanup = cleanup
     self.executable_path_prefix = executable_path_prefix
Example #2
0
 def __init__(self, endpoint_url, minio_secret, minio_secret_key,
              region_name):
     self.endpoint_url = endpoint_url
     self.minio_secret = minio_secret
     self.minio_secret_key = minio_secret_key
     self.region_name = region_name
     self.Manager = KubeManager()
Example #3
0
    def __init__(self, namespace=None, runs=1, output=None,
                 cleanup=True, labels=None, job_name=constants.JOB_DEFAULT_NAME,
                 stream_log=True, deployer_type=constants.JOB_DEPLOPYER_TYPE,
                 pod_spec_mutators=None, annotations=None):
        """

        :param namespace: k8s namespace where the training's components will be deployed.
        :param runs: Number of training(s) to be deployed. Hyperparameter search
                will generate multiple jobs.
        :param output: output
        :param cleanup: clean up deletes components after job finished
        :param labels: labels to be assigned to the training job
        :param job_name: name of the job
        :param stream_log: stream the log?
        :param deployer_type: type of deployer
        :param pod_spec_mutators: pod spec mutators (Default value = None)
        """
        if namespace is None:
            self.namespace = utils.get_default_target_namespace()
        else:
            self.namespace = namespace

        # Used as pod and job name
        self.job_name = job_name
        self.deployer_type = deployer_type
        self.deployment_spec = None
        self.runs = runs
        self.output = output
        self.backend = KubeManager()
        self.cleanup = cleanup
        self.stream_log = stream_log
        self.set_labels(labels, deployer_type)
        self.set_anotations(annotations)
        self.pod_spec_mutators = pod_spec_mutators or []
Example #4
0
def get_ibm_cos_credentials(namespace):
    """
    Get the IBM COS credential from secret.

    :param namespace(str): The namespace that IBM COS credential secret created in.
    """
    secret_name = constants.IBM_COS_CREDS_SECRET_NAME
    if not KubeManager().secret_exists(secret_name, namespace):
        raise Exception("Secret '{}' not found in namespace '{}'".format(
            secret_name, namespace))

    secret = client.CoreV1Api().read_namespaced_secret(secret_name, namespace)
    creds_data = secret.data[constants.IBM_COS_CREDS_FILE_NAME]
    creds_json = base64.b64decode(creds_data).decode('utf-8')

    cos_creds = json.loads(creds_json)
    if cos_creds.get('cos_hmac_keys', ''):
        aws_access_key_id = cos_creds['cos_hmac_keys'].get('access_key_id', '')
        aws_secret_accesss_key = cos_creds['cos_hmac_keys'].get(
            'secret_access_key', '')
    else:
        raise RuntimeError(
            "Kaniko needs AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY\
                           if using S3 Bucket. Please use HMAC Credential.")

    return aws_access_key_id, aws_secret_accesss_key
Example #5
0
 def __init__(self,
              gcp_project=None,
              credentials_file=os.environ.get(constants.GOOGLE_CREDS_ENV),
              namespace='default'):
     self.gcp_project = gcp_project
     self.credentials_file = credentials_file
     self.manager = KubeManager()
     self.namespace = namespace
Example #6
0
 def __init__(self,
              aws_account=None,
              region=None,
              bucket_name=None):
     self.aws_account = aws_account
     self.manager = KubeManager()
     self.region = region or 'us-east-1'
     self.bucket_name = bucket_name
Example #7
0
    def __init__(self,
                 namespace=None,
                 runs=1,
                 output=None,
                 cleanup=True,
                 labels=None,
                 job_name=None,
                 stream_log=True,
                 deployer_type=constants.JOB_DEPLOPYER_TYPE,
                 pod_spec_mutators=None,
                 annotations=None,
                 config_file=None,
                 context=None,
                 client_configuration=None,
                 persist_config=True,
                 verify_ssl=True):
        """

        :param namespace: k8s namespace where the training's components will be deployed.
        :param runs: Number of training(s) to be deployed. Hyperparameter search
                will generate multiple jobs.
        :param output: output
        :param cleanup: clean up deletes components after job finished
        :param labels: labels to be assigned to the training job
        :param job_name: name of the job
        :param stream_log: stream the log?
        :param deployer_type: type of deployer
        :param pod_spec_mutators: pod spec mutators (Default value = None)
        :param config_file: kubeconfig file, defaults to ~/.kube/config. Note that for the case
               that the SDK is running in cluster and you want to operate in another remote
               cluster, user must set config_file to load kube-config file explicitly.
        :param context: kubernetes context
        :param client_configuration: The kubernetes.client.Configuration to set configs to.
        :param persist_config: If True, config file will be updated when changed
        :param verify_ssl: use ssl verify or not, set in the client config
        """
        if namespace is None:
            self.namespace = utils.get_default_target_namespace()
        else:
            self.namespace = namespace

        # Used as pod and job name
        self.job_name = job_name
        self.deployer_type = deployer_type
        self.deployment_spec = None
        self.runs = runs
        self.output = output
        self.backend = KubeManager(config_file=config_file,
                                   context=context,
                                   client_configuration=client_configuration,
                                   persist_config=persist_config,
                                   verify_ssl=verify_ssl)
        self.cleanup = cleanup
        self.stream_log = stream_log
        self.set_labels(labels, deployer_type)
        self.set_anotations(annotations)
        self.pod_spec_mutators = pod_spec_mutators or []
        self.verify_ssl = verify_ssl
Example #8
0
    def __init__(self,
                 framework,
                 default_storage_uri=None,
                 canary_storage_uri=None,
                 canary_traffic_percent=0,
                 namespace=None,
                 labels=None,
                 annotations=None,
                 custom_default_container=None,
                 custom_canary_container=None,
                 isvc_name=None,
                 stream_log=False,
                 cleanup=False):
        """
        :param framework: The framework for the InferenceService, such as Tensorflow,
            XGBoost and ScikitLearn etc.
        :param default_storage_uri: URI pointing to Saved Model assets for default service.
        :param canary_storage_uri: URI pointing to Saved Model assets for canary service.
        :param canary_traffic_percent: The amount of traffic to sent to the canary, defaults to 0.
        :param namespace: The k8s namespace where the InferenceService will be deployed.
        :param labels: Labels for the InferenceService, separate with commas if have more than one.
        :param annotations: Annotations for the InferenceService,
            separate with commas if have more than one.
        :param custom_default_container: A flexible custom default container for arbitrary customer
                                 provided containers.
        :param custom_canary_container: A flexible custom canary container for arbitrary customer
                                 provided containers.
        :param isvc_name: The InferenceService name.
        :param stream_log: Show log or not when InferenceService started, defaults to True.
        :param cleanup: Delete the kfserving or not, defaults to False.
        """
        self.framework = framework
        self.isvc_name = isvc_name
        self.default_storage_uri = default_storage_uri
        self.canary_storage_uri = canary_storage_uri
        self.canary_traffic_percent = canary_traffic_percent
        self.annotations = annotations
        self.set_labels(labels)
        self.cleanup = cleanup
        self.custom_default_container = custom_default_container
        self.custom_canary_container = custom_canary_container
        self.stream_log = stream_log
        self.backend = KubeManager()

        if namespace is None:
            self.namespace = utils.get_default_target_namespace()
        else:
            self.namespace = namespace

        if self.framework != 'custom' and self.default_storage_uri is None:
            raise RuntimeError("The default_storage_uri must be specified for "
                               "{} framework.".format(self.framework))
        if self.framework == 'custom' and self.custom_default_container is None:
            raise RuntimeError(
                "The custom_default_container must be specified "
                "for custom framework.")
Example #9
0
def get_azure_credentials(namespace):
    secret_name = constants.AZURE_CREDS_SECRET_NAME
    if not KubeManager().secret_exists(secret_name, namespace):
        raise Exception("Secret '{}' not found in namespace '{}'".format(
            secret_name, namespace))

    v1 = client.CoreV1Api()
    secret = v1.read_namespaced_secret(secret_name, namespace)
    sp_credentials = ServicePrincipalCredentials(
        client_id=get_plain_secret_value(secret.data, 'AZ_CLIENT_ID'),
        secret=get_plain_secret_value(secret.data, 'AZ_CLIENT_SECRET'),
        tenant=get_plain_secret_value(secret.data, 'AZ_TENANT_ID'))
    subscription_id = get_plain_secret_value(secret.data, 'AZ_SUBSCRIPTION_ID')
    return sp_credentials, subscription_id
Example #10
0
    def get_builder(self, preprocessor, base_image, registry, needs_deps_installation=True,
                    pod_spec_mutators=None):
        """Creates a builder instance with right config for GKE

        :param preprocessor: Preprocessor to use to modify inputs
        :param base_image: Base image to use for this job
        :param registry: Registry to push image to. Example: gcr.io/kubeflow-images
        :param needs_deps_installation:  need depends on installation(Default value = True)
        :param pod_spec_mutators: list of functions that is used to mutate the podsspec.
                                  e.g. fairing.cloud.gcp.add_gcp_credentials_if_exists
                                  This can used to set things like volumes and security context.
                                  (Default value =None)

        """

        pod_spec_mutators = pod_spec_mutators or []
        pod_spec_mutators.append(gcp.add_gcp_credentials_if_exists)

        if not needs_deps_installation:
            return AppendBuilder(preprocessor=preprocessor,
                                 base_image=base_image,
                                 registry=registry)
        elif (utils.is_running_in_k8s() or
              not ml_tasks_utils.is_docker_daemon_exists()):
            return ClusterBuilder(preprocessor=preprocessor,
                                  base_image=base_image,
                                  registry=registry,
                                  pod_spec_mutators=pod_spec_mutators,
                                  namespace=self._namespace,
                                  context_source=self._build_context_source)
        elif ml_tasks_utils.is_docker_daemon_exists():
            return DockerBuilder(preprocessor=preprocessor,
                                 base_image=base_image,
                                 registry=registry)
        else:
            msg = ["Not able to guess the right builder for this job!"]
            if KubeManager().secret_exists(constants.GCP_CREDS_SECRET_NAME, self._namespace):
                msg.append("It seems you don't have permission to list/access secrets in your "
                           "Kubeflow cluster. We need this permission in order to build a docker "
                           "image using Kubeflow cluster. Adding Kubeneters Admin role to the "
                           "service account you are using might solve this issue.")
            if not utils.is_running_in_k8s():
                msg.append(" Also If you are using 'sudo' to access docker in your system you can"
                           " solve this problem by adding your username to the docker group. "
                           "Reference: https://docs.docker.com/install/linux/linux-postinstall/"
                           "#manage-docker-as-a-non-root-user You need to logout and login to "
                           "get change activated.")
            message = " ".join(msg)
            raise RuntimeError(message)
Example #11
0
    def __init__(self, framework, default_model_uri=None, canary_model_uri=None,
                 canary_traffic_percent=0, namespace=None, labels=None, annotations=None,
                 custom_default_spec=None, custom_canary_spec=None, stream_log=True,
                 cleanup=False):
        """

        :param framework: The framework for the kfservice, such as Tensorflow,
            XGBoost and ScikitLearn etc.
        :param default_model_uri: URI pointing to Saved Model assets for default service.
        :param canary_model_uri: URI pointing to Saved Model assets for canary service.
        :param canary_traffic_percent: The amount of traffic to sent to the canary, defaults to 0.
        :param namespace: The k8s namespace where the kfservice will be deployed.
        :param labels: Labels for the kfservice, separate with commas if have more than one.
        :param annotations: Annotations for the kfservice,
            separate with commas if have more than one.
        :param custom_default_spec: A flexible custom default specification for arbitrary customer
                                 provided containers.
        :param custom_canary_spec: A flexible custom canary specification for arbitrary customer
                                 provided containers.
        :param stream_log: Show log or not when kfservice started, defaults to True.
        :param cleanup: Delete the kfserving or not, defaults to False.
        """
        self.framework = framework
        self.default_model_uri = default_model_uri
        self.canary_model_uri = canary_model_uri
        self.canary_traffic_percent = canary_traffic_percent
        self.annotations = annotations
        self.set_labels(labels)
        self.cleanup = cleanup
        self.custom_default_spec = custom_default_spec
        self.custom_canary_spec = custom_canary_spec
        self.stream_log = stream_log
        self.backend = KubeManager()

        if namespace is None:
            self.namespace = utils.get_default_target_namespace()
        else:
            self.namespace = namespace
Example #12
0
    def __init__(self,
                 framework,
                 default_storage_uri=None,
                 canary_storage_uri=None,
                 canary_traffic_percent=0,
                 namespace=None,
                 labels=None,
                 annotations=None,
                 custom_default_container=None,
                 custom_canary_container=None,
                 isvc_name=None,
                 stream_log=False,
                 cleanup=False,
                 config_file=None,
                 context=None,
                 client_configuration=None,
                 persist_config=True):
        """
        :param framework: The framework for the InferenceService, such as Tensorflow,
            XGBoost and ScikitLearn etc.
        :param default_storage_uri: URI pointing to Saved Model assets for default service.
        :param canary_storage_uri: URI pointing to Saved Model assets for canary service.
        :param canary_traffic_percent: The amount of traffic to sent to the canary, defaults to 0.
        :param namespace: The k8s namespace where the InferenceService will be deployed.
        :param labels: Labels for the InferenceService, separate with commas if have more than one.
        :param annotations: Annotations for the InferenceService,
            separate with commas if have more than one.
        :param custom_default_container: A flexible custom default container for arbitrary customer
                                 provided containers.
        :param custom_canary_container: A flexible custom canary container for arbitrary customer
                                 provided containers.
        :param isvc_name: The InferenceService name.
        :param stream_log: Show log or not when InferenceService started, defaults to True.
        :param cleanup: Delete the kfserving or not, defaults to False.
        :param config_file: kubeconfig file, defaults to ~/.kube/config. Note that for the case
               that the SDK is running in cluster and you want to operate in another remote
               cluster, user must set config_file to load kube-config file explicitly.
        :param context: kubernetes context
        :param client_configuration: The kubernetes.client.Configuration to set configs to.
        :param persist_config: If True, config file will be updated when changed
        """
        self.framework = framework
        self.isvc_name = isvc_name
        self.default_storage_uri = default_storage_uri
        self.canary_storage_uri = canary_storage_uri
        self.canary_traffic_percent = canary_traffic_percent
        self.annotations = annotations
        self.set_labels(labels)
        self.cleanup = cleanup
        self.custom_default_container = custom_default_container
        self.custom_canary_container = custom_canary_container
        self.stream_log = stream_log
        self.backend = KubeManager(config_file=config_file,
                                   context=context,
                                   client_configuration=client_configuration,
                                   persist_config=persist_config)

        if namespace is None:
            self.namespace = utils.get_default_target_namespace()
        else:
            self.namespace = namespace

        if self.framework != 'custom' and self.default_storage_uri is None:
            raise RuntimeError("The default_storage_uri must be specified for "
                               "{} framework.".format(self.framework))
        if self.framework == 'custom' and self.custom_default_container is None:
            raise RuntimeError(
                "The custom_default_container must be specified "
                "for custom framework.")
Example #13
0
      template:
        spec:
          containers:
            - name: tensorflow
              image: kubeflow/tf-dist-mnist-test:1.0
    Worker:
      replicas: 1
      restartPolicy: Never
      template:
        spec:
          containers:
            - name: tensorflow
              image: kubeflow/tf-dist-mnist-test:1.0
'''

kubeflow_client = KubeManager()


def test_apply_namespaced_object_core_v1_api():
    '''
    Test apply_namespaced_object API for CoreV1Api
    '''
    kubeflow_client.apply_namespaced_object(core_api_test)
    kubeflow_client.apply_namespaced_object(core_api_test, mode='patch')
    kubeflow_client.apply_namespaced_object(core_api_test, mode='delete')


def test_apply_namespaced_object_apps_v1_api():
    '''
    Test apply_namespaced_object API for AppV1Api
    '''