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