You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How can I make request like this by k8s python client, the below request is made by "kubectl edit", and it works! #641
It's intended behavior. patch_namespaced_service sends strategic-merge-patch (also please refer to this doc) by default, service.spec.ports uses "merge" patchStrategy and "port" as patchMergeKey. Therefore:
service.spec.ports[0].target_port = 3321 sends a patch that contains {"port":3315, "targetPort":3321}-- apiserver sees patchMergeKey is the same as existing ports[0], so ports[0].target_port gets updated.
service.spec.ports[0].port = 3309 sends a patch that contains {"port":3309, "targetPort":XXXX(previous value)}-- apiserver sees patchMergeKey is different from existing ports[0], so it tries appending ports[1] to the list, but fails validation.
You could try enable debugging log but passing debug=True to configuration, to see the HTTP response from apiserver. The request is likely to fail the validation in apiserver:
kubectl edit performs client-side apply (three-way diff) and calculates the needed patch body (the python client library currently doesn't have this functionality):
{"nodePort":30005,"port":3315,"protocol":"TCP","targetPort":3321} appends a ports[1] with same fields except port
{"$patch":"delete","port":3310}]} deletes the existing ports[0]
The result appears to be spec.ports[0].port getting updated.
It's intended behavior. patch_namespaced_service sends strategic-merge-patch (also please refer to this doc) by default, service.spec.ports uses "merge" patchStrategy and "port" as patchMergeKey. Therefore:
service.spec.ports[0].target_port = 3321 sends a patch that contains {"port":3315, "targetPort":3321}-- apiserver sees patchMergeKey is the same as existing ports[0], so ports[0].target_port gets updated.
service.spec.ports[0].port = 3309 sends a patch that contains {"port":3309, "targetPort":XXXX(previous value)}-- apiserver sees patchMergeKey is different from existing ports[0], so it tries appending ports[1] to the list, but fails validation.
You could try enable debugging log but passing debug=True to configuration, to see the HTTP response from apiserver. The request is likely to fail the validation in apiserver:
kubectl edit performs client-side apply (three-way diff) and calculates the needed patch body (the python client library currently doesn't have this functionality):
{"nodePort":30005,"port":3315,"protocol":"TCP","targetPort":3321} appends a ports[1] with same fields except port
{"$patch":"delete","port":3310}]} deletes the existing ports[0]
The result appears to be spec.ports[0].port getting updated.
OK, I see your point, thanks very much!
So I wonder how can I make a request by python client to delete the port[0] of service?
Hey thanks for the response @tomplus
I also found the RFC for this here https://tools.ietf.org/html/rfc6902
And through that found a way to delete specs via json-patch. patch = [{'op': 'remove', 'path': '/spec/ports/1'}] client.patch_namespaced_service(name=self._component_info.get('name'), namespace=self._component_info.get('namespace'), body=patch)
Activity
tomplus commentedon Oct 9, 2018
Have you tried patch_namespaced_service?
SailorCrane commentedon Oct 10, 2018
Yes, I have tried, but I do not know what should I set to patch_namespaced_service's body parameter.
I can set service's "target_port" a new value and it works.
but I set service's "port", it do not works!
roycaihw commentedon Oct 12, 2018
It's intended behavior.
patch_namespaced_servicesends strategic-merge-patch (also please refer to this doc) by default,service.spec.portsuses "merge" patchStrategy and "port" as patchMergeKey. Therefore:service.spec.ports[0].target_port = 3321sends a patch that contains {"port":3315, "targetPort":3321}-- apiserver sees patchMergeKey is the same as existing ports[0], so ports[0].target_port gets updated.service.spec.ports[0].port = 3309sends a patch that contains {"port":3309, "targetPort":XXXX(previous value)}-- apiserver sees patchMergeKey is different from existing ports[0], so it tries appending ports[1] to the list, but fails validation.You could try enable debugging log but passing
debug=Trueto configuration, to see the HTTP response from apiserver. The request is likely to fail the validation in apiserver:kubectl editperforms client-side apply (three-way diff) and calculates the needed patch body (the python client library currently doesn't have this functionality):{"nodePort":30005,"port":3315,"protocol":"TCP","targetPort":3321}appends a ports[1] with same fields except port{"$patch":"delete","port":3310}]}deletes the existing ports[0]The result appears to be spec.ports[0].port getting updated.
SailorCrane commentedon Oct 24, 2018
OK, I see your point, thanks very much!
So I wonder how can I make a request by python client to delete the port[0] of service?
sanster23 commentedon Feb 28, 2019
Any update on this as how to make a request by python client to delete the port[0] or any spec in service ?
tomplus commentedon Feb 28, 2019
You can use json-patch which is supported by k8s and this library:
More information:
sanster23 commentedon Mar 1, 2019
Hey thanks for the response @tomplus
I also found the RFC for this here https://tools.ietf.org/html/rfc6902
And through that found a way to delete specs via json-patch.
patch = [{'op': 'remove', 'path': '/spec/ports/1'}] client.patch_namespaced_service(name=self._component_info.get('name'), namespace=self._component_info.get('namespace'), body=patch)