adlApiClient.ts 1.68 KB
Newer Older
1
2
3
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

4
import fs from 'fs';
5
6
7
8
9
10
11
12
13
import { GeneralK8sClient, KubernetesCRDClient } from '../kubernetesApiClient';

/**
 * Adl ClientV1
 */
class AdlClientV1 extends KubernetesCRDClient {
    /**
     * constructor, to initialize adl CRD definition
     */
J-shang's avatar
J-shang committed
14
    public readonly namespace: string;
15
16

    public constructor(namespace: string) {
17
        super();
18
        this.namespace = namespace;
19
20
21
22
23
        this.crdSchema = JSON.parse(fs.readFileSync('./config/adl/adaptdl-crd-v1.json', 'utf8'));
        this.client.addCustomResourceDefinition(this.crdSchema);
    }

    protected get operator(): any {
24
        return this.client.apis['adaptdl.petuum.com'].v1.namespaces(this.namespace).adaptdljobs;
25
26
27
28
29
30
31
32
    }

    public get containerName(): string {
        return 'main';
    }

    public async getKubernetesPods(jobName: string): Promise<any> {
        let result: Promise<any>;
33
        const response = await this.client.api.v1.namespaces(this.namespace).pods
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
            .get({ qs: { labelSelector: `adaptdl/job=${jobName}` } });
        if (response.statusCode && (response.statusCode >= 200 && response.statusCode <= 299)) {
            result = Promise.resolve(response.body);
        } else {
            result = Promise.reject(`AdlClient getKubernetesPods failed, statusCode is ${response.statusCode}`);
        }
        return result;
    }
}

/**
 * Adl Client
 */
class AdlClientFactory {
    /**
     * Factory method to generate operator client
     */
51
52
    public static createClient(namespace: string): KubernetesCRDClient {
        return new AdlClientV1(namespace);
53
54
55
56
57
    }
}

export { AdlClientFactory, GeneralK8sClient };
export { AdlClientV1 }