Repository: kubernetes/kubernetes
File: test/images/agnhost/porter/porter.go (Go)

Code before:
	"net"
	"net/http"
	"os"
	"strings"

	"github.com/ishidawataru/sctp"
	"github.com/spf13/cobra"
)

const (
	portPrefix = "SERVE_PORT_"
	tcpPrefix  = "SERVE_TCP_PORT_"
	udpPrefix  = "SERVE_UDP_PORT_"
	tlsPrefix  = "SERVE_TLS_PORT_"
	sctpPrefix = "SERVE_SCTP_PORT_"
)

// CmdPorter is used by agnhost Cobra.
var CmdPorter = &cobra.Command{
	Use:   "porter",
	Short: "Serves requested data on ports specified in ENV variables",
	Long: `Serves requested data on ports specified in environment variables of the form SERVE_{PORT,TCP_PORT,UDP_PORT,TLS_PORT,SCTP_PORT}_[NNNN]. 
	
eg:
* SERVE_PORT_9001 - serve TCP connections on port 9001
* SERVE_TCP_PORT_9002 - serve TCP connections on port 9002
* SERVE_UDP_PORT_9003 - serve UDP connections on port 9003
* SERVE_TLS_PORT_9004 - serve TLS-encrypted TCP connections on port 9004
* SERVE_SCTP_PORT_9005 - serve SCTP connections on port 9005

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".

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.`,
	Args: cobra.MaximumNArgs(0),
	Run:  main,
}

// JSONResponse enables --json-response flag
var JSONResponse bool

type jsonResponse struct {
	Method string
	Body   string
}

func init() {
	CmdPorter.Flags().BoolVar(&JSONResponse, "json-response", false, "Responds to requests with a json response that includes the default value with the http.Request Method")
}

func main(cmd *cobra.Command, args []string) {
	for _, vk := range os.Environ() {

Review comment (suggestion):
should probably document `SERVE_PORT_*` and `SERVE_TCP_PORT_*` on the same line and using the same port number, just to make it clearer that it's two ways of saying the same thing, as opposed to being subtly different in some way.

Code after:
	"net"
	"net/http"
	"os"
	"strings"

	"github.com/ishidawataru/sctp"
	"github.com/spf13/cobra"
)

const (
	portPrefix = "SERVE_PORT_"
	tcpPrefix  = "SERVE_TCP_PORT_"
	udpPrefix  = "SERVE_UDP_PORT_"
	tlsPrefix  = "SERVE_TLS_PORT_"
	sctpPrefix = "SERVE_SCTP_PORT_"
)

// CmdPorter is used by agnhost Cobra.
var CmdPorter = &cobra.Command{
	Use:   "porter",
	Short: "Serves requested data on ports specified in ENV variables",
	Long: `Serves requested data on ports specified in environment variables of the form SERVE_{PORT,TCP_PORT,UDP_PORT,TLS_PORT,SCTP_PORT}_[NNNN]. 
	
eg:
* SERVE_PORT_9001 (or SERVE_TCP_PORT_9001)- serve TCP connections on port 9001
* SERVE_TLS_PORT_9002 - serve TLS-encrypted TCP connections on port 9002
* SERVE_SCTP_PORT_9003 - serve SCTP connections on port 9003
* SERVE_UDP_PORT_9004 - serve UDP connections on port 9004

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".

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.`,
	Args: cobra.MaximumNArgs(0),
	Run:  main,
}

// JSONResponse enables --json-response flag
var JSONResponse bool

type jsonResponse struct {
	Method string
	Body   string
}

func init() {
	CmdPorter.Flags().BoolVar(&JSONResponse, "json-response", false, "Responds to requests with a json response that includes the default value with the http.Request Method")
}

func main(cmd *cobra.Command, args []string) {
	for _, vk := range os.Environ() {
		// Put everything before the first = sign in parts[0], and