main.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. Copyright 2017 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. package nosnatproxy
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "net/http"
  18. "os"
  19. "github.com/spf13/cobra"
  20. "k8s.io/component-base/logs"
  21. )
  22. // CmdNoSnatTestProxy is used by agnhost Cobra.
  23. var CmdNoSnatTestProxy = &cobra.Command{
  24. Use: "no-snat-test-proxy",
  25. Short: "Creates a proxy for the /checknosnat endpoint",
  26. Long: `Creates the /checknosnat endpoint which proxies the request to the given target (/checknosnat?target=target_ip&ips=ip1,ip2) and returns its response, or a 500 response on error.`,
  27. Args: cobra.MaximumNArgs(0),
  28. Run: main,
  29. }
  30. var port string
  31. func init() {
  32. CmdNoSnatTestProxy.Flags().StringVar(&port, "port", "31235", "The port to serve /checknosnat endpoint on.")
  33. }
  34. // This Pod's /checknosnat takes `target` and `ips` arguments, and queries {target}/checknosnat?ips={ips}
  35. type masqTestProxy struct {
  36. Port string
  37. }
  38. func main(cmd *cobra.Command, args []string) {
  39. m := &masqTestProxy{
  40. Port: port,
  41. }
  42. logs.InitLogs()
  43. defer logs.FlushLogs()
  44. if err := m.Run(); err != nil {
  45. fmt.Fprintf(os.Stderr, "%v\n", err)
  46. os.Exit(1)
  47. }
  48. }
  49. func (m *masqTestProxy) Run() error {
  50. // register handler
  51. http.HandleFunc("/checknosnat", checknosnat)
  52. // spin up the server
  53. return http.ListenAndServe(":"+m.Port, nil)
  54. }
  55. func checknosnatURL(pip, ips string) string {
  56. return fmt.Sprintf("http://%s/checknosnat?ips=%s", pip, ips)
  57. }
  58. func checknosnat(w http.ResponseWriter, req *http.Request) {
  59. url := checknosnatURL(req.URL.Query().Get("target"), req.URL.Query().Get("ips"))
  60. resp, err := http.Get(url)
  61. if err != nil {
  62. w.WriteHeader(500)
  63. fmt.Fprintf(w, "error querying %q, err: %v", url, err)
  64. } else {
  65. body, err := ioutil.ReadAll(resp.Body)
  66. if err != nil {
  67. w.WriteHeader(500)
  68. fmt.Fprintf(w, "error reading body of response from %q, err: %v", url, err)
  69. } else {
  70. // Respond the same status code and body as /checknosnat on the internal Pod
  71. w.WriteHeader(resp.StatusCode)
  72. w.Write(body)
  73. }
  74. }
  75. resp.Body.Close()
  76. }