Fix bug with CloudRunner and K8s with Namespaces (#763)

* Fixes bug where kubectl picks a different namespace (e.g. cloud runner is kicked from self hosted k8s agents that are in a non default namespace)

* update generated content

* Add support for setting a namespace for containers in Cloud Runner
This commit is contained in:
harry8525
2025-12-04 14:47:45 -08:00
committed by GitHub
parent 1d4ee0697f
commit 0c82a58873
6 changed files with 17 additions and 9 deletions

View File

@@ -59,6 +59,7 @@ class BuildParameters {
public kubeConfig!: string;
public containerMemory!: string;
public containerCpu!: string;
public containerNamespace!: string;
public kubeVolumeSize!: string;
public kubeVolume!: string;
public kubeStorageClass!: string;
@@ -187,6 +188,7 @@ class BuildParameters {
kubeConfig: CloudRunnerOptions.kubeConfig,
containerMemory: CloudRunnerOptions.containerMemory,
containerCpu: CloudRunnerOptions.containerCpu,
containerNamespace: CloudRunnerOptions.containerNamespace,
kubeVolumeSize: CloudRunnerOptions.kubeVolumeSize,
kubeVolume: CloudRunnerOptions.kubeVolume,
postBuildContainerHooks: CloudRunnerOptions.postBuildContainerHooks,

View File

@@ -135,6 +135,10 @@ class CloudRunnerOptions {
return CloudRunnerOptions.getInput('containerMemory') || `3072`;
}
static get containerNamespace(): string {
return CloudRunnerOptions.getInput('containerNamespace') || `default`;
}
static get customJob(): string {
return CloudRunnerOptions.getInput('customJob') || '';
}

View File

@@ -37,7 +37,6 @@ class Kubernetes implements ProviderInterface {
public serviceAccountName: string = '';
public ip: string = '';
// eslint-disable-next-line no-unused-vars
constructor(buildParameters: BuildParameters) {
Kubernetes.Instance = this;
this.kubeConfig = new k8s.KubeConfig();
@@ -46,7 +45,7 @@ class Kubernetes implements ProviderInterface {
this.kubeClientApps = this.kubeConfig.makeApiClient(k8s.AppsV1Api);
this.kubeClientBatch = this.kubeConfig.makeApiClient(k8s.BatchV1Api);
this.rbacAuthorizationV1Api = this.kubeConfig.makeApiClient(k8s.RbacAuthorizationV1Api);
this.namespace = 'default';
this.namespace = buildParameters.containerNamespace ? buildParameters.containerNamespace : 'default';
CloudRunnerLogger.log('Loaded default Kubernetes configuration for this environment');
}

View File

@@ -30,8 +30,8 @@ class KubernetesTaskRunner {
);
let extraFlags = ``;
extraFlags += (await KubernetesPods.IsPodRunning(podName, namespace, kubeClient))
? ` -f -c ${containerName}`
: ` --previous`;
? ` -f -c ${containerName} -n ${namespace}`
: ` --previous -n ${namespace}`;
const callback = (outputChunk: string) => {
output += outputChunk;