included generation of random handle/image

This commit is contained in:
Nikola Jokic
2022-09-21 15:29:39 +02:00
parent f400db92cc
commit c4aa97c974
4 changed files with 40 additions and 11 deletions

View File

@@ -1,4 +1,5 @@
import * as core from '@actions/core'
import { v4 as uuidv4 } from 'uuid'
import * as k8s from '@kubernetes/client-node'
import { RunContainerStepArgs } from 'hooklib'
import {
@@ -8,7 +9,8 @@ import {
getPodLogs,
getPodStatus,
waitForJobToComplete,
waitForPodPhases
waitForPodPhases,
containerBuild
} from '../k8s'
import {
containerVolumes,
@@ -17,12 +19,14 @@ import {
PodPhase,
writeEntryPointScript
} from '../k8s/utils'
import { JOB_CONTAINER_NAME } from './constants'
import { getRunnerPodName, JOB_CONTAINER_NAME } from './constants'
export async function runContainerStep(
stepContainer: RunContainerStepArgs
): Promise<number> {
if (stepContainer.dockerfile) {
const imagePath = `${generateRandomHandle()}/${generateBuildTag()}`
await containerBuild(stepContainer, imagePath)
throw new Error('Building container actions is not currently supported')
}
@@ -108,3 +112,16 @@ function createPodSpec(
return podContainer
}
function generateBuildTag(): string {
return `${getRunnerPodName}:${uuidv4().substring(0, 6)}`
}
function generateRandomHandle(length = 10): string {
let handle = ''
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
for (let i = 0; i < length; i++) {
handle += chars.charAt(Math.floor(Math.random() * length))
}
return handle
}

View File

@@ -1,6 +1,6 @@
import * as core from '@actions/core'
import * as k8s from '@kubernetes/client-node'
import { ContainerInfo, Registry } from 'hooklib'
import { RunContainerStepArgs, ContainerInfo, Registry } from 'hooklib'
import * as stream from 'stream'
import {
getJobPodName,
@@ -485,12 +485,15 @@ export async function isPodContainerAlpine(
return isAlpine
}
export async function buildContainer(): Promise<void> {
export async function containerBuild(
args: RunContainerStepArgs,
imagePath: string
): Promise<void> {
const cm = registryConfigMap()
const secret = registrySecret()
const ss = registryStatefulSet()
const svc = registryService()
const pod = kanikoPod()
const pod = kanikoPod(args.workingDirectory, imagePath)
await Promise.all([
k8sApi.createNamespacedConfigMap(namespace(), cm),
k8sApi.createNamespacedSecret(namespace(), secret)

View File

@@ -173,7 +173,10 @@ export function registryService(): k8s.V1Service {
return svc
}
export function kanikoPod(): k8s.V1Pod {
export function kanikoPod(
workingDirectory: string, // git://github.com/<handle>/<repo>
imagePath: string // <handle>/<image>:<tag>
): k8s.V1Pod {
const pod = new k8s.V1Pod()
pod.apiVersion = 'v1'
pod.kind = 'Pod'
@@ -193,8 +196,8 @@ export function kanikoPod(): k8s.V1Pod {
]
c.args = [
'--dockerfile=Dockerfile',
'--context=git://github.com/nikola-jokic/dockeraction.git',
'--destination=docker-registry.default.svc.cluster.local:5000/test/app:1.0'
`--context=${workingDirectory}`,
`--destination=docker-registry.default.svc.cluster.local:5000/${imagePath}`
]
spec.containers = [c]
spec.dnsPolicy = 'ClusterFirst'

View File

@@ -1,5 +1,4 @@
import { buildContainer } from '../src/k8s'
import { TestHelper } from './test-setup'
import { containerBuild } from '../src/k8s'
jest.useRealTimers()
@@ -9,6 +8,13 @@ describe('container build', () => {
})
it('should finish without throwing an exception', async () => {
await expect(buildContainer()).resolves.not.toThrow()
await expect(
containerBuild(
{
workingDirectory: 'git://github.com/nikola-jokic/dockeraction.git'
},
'randhandle/randimg:123123'
)
).resolves.not.toThrow()
})
})