UNPKG

@atomist/sdm

Version:

Atomist Software Delivery Machine SDK

151 lines 5.85 kB
import { Configuration } from "@atomist/automation-client/lib/configuration"; import { HandlerContext } from "@atomist/automation-client/lib/HandlerContext"; import * as k8s from "@kubernetes/client-node"; import { ExecuteGoalResult } from "../../../api/goal/ExecuteGoalResult"; import { GoalInvocation } from "../../../api/goal/GoalInvocation"; import { SdmGoalEvent } from "../../../api/goal/SdmGoalEvent"; import { GoalScheduler } from "../../../api/goal/support/GoalScheduler"; /** * Options to configure the Kubernetes goal scheduling support. */ export interface KubernetesGoalSchedulerOptions { /** * Set to `true` to run all goals as Kubernetes jobs if the * `ATOMIST_GOAL_SCHEDULER` environment variable is set to * "kubernetes". */ isolateAll?: boolean; /** * Pod spec to use as basis for job spec. This pod spec is deeply * merged with the running SDM pod spec, if available, with this * pod spec taking preference. If the running SDM pod spec is not * available and this is not provided, an error is thrown during * initialization. */ podSpec?: k8s.V1PodSpec; } /** * Return the configured Kubernetes job time-to-live, * `sdm.k8s.job.ttl` or, if that is not available, twice the value * returned by [[sdmGoalTimeout]]. */ export declare function k8sJobTtl(cfg?: Configuration): number; /** * GoalScheduler implementation that schedules SDM goals as Kubernetes * jobs. */ export declare class KubernetesGoalScheduler implements GoalScheduler { private readonly options; podSpec: k8s.V1PodSpec; constructor(options?: KubernetesGoalSchedulerOptions); supports(gi: GoalInvocation): Promise<boolean>; schedule(gi: GoalInvocation): Promise<ExecuteGoalResult>; /** * Extension point for sub classes to modify the provided jobSpec * before the Job gets created in k8s. It should return the * modified jobSpec. * @param gi goal invocation * @param jobSpec Default job spec * @return desired job spec */ protected beforeCreation(gi: GoalInvocation, jobSpec: k8s.V1Job): Promise<k8s.V1Job>; /** * Extension point for sub classes to modify k8s resources after the job has been created. * The provided jobSpec contains the result of the job creation API call. * @param gi * @param jobSpec */ protected afterCreation(gi: GoalInvocation, jobSpec: k8s.V1Job): Promise<void>; /** * If running in Kubernetes, read current pod spec. Populate * `this.podSpec` with a merge of `this.options.podSpec` and the * current pod spec. If neither is available, throw an error. */ initialize(configuration: Configuration): Promise<void>; /** * Extension point to allow for custom clean up logic. */ protected cleanUp(configuration: Configuration): Promise<void>; } /** * Return true for pods whose first container has terminated but at * least one other container has not. */ export declare function zombiePodFilter(pod: k8s.V1Pod): boolean; /** * Return true for jobs that have exceeded the TTL or whose child is * in the provided list of pods. Return false otherwise. */ export declare function killJobFilter(pods: k8s.V1Pod[], ttl: number): (j: k8s.V1Job) => boolean; /** Unique name for job to use in k8s job spec. */ export declare function k8sJobName(podSpec: k8s.V1PodSpec, goalEvent: SdmGoalEvent): string; /** * Kubernetes container spec environment variables that specify an SDM * running in single-goal mode. */ export declare function k8sJobEnv(podSpec: k8s.V1PodSpec, goalEvent: SdmGoalEvent, context: HandlerContext): k8s.V1EnvVar[]; /** * Create a jobSpec by modifying the provided podSpec * @param podSpec * @param podNs * @param gi */ export declare function createJobSpec(podSpec: k8s.V1PodSpec, podNs: string, gi: GoalInvocation): k8s.V1Job; /** * Checks if one of the provided values is configured in ATOMIST_GOAL_SCHEDULER or - * for backwards compatibility reasons - ATOMIST_GOAL_LAUNCHER. * @param values */ export declare function isConfiguredInEnv(...values: string[]): boolean; /** * Strip out any characters that aren't allowed a k8s label value * @param name */ export declare function sanitizeName(name: string): string; /** * Read the namespace from the following sources in order. It returns * the first truthy value found. * * 1. ATOMIST_POD_NAMESPACE environment variable * 2. ATOMIST_DEPLOYMENT_NAMESPACE environment variable * 3. Contents of [[K8sNamespaceFile]] * 4. "default" * * service account files. Falls back to the default namespace if no * other configuration can be found. */ export declare function readNamespace(): Promise<string>; /** * List Kubernetes jobs matching the provided label selector. Jobs * are listed across all namespaces if * `configuration.sdm.k8s.job.singleNamespace` is not set to `false`. * If that configuration value is not set or set to `true`, jobs are * listed from the namespace provide by [[readNamespace]]. * * @param labelSelector * @return array of Kubernetes jobs matching the label selector */ export declare function listJobs(labelSelector?: string): Promise<k8s.V1Job[]>; /** * Delete the provided job. Failures are ignored. */ export declare function deleteJob(job: { name: string; namespace: string; }): Promise<void>; /** * List Kubernetes pods matching the provided label selector. Jobs * are listed in a the current namespace or cluster-wide depending on * evn configuration * * @param labelSelector */ export declare function listPods(labelSelector?: string): Promise<k8s.V1Pod[]>; /** * Delete the provided pods. Failures are ignored. */ export declare function deletePods(job: { name: string; namespace: string; }): Promise<void>; //# sourceMappingURL=KubernetesGoalScheduler.d.ts.map