from kubernetes import client

from kfserving import KFServingClient
from kfserving import constants
from kfserving import utils
from kfserving import V1alpha2EndpointSpec
from kfserving import V1alpha2PredictorSpec
from kfserving import V1alpha2TensorflowSpec
from kfserving import V1alpha2InferenceServiceSpec
from kfserving import V1alpha2InferenceService
from kubernetes.client import V1ResourceRequirements

"""
Explanation: Sample for KFServing SDK v1alpha2
This is a sample for KFServing SDK v1alpha2. 
The notebook shows how to use KFServing SDK to create, get, rollout_canary, promote and delete InferenceService.
End of explanation
"""


namespace = utils.get_default_target_namespace()

"""
Explanation: Define namespace where InferenceService needs to be deployed to. If not specified, below function defines namespace to the current one where SDK is running in the cluster, otherwise it will deploy to default namespace.
End of explanation
"""


api_version = constants.KFSERVING_GROUP + '/' + constants.KFSERVING_VERSION
default_endpoint_spec = V1alpha2EndpointSpec(
                          predictor=V1alpha2PredictorSpec(
                            tensorflow=V1alpha2TensorflowSpec(
                              storage_uri='gs://kfserving-samples/models/tensorflow/flowers',
                              resources=V1ResourceRequirements(
                                  requests={'cpu':'100m','memory':'1Gi'},
                                  limits={'cpu':'100m', 'memory':'1Gi'}))))
    
isvc = V1alpha2InferenceService(api_version=api_version,
                          kind=constants.KFSERVING_KIND,
                          metadata=client.V1ObjectMeta(
                              name='flower-sample', namespace=namespace),
                          spec=V1alpha2InferenceServiceSpec(default=default_endpoint_spec))

"""
Explanation: Define InferenceService
Firstly define default endpoint spec, and then define the inferenceservice basic on the endpoint spec.
End of explanation
"""


KFServing = KFServingClient()
KFServing.create(isvc)

"""
Explanation: Create InferenceService
Call KFServingClient to create InferenceService.
End of explanation
"""


KFServing.get('flower-sample', namespace=namespace, watch=True, timeout_seconds=120)

"""
Explanation: Check the InferenceService
End of explanation
"""


KFServing.delete('flower-sample', namespace=namespace)

"""
Explanation: Delete the InferenceService
End of explanation
"""


