log stream from k8s http api
parent
1d4028c426
commit
e75427a6be
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
|
|
@ -2228,6 +2228,32 @@ 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
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,16 @@
|
||||||
// @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;
|
||||||
|
|
@ -37,6 +41,7 @@ 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;
|
||||||
|
|
@ -318,6 +323,48 @@ 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()) || {};
|
||||||
|
|
||||||
|
|
@ -342,7 +389,12 @@ class Kubernetes {
|
||||||
);
|
);
|
||||||
let logs;
|
let logs;
|
||||||
try {
|
try {
|
||||||
logs = await this.kubeClient.readNamespacedPodLog(pod.metadata?.name || '', this.namespace, undefined, true);
|
logs = await Kubernetes.getLogs(
|
||||||
|
this.kubeConfig,
|
||||||
|
this.namespace,
|
||||||
|
pod.metadata?.name || '',
|
||||||
|
pod.status?.containerStatuses?.[0].name || '',
|
||||||
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
core.error(error);
|
core.error(error);
|
||||||
throw error;
|
throw error;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue