Kubernetes refactoring

pull/273/head
Frostebite 2021-06-06 21:32:24 +01:00
parent 045c4217d1
commit 8f79ef98f8
4 changed files with 148 additions and 142 deletions

View File

@ -46,10 +46,6 @@ jobs:
########################### ###########################
# Build # # Build #
########################### ###########################
# - uses: frostebite/File-To-Base64@master
# id: read-base64
# with:
# filePath: ~/.kube/config
- uses: ./ - uses: ./
id: k8s-unity-build id: k8s-unity-build
with: with:

245
dist/index.js vendored
View File

@ -838,134 +838,137 @@ class Kubernetes {
core.info('Persistent Volume created, waiting for ready state...'); core.info('Persistent Volume created, waiting for ready state...');
}); });
} }
runJob(command, image) { getJobSpec(command, image) {
return __awaiter(this, void 0, void 0, function* () { const job = new k8s.V1Job();
core.info('Creating build job'); job.apiVersion = 'batch/v1';
const job = new k8s.V1Job(); job.kind = 'Job';
job.apiVersion = 'batch/v1'; job.metadata = {
job.kind = 'Job'; name: this.jobName,
job.metadata = { labels: {
name: this.jobName, app: 'unity-builder',
labels: { },
app: 'unity-builder', };
}, job.spec = {
}; template: {
job.spec = { spec: {
template: { volumes: [
spec: { {
volumes: [ name: 'data',
{ persistentVolumeClaim: {
name: 'data', claimName: this.pvcName,
persistentVolumeClaim: { },
claimName: this.pvcName, },
{
name: 'credentials',
secret: {
secretName: this.secretName,
},
},
],
containers: [
{
name: 'main',
image,
command,
resources: {
requests: {
memory: this.buildParameters.remoteBuildMemory,
cpu: this.buildParameters.remoteBuildCpu,
}, },
}, },
{ env: [
name: 'credentials', {
secret: { name: 'GITHUB_SHA',
secretName: this.secretName, value: this.buildId,
}, },
}, {
], name: 'GITHUB_WORKSPACE',
containers: [ value: '/data/repo',
{
name: 'main',
image,
command,
resources: {
requests: {
memory: this.buildParameters.remoteBuildMemory,
cpu: this.buildParameters.remoteBuildCpu,
},
}, },
env: [ {
{ name: 'PROJECT_PATH',
name: 'GITHUB_SHA', value: this.buildParameters.projectPath,
value: this.buildId, },
}, {
{ name: 'BUILD_PATH',
name: 'GITHUB_WORKSPACE', value: this.buildParameters.buildPath,
value: '/data/repo', },
}, {
{ name: 'BUILD_FILE',
name: 'PROJECT_PATH', value: this.buildParameters.buildFile,
value: this.buildParameters.projectPath, },
}, {
{ name: 'BUILD_NAME',
name: 'BUILD_PATH', value: this.buildParameters.buildName,
value: this.buildParameters.buildPath, },
}, {
{ name: 'BUILD_METHOD',
name: 'BUILD_FILE', value: this.buildParameters.buildMethod,
value: this.buildParameters.buildFile, },
}, {
{ name: 'CUSTOM_PARAMETERS',
name: 'BUILD_NAME', value: this.buildParameters.customParameters,
value: this.buildParameters.buildName, },
}, {
{ name: 'CHOWN_FILES_TO',
name: 'BUILD_METHOD', value: this.buildParameters.chownFilesTo,
value: this.buildParameters.buildMethod, },
}, {
{ name: 'BUILD_TARGET',
name: 'CUSTOM_PARAMETERS', value: this.buildParameters.platform,
value: this.buildParameters.customParameters, },
}, {
{ name: 'ANDROID_VERSION_CODE',
name: 'CHOWN_FILES_TO', value: this.buildParameters.androidVersionCode.toString(),
value: this.buildParameters.chownFilesTo, },
}, {
{ name: 'ANDROID_KEYSTORE_NAME',
name: 'BUILD_TARGET', value: this.buildParameters.androidKeystoreName,
value: this.buildParameters.platform, },
}, {
{ name: 'ANDROID_KEYALIAS_NAME',
name: 'ANDROID_VERSION_CODE', value: this.buildParameters.androidKeyaliasName,
value: this.buildParameters.androidVersionCode.toString(), },
}, ],
{ volumeMounts: [
name: 'ANDROID_KEYSTORE_NAME', {
value: this.buildParameters.androidKeystoreName, name: 'data',
}, mountPath: '/data',
{ },
name: 'ANDROID_KEYALIAS_NAME', {
value: this.buildParameters.androidKeyaliasName, name: 'credentials',
}, mountPath: '/credentials',
], readOnly: true,
volumeMounts: [ },
{ ],
name: 'data', lifecycle: {
mountPath: '/data', preStop: {
}, exec: {
{ command: [
name: 'credentials', 'bin/bash',
mountPath: '/credentials', '-c',
readOnly: true, `cd /data/builder/action/steps;
},
],
lifecycle: {
preStop: {
exec: {
command: [
'bin/bash',
'-c',
`cd /data/builder/action/steps;
chmod +x /return_license.sh; chmod +x /return_license.sh;
/return_license.sh;`, /return_license.sh;`,
], ],
},
}, },
}, },
}, },
], },
restartPolicy: 'Never', ],
}, restartPolicy: 'Never',
}, },
}; },
job.spec.backoffLimit = 1; };
yield this.kubeClientBatch.createNamespacedJob(this.namespace, job); job.spec.backoffLimit = 1;
core.info('Job created'); return job;
}
runJob(jobSpec) {
return __awaiter(this, void 0, void 0, function* () {
try { try {
core.info('Creating build job');
yield this.kubeClientBatch.createNamespacedJob(this.namespace, jobSpec);
core.info('Job created');
// We watch the PVC first to allow some time for K8s to notice the job we created and setup a pod. // We watch the PVC first to allow some time for K8s to notice the job we created and setup a pod.
yield this.watchPersistentVolumeClaimUntilReady(); yield this.watchPersistentVolumeClaimUntilReady();
// TODO: Wait for something more reliable so we don't potentially get the pod before k8s has created it based on the job. // TODO: Wait for something more reliable so we don't potentially get the pod before k8s has created it based on the job.
@ -994,7 +997,7 @@ class Kubernetes {
} }
runCloneJob() { runCloneJob() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
yield this.runJob([ yield this.runJob(this.getJobSpec([
'/bin/ash', '/bin/ash',
'-c', '-c',
`apk update; `apk update;
@ -1008,12 +1011,12 @@ class Kubernetes {
git checkout $GITHUB_SHA; git checkout $GITHUB_SHA;
ls ls
echo "end"`, echo "end"`,
], 'alpine/git'); ], 'alpine/git'));
}); });
} }
runBuildJob() { runBuildJob() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
yield this.runJob([ yield this.runJob(this.getJobSpec([
'bin/bash', 'bin/bash',
'-c', '-c',
`ls `ls
@ -1028,7 +1031,7 @@ class Kubernetes {
chmod -R +x /steps chmod -R +x /steps
/entrypoint.sh /entrypoint.sh
`, `,
], this.baseImage.toString()); ], this.baseImage.toString()));
}); });
} }
watchPersistentVolumeClaimUntilReady() { watchPersistentVolumeClaimUntilReady() {

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View File

@ -125,8 +125,7 @@ class Kubernetes implements RemoteBuilderProviderInterface {
core.info('Persistent Volume created, waiting for ready state...'); core.info('Persistent Volume created, waiting for ready state...');
} }
async runJob(command: string[], image: string) { getJobSpec(command: string[], image: string) {
core.info('Creating build job');
const job = new k8s.V1Job(); const job = new k8s.V1Job();
job.apiVersion = 'batch/v1'; job.apiVersion = 'batch/v1';
job.kind = 'Job'; job.kind = 'Job';
@ -249,10 +248,14 @@ class Kubernetes implements RemoteBuilderProviderInterface {
}, },
}; };
job.spec.backoffLimit = 1; job.spec.backoffLimit = 1;
await this.kubeClientBatch.createNamespacedJob(this.namespace, job); return job;
core.info('Job created'); }
async runJob(jobSpec: k8s.V1Job) {
try { try {
core.info('Creating build job');
await this.kubeClientBatch.createNamespacedJob(this.namespace, jobSpec);
core.info('Job created');
// We watch the PVC first to allow some time for K8s to notice the job we created and setup a pod. // We watch the PVC first to allow some time for K8s to notice the job we created and setup a pod.
await this.watchPersistentVolumeClaimUntilReady(); await this.watchPersistentVolumeClaimUntilReady();
@ -281,10 +284,11 @@ class Kubernetes implements RemoteBuilderProviderInterface {
async runCloneJob() { async runCloneJob() {
await this.runJob( await this.runJob(
[ this.getJobSpec(
'/bin/ash', [
'-c', '/bin/ash',
`apk update; '-c',
`apk update;
apk add git-lfs; apk add git-lfs;
ls /credentials/ ls /credentials/
export GITHUB_TOKEN=$(cat /credentials/GITHUB_TOKEN); export GITHUB_TOKEN=$(cat /credentials/GITHUB_TOKEN);
@ -295,17 +299,19 @@ class Kubernetes implements RemoteBuilderProviderInterface {
git checkout $GITHUB_SHA; git checkout $GITHUB_SHA;
ls ls
echo "end"`, echo "end"`,
], ],
'alpine/git', 'alpine/git',
),
); );
} }
async runBuildJob() { async runBuildJob() {
await this.runJob( await this.runJob(
[ this.getJobSpec(
'bin/bash', [
'-c', 'bin/bash',
`ls '-c',
`ls
for f in ./credentials/*; do export $(basename $f)="$(cat $f)"; done for f in ./credentials/*; do export $(basename $f)="$(cat $f)"; done
ls /data ls /data
ls /data/builder ls /data/builder
@ -317,8 +323,9 @@ class Kubernetes implements RemoteBuilderProviderInterface {
chmod -R +x /steps chmod -R +x /steps
/entrypoint.sh /entrypoint.sh
`, `,
], ],
this.baseImage.toString(), this.baseImage.toString(),
),
); );
} }