diff --git a/modules/cloud_service_providers/k8s/src/Service/K8sClientExtension/K8sClient.php b/modules/cloud_service_providers/k8s/src/Service/K8sClientExtension/K8sClient.php index 7986849ee75175a009486eb17b1fe9ec1b0a39f1..0f134f0ca4cf2ee344021f37b0eb1a384920b2f8 100644 --- a/modules/cloud_service_providers/k8s/src/Service/K8sClientExtension/K8sClient.php +++ b/modules/cloud_service_providers/k8s/src/Service/K8sClientExtension/K8sClient.php @@ -26,25 +26,32 @@ class K8sClient extends Client { $method, $uri, $query = [], - $body = [], + $body = NULL, $namespace = TRUE, $apiVersion = NULL, array $requestOptions = [], ) { + + // Normalize $body to either NULL or a non-empty string if appropriate. + $body = empty($body) && !is_bool($body) && !is_numeric($body) ? NULL : $body; + if (!is_null($body) && !is_string($body)) { + throw new \InvalidArgumentException('A variable $body must be a string, array, or NULL'); + } + try { - if ($method === 'PUT' || $method === 'POST') { - // If the resource type is role or cluster role, the apiGroups maybe - // empty, and it cannot be removed. - if (!in_array($uri, ['/roles', '/clusterroles'], TRUE)) { - $array = $this->removeEmptyProperties(json_decode($body, TRUE)); - $body = json_encode($array, JSON_PRETTY_PRINT); - } + // If the resource type is K8s role or cluster role, the apiGroups maybe + // empty, then it cannot be removed. + if (!empty($body) + && in_array($method, ['PUT', 'POST']) + && !in_array($uri, ['/roles', '/clusterroles'], TRUE)) { + $array = $this->removeEmptyProperties(json_decode($body, TRUE)); + $body = json_encode($array, JSON_PRETTY_PRINT); } - if (empty($this->namespace)) { - $namespace = FALSE; - } + // Fallback for namespace availability. + $namespace = !empty($this->namespace); + // Handling different response statuses. $response = parent::sendRequest( $method, $uri,