Khi thực hiện change configuration của k8s thông qua API của rancher.
Có thể bạn sẽ gặp lối bên dưới:
{
"status": "error",
"message": "An error occurred: Failed to update ScaleObject: {\"type\":\"error\",\"links\":{},\"code\":\"BadRequest\",\"message\":\"Deployment in version \\\"v1\\\" cannot be handled as a Deployment: json: cannot unmarshal array into Go struct field PodSpec.spec.template.spec.securityContext of type v1.PodSecurityContext\",\"status\":400,\"type\":\"error\"}\n"
}
{
"status": "error",
"message": "An error occurred: Failed to update ScaleObject: {\"type\":\"error\",\"links\":{},\"code\":\"BadRequest\",\"message\":\"Deployment in version \\\"v1\\\" cannot be handled as a Deployment: json: cannot unmarshal array into Go struct field Container.spec.template.spec.initContainers.resources of type v1.ResourceRequirements\",\"status\":400,\"type\":\"error\"}\n"
}
Lý do là với các trường rỗng laravel sẽ get lên là dấu ngoặc vuông [] mà đúng ra chỗ đó phải là ngoặc nhọn {}

Vậy ở đây chúng ta cần migrate từ ngoặc vuông [] sang ngoặc nhọn {} bằng php laravel:
ở đây mình viết thêm 2 function:
// Function to normalize the securityContext field
private function normalizeSecurityContext(&$data)
{
// Ensure securityContext field is an object, not an array
if (isset($data['spec']['template']['spec']['securityContext']) && is_array($data['spec']['template']['spec']['securityContext'])) {
$data['spec']['template']['spec']['securityContext'] = (object) []; // Convert empty array to empty object
}
}
private function normalizeInitContainers(&$data)
{
// Ensure resources and other fields in initContainers are objects, not arrays
if (isset($data['spec']['template']['spec']['initContainers'])) {
foreach ($data['spec']['template']['spec']['initContainers'] as &$initContainer) {
// Normalize resources to an empty object if it's an array
if (isset($initContainer['resources']) && is_array($initContainer['resources'])) {
$initContainer['resources'] = (object) []; // Convert empty array to empty object
}
}
}
}
Và chúng ta sẽ sử dụng như sau:
Khi bạn có kết quả bạn sẽ thực hiện edit data từ [] sang -> {}
public function getDeployment($clusterId, $namespace, $deployment)
{
$deploymentURL = config('infra.k8s.url').'/k8s/clusters/'.$clusterId.'/v1/apps.deployments/'.$namespace .'/'.$deployment;
$response = $this->createHttpClient()->get($deploymentURL);
if ($response->successful()) {
$data = json_decode($response, true);
// Normalize the resources and initContainers fields
$this->normalizeInitContainers($data); // New normalization for initContainers
$this->normalizeSecurityContext($data);
return $data ?? null;
} else {
return [
'error' => $response->status(),
'message' => $response->body()
];
}
}
Ở trên là cách bạn sử dụng Function.