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

'use strict';

import * as fs from 'fs';
7
import { GeneralK8sClient, KubernetesCRDClient } from '../kubernetesApiClient';
8
9
10
import { KubeflowOperator } from './kubeflowConfig';


11
class TFOperatorClientV1Alpha2 extends KubernetesCRDClient {
12
13
14
15
16
17
18
19
20
21
    /**
     * constructor, to initialize tfjob CRD definition
     */
    public constructor() {
        super();
        this.crdSchema = JSON.parse(fs.readFileSync('./config/kubeflow/tfjob-crd-v1alpha2.json', 'utf8'));
        this.client.addCustomResourceDefinition(this.crdSchema);
    }

    protected get operator(): any {
22
        return this.client.apis['kubeflow.org'].v1alpha2.namespaces('default').tfjobs;
23
24
25
26
    }

    public get containerName(): string {
        return 'tensorflow';
27
    }
28
29
30
31
32
33
34
35
36
37
38
39
40
}

class TFOperatorClientV1Beta1 extends KubernetesCRDClient {
    /**
     * constructor, to initialize tfjob CRD definition
     */
    public constructor() {
        super();
        this.crdSchema = JSON.parse(fs.readFileSync('./config/kubeflow/tfjob-crd-v1beta1.json', 'utf8'));
        this.client.addCustomResourceDefinition(this.crdSchema);
    }

    protected get operator(): any {
41
        return this.client.apis['kubeflow.org'].v1beta1.namespaces('default').tfjobs;
42
43
44
45
    }

    public get containerName(): string {
        return 'tensorflow';
46
    }
47
48
}

49
50
51
52
53
54
55
56
57
58
59
class TFOperatorClientV1Beta2 extends KubernetesCRDClient {
    /**
     * constructor, to initialize tfjob CRD definition
     */
    public constructor() {
        super();
        this.crdSchema = JSON.parse(fs.readFileSync('./config/kubeflow/tfjob-crd-v1beta2.json', 'utf8'));
        this.client.addCustomResourceDefinition(this.crdSchema);
    }

    protected get operator(): any {
60
        return this.client.apis['kubeflow.org'].v1beta2.namespaces('default').tfjobs;
61
62
63
64
    }

    public get containerName(): string {
        return 'tensorflow';
65
    }
66
67
}

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
class TFOperatorClientV1 extends KubernetesCRDClient {
    /**
     * constructor, to initialize tfjob CRD definition
     */
    public constructor() {
        super();
        this.crdSchema = JSON.parse(fs.readFileSync('./config/kubeflow/tfjob-crd-v1.json', 'utf8'));
        this.client.addCustomResourceDefinition(this.crdSchema);
    }

    protected get operator(): any {
        return this.client.apis['kubeflow.org'].v1.namespaces('default').tfjobs;
    }

    public get containerName(): string {
        return 'tensorflow';
    }
}
86
87
88
89
90
91
92
93
94
95
96
97
98
class PyTorchOperatorClientV1 extends KubernetesCRDClient {
    /**
     * constructor, to initialize tfjob CRD definition
     */
    public constructor() {
        super();
        this.crdSchema = JSON.parse(fs.readFileSync('./config/kubeflow/pytorchjob-crd-v1.json', 'utf8'));
        this.client.addCustomResourceDefinition(this.crdSchema);
    }

    protected get operator(): any {
        return this.client.apis['kubeflow.org'].v1.namespaces('default').pytorchjobs;
    }
99

100
101
102
103
    public get containerName(): string {
        return 'pytorch';
    }
}
104
class PyTorchOperatorClientV1Alpha2 extends KubernetesCRDClient {
105
106
107
108
109
110
111
112
113
114
    /**
     * constructor, to initialize tfjob CRD definition
     */
    public constructor() {
        super();
        this.crdSchema = JSON.parse(fs.readFileSync('./config/kubeflow/pytorchjob-crd-v1alpha2.json', 'utf8'));
        this.client.addCustomResourceDefinition(this.crdSchema);
    }

    protected get operator(): any {
115
        return this.client.apis['kubeflow.org'].v1alpha2.namespaces('default').pytorchjobs;
116
117
118
119
120
121
122
    }

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

123
class PyTorchOperatorClientV1Beta1 extends KubernetesCRDClient {
124
125
126
127
128
129
130
131
132
133
    /**
     * constructor, to initialize tfjob CRD definition
     */
    public constructor() {
        super();
        this.crdSchema = JSON.parse(fs.readFileSync('./config/kubeflow/pytorchjob-crd-v1beta1.json', 'utf8'));
        this.client.addCustomResourceDefinition(this.crdSchema);
    }

    protected get operator(): any {
134
        return this.client.apis['kubeflow.org'].v1beta1.namespaces('default').pytorchjobs;
135
136
137
138
139
140
141
    }

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

142
143
144
145
146
147
148
149
150
151
152
class PyTorchOperatorClientV1Beta2 extends KubernetesCRDClient {
    /**
     * constructor, to initialize tfjob CRD definition
     */
    public constructor() {
        super();
        this.crdSchema = JSON.parse(fs.readFileSync('./config/kubeflow/pytorchjob-crd-v1beta2.json', 'utf8'));
        this.client.addCustomResourceDefinition(this.crdSchema);
    }

    protected get operator(): any {
153
        return this.client.apis['kubeflow.org'].v1beta2.namespaces('default').pytorchjobs;
154
155
156
157
158
159
160
    }

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

161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
 * KubeflowOperator Client
 */
class KubeflowOperatorClientFactory {
    /**
     * Factory method to generate operator client
     */
    public static createClient(kubeflowOperator: KubeflowOperator, operatorApiVersion: string): KubernetesCRDClient {
        switch (kubeflowOperator) {
            case 'tf-operator': {
                switch (operatorApiVersion) {
                    case 'v1alpha2': {
                        return new TFOperatorClientV1Alpha2();
                    }
                    case 'v1beta1': {
                        return new TFOperatorClientV1Beta1();
                    }
                    case 'v1beta2': {
                        return new TFOperatorClientV1Beta2();
                    }
181
182
183
                    case 'v1': {
                        return new TFOperatorClientV1();
                    }
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
                    default:
                        throw new Error(`Invalid tf-operator apiVersion ${operatorApiVersion}`);
                }
            }
            case 'pytorch-operator': {
                switch (operatorApiVersion) {
                    case 'v1alpha2': {
                        return new PyTorchOperatorClientV1Alpha2();
                    }
                    case 'v1beta1': {
                        return new PyTorchOperatorClientV1Beta1();
                    }
                    case 'v1beta2': {
                        return new PyTorchOperatorClientV1Beta2();
                    }
199
200
201
                    case 'v1': {
                        return new PyTorchOperatorClientV1();
                    }
202
203
204
205
206
207
208
209
210
211
212
                    default:
                        throw new Error(`Invalid pytorch-operator apiVersion ${operatorApiVersion}`);
                }
            }
            default:
                throw new Error(`Invalid operator ${kubeflowOperator}`);
        }
    }
}

export { KubeflowOperatorClientFactory, GeneralK8sClient };