porter.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // A tiny binary for testing ports.
  14. //
  15. // Reads env vars; for every var of the form SERVE_PORT_X, where X is a valid
  16. // port number, porter starts an HTTP server which serves the env var's value
  17. // in response to any query.
  18. package porter
  19. import (
  20. "fmt"
  21. "log"
  22. "net/http"
  23. "os"
  24. "strings"
  25. "github.com/spf13/cobra"
  26. )
  27. const prefix = "SERVE_PORT_"
  28. const tlsPrefix = "SERVE_TLS_PORT_"
  29. // CmdPorter is used by agnhost Cobra.
  30. var CmdPorter = &cobra.Command{
  31. Use: "porter",
  32. Short: "Serves requested data on ports specified in ENV variables",
  33. Long: `Serves requested data on ports specified in ENV variables. For example, if the environment variable "SERVE_PORT_9001" is set, then the subcommand will start serving on the port 9001.
  34. Additionally, if the environment variable "SERVE_TLS_PORT_9002" is set, then the subcommand will start a TLS server on that port.
  35. The included "localhost.crt" is a PEM-encoded TLS cert with SAN IPs "127.0.0.1" and "[::1]", expiring in January 2084, generated from "src/crypto/tls".
  36. To use a different cert/key, mount them into the pod and set the "CERT_FILE" and "KEY_FILE" environment variables to the desired paths.`,
  37. Args: cobra.MaximumNArgs(0),
  38. Run: main,
  39. }
  40. func main(cmd *cobra.Command, args []string) {
  41. for _, vk := range os.Environ() {
  42. // Put everything before the first = sign in parts[0], and
  43. // everything else in parts[1] (even if there are multiple =
  44. // characters).
  45. parts := strings.SplitN(vk, "=", 2)
  46. key := parts[0]
  47. value := parts[1]
  48. if strings.HasPrefix(key, prefix) {
  49. port := strings.TrimPrefix(key, prefix)
  50. go servePort(port, value)
  51. }
  52. if strings.HasPrefix(key, tlsPrefix) {
  53. port := strings.TrimPrefix(key, tlsPrefix)
  54. go serveTLSPort(port, value)
  55. }
  56. }
  57. select {}
  58. }
  59. func servePort(port, value string) {
  60. s := &http.Server{
  61. Addr: "0.0.0.0:" + port,
  62. Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  63. fmt.Fprint(w, value)
  64. }),
  65. }
  66. log.Printf("server on port %q failed: %v", port, s.ListenAndServe())
  67. }
  68. func serveTLSPort(port, value string) {
  69. s := &http.Server{
  70. Addr: "0.0.0.0:" + port,
  71. Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  72. fmt.Fprint(w, value)
  73. }),
  74. }
  75. certFile := os.Getenv("CERT_FILE")
  76. if len(certFile) == 0 {
  77. certFile = "localhost.crt"
  78. }
  79. keyFile := os.Getenv("KEY_FILE")
  80. if len(keyFile) == 0 {
  81. keyFile = "localhost.key"
  82. }
  83. log.Printf("tls server on port %q with certFile=%q, keyFile=%q failed: %v", port, certFile, keyFile, s.ListenAndServeTLS(certFile, keyFile))
  84. }