Skip to content

Commit

Permalink
extend k8s resources to TS resources that have required fields
Browse files Browse the repository at this point in the history
  • Loading branch information
busma13 committed Nov 6, 2024
1 parent 839db27 commit 4c6ab40
Show file tree
Hide file tree
Showing 14 changed files with 706 additions and 531 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class KubernetesClusterBackendV2 {
*/
private async _getClusterState() {
return this.k8s.list(`app.kubernetes.io/name=teraslice,app.kubernetes.io/instance=${this.clusterNameLabel}`, 'pods')
.then((k8sPods) => gen(k8sPods, this.clusterState, this.logger))
.then((tsPodList) => gen(tsPodList, this.clusterState))
.catch((err) => {
// TODO: We might need to do more here. I think it's OK to just
// log though. This only gets used to show slicer info through
Expand Down Expand Up @@ -123,17 +123,6 @@ export class KubernetesClusterBackendV2 {

const jobResult = await this.k8s.post(exJob);

// fixme
if (!jobResult.metadata) {
throw new Error('Required field metadata missing from jobResult');
}
if (!jobResult.metadata.name) {
throw new Error('Required field name missing from jobResult.metadata');
}
if (!jobResult.metadata.uid) {
throw new Error('Required field uid missing from jobResult.metadata');
}

const exServiceResource = new K8sServiceResource(
this.context.sysconfig.teraslice,
execution,
Expand All @@ -150,15 +139,15 @@ export class KubernetesClusterBackendV2 {
this.logger.debug(jobResult, 'k8s slicer job submitted');

let controllerLabel: string;
if (jobResult.spec?.selector?.matchLabels?.['controller-uid'] !== undefined) {
if (jobResult.spec.selector.matchLabels['controller-uid'] !== undefined) {
/// If running on kubernetes < v1.27.0
controllerLabel = 'controller-uid';
} else {
/// If running on kubernetes v1.27.0 or later
controllerLabel = 'batch.kubernetes.io/controller-uid';
}

const controllerUid = jobResult.spec?.selector?.matchLabels?.[controllerLabel];
const controllerUid = jobResult.spec.selector.matchLabels[controllerLabel];

const pod = await this.k8s.waitForSelectedPod(
`${controllerLabel}=${controllerUid}`,
Expand All @@ -171,7 +160,7 @@ export class KubernetesClusterBackendV2 {
const error = new Error('pod.status.podIP must be defined');
return Promise.reject(error);
}
const exServiceName = serviceResult.metadata?.name;
const exServiceName = serviceResult.metadata.name;
const exServiceHostName = `${exServiceName}.${this.k8s.defaultNamespace}`;
this.logger.debug(`Slicer is using host name: ${exServiceHostName}`);

Expand Down Expand Up @@ -204,23 +193,12 @@ export class KubernetesClusterBackendV2 {
this.context.sysconfig.teraslice.slicer_timeout
);

// fixme
if (!jobs.items[0].metadata) {
throw new Error('Required field metadata missing from jobResult');
}
if (!jobs.items[0].metadata.name) {
throw new Error('Required field name missing from jobResult.metadata');
}
if (!jobs.items[0].metadata.uid) {
throw new Error('Required field uid missing from jobResult.metadata');
}

const kr = new K8sDeploymentResource(
this.context.sysconfig.teraslice,
execution,
this.logger,
jobs.items[0].metadata?.name,
jobs.items[0].metadata?.uid
jobs.items[0].metadata.name,
jobs.items[0].metadata.uid
);

const workerDeployment = kr.resource;
Expand Down Expand Up @@ -277,8 +255,8 @@ export class KubernetesClusterBackendV2 {
/**
* Returns a list of all k8s resources associated with a job ID
* @param {string} jobId The job ID of the job to list associated resources
* @returns {Array<K8sClient.V1PodList | K8sClient.V1DeploymentList | K8sClient.V1ServiceList
* | K8sClient.V1JobList | K8sClient.V1ReplicaSetList>}
* @returns {Array<TSPod[] | TSDeployment[] | TSService[]
* | TSJob[] | TSReplicaSet[]>}
*/
async listResourcesForJobId(jobId: string) {
const resources = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ export interface KubeConfigOptions {
users: k8s.User[];
}

export type K8sObjectList =
k8s.V1DeploymentList | k8s.V1ServiceList
| k8s.V1JobList | k8s.V1PodList | k8s.V1ReplicaSetList;

export interface K8sConfig {
clusterName: string;
clusterNameLabel: string;
Expand All @@ -32,19 +28,135 @@ export interface K8sConfig {

export type ResourceType = 'deployments' | 'jobs' | 'pods' | 'replicasets' | 'services';

export type ResourceList = k8s.V1DeploymentList | k8s.V1JobList
export type K8sResource = k8s.V1Deployment | k8s.V1Job
| k8s.V1Pod | k8s.V1ReplicaSet | k8s.V1Service;

export type TSResource = TSDeployment | TSJob | TSPod | TSReplicaSet | TSService;

export interface TSDeployment extends k8s.V1Deployment {
kind: NonNullable<string>;
metadata: NonNullable<k8s.V1ObjectMeta> & {
labels: {
[key: string]: string;
};
name: string;
};
spec: NonNullable<k8s.V1DeploymentSpec> & {
replicas: NonNullable<number>;
template: NonNullable<k8s.V1PodTemplateSpec> & {
metadata: NonNullable<k8s.V1ObjectMeta> & {
labels: {
[key: string]: string;
};
};
spec: NonNullable<k8s.V1PodSpec> & {
containers: k8s.V1Container[] & {
ports: NonNullable<k8s.V1ContainerPort[]>;
volumeMounts: [k8s.V1VolumeMount, ...k8s.V1VolumeMount[]];
}[];
volumes: NonNullable<k8s.V1Volume[]>;
};
};
};
}

export interface TSJob extends k8s.V1Job {
kind: NonNullable<string>;
metadata: NonNullable<k8s.V1ObjectMeta> & {
labels: {
[key: string]: string;
};
name: string;
uid: string;
};
spec: NonNullable<k8s.V1JobSpec> & {
template: k8s.V1PodTemplateSpec & {
metadata: NonNullable<k8s.V1ObjectMeta> & {
labels: {
[key: string]: string;
};
};
spec: NonNullable<k8s.V1PodSpec> & {
containers: k8s.V1Container[] & {
ports: NonNullable<k8s.V1ContainerPort[]>;
volumeMounts: [k8s.V1VolumeMount, ...k8s.V1VolumeMount[]];
}[];
volumes: NonNullable<k8s.V1Volume>;
};
};
selector: NonNullable<k8s.V1LabelSelector> & {
matchLabels: {
[key: string]: string;
};
};
};
}

export interface TSPod extends k8s.V1Pod {
kind: NonNullable<string>;
metadata: NonNullable<k8s.V1ObjectMeta> & {
labels: {
[key: string]: string;
};
name: string;
};
spec: NonNullable<k8s.V1PodSpec>;
status: NonNullable<k8s.V1PodStatus> & {
hostIP: 'string';
};
}

export interface TSReplicaSet extends k8s.V1ReplicaSet {
kind: NonNullable<string>;
metadata: NonNullable<k8s.V1ObjectMeta> & {
name: string;
};
status: NonNullable<k8s.V1ReplicaSetStatus>;
}

export interface TSService extends k8s.V1Service {
kind: NonNullable<string>;
metadata: NonNullable<k8s.V1ObjectMeta> & {
name: string;
};
spec: NonNullable<k8s.V1ServiceSpec> & {
selector: {
[key: string]: string;
};
ports: NonNullable<k8s.V1ServicePort[]>;
};
}

export type K8sResourceList = k8s.V1DeploymentList | k8s.V1JobList
| k8s.V1PodList | k8s.V1ReplicaSetList | k8s.V1ServiceList;

export type Resource = k8s.V1Deployment | k8s.V1Job | k8s.V1Pod | k8s.V1ReplicaSet | k8s.V1Service;
export type TSResourceList = TSDeploymentList | TSJobList
| TSPodList | TSReplicaSetList | TSServiceList;

export interface TSDeploymentList extends k8s.V1DeploymentList {
items: TSDeployment[];
}
export interface TSJobList extends k8s.V1JobList {
items: TSJob[];
}
export interface TSPodList extends k8s.V1PodList {
items: TSPod[];
}
export interface TSReplicaSetList extends k8s.V1ReplicaSetList {
items: TSReplicaSet[];
}
export interface TSServiceList extends k8s.V1ServiceList {
items: TSService[];
}

export interface ResourceListApiResponse {
response: IncomingMessage;
body: ResourceList;
body: K8sResourceList;
}

export interface ResourceApiResponse {
response: IncomingMessage;
body: Resource;
body: K8sResource;
}

export interface PatchApiResponse {
Expand Down
Loading

0 comments on commit 4c6ab40

Please sign in to comment.