用于扩展的OpenShift REST API,无效字符'寻找值的开头

时间:2017-11-13 08:12:02

标签: python rest kubernetes openshift

我正在尝试使用openshift rest api扩展我的部署,但我遇到错误“无效字符'寻找值的开头”。 我可以成功获取部署配置详细信息,但这是令我不安的补丁请求。 从文档我尝试了Content-Type如下3,但没有任何作用:

  • 应用/ JSON-补丁+ JSON
  • 应用/合并贴片+ JSON
  • 应用/战略合并贴片+ JSON

这是我的代码:

data = {'spec':{'replicas':2}}
headers = {"Authorization": token, "Content-Type": "application/json-patch+json"}
def updateReplicas():
   url = root + "namespaces" + namespace + "deploymentconfigs" + dc + "scale"  
   resp = requests.patch(url, headers=headers, data=data, verify=False)
   print(resp.content)

谢谢。

2 个答案:

答案 0 :(得分:1)

好的,我发现了这个问题。愚蠢的事情首先,数据应该在单引号数据=' {' spec':{' replicas':2}}'内。

然后,我们在数据中需要更多信息,最终看起来像:

data =' {" kind":" Scale"," apiVersion":" extensions / v1beta1",&# 34;元数据" {"名称":" deployment_name""命名空间":" namespace_name"}"规格& #34; {"复制品":1}}'

感谢您的时间。

答案 1 :(得分:0)

我有相同的用例,@ GrahamDumpleton的提示与oc一起运行--loglevel 9很有帮助。 oc scale就是这样的:

  1. 它向资源发出get请求,并接收一些JSON对象
  2. 然后使用修改后的JSON向资源发出放置请求 对象(副本数已更改)作为有效载荷

如果您正在执行此操作,则不必担心设置apiVersion,只需要重用最初获得的内容即可。

这是一个遵循此方法的小python脚本:

"""
Login into your project first `oc login` and `oc project <your-project>` before running this script.

Usage:
    pip install requests
    python scale_pods.py --deployment-name <your-deployment> --nof-replicas <number>

"""
import argparse
import requests
from subprocess import check_output
import warnings
warnings.filterwarnings("ignore")  # ignore insecure request warnings


def byte_to_str(bs):
    return bs.decode("utf-8").strip()


def get_endpoint():
    byte_str = check_output("echo $(oc config current-context | cut -d/ -f2 | tr - .)", shell=True)
    return byte_to_str(byte_str)


def get_namespace():
    byte_str = check_output("echo $(oc config current-context | cut -d/ -f1)", shell=True)
    return byte_to_str(byte_str)


def get_token():
    byte_str = check_output("echo $(oc whoami -t)", shell=True)
    return byte_to_str(byte_str)


def scale_pods(deployment_name, nof_replicas):
    url = "https://{endpoint}/apis/apps.openshift.io/v1/namespaces/{namespace}/deploymentconfigs/{deplyoment_name}/scale".format(
        endpoint=get_endpoint(),
        namespace=get_namespace(),
        deplyoment_name=deployment_name
    )
    headers = {
        "Authorization": "Bearer %s" % get_token()
    }
    get_response = requests.get(url, headers=headers, verify=False)

    data = get_response.json()
    data["spec"]["replicas"] = nof_replicas
    print(data)

    response_put = requests.put(url, headers=headers, json=data, verify=False)
    print(response_put.status_code)


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--deployment-name", type=str, required=True, help="deployment name")
    parser.add_argument("--nof-replicas", type=int, required=True, help="nof replicas")
    args = parser.parse_args()
    scale_pods(args.deployment_name, args.nof_replicas)


if __name__ == "__main__":
    main()