Skip to content

Commit

Permalink
initial port-forwarding API PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeseese committed Apr 8, 2024
1 parent 6ea9ff5 commit 82a00a0
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 21 deletions.
134 changes: 134 additions & 0 deletions bats/tests/k8s/port-forwarding.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
load '../helpers/load'

local_setup() {
if using_docker; then
skip "this test only works on containerd right now"
fi
}

assert_traefik_crd_established() {
local jsonpath="{.status.conditions[?(@.type=='Established')].status}"
run kubectl get crd traefikservices.traefik.containo.us --output jsonpath="$jsonpath"
assert_success || return
assert_output 'True'
}

@test 'start k8s' {
factory_reset
start_kubernetes
wait_for_kubelet

# The manifests in /var/lib/rancher/k3s/server/manifests are processed
# in alphabetical order. So when traefik.yaml has been loaded we know that
# rd-runtime.yaml has already been processed.
try assert_traefik_crd_established
}

@test 'deploy sample app' {
kubectl apply --filename - <<EOF
apiVersion v1
kind: ConfigMap
metadata:
name: webapp-configmap
data:
index: "Hello World!"
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: webapp
spec:
replicas: 1
selector:
matchLabels:
app: webapp
template:
metadata:
labels:
app: webapp
spec:
volumes:
- name: webapp-config-volume
configMap:
name: webapp-configmap
items:
- key: index
path: index.html
containers:
- name: webapp
image: nginx
volumeMounts:
- name: webapp-config-volume
mountPath: /usr/share/nginx/html
EOF
}

get_host() {
if is_windows; then
local LB_IP
local output='jsonpath={.status.loadBalancer.ingress[0].ip}'
LB_IP=$(kubectl get service traefik --namespace kube-system --output "$output")
echo "$LB_IP.sslip.io"
else
echo "localhost"
fi
}

@test 'deploy ingress' {
kubectl apply --filename - <<EOF
apiVersion: v1
kind: Service
metadata:
name: webapp
spec:
type: ClusterIP
selector:
app: webapp
ports:
- port: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: webapp
annotations:
traefik.ingress.kubernetes.io/router.entrypoints: web
spec:
rules:
- host: $(get_host)
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: webapp
port:
number: 80
EOF
}

@test 'connect to the service' {
# This can take 100s with old versions of traefik, and 15s with newer ones.
run try curl --silent --fail "http://$(get_host)"
assert_success
assert_output "Hello World!"
}

@test 'fail to connect to the service on localhost without port forwarding' {
run try curl --silent --fail "http://localhost:8080"
assert_failure
}

@test 'connect to the service on localhost with port forwarding' {
rdctl api -X POST -b '{ "namespace": "default", "service": "webapp", "k8sPort": 80, "hostPort": 8080 }' port_forwarding
run try curl --silent --fail "http://localhost:8080"
assert_success
assert_output "Hello World!"
}

@test 'fail to connect to the service on localhost after removing port forwarding' {
rdctl api -X DELETE "port_forwarding?namespace=default&service=webapp&k8sPort=80"
run try curl --silent --fail "http://localhost:8080"
assert_failure
}
18 changes: 0 additions & 18 deletions bats/tests/k8s/wasm.bats
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,3 @@ EOF
assert_success
assert_output "Hello world from Spin!"
}

@test 'fail to connect to the service on localhost without port forwarding' {
run try curl --silent --fail "http://localhost:8080/hello"
assert_failure
}

@test 'connect to the service on localhost with port forwarding' {
rdctl api -X POST -b '{ "namespace": "default", "service": "hello-spin", "k8sPort": 80, "hostPort": 8080 }' port_forwarding
run try curl --silent --fail "http://localhost:8080/hello"
assert_success
assert_output "Hello world from Spin!"
}

@test 'fail to connect to the service on localhost after removing port forwarding' {
rdctl api -X DELETE "port_forwarding?namespace=default&service=hello-spin&k8sPort=80"
run try curl --silent --fail "http://localhost:8080/hello"
assert_failure
}
12 changes: 9 additions & 3 deletions pkg/rancher-desktop/assets/specs/command-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,19 @@ paths:
service:
type: string
k8sPort:
type: string
type:
- string
- integer
hostPort:
type: integer
required: true
responses:
'200':
description: The port forwarding was created.
description: The port forwarding was created or already exists; the response contains the listening host port.
content:
text/plain:
schema:
type: integer
'400':
description: The port forwarding could not be created.
delete:
Expand All @@ -245,7 +251,7 @@ paths:
name: k8sPort
responses:
'200':
description: The port forwarding was deleted.
description: The port forwarding was deleted or doesn't exist.
'400':
description: The port forwarding could not be deleted.

Expand Down

0 comments on commit 82a00a0

Please sign in to comment.