# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Rolling Update Tests
#
# Check rolling updates function as expected.

import json
import time

# !kubectl create namespace seldon

# !kubectl config set-context $(kubectl config current-context) --namespace=seldon

# ## Change Image

# !kubectl apply -f resources/fixed_v1.yaml

# !kubectl rollout status deploy/$(kubectl get deploy -l seldon-deployment-id=fixed \
#                                  -o jsonpath='{.items[0].metadata.name}')

# !curl -d '{"data": {"ndarray":[[1.0, 2.0, 5.0]]}}' \
#    -X POST http://localhost:8003/seldon/seldon/fixed/api/v1.0/predictions \
#    -H "Content-Type: application/json"

# !kubectl apply -f resources/fixed_v2.yaml

time.sleep(5) # To allow operator to start the update
for i in range(60):
    # responseRaw=!curl -s -d '{"data": {"ndarray":[[1.0, 2.0, 5.0]]}}' -X POST http://localhost:8003/seldon/seldon/fixed/api/v1.0/predictions -H "Content-Type: application/json"
    response = json.loads(responseRaw[0])
    assert(response['data']['ndarray'][0]==1 or response['data']['ndarray'][0]==5)
    # jsonRaw=!kubectl get deploy -l seldon-deployment-id=fixed -o json
    data="".join(jsonRaw)
    resources = json.loads(data)
    numReplicas = int(resources["items"][0]["status"]["replicas"])
    if numReplicas == 3:
        break
    time.sleep(1)
print("Rollout Success")

# !kubectl delete -f resources/fixed_v1.yaml

# ## Separate Service Orchestrator

# !kubectl apply -f resources/fixed_v1_sep.yaml

# !kubectl rollout status deploy/$(kubectl get deploy -l seldon-deployment-id=fixed \
#                                  -o jsonpath='{.items[0].metadata.name}')

# !curl -d '{"data": {"ndarray":[[1.0, 2.0, 5.0]]}}' \
#    -X POST http://localhost:8003/seldon/seldon/fixed/api/v1.0/predictions \
#    -H "Content-Type: application/json"

# !kubectl apply -f resources/fixed_v2_sep.yaml

time.sleep(5) # To allow operator to start the update
for i in range(60):
    # responseRaw=!curl -s -d '{"data": {"ndarray":[[1.0, 2.0, 5.0]]}}' -X POST http://localhost:8003/seldon/seldon/fixed/api/v1.0/predictions -H "Content-Type: application/json"
    response = json.loads(responseRaw[0])
    assert(response['data']['ndarray'][0]==1 or response['data']['ndarray'][0]==5)
    # jsonRaw=!kubectl get deploy -l seldon-deployment-id=fixed -o json
    data="".join(jsonRaw)
    resources = json.loads(data)
    numReplicas = int(resources["items"][0]["status"]["replicas"])
    if numReplicas == 1:
        break
    time.sleep(1)
print("Rollout Success")

# !kubectl delete -f resources/fixed_v1_sep.yaml

# ## Two PodSpecs

# !kubectl apply -f resources/fixed_v1_2podspecs.yaml

# !kubectl rollout status deploy/$(kubectl get deploy -l seldon-deployment-id=fixed \
#                                  -o jsonpath='{.items[0].metadata.name}')

# !curl -d '{"data": {"ndarray":[[1.0, 2.0, 5.0]]}}' \
#    -X POST http://localhost:8003/seldon/seldon/fixed/api/v1.0/predictions \
#    -H "Content-Type: application/json"

# !kubectl apply -f resources/fixed_v2_2podspecs.yaml

time.sleep(5) # To allow operator to start the update
for i in range(60):
    # responseRaw=!curl -s -d '{"data": {"ndarray":[[1.0, 2.0, 5.0]]}}' -X POST http://localhost:8003/seldon/seldon/fixed/api/v1.0/predictions -H "Content-Type: application/json"
    response = json.loads(responseRaw[0])
    assert(response['data']['ndarray'][0]==1 or response['data']['ndarray'][0]==5)
    # jsonRaw=!kubectl get deploy -l seldon-deployment-id=fixed -o json
    data="".join(jsonRaw)
    resources = json.loads(data)
    numReplicas = int(resources["items"][0]["status"]["replicas"])
    if numReplicas == 1:
        break
    time.sleep(1)
print("Rollout Success")

# !kubectl delete -f resources/fixed_v1_2podspecs.yaml

# ## Two Models

# !kubectl apply -f resources/fixed_v1_2models.yaml

# !kubectl rollout status deploy/$(kubectl get deploy -l seldon-deployment-id=fixed \
#                                  -o jsonpath='{.items[0].metadata.name}')

# !curl -d '{"data": {"ndarray":[[1.0, 2.0, 5.0]]}}' \
#    -X POST http://localhost:8003/seldon/seldon/fixed/api/v1.0/predictions \
#    -H "Content-Type: application/json"

# !kubectl apply -f resources/fixed_v2_2models.yaml

time.sleep(5) # To allow operator to start the update
for i in range(60):
    # responseRaw=!curl -s -d '{"data": {"ndarray":[[1.0, 2.0, 5.0]]}}' -X POST http://localhost:8003/seldon/seldon/fixed/api/v1.0/predictions -H "Content-Type: application/json"
    response = json.loads(responseRaw[0])
    assert(response['data']['ndarray'][0]==1 or response['data']['ndarray'][0]==5)
    # jsonRaw=!kubectl get deploy -l seldon-deployment-id=fixed -o json
    data="".join(jsonRaw)
    resources = json.loads(data)
    numReplicas = int(resources["items"][0]["status"]["replicas"])
    if numReplicas == 3:
        break
    time.sleep(1)
print("Rollout Success")

# !kubectl delete -f resources/fixed_v2_2models.yaml

# ## Model name changes
#
# This will not do a rolling update but create a new deployment.

# !kubectl apply -f resources/fixed_v1.yaml

# !kubectl rollout status deploy/$(kubectl get deploy -l seldon-deployment-id=fixed \
#                                  -o jsonpath='{.items[0].metadata.name}')

# !curl -d '{"data": {"ndarray":[[1.0, 2.0, 5.0]]}}' \
#    -X POST http://localhost:8003/seldon/seldon/fixed/api/v1.0/predictions \
#    -H "Content-Type: application/json"

# !kubectl apply -f resources/fixed_v2_new_name.yaml

time.sleep(5) # To allow operator to start the update
for i in range(60):
    # responseRaw=!curl -s -d '{"data": {"ndarray":[[1.0, 2.0, 5.0]]}}' -X POST http://localhost:8003/seldon/seldon/fixed/api/v1.0/predictions -H "Content-Type: application/json"
    response = json.loads(responseRaw[0])
    assert(response['data']['ndarray'][0]==1 or response['data']['ndarray'][0]==5)
    # jsonRaw=!kubectl get deploy -l seldon-deployment-id=fixed -o json
    data="".join(jsonRaw)
    resources = json.loads(data)
    numItems = len(resources["items"])
    if numItems == 1:
        break
    time.sleep(1)
print("Rollout Success")

# !kubectl delete -f resources/fixed_v2_new_name.yaml



StarcoderdataJupyterScriptsDedupFiltered