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

4
import fs from 'fs';
5
import { GeneralK8sClient, KubernetesCRDClient } from '../kubernetesApiClient';
6
7
8
import { KubeflowOperator } from './kubeflowConfig';


9
class TFOperatorClientV1Alpha2 extends KubernetesCRDClient {
10
11
12
13
14
15
16
17
18
19
    /**
     * 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 {
J-shang's avatar
J-shang committed
20
        return this.client.apis['kubeflow.org'].v1alpha2.namespaces(this.namespace).tfjobs;
21
22
23
24
    }

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

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 {
J-shang's avatar
J-shang committed
39
        return this.client.apis['kubeflow.org'].v1beta1.namespaces(this.namespace).tfjobs;
40
41
42
43
    }

    public get containerName(): string {
        return 'tensorflow';
44
    }
45
46
}

47
48
49
50
51
52
53
54
55
56
57
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 {
J-shang's avatar
J-shang committed
58
        return this.client.apis['kubeflow.org'].v1beta2.namespaces(this.namespace).tfjobs;
59
60
61
62
    }

    public get containerName(): string {
        return 'tensorflow';
63
    }
64
65
}

66
67
68
69
70
71
72
73
74
75
76
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 {
J-shang's avatar
J-shang committed
77
        return this.client.apis['kubeflow.org'].v1.namespaces(this.namespace).tfjobs;
78
79
80
81
82
83
    }

    public get containerName(): string {
        return 'tensorflow';
    }
}
84
85
86
87
88
89
90
91
92
93
94
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 {
J-shang's avatar
J-shang committed
95
        return this.client.apis['kubeflow.org'].v1.namespaces(this.namespace).pytorchjobs;
96
    }
97

98
99
100
101
    public get containerName(): string {
        return 'pytorch';
    }
}
102
class PyTorchOperatorClientV1Alpha2 extends KubernetesCRDClient {
103
104
105
106
107
108
109
110
111
112
    /**
     * 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 {
J-shang's avatar
J-shang committed
113
        return this.client.apis['kubeflow.org'].v1alpha2.namespaces(this.namespace).pytorchjobs;
114
115
116
117
118
119
120
    }

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

121
class PyTorchOperatorClientV1Beta1 extends KubernetesCRDClient {
122
123
124
125
126
127
128
129
130
131
    /**
     * 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 {
J-shang's avatar
J-shang committed
132
        return this.client.apis['kubeflow.org'].v1beta1.namespaces(this.namespace).pytorchjobs;
133
134
135
136
137
138
139
    }

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

140
141
142
143
144
145
146
147
148
149
150
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 {
J-shang's avatar
J-shang committed
151
        return this.client.apis['kubeflow.org'].v1beta2.namespaces(this.namespace).pytorchjobs;
152
153
154
155
156
157
158
    }

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

159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
 * 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();
                    }
179
180
181
                    case 'v1': {
                        return new TFOperatorClientV1();
                    }
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
                    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();
                    }
197
198
199
                    case 'v1': {
                        return new PyTorchOperatorClientV1();
                    }
200
201
202
203
204
205
206
207
208
209
210
                    default:
                        throw new Error(`Invalid pytorch-operator apiVersion ${operatorApiVersion}`);
                }
            }
            default:
                throw new Error(`Invalid operator ${kubeflowOperator}`);
        }
    }
}

export { KubeflowOperatorClientFactory, GeneralK8sClient };