kubernetesApiClient.ts 5.41 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
 * Copyright (c) Microsoft Corporation
 * All rights reserved.
 *
 * MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

'use strict';

22
import { Client1_10, config } from 'kubernetes-client';
23
24
25
26
27
import { getLogger, Logger } from '../../common/log';

/**
 * Generict Kubernetes client, target version >= 1.9
 */
28
// tslint:disable: no-any no-unsafe-any
29
30
31
32
33
class GeneralK8sClient {
    protected readonly client: any;
    protected readonly log: Logger = getLogger();

    constructor() {
34
        this.client = new Client1_10({ config: config.fromKubeconfig(), version: '1.9'});
35
36
37
38
        this.client.loadSpec();
    }

    public async createSecret(secretManifest: any): Promise<boolean> {
39
        let result: Promise<boolean>;
40
41
42
        const response : any = await this.client.api.v1.namespaces('default').secrets
          .post({body: secretManifest});
        if (response.statusCode && (response.statusCode >= 200 && response.statusCode <= 299)) {
43
44
45
46
            result = Promise.resolve(true);
        } else {
            result = Promise.reject(`Create secrets failed, statusCode is ${response.statusCode}`);
        }
47

48
49
50
51
        return result;
    }
}

52
53
54
/**
 * Kubernetes CRD client
 */
55
abstract class KubernetesCRDClient {
56
57
58
59
60
    protected readonly client: any;
    protected readonly log: Logger = getLogger();
    protected crdSchema: any;

    constructor() {
61
        this.client = new Client1_10({ config: config.fromKubeconfig() });
62
63
64
65
66
67
68
69
        this.client.loadSpec();
    }

    protected abstract get operator(): any;

    public abstract get containerName(): string;

    public get jobKind(): string {
70
        if (this.crdSchema
71
            && this.crdSchema.spec
72
73
74
75
76
77
78
79
80
            && 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 {
81
        if (this.crdSchema
82
            && this.crdSchema.spec
83
84
85
86
87
88
            && this.crdSchema.spec.version) {
            return this.crdSchema.spec.version;
        } else {
            throw new Error('KubeflowOperatorClient: get apiVersion failed, version is undefined in crd schema!');
        }
    }
89

90
    public async createKubernetesJob(jobManifest: any): Promise<boolean> {
91
92
        let result: Promise<boolean>;
        const response : any = await this.operator.post({body: jobManifest});
93
        if (response.statusCode && (response.statusCode >= 200 && response.statusCode <= 299)) {
94
95
            result = Promise.resolve(true);
        } else {
96
            result = Promise.reject(`Create kubernetes job failed, statusCode is ${response.statusCode}`);
97
        }
98

99
100
101
102
        return result;
    }

    //TODO : replace any
103
    public async getKubernetesJob(kubeflowJobName: string): Promise<any> {
104
        let result: Promise<any>;
105
106
107
        const response : any = await this.operator(kubeflowJobName)
          .get();
        if (response.statusCode && (response.statusCode >= 200 && response.statusCode <= 299)) {
108
109
110
111
            result = Promise.resolve(response.body);
        } else {
            result = Promise.reject(`KubeflowOperatorClient get tfjobs failed, statusCode is ${response.statusCode}`);
        }
112

113
114
115
        return result;
    }

116
    public async deleteKubernetesJob(labels: Map<string, string>): Promise<boolean> {
117
118
        let result: Promise<boolean>;
        // construct match query from labels for deleting tfjob
119
120
121
        const matchQuery: string = Array.from(labels.keys())
                                     .map((labelKey: string) => `${labelKey}=${labels.get(labelKey)}`)
                                     .join(',');
122
        try {
123
124
            const deleteResult : any = await this.operator()
              .delete({
125
126
                 qs: {
                      labelSelector: matchQuery,
127
                      propagationPolicy: 'Background'
128
                     }
129
            });
130
            if (deleteResult.statusCode && deleteResult.statusCode >= 200 && deleteResult.statusCode <= 299) {
131
132
                result = Promise.resolve(true);
            } else {
133
134
                result = Promise.reject(
                    `KubeflowOperatorClient, delete labels ${matchQuery} get wrong statusCode ${deleteResult.statusCode}`);
135
            }
136
        } catch (err) {
137
138
139
140
141
142
143
            result = Promise.reject(err);
        }

        return result;
    }
}

144
export { KubernetesCRDClient, GeneralK8sClient };