adlApiClient.ts 1.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

'use strict';

import * as fs from 'fs';
import { GeneralK8sClient, KubernetesCRDClient } from '../kubernetesApiClient';

/**
 * Adl ClientV1
 */
class AdlClientV1 extends KubernetesCRDClient {
    /**
     * constructor, to initialize adl CRD definition
     */
16
17
18
    protected readonly namespace: string;

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

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

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

    public async getKubernetesPods(jobName: string): Promise<any> {
        let result: Promise<any>;
35
        const response = await this.client.api.v1.namespaces(this.namespace).pods
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
            .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
     */
53
54
    public static createClient(namespace: string): KubernetesCRDClient {
        return new AdlClientV1(namespace);
55
56
57
58
59
    }
}

export { AdlClientFactory, GeneralK8sClient };
export { AdlClientV1 }