PATCH operation results in response code 415
Closed this issue · 6 comments
Patching Kubernetes objects doesn't work because the content-type is wrong. The default content type is 'application/json' but should be 'application/json-patch+json' or 'application/merge-patch+json' or 'application/strategic-merge-patch+json'
See https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#patch-operations for more information
With the 'patch' method, you are supposed to send a Patch object instead of the standard model object.
For example:
Kubernetes\API\Deployment
/**
* partially update the specified Deployment
*
* @param $namespace
* @param $name
* @param Patch $Model
* @return TheDeployment|mixed
*/
public function patch($namespace = 'default', $name, \Kubernetes\Model\Io\K8s\Apimachinery\Pkg\Apis\Meta\V1\Patch $Model)
{
return $this->parseResponse(
$this->client->request('patch',
"/apis/apps/v1/namespaces/{$namespace}/deployments/{$name}"
,[
'json' => $Model->getArrayCopy(),
]
)
, 'patchAppsV1NamespacedDeployment'
);
}
Kubernetes\Model\Io\K8s\Apimachinery\Pkg\Apis\Meta\V1
<?php
namespace Kubernetes\Model\Io\K8s\Apimachinery\Pkg\Apis\Meta\V1;
/**
* Patch is provided to give a concrete name and type to the Kubernetes PATCH
* request body.
*/
class Patch extends \KubernetesRuntime\AbstractModel
{
}
I already checked the code but I can't find how to change the content-type using the Patch object. I do use the patch object but can you provide me a working example of how to patch the replicas of a replica set or deployment?
I've tried to do $replicaSetAPI->patchExtensionsV1beta1($namespace, $replicaSet->metadata->name, new Patch(['op' => 'replace', 'path' => '/spec/replicas', 'value' => 0]));
but this doesn't work.
Ha I see what you mean! Yes you are correct indeed, all requests are sent in application/json
instead of application/json-patch+json
.
I overlooked that bit in the documentation.
I will work something out to fulfil the requirement.
Meanwhile, you might want to try what I am doing:
# Programatically create your deployment.
$Deployment = new Deployment(['with'=>'what ever settings you want to put into']);
# Set a timestamp with current time into the annotation, this would make Kubernetes knows it is a new version
$Deployment->spec->template->metadata->annotations['deployment/update-timestamp'] = date('c');
# Replace it with your existing deployment
$DeploymentAPI->replace('YOUR_NAME_SPACE', $Deployment->metadata->name, $Deployment);
This way you replace your entire deployment with the one generated from your programme. Keep your declarations consistent with the specification you generated.
I've added the support for proper usage of Patch
operation. Please refer to the updated README.md
Because the code has been re-generated, you need to do composer clear-cache && composer update
The following versions should have correct code now:
v1.9.11
v1.10.13
v1.11.8
v1.12.6
v1.13.4
@piwi91 Any feedbacks on the improvement I did last week? Shall I close this issue?
Looks good to me :) Thx for the quick fix!