Home > Blockchain >  Kubeclt exec issues with variable via php script
Kubeclt exec issues with variable via php script

Time:05-19

New to kubernetes and php, so I'm having some issues. Any and all help is greatly appreciated!

<?php
$postgres = 'kubectl get pods -n migrationnamespace | grep postgres | cut -d " " -f1 2>&1';
$postgres_pod = shell_exec($postgres);
echo $postgres_pod;

$list2 = 'kubectl exec -it -n migrationnamespace ' . $postgres_pod . ' -- psql -U postgres -c \'SELECT * FROM mywhales\'; 2>&1';
echo "<pre>";
echo shell_exec($list2);
echo "<pre>";
?>

results in error

postgres-7957478b7d-tmw6m
error: you must specify at least one command for the container
sh: line 1: --: command not found

When switching '.$postgres_pod.' for postgres-7957478b7d-tmw6m as below - it executes fully

$list2 = 'kubectl exec -it -n migrationnamespace postgres-7957478b7d-tmw6m -- psql -U postgres -c \'SELECT * FROM mywhales\';';

postgres-7957478b7d-tmw6m
  whale  
---------
 16:117
 ......
 561:539
(17 rows)

Thanks - Mike

CodePudding user response:

There can sometimes be extra whitespace before or after strings, especially return characters which don't always show up when echoing the result.

Using trim($postgres_pod) will ensure they are removed.

  • Related