【问题标题】:OpenShift REST API for scaling, invalid character 's' looking for beginning of value用于缩放的 OpenShift REST API,无效字符 's' 寻找值的开头
【发布时间】:2017-11-13 08:12:02
【问题描述】:

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

  • 应用程序/json-patch+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)

谢谢。

【问题讨论】:

  • 你能分享回溯吗?
  • @Mureinik 这是我从 python 请求库中获得的回溯:{"kind":"Status","apiVersion":"v1","metadata":{},"status":"失败","message":"无效字符 's' 寻找值的开头","code":500}
  • 您提供的数据对象不是 json-patch 格式。格式的详细信息可以在jsonpatch.com我假设这是你的问题。
  • 您可能应该做的是运行类似oc scale --loglevel 9 --replicas=10 dc bar 的命令并查看调试输出,了解该命令如何使用 REST API 对其进行扩展。
  • 谢谢@GrahamDumpleton,这很有帮助。我早先发现了问题所在,然后我从补丁切换到了补丁。

标签: python rest kubernetes openshift


【解决方案1】:

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

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

data = '{"kind":"Scale","apiVersion":"extensions/v1beta1","metadata":{"name":"deployment_name","namespace":"namespace_name"},"spec" :{"副本":1}}'

感谢您的宝贵时间。

【讨论】:

    【解决方案2】:

    我有相同的用例,@GrahamDumpleton 提示使用--loglevel 9 运行oc 非常有帮助。 这就是oc scale 所做的:

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

    如果您这样做,则不必担心设置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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-12
      • 1970-01-01
      • 1970-01-01
      • 2018-10-11
      • 1970-01-01
      • 2022-07-20
      • 2018-10-16
      • 1970-01-01
      相关资源
      最近更新 更多