log stream from k8s http api

pull/265/head
Frostebite 2021-05-24 21:54:41 +01:00
parent 5dab3928b9
commit cc45b2d8bf
4 changed files with 51 additions and 1820 deletions

1751
dist/index.js vendored

File diff suppressed because it is too large Load Diff

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

26
dist/licenses.txt vendored
View File

@ -2228,32 +2228,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. THE SOFTWARE.
node-fetch
MIT
The MIT License (MIT)
Copyright (c) 2016 David Frank
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
normalize-url normalize-url
MIT MIT
MIT License MIT License

View File

@ -1,16 +1,12 @@
// @ts-ignore // @ts-ignore
import * as k8s from '@kubernetes/client-node'; import * as k8s from '@kubernetes/client-node';
import { BuildParameters } from '.'; import { BuildParameters } from '.';
import fetch, { Response } from 'node-fetch';
import request from 'request';
const core = require('@actions/core'); const core = require('@actions/core');
const https = require('https');
const base64 = require('base-64'); const base64 = require('base-64');
const pollInterval = 10000; const pollInterval = 10000;
class Kubernetes { class Kubernetes {
private static kubeConfig: k8s.KubeConfig;
private static kubeClient: k8s.CoreV1Api; private static kubeClient: k8s.CoreV1Api;
private static kubeClientBatch: k8s.BatchV1Api; private static kubeClientBatch: k8s.BatchV1Api;
private static buildId: string; private static buildId: string;
@ -41,7 +37,6 @@ class Kubernetes {
const jobName = `unity-builder-job-${buildId}`; const jobName = `unity-builder-job-${buildId}`;
const namespace = 'default'; const namespace = 'default';
this.kubeConfig = kc;
this.kubeClient = k8sApi; this.kubeClient = k8sApi;
this.kubeClientBatch = k8sBatchApi; this.kubeClientBatch = k8sBatchApi;
this.buildId = buildId; this.buildId = buildId;
@ -323,48 +318,6 @@ class Kubernetes {
} }
} }
static async getLogs(
config: k8s.KubeConfig,
namespace: string,
podName: string,
container: string,
): Promise<Response> {
const cluster = config.getCurrentCluster();
if (!cluster) {
throw new Error('No current cluster found');
}
const server = cluster.server;
const parameters = new URLSearchParams({ container, follow: 'true' });
const url = `${server}/api/v1/namespaces/${encodeURIComponent(namespace)}/pods/${encodeURIComponent(
podName,
)}/log?${parameters}`;
const options = await Kubernetes.buildOptions(config, url);
return await fetch(url, options);
}
static async buildOptions(config: k8s.KubeConfig, url: string) {
const draftOptions: request.Options = {
url,
headers: {
Accept: 'application/json',
},
};
await config.applyToRequest(draftOptions);
const { headers, ca, key, cert } = draftOptions;
// fetch has a different parameter handling for ssl than request lib, this i
const httpsAgent = new https.Agent({
cert,
ca,
key,
});
return {
agent: httpsAgent,
headers,
};
}
static async watchBuildJobUntilFinished() { static async watchBuildJobUntilFinished() {
const pod = (await Kubernetes.watchPodUntilReadyAndRead()) || {}; const pod = (await Kubernetes.watchPodUntilReadyAndRead()) || {};
@ -375,7 +328,6 @@ class Kubernetes {
4, 4,
)}`, )}`,
); );
core.info( core.info(
JSON.stringify( JSON.stringify(
{ {
@ -387,31 +339,35 @@ class Kubernetes {
4, 4,
), ),
); );
let logs;
try { try {
logs = await Kubernetes.getLogs(this.kubeConfig, this.namespace, pod.metadata?.name || '', ''); await new Promise((resolve, reject) => {
this.kubeClient
.readNamespacedPodLog(pod.metadata?.name || '', this.namespace, undefined, true)
// eslint-disable-next-line github/no-then
.then((logs) => {
try {
logs.response.on('data', (chunk) => {
core.info(chunk.toString());
});
logs.response.on('close', resolve);
logs.response.on('error', reject);
logs.response.on('end', resolve);
} catch (error) {
core.error(error);
throw error;
}
})
// eslint-disable-next-line github/no-then
.catch((error) => {
reject(error);
});
});
core.info('opening log stream');
} catch (error) { } catch (error) {
core.error(error); core.error(error);
throw error; throw error;
} }
core.info(JSON.stringify(logs, undefined, 4));
core.info('opening log stream');
await new Promise((resolve, reject) => {
try {
logs.response.on('data', (chunk) => {
core.info(chunk.toString());
});
logs.response.on('close', resolve);
logs.response.on('error', reject);
logs.response.on('end', resolve);
} catch (error) {
core.error(error);
throw error;
}
});
} }
static async cleanup() { static async cleanup() {