kubernetesApiClient.ts 4.38 KB
Newer Older
liuzhe-lz's avatar
liuzhe-lz committed
1
2
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
3
4
5

'use strict';

chicm-ms's avatar
chicm-ms committed
6
// eslint-disable-next-line @typescript-eslint/camelcase
7
import { Client1_10, config } from 'kubernetes-client';
8
9
10
11
12
13
14
15
16
17
import { getLogger, Logger } from '../../common/log';

/**
 * Generict Kubernetes client, target version >= 1.9
 */
class GeneralK8sClient {
    protected readonly client: any;
    protected readonly log: Logger = getLogger();

    constructor() {
18
        this.client = new Client1_10({ config: config.fromKubeconfig(), version: '1.9'});
19
20
21
22
        this.client.loadSpec();
    }

    public async createSecret(secretManifest: any): Promise<boolean> {
23
        let result: Promise<boolean>;
chicm-ms's avatar
chicm-ms committed
24
        const response: any = await this.client.api.v1.namespaces('default').secrets
25
26
          .post({body: secretManifest});
        if (response.statusCode && (response.statusCode >= 200 && response.statusCode <= 299)) {
27
28
29
30
            result = Promise.resolve(true);
        } else {
            result = Promise.reject(`Create secrets failed, statusCode is ${response.statusCode}`);
        }
31

32
33
34
35
        return result;
    }
}

36
37
38
/**
 * Kubernetes CRD client
 */
39
abstract class KubernetesCRDClient {
40
41
42
43
44
    protected readonly client: any;
    protected readonly log: Logger = getLogger();
    protected crdSchema: any;

    constructor() {
45
        this.client = new Client1_10({ config: config.fromKubeconfig() });
46
47
48
49
50
51
52
53
        this.client.loadSpec();
    }

    protected abstract get operator(): any;

    public abstract get containerName(): string;

    public get jobKind(): string {
54
        if (this.crdSchema
55
            && this.crdSchema.spec
56
57
58
59
60
61
62
63
64
            && this.crdSchema.spec.names
            && this.crdSchema.spec.names.kind) {
            return this.crdSchema.spec.names.kind;
        } else {
            throw new Error('KubeflowOperatorClient: getJobKind failed, kind is undefined in crd schema!');
        }
    }

    public get apiVersion(): string {
65
        if (this.crdSchema
66
            && this.crdSchema.spec
67
68
69
70
71
72
            && this.crdSchema.spec.version) {
            return this.crdSchema.spec.version;
        } else {
            throw new Error('KubeflowOperatorClient: get apiVersion failed, version is undefined in crd schema!');
        }
    }
73

74
    public async createKubernetesJob(jobManifest: any): Promise<boolean> {
75
        let result: Promise<boolean>;
chicm-ms's avatar
chicm-ms committed
76
        const response: any = await this.operator.post({body: jobManifest});
77
        if (response.statusCode && (response.statusCode >= 200 && response.statusCode <= 299)) {
78
79
            result = Promise.resolve(true);
        } else {
80
            result = Promise.reject(`Create kubernetes job failed, statusCode is ${response.statusCode}`);
81
        }
82

83
84
85
86
        return result;
    }

    //TODO : replace any
87
    public async getKubernetesJob(kubeflowJobName: string): Promise<any> {
88
        let result: Promise<any>;
chicm-ms's avatar
chicm-ms committed
89
        const response: any = await this.operator(kubeflowJobName)
90
91
          .get();
        if (response.statusCode && (response.statusCode >= 200 && response.statusCode <= 299)) {
92
93
94
95
            result = Promise.resolve(response.body);
        } else {
            result = Promise.reject(`KubeflowOperatorClient get tfjobs failed, statusCode is ${response.statusCode}`);
        }
96

97
98
99
        return result;
    }

100
    public async deleteKubernetesJob(labels: Map<string, string>): Promise<boolean> {
101
102
        let result: Promise<boolean>;
        // construct match query from labels for deleting tfjob
103
104
105
        const matchQuery: string = Array.from(labels.keys())
                                     .map((labelKey: string) => `${labelKey}=${labels.get(labelKey)}`)
                                     .join(',');
106
        try {
chicm-ms's avatar
chicm-ms committed
107
            const deleteResult: any = await this.operator()
108
              .delete({
109
110
                 qs: {
                      labelSelector: matchQuery,
111
                      propagationPolicy: 'Background'
112
                     }
113
            });
114
            if (deleteResult.statusCode && deleteResult.statusCode >= 200 && deleteResult.statusCode <= 299) {
115
116
                result = Promise.resolve(true);
            } else {
117
118
                result = Promise.reject(
                    `KubeflowOperatorClient, delete labels ${matchQuery} get wrong statusCode ${deleteResult.statusCode}`);
119
            }
120
        } catch (err) {
121
122
123
124
125
126
127
            result = Promise.reject(err);
        }

        return result;
    }
}

128
export { KubernetesCRDClient, GeneralK8sClient };