Merge pull request #18288 from luchillo17/feat/BCKSTG-209-proxy-endpoint-supports-webso

feat(kubernetes-backend): Add WebSocket support to `kubernetes-backend` Proxy
This commit is contained in:
Jamie Klassen
2023-06-27 16:41:33 -04:00
committed by GitHub
3 changed files with 13 additions and 2 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-kubernetes-backend': patch
---
Add WebSocket support to `kubernetes-backend` proxy.
@@ -54,6 +54,10 @@ describe('KubernetesProxy', () => {
params: {
path,
},
headers: {
'content-type': 'application/json',
[HEADER_KUBERNETES_CLUSTER.toLowerCase()]: clusterName,
},
header: jest.fn((key: string) => {
switch (key) {
case 'Content-Type': {
@@ -135,12 +135,15 @@ export class KubernetesProxy {
const logger = this.logger.child({ cluster: originalCluster.name });
middleware = createProxyMiddleware({
logProvider: () => logger,
ws: true,
secure: !originalCluster.skipTLSVerify,
changeOrigin: true,
pathRewrite: { [`^${originalReq.baseUrl}`]: '' },
router: async req => {
// Re-evaluate the cluster on each request, in case it has changed
const cluster = await this.getClusterForRequest(req);
const url = new URL(cluster.url);
return {
protocol: url.protocol,
host: url.hostname,
@@ -148,7 +151,6 @@ export class KubernetesProxy {
ca: bufferFromFileOrString('', cluster.caData)?.toString(),
};
},
pathRewrite: { [`^${originalReq.baseUrl}`]: '' },
onError: (error, req, res) => {
const wrappedError = new ForwardedError(
`Cluster '${originalCluster.name}' request error`,
@@ -173,7 +175,7 @@ export class KubernetesProxy {
}
private async getClusterForRequest(req: Request): Promise<ClusterDetails> {
const clusterName = req.header(HEADER_KUBERNETES_CLUSTER);
const clusterName = req.headers[HEADER_KUBERNETES_CLUSTER.toLowerCase()];
const clusters = await this.clusterSupplier.getClusters();
if (!clusters || clusters.length <= 0) {