mirror of https://github.com/k3s-io/k3s
netexec: Add / and /echo handlers, bump to 1.4
Add some logs, allow simple "cmd" arg for shell.pull/6/head
parent
fd5cbdf73f
commit
bb460c04dd
|
@ -18,16 +18,17 @@ package e2e
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
api "k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/unversioned"
|
||||
"k8s.io/kubernetes/pkg/apimachinery/registered"
|
||||
client "k8s.io/kubernetes/pkg/client/unversioned"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
kubeletEtcHostsImageName = "gcr.io/google_containers/netexec:1.0"
|
||||
kubeletEtcHostsImageName = "gcr.io/google_containers/netexec:1.4"
|
||||
kubeletEtcHostsPodName = "test-pod"
|
||||
kubeletEtcHostsHostNetworkPodName = "test-host-network-pod"
|
||||
etcHostsPartialContent = "# Kubernetes-managed hosts file."
|
||||
|
|
|
@ -46,7 +46,7 @@ const (
|
|||
nodeHttpPort = 32080
|
||||
nodeUdpPort = 32081
|
||||
loadBalancerHttpPort = 100
|
||||
netexecImageName = "gcr.io/google_containers/netexec:1.0"
|
||||
netexecImageName = "gcr.io/google_containers/netexec:1.4"
|
||||
testPodName = "test-container-pod"
|
||||
hostTestPodName = "host-test-container-pod"
|
||||
nodePortServiceName = "node-port-service"
|
||||
|
|
|
@ -19,13 +19,14 @@ package e2e
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/unversioned"
|
||||
"k8s.io/kubernetes/pkg/apimachinery/registered"
|
||||
client "k8s.io/kubernetes/pkg/client/unversioned"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -36,7 +37,7 @@ const (
|
|||
notPrivilegedHttpPort = 9090
|
||||
notPrivilegedUdpPort = 9091
|
||||
notPrivilegedContainerName = "not-privileged-container"
|
||||
privilegedContainerImage = "gcr.io/google_containers/netexec:1.1"
|
||||
privilegedContainerImage = "gcr.io/google_containers/netexec:1.4"
|
||||
privilegedCommand = "ip link add dummy1 type dummy"
|
||||
)
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ spec:
|
|||
spec:
|
||||
containers:
|
||||
- name: netexec
|
||||
image: gcr.io/google_containers/netexec:1.0
|
||||
image: gcr.io/google_containers/netexec:1.4
|
||||
ports:
|
||||
- containerPort: 8080
|
||||
# This is to force these pods to land on different hosts.
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
.PHONY: all netexec image push clean
|
||||
|
||||
TAG = 1.3.1
|
||||
TAG = 1.4
|
||||
PREFIX = gcr.io/google_containers
|
||||
|
||||
|
||||
|
|
|
@ -56,15 +56,45 @@ func main() {
|
|||
}
|
||||
|
||||
func startHTTPServer(httpPort int) {
|
||||
http.HandleFunc("/shutdown", shutdownHandler)
|
||||
http.HandleFunc("/hostName", hostNameHandler)
|
||||
http.HandleFunc("/", rootHandler)
|
||||
http.HandleFunc("/echo", echoHandler)
|
||||
http.HandleFunc("/exit", exitHandler)
|
||||
http.HandleFunc("/hostname", hostnameHandler)
|
||||
http.HandleFunc("/shell", shellHandler)
|
||||
http.HandleFunc("/upload", uploadHandler)
|
||||
http.HandleFunc("/dial", dialHandler)
|
||||
// older handlers
|
||||
http.HandleFunc("/hostName", hostNameHandler)
|
||||
http.HandleFunc("/shutdown", shutdownHandler)
|
||||
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", httpPort), nil))
|
||||
}
|
||||
|
||||
func rootHandler(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("GET /")
|
||||
fmt.Fprintf(w, "NOW: %v", time.Now())
|
||||
}
|
||||
|
||||
func echoHandler(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("GET /echo?msg=%s", r.FormValue("msg"))
|
||||
fmt.Fprintf(w, "%s", r.FormValue("msg"))
|
||||
}
|
||||
|
||||
func exitHandler(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("GET /exit?code=%s", r.FormValue("code"))
|
||||
code, err := strconv.Atoi(r.FormValue("code"))
|
||||
if err == nil || r.FormValue("code") == "" {
|
||||
os.Exit(code)
|
||||
}
|
||||
fmt.Fprintf(w, "argument 'code' must be an integer [0-127] or empty, got %q", r.FormValue("code"))
|
||||
}
|
||||
|
||||
func hostnameHandler(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("GET /hostname")
|
||||
fmt.Fprintf(w, getHostName())
|
||||
}
|
||||
|
||||
func shutdownHandler(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("GET /shutdown")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
|
@ -80,6 +110,7 @@ func dialHandler(w http.ResponseWriter, r *http.Request) {
|
|||
request := values.Query().Get("request") // hostName
|
||||
protocol := values.Query().Get("protocol")
|
||||
tryParam := values.Query().Get("tries")
|
||||
log.Printf("GET /dial?host=%s&protocol=%s&port=%s&request=%s&tries=%s", host, protocol, port, request, tryParam)
|
||||
tries := 1
|
||||
if len(tryParam) > 0 {
|
||||
tries, err = strconv.Atoi(tryParam)
|
||||
|
@ -192,9 +223,12 @@ func dialUDP(request string, remoteAddress *net.UDPAddr) (string, error) {
|
|||
}
|
||||
|
||||
func shellHandler(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println(r.FormValue("shellCommand"))
|
||||
log.Printf("%s %s %s\n", shellPath, "-c", r.FormValue("shellCommand"))
|
||||
cmdOut, err := exec.Command(shellPath, "-c", r.FormValue("shellCommand")).CombinedOutput()
|
||||
cmd := r.FormValue("shellCommand")
|
||||
if cmd == "" {
|
||||
cmd = r.FormValue("cmd")
|
||||
}
|
||||
log.Printf("GET /shell?cmd=%s", cmd)
|
||||
cmdOut, err := exec.Command(shellPath, "-c", cmd).CombinedOutput()
|
||||
output := map[string]string{}
|
||||
if len(cmdOut) > 0 {
|
||||
output["output"] = string(cmdOut)
|
||||
|
@ -212,6 +246,7 @@ func shellHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("GET /upload")
|
||||
result := map[string]string{}
|
||||
file, _, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
|
@ -287,10 +322,19 @@ func startUDPServer(udpPort int) {
|
|||
n, clientAddress, err := serverConn.ReadFromUDP(buf)
|
||||
assertNoError(err)
|
||||
receivedText := strings.TrimSpace(string(buf[0:n]))
|
||||
if receivedText == "hostName" {
|
||||
if receivedText == "hostName" || receivedText == "hostname" {
|
||||
log.Println("Sending udp hostName response")
|
||||
_, err = serverConn.WriteToUDP([]byte(getHostName()), clientAddress)
|
||||
assertNoError(err)
|
||||
} else if strings.HasPrefix(receivedText, "echo ") {
|
||||
parts := strings.SplitN(receivedText, " ", 2)
|
||||
resp := ""
|
||||
if len(parts) == 2 {
|
||||
resp = parts[1]
|
||||
}
|
||||
log.Println("Echoing %q")
|
||||
_, err = serverConn.WriteToUDP([]byte(resp), clientAddress)
|
||||
assertNoError(err)
|
||||
} else if len(receivedText) > 0 {
|
||||
log.Println("Unknown udp command received. ", receivedText)
|
||||
}
|
||||
|
|
|
@ -7,7 +7,9 @@ metadata:
|
|||
spec:
|
||||
containers:
|
||||
- name: netexec
|
||||
image: gcr.io/google_containers/netexec:1.3.1
|
||||
image: gcr.io/google_containers/netexec:1.4
|
||||
ports:
|
||||
- containerPort: 8080
|
||||
protocol: TCP
|
||||
- containerPort: 8081
|
||||
protocol: UDP
|
||||
|
|
Loading…
Reference in New Issue