index.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <p>Hello, world!</p>
  2. <ul>
  3. <li/>Hostname is <?= getenv('HOSTNAME') ?> (<?= $_ENV["HOSTNAME"] ?>)
  4. <li/>Server name is <?= getenv('SERVER_NAME') ?> (<?= $_ENV["SERVER_NAME"] ?>)
  5. <li/>Namespace is <?= getenv('NAMESPACE') ?> (<?= $_ENV["NAMESPACE"] ?>)
  6. <li/>Service account name is <?= getenv('ACCOUNT_NAME') ?> (<?= $_ENV["ACCOUNT_NAME"] ?>)
  7. <li/>Service name is <?= getenv('SERVICE_NAME') ?> (<?= $_ENV["SERVICE_NAME"] ?>)
  8. <li/>Pod name is <?= getenv('POD_NAME') ?> (<?= $_ENV["POD_NAME"] ?>)
  9. <li/>Pod IP is <?= getenv('POD_IP') ?> (<?= $_ENV["POD_IP"] ?>)
  10. <li/>Application label is <code>app=<?= getenv('APP_LABEL') ?></code> (<?= $_ENV["APP_LABEL"] ?>)
  11. </ul>
  12. <?php
  13. $tfile = "/token/token";
  14. if (isset($_ENV['TOKEN_FILE'])) {
  15. $tfile = $_ENV['TOKEN_FILE'];
  16. }
  17. if (!file_exists($tfile)) {
  18. print("<p>Can not read the token file. Exiting.</p>\n");
  19. exit();
  20. }
  21. $token = file_get_contents($tfile);;
  22. print("<p>Using token <code>" . $token . "</code>.</p>\n");
  23. $errors = 0;
  24. if (!isset($_ENV["NAMESPACE"])) {
  25. print("<p>Namespace variable not set (NAMESPACE).</p>\n");
  26. $errors++;
  27. }
  28. if (!isset($_ENV["APP_LABEL"])) {
  29. print("<p>App label variable not set (APP_LABEL).</p>\n");
  30. $errors++;
  31. }
  32. if ($errors == 0) {
  33. $cs = curl_init("https://kubernetes.default/api/v1/namespaces/" . $_ENV["NAMESPACE"] . "/pods?labelSelector=app=" . $_ENV["APP_LABEL"]);
  34. curl_setopt($cs, CURLOPT_SSL_VERIFYPEER, false);
  35. curl_setopt($cs, CURLOPT_RETURNTRANSFER, true);
  36. curl_setopt($cs, CURLOPT_HTTPHEADER, [
  37. "Authorization: Bearer " . $token,
  38. "Accept: application/json"
  39. ]);
  40. $response = curl_exec($cs);
  41. print("<p>Got pod list response:<p>\n");
  42. ?><code>
  43. <?= $response ?>
  44. </code>
  45. <?php
  46. } else {
  47. print("<p>Skipping pod list due to errors.</p>\n");
  48. }
  49. $errors = 0;
  50. if (!isset($_ENV["NAMESPACE"])) {
  51. print("<p>Namespace variable not set (NAMESPACE).</p>\n");
  52. $errors++;
  53. }
  54. if (!isset($_ENV["SERVICE_NAME"])) {
  55. print("<p>Service name variable not set (SERVICE_NAME).</p>\n");
  56. $errors++;
  57. }
  58. if ($errors == 0) {
  59. $cs = curl_init("https://kubernetes.default/api/v1/namespaces/" . $_ENV["NAMESPACE"] . "/endpoints/" . $_ENV["SERVICE_NAME"]);
  60. curl_setopt($cs, CURLOPT_SSL_VERIFYPEER, false);
  61. curl_setopt($cs, CURLOPT_RETURNTRANSFER, true);
  62. curl_setopt($cs, CURLOPT_HTTPHEADER, [
  63. "Authorization: Bearer " . $token,
  64. "Accept: application/json"
  65. ]);
  66. $response = curl_exec($cs);
  67. print("<p>Got endpoint list response:<p>\n");
  68. ?><code>
  69. <?= $response ?>
  70. </code>
  71. <?php
  72. $dnsrec = dns_get_record($_ENV["SERVICE_NAME"] . "." . $_ENV["NAMESPACE"] . ".svc.cluster.local", DNS_A);
  73. if ($dnsrec) {
  74. print("<p>Got dns query response:<p>\n<code>\n");
  75. var_dump($dnsrec);
  76. print("\n</code>\n");
  77. } else {
  78. print("<p>Could not look up DNS record for service.</p>\n");
  79. }
  80. } else {
  81. print("<p>Skipping service test due to errors.</p>\n");
  82. }
  83. ?>