Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ACTIONS_RUNNER_INJECT_RUNNER_SERVICE_ACCOUNT environment variable #186

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/k8s/src/hooks/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export function getSecretName(): string {
)}-secret-${uuidv4().substring(0, STEP_POD_NAME_SUFFIX_LENGTH)}`
}

export function shouldInjectRunnerServiceAccount(): boolean {
return process.env.ACTIONS_RUNNER_INJECT_RUNNER_SERVICE_ACCOUNT === 'true'
}

export const MAX_POD_NAME_LENGTH = 63
export const STEP_POD_NAME_SUFFIX_LENGTH = 8
export const CONTAINER_EXTENSION_PREFIX = '$'
Expand Down
11 changes: 9 additions & 2 deletions packages/k8s/src/k8s/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import {
getSecretName,
getStepPodName,
getVolumeClaimName,
RunnerInstanceLabel
RunnerInstanceLabel,
shouldInjectRunnerServiceAccount
} from '../hooks/constants'
import {
PodPhase,
mergePodSpecWithOptions,
mergeObjectMeta,
useKubeScheduler,
fixArgs
fixArgs,
getCurrentServiceAccountName
} from './utils'

const kc = new k8s.KubeConfig()
Expand Down Expand Up @@ -125,6 +127,10 @@ export async function createPod(
mergePodSpecWithOptions(appPod.spec, extension.spec)
}

if (shouldInjectRunnerServiceAccount()) {
appPod.spec.serviceAccountName = await getCurrentServiceAccountName(k8sApi)
}

const { body } = await k8sApi.createNamespacedPod(namespace(), appPod)
return body
}
Expand Down Expand Up @@ -557,6 +563,7 @@ export function namespace(): string {
class BackOffManager {
private backOffSeconds = 1
totalTime = 0

constructor(private throwAfterSeconds?: number) {
if (!throwAfterSeconds || throwAfterSeconds < 0) {
this.throwAfterSeconds = undefined
Expand Down
21 changes: 19 additions & 2 deletions packages/k8s/src/k8s/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import * as core from '@actions/core'
import { Mount } from 'hooklib'
import * as path from 'path'
import { v1 as uuidv4 } from 'uuid'
import { POD_VOLUME_NAME } from './index'
import { CONTAINER_EXTENSION_PREFIX } from '../hooks/constants'
import { namespace, POD_VOLUME_NAME } from './index'
import {
CONTAINER_EXTENSION_PREFIX,
getRunnerPodName
} from '../hooks/constants'
import * as shlex from 'shlex'

export const DEFAULT_CONTAINER_ENTRY_POINT_ARGS = [`-f`, `/dev/null`]
Expand Down Expand Up @@ -294,3 +297,17 @@ function mergeLists<T>(base?: T[], from?: T[]): T[] {
export function fixArgs(args: string[]): string[] {
return shlex.split(args.join(' '))
}

export async function getCurrentServiceAccountName(
kubernetesApiClient: k8s.CoreV1Api
): Promise<string | undefined> {
try {
const { body } = await kubernetesApiClient.readNamespacedPod(
getRunnerPodName(),
namespace()
)
return body.spec?.serviceAccountName
} catch (e) {
core.error(e as Error)
}
}