kubernetesApiClient.ts 4.41 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
import { getLogger, Logger } from '../../common/log';

/**
 * Generict Kubernetes client, target version >= 1.9
 */
13
// tslint:disable: no-any no-unsafe-any
14
15
16
17
18
class GeneralK8sClient {
    protected readonly client: any;
    protected readonly log: Logger = getLogger();

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

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

33
34
35
36
        return result;
    }
}

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

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

    protected abstract get operator(): any;

    public abstract get containerName(): string;

    public get jobKind(): string {
55
        if (this.crdSchema
56
            && this.crdSchema.spec
57
58
59
60
61
62
63
64
65
            && 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 {
66
        if (this.crdSchema
67
            && this.crdSchema.spec
68
69
70
71
72
73
            && this.crdSchema.spec.version) {
            return this.crdSchema.spec.version;
        } else {
            throw new Error('KubeflowOperatorClient: get apiVersion failed, version is undefined in crd schema!');
        }
    }
74

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

84
85
86
87
        return result;
    }

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

98
99
100
        return result;
    }

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

        return result;
    }
}

129
export { KubernetesCRDClient, GeneralK8sClient };