kubeflowApiClient.ts 6.49 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
 * 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';

import * as fs from 'fs';
23
import { GeneralK8sClient, KubernetesCRDClient } from '../kubernetesApiClient';
24
25
import { KubeflowOperator } from './kubeflowConfig';

26
27
28
29
/**
 * KubeflowOperator Client
 */
abstract class KubeflowOperatorClient extends KubernetesCRDClient {
30
    /**
31
     * Factory method to generate operator client
32
     */
33
    // tslint:disable-next-line:function-name
34
    public static generateOperatorClient(kubeflowOperator: KubeflowOperator,
35
36
                                         operatorApiVersion: string): KubernetesCRDClient {
        switch (kubeflowOperator) {
37
            case 'tf-operator': {
38
                switch (operatorApiVersion) {
39
40
41
42
43
44
45
46
47
                    case 'v1alpha2': {
                        return new TFOperatorClientV1Alpha2();
                    }
                    case 'v1beta1': {
                        return new TFOperatorClientV1Beta1();
                    }
                    case 'v1beta2': {
                        return new TFOperatorClientV1Beta2();
                    }
48
49
                    default:
                        throw new Error(`Invalid tf-operator apiVersion ${operatorApiVersion}`);
50
                }
51
            }
52
            case 'pytorch-operator': {
53
                switch (operatorApiVersion) {
54
55
56
57
58
59
60
61
62
                    case 'v1alpha2': {
                        return new PyTorchOperatorClientV1Alpha2();
                    }
                    case 'v1beta1': {
                        return new PyTorchOperatorClientV1Beta1();
                    }
                    case 'v1beta2': {
                        return new PyTorchOperatorClientV1Beta2();
                    }
63
64
                    default:
                        throw new Error(`Invalid pytorch-operator apiVersion ${operatorApiVersion}`);
65
                }
66
67
68
            }
            default:
                throw new Error(`Invalid operator ${kubeflowOperator}`);
69
70
71
72
        }
    }
}

73
// tslint:disable: no-unsafe-any no-any completed-docs
74
75
76
77
78
79
80
81
82
83
84
class TFOperatorClientV1Alpha2 extends KubeflowOperatorClient {
    /**
     * 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 {
85
        return this.client.apis['kubeflow.org'].v1alpha2.namespaces('default').tfjobs;
86
87
88
89
    }

    public get containerName(): string {
        return 'tensorflow';
90
    }
91
92
93
94
95
96
97
98
99
100
101
102
103
}

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 {
104
        return this.client.apis['kubeflow.org'].v1beta1.namespaces('default').tfjobs;
105
106
107
108
    }

    public get containerName(): string {
        return 'tensorflow';
109
    }
110
111
}

112
113
114
115
116
117
118
119
120
121
122
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 {
123
        return this.client.apis['kubeflow.org'].v1beta2.namespaces('default').tfjobs;
124
125
126
127
    }

    public get containerName(): string {
        return 'tensorflow';
128
    }
129
130
131
}

class PyTorchOperatorClientV1Alpha2 extends KubeflowOperatorClient {
132
133
134
135
136
137
138
139
140
141
    /**
     * 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 {
142
        return this.client.apis['kubeflow.org'].v1alpha2.namespaces('default').pytorchjobs;
143
144
145
146
147
148
149
    }

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

150
class PyTorchOperatorClientV1Beta1 extends KubernetesCRDClient {
151
152
153
154
155
156
157
158
159
160
    /**
     * 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 {
161
        return this.client.apis['kubeflow.org'].v1beta1.namespaces('default').pytorchjobs;
162
163
164
165
166
167
168
    }

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

169
170
171
172
173
174
175
176
177
178
179
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 {
180
        return this.client.apis['kubeflow.org'].v1beta2.namespaces('default').pytorchjobs;
181
182
183
184
185
186
187
    }

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

188
// tslint:enable: no-unsafe-any
189
export { KubeflowOperatorClient, GeneralK8sClient };