Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function newRunnerPod(
podname: string,
brigadeImage: string,
imageForcePull: boolean,
serviceAccount: string,
resourceRequests: jobs.JobResourceRequest,
resourceLimits: jobs.JobResourceLimit,
jobAnnotations: { [key: string]: string },
jobShell: string
): kubernetes.V1Pod {
let pod = new kubernetes.V1Pod();
pod.metadata = new kubernetes.V1ObjectMeta();
pod.metadata.name = podname;
pod.metadata.labels = {
heritage: "brigade",
component: "job"
};
pod.metadata.annotations = jobAnnotations;
let c1 = new kubernetes.V1Container();
c1.name = "brigaderun";
c1.image = brigadeImage;
if (jobShell == "") {
jobShell = "/bin/sh";
}
c1.command = [jobShell, "/hook/main.sh"];
async createAdminRoleBinding(name = '', serviceAccount = '', namespace = '') {
const k8sRbacAuthApi = this.kc.makeApiClient(RbacAuthorizationV1Api)
let rb = new V1RoleBinding()
rb.metadata = new V1ObjectMeta()
rb.metadata.name = name
rb.metadata.namespace = namespace
rb.roleRef = new V1RoleRef()
rb.roleRef.kind = 'ClusterRole'
rb.roleRef.name = 'admin'
let subject = new V1Subject()
subject.kind = 'ServiceAccount'
subject.name = serviceAccount
subject.namespace = namespace
rb.subjects = [subject]
try {
return await k8sRbacAuthApi.createNamespacedRoleBinding(namespace, rb)
} catch (e) {
throw this.wrapK8sClientError(e)
}
}
protected buildPVC(size: string): kubernetes.V1PersistentVolumeClaim {
let s = new kubernetes.V1PersistentVolumeClaim();
s.metadata = new kubernetes.V1ObjectMeta();
s.metadata.name = this.name;
s.metadata.labels = {
heritage: "brigade",
component: "buildStorage",
project: this.proj.id,
worker: this.name,
build: this.build
};
s.spec = new kubernetes.V1PersistentVolumeClaimSpec();
s.spec.accessModes = ["ReadWriteMany"];
let res = new kubernetes.V1ResourceRequirements();
res.requests = { storage: size };
s.spec.resources = res;
if (this.proj.kubernetes.buildStorageClass.length > 0) {
function newSecret(name: string): kubernetes.V1Secret {
let s = new kubernetes.V1Secret();
s.type = "brigade.sh/job";
s.metadata = new kubernetes.V1ObjectMeta();
s.metadata.name = name;
s.metadata.labels = {
heritage: "brigade",
component: "job"
};
s.data = {}; //{"main.sh": b64enc("echo hello && echo goodbye")}
return s;
}
async createServiceAccount(name = '', namespace = '') {
const k8sCoreApi = this.kc.makeApiClient(CoreV1Api)
let sa = new V1ServiceAccount()
sa.metadata = new V1ObjectMeta()
sa.metadata.name = name
sa.metadata.namespace = namespace
try {
return await k8sCoreApi.createNamespacedServiceAccount(namespace, sa)
} catch (e) {
throw this.wrapK8sClientError(e)
}
}
async createDeployment(name: string,
image: string,
serviceAccount: string,
pullPolicy: string,
configMapEnvSource: string,
namespace: string) {
const k8sAppsApi = this.kc.makeApiClient(AppsV1Api)
let deployment = new V1Deployment()
deployment.metadata = new V1ObjectMeta()
deployment.metadata.name = name
deployment.metadata.namespace = namespace
deployment.spec = new V1DeploymentSpec()
deployment.spec.selector = new V1LabelSelector()
deployment.spec.selector.matchLabels = { app: name }
deployment.spec.template = new V1PodTemplateSpec()
deployment.spec.template.metadata = new V1ObjectMeta()
deployment.spec.template.metadata.name = name
deployment.spec.template.metadata.labels = { app: name }
deployment.spec.template.spec = new V1PodSpec()
deployment.spec.template.spec.serviceAccountName = serviceAccount
let opContainer = new V1Container()
opContainer.name = name
opContainer.image = image
opContainer.imagePullPolicy = pullPolicy
let envFromSource = new V1EnvFromSource()
envFromSource.configMapRef = new V1ConfigMapEnvSource()
envFromSource.configMapRef.name = configMapEnvSource
opContainer.envFrom = [envFromSource]
deployment.spec.template.spec.containers = [opContainer]
try {
return await k8sAppsApi.createNamespacedDeployment(namespace, deployment)
k8sDeployList.items.forEach(async item => {
let deployment = new V1Deployment()
deployment.apiVersion = 'apps/v1'
deployment.kind = 'Deployment'
deployment.metadata = new V1ObjectMeta()
deployment.metadata.labels = { ...item.metadata!.labels }
deployment.metadata.name = item.metadata!.name
deployment.spec = new V1DeploymentSpec()
deployment.spec.selector = item.spec!.selector
deployment.spec.template = new V1PodTemplateSpec()
deployment.spec.template.metadata = new V1ObjectMeta()
deployment.spec.template.metadata.labels = { ...item.spec!.template.metadata!.labels }
deployment.spec.template.metadata.name = item.spec!.template.metadata!.name
deployment.spec.template.spec = item.spec!.template.spec
await items.push(deployment)
})
async function createNamespace(api: KubernetesApi, name: string): Promise {
try {
logger.info(`Create preview namespace "${name}".`);
await api.core.readNamespace(name);
logger.info(`Namespace "${name}" already exists.`);
} catch (e) {
if (e.response.statusCode !== 404) {
throw e;
}
await api.core.createNamespace({
...new V1Namespace(),
apiVersion: 'v1',
kind: 'Namespace',
metadata: {
...new V1ObjectMeta(),
name,
},
});
logger.success(`Created namespace "${name}".`);
}
}