Repository navigation
297 lines (251 loc) · 11.6 KB
/
Copy pathbuild-portforward.yml
File metadata and controls
297 lines (251 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# SPDX-FileCopyrightText: 2026 The RISE Project
# SPDX-License-Identifier: MIT
---
# This workflow is based on the `linux` job of
# https://github.com/pytogo/portforward/blob/v0.7.6/.github/workflows/release.yml
name: Build portforward wheels (riscv64)
on:
workflow_dispatch:
inputs:
version:
description: 'Version glob to (re)build; empty builds every version of docs/packages/portforward.yaml not released yet'
required: false
default: ''
pull_request:
branches: [main]
paths:
- '.github/workflows/build-portforward.yml'
- 'docs/packages/portforward.yaml'
push:
branches: [main]
paths:
- '.github/workflows/build-portforward.yml'
- 'docs/packages/portforward.yaml'
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true
permissions:
contents: read # to fetch code (actions/checkout)
jobs:
setup:
uses: $/.github/workflows/_setup.yml
with:
package: portforward
version: ${{ inputs.version }}
build_wheels:
needs: [setup]
if: needs.setup.outputs.versions != '[]'
name: Build portforward ${{ matrix.version }} ${{ matrix.python }}-manylinux_riscv64
runs-on: ubuntu-24.04-riscv
timeout-minutes: 180
strategy:
fail-fast: false
matrix:
version: ${{ fromJSON(needs.setup.outputs.versions) }}
python: ['cp312', 'cp313', 'cp314', 'cp314t']
env:
PORTFORWARD_VERSION: ${{ matrix.version }}
steps:
- name: Checkout portforward v${{ env.PORTFORWARD_VERSION }}
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
repository: pytogo/portforward
ref: v${{ env.PORTFORWARD_VERSION }}
persist-credentials: false
- name: Determine target interpreter
id: interp
run: |
v="${{ matrix.python }}"
v="${v#cp}"
echo "value=python${v:0:1}.${v:1}" >> "$GITHUB_OUTPUT"
- name: Build wheel
uses: PyO3/maturin-action@e83996d129638aa358a18fbd1dfb82f0b0fb5d3b # v1.51.0
with:
target: riscv64gc-unknown-linux-gnu
args: --release --out dist -i ${{ steps.interp.outputs.value }}
manylinux: '2_39'
before-script-linux: yum install -y perl-core openssl openssl-devel pkgconfig libatomic
- name: Verify the wheel ships the compiled extension
run: |
set -euo pipefail
python3 - dist/*.whl <<'EOF'
import sys, zipfile
names = zipfile.ZipFile(sys.argv[1]).namelist()
sos = [n for n in names if n.endswith(".so")]
assert any(n.startswith("portforward/_portforward.") for n in sos), sos
assert any(n.endswith(".dist-info/licenses/LICENSE") for n in names), names
print(sys.argv[1], "->", sos)
EOF
- name: Install Python
uses: astral-sh/setup-uv@20cfd1bf945f4377ade1205e4dbc17946fc9a30d # v10.0.1
with:
python-version: ${{ steps.interp.outputs.value }}
activate-environment: true
enable-cache: false
# Upstream's tests/test_portforward.py needs a pytest-kind cluster; this
# drives the extension against a local TLS fake of the Kubernetes API instead.
- name: Stage the riscv64 tests
run: |
mkdir -p riscv64-tests
cat > riscv64-tests/test_riscv64_smoke.py <<'EOF'
import json
import socket
import ssl
import subprocess
import threading
import time
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from urllib.parse import parse_qs, urlsplit
import pytest
import portforward
NAMESPACE = "pftest"
POD = "test-pod"
SERVICE = "web-svc"
POD_OBJ = {
"apiVersion": "v1",
"kind": "Pod",
"metadata": {"name": POD, "namespace": NAMESPACE, "resourceVersion": "1", "labels": {"app": "web"}},
"spec": {"containers": [{"name": "web", "image": "web"}]},
"status": {"phase": "Running"},
}
SERVICE_OBJ = {
"apiVersion": "v1",
"kind": "Service",
"metadata": {"name": SERVICE, "namespace": NAMESPACE, "resourceVersion": "1"},
"spec": {"selector": {"app": "web"}, "ports": [{"port": 80}]},
}
NOT_FOUND = {"apiVersion": "v1", "kind": "Status", "metadata": {}, "status": "Failure",
"message": "not found", "reason": "NotFound", "code": 404}
class FakeApiServer(BaseHTTPRequestHandler):
protocol_version = "HTTP/1.1"
def log_message(self, *args):
pass
def _send(self, code, body):
data = json.dumps(body).encode()
self.send_response(code)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(data)))
self.end_headers()
self.wfile.write(data)
def do_GET(self):
url = urlsplit(self.path)
query = parse_qs(url.query)
prefix = f"/api/v1/namespaces/{NAMESPACE}"
if url.path == f"{prefix}/pods/{POD}":
return self._send(200, POD_OBJ)
if url.path == f"{prefix}/services/{SERVICE}":
return self._send(200, SERVICE_OBJ)
if url.path == f"{prefix}/pods" and "watch" not in query:
selector = query.get("labelSelector", []) + query.get("fieldSelector", [])
if selector in (["app=web"], [f"metadata.name={POD}"]):
return self._send(200, {"apiVersion": "v1", "kind": "PodList",
"metadata": {"resourceVersion": "1"}, "items": [POD_OBJ]})
return self._send(404, NOT_FOUND)
def _openssl(*args, cwd):
subprocess.run(["openssl", *args], cwd=cwd, check=True, capture_output=True)
def _make_ca(tmp, name):
_openssl("req", "-x509", "-newkey", "rsa:2048", "-nodes", "-days", "1", "-subj", f"/CN={name}",
"-keyout", f"{name}.key", "-out", f"{name}.crt", cwd=tmp)
return tmp / f"{name}.crt"
@pytest.fixture(autouse=True)
def no_proxy(monkeypatch):
for var in ("HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy", "ALL_PROXY", "all_proxy"):
monkeypatch.delenv(var, raising=False)
@pytest.fixture(scope="module")
def pki(tmp_path_factory):
tmp = tmp_path_factory.mktemp("pki")
ca = _make_ca(tmp, "ca")
other_ca = _make_ca(tmp, "other-ca")
_openssl("req", "-newkey", "rsa:2048", "-nodes", "-subj", "/CN=127.0.0.1",
"-keyout", "server.key", "-out", "server.csr", cwd=tmp)
(tmp / "ext.cnf").write_text(
"basicConstraints=CA:FALSE\nkeyUsage=digitalSignature,keyEncipherment\n"
"extendedKeyUsage=serverAuth\nsubjectAltName=IP:127.0.0.1\n"
)
_openssl("x509", "-req", "-in", "server.csr", "-CA", "ca.crt", "-CAkey", "ca.key",
"-CAcreateserial", "-days", "1", "-extfile", "ext.cnf", "-out", "server.crt", cwd=tmp)
return {"dir": tmp, "ca": ca, "other_ca": other_ca}
@pytest.fixture(scope="module")
def api_server(pki):
httpd = ThreadingHTTPServer(("127.0.0.1", 0), FakeApiServer)
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ctx.load_cert_chain(pki["dir"] / "server.crt", pki["dir"] / "server.key")
httpd.socket = ctx.wrap_socket(httpd.socket, server_side=True)
thread = threading.Thread(target=httpd.serve_forever, daemon=True)
thread.start()
yield f"https://127.0.0.1:{httpd.server_address[1]}"
httpd.shutdown()
def _kubeconfig(tmp_path, server, ca):
config = {
"apiVersion": "v1",
"kind": "Config",
"clusters": [{"name": "fake", "cluster": {"server": server, "certificate-authority": str(ca)}}],
"users": [{"name": "fake", "user": {"token": "fake-token"}}],
"contexts": [{"name": "fake", "context": {"cluster": "fake", "user": "fake", "namespace": NAMESPACE}}],
"current-context": "fake",
}
path = tmp_path / "kubeconfig"
path.write_text(json.dumps(config))
return str(path)
def _listening(port):
try:
socket.create_connection(("127.0.0.1", port), timeout=1).close()
return True
except OSError:
return False
def test_extension_is_compiled():
from portforward import _portforward
assert _portforward.__file__.endswith(".so"), _portforward.__file__
@pytest.mark.parametrize("target", [POD, SERVICE])
def test_forward_binds_and_stops(tmp_path, pki, api_server, target):
config = _kubeconfig(tmp_path, api_server, pki["ca"])
with portforward.forward(NAMESPACE, target, 0, 3000, config_path=config,
log_level=portforward.LogLevel.OFF) as forwarder:
assert forwarder._async_forwarder.actual_pod_name == POD
port = forwarder.from_port
assert port > 0
assert _listening(port)
for _ in range(50):
if not _listening(port):
break
time.sleep(0.1)
assert not _listening(port)
def test_unknown_target_raises(tmp_path, pki, api_server):
config = _kubeconfig(tmp_path, api_server, pki["ca"])
with pytest.raises(portforward.PortforwardError, match="NotFound"):
with portforward.forward(NAMESPACE, "missing", 0, 3000, config_path=config,
log_level=portforward.LogLevel.OFF):
pass
def test_untrusted_server_certificate_is_rejected(tmp_path, pki, api_server):
config = _kubeconfig(tmp_path, api_server, pki["other_ca"])
with pytest.raises(portforward.PortforwardError, match="(?i)certificate|UnknownIssuer"):
with portforward.forward(NAMESPACE, POD, 0, 3000, config_path=config,
log_level=portforward.LogLevel.OFF):
pass
EOF
- name: Test wheel
run: |
uv pip install --reinstall dist/*.whl
uv pip install pytest
python -m pytest -v riscv64-tests
- uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: portforward-${{ env.PORTFORWARD_VERSION }}-${{ matrix.python }}-manylinux_riscv64
path: dist/*.whl
if-no-files-found: error
publish:
name: Publish portforward ${{ matrix.version }}
needs: [setup, build_wheels]
if: needs.setup.outputs.versions != '[]'
strategy:
fail-fast: false
matrix:
version: ${{ fromJSON(needs.setup.outputs.versions) }}
permissions:
contents: write
pull-requests: write
uses: $/.github/workflows/_publish-wheel.yml
secrets:
app-private-key: ${{ secrets.RISEPROJECT_APP_PRIVATE_KEY }}
with:
artifact-pattern: portforward-${{ matrix.version }}-*-manylinux_riscv64