kubeflowApiClient.ts 5.88 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
86
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';
    }
}

87
class PyTorchOperatorClientV1Alpha2 extends KubernetesCRDClient {
88
89
90
91
92
93
94
95
96
97
    /**
     * 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 {
98
        return this.client.apis['kubeflow.org'].v1alpha2.namespaces('default').pytorchjobs;
99
100
101
102
103
104
105
    }

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

106
class PyTorchOperatorClientV1Beta1 extends KubernetesCRDClient {
107
108
109
110
111
112
113
114
115
116
    /**
     * 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 {
117
        return this.client.apis['kubeflow.org'].v1beta1.namespaces('default').pytorchjobs;
118
119
120
121
122
123
124
    }

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

125
126
127
128
129
130
131
132
133
134
135
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 {
136
        return this.client.apis['kubeflow.org'].v1beta2.namespaces('default').pytorchjobs;
137
138
139
140
141
142
143
    }

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

144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
 * 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();
                    }
164
165
166
                    case 'v1': {
                        return new TFOperatorClientV1();
                    }
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
                    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();
                    }
                    default:
                        throw new Error(`Invalid pytorch-operator apiVersion ${operatorApiVersion}`);
                }
            }
            default:
                throw new Error(`Invalid operator ${kubeflowOperator}`);
        }
    }
}

export { KubeflowOperatorClientFactory, GeneralK8sClient };