kubeflowApiClient.ts 5.44 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
import { KubeflowOperator } from './kubeflowConfig';

10
11
12
13
/**
 * KubeflowOperator Client
 */
abstract class KubeflowOperatorClient extends KubernetesCRDClient {
14
    /**
15
     * Factory method to generate operator client
16
     */
17
    // tslint:disable-next-line:function-name
18
    public static generateOperatorClient(kubeflowOperator: KubeflowOperator,
19
20
                                         operatorApiVersion: string): KubernetesCRDClient {
        switch (kubeflowOperator) {
21
            case 'tf-operator': {
22
                switch (operatorApiVersion) {
23
24
25
26
27
28
29
30
31
                    case 'v1alpha2': {
                        return new TFOperatorClientV1Alpha2();
                    }
                    case 'v1beta1': {
                        return new TFOperatorClientV1Beta1();
                    }
                    case 'v1beta2': {
                        return new TFOperatorClientV1Beta2();
                    }
32
33
                    default:
                        throw new Error(`Invalid tf-operator apiVersion ${operatorApiVersion}`);
34
                }
35
            }
36
            case 'pytorch-operator': {
37
                switch (operatorApiVersion) {
38
39
40
41
42
43
44
45
46
                    case 'v1alpha2': {
                        return new PyTorchOperatorClientV1Alpha2();
                    }
                    case 'v1beta1': {
                        return new PyTorchOperatorClientV1Beta1();
                    }
                    case 'v1beta2': {
                        return new PyTorchOperatorClientV1Beta2();
                    }
47
48
                    default:
                        throw new Error(`Invalid pytorch-operator apiVersion ${operatorApiVersion}`);
49
                }
50
51
52
            }
            default:
                throw new Error(`Invalid operator ${kubeflowOperator}`);
53
54
55
56
        }
    }
}

57
// tslint:disable: no-unsafe-any no-any completed-docs
58
59
60
61
62
63
64
65
66
67
68
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 {
69
        return this.client.apis['kubeflow.org'].v1alpha2.namespaces('default').tfjobs;
70
71
72
73
    }

    public get containerName(): string {
        return 'tensorflow';
74
    }
75
76
77
78
79
80
81
82
83
84
85
86
87
}

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 {
88
        return this.client.apis['kubeflow.org'].v1beta1.namespaces('default').tfjobs;
89
90
91
92
    }

    public get containerName(): string {
        return 'tensorflow';
93
    }
94
95
}

96
97
98
99
100
101
102
103
104
105
106
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 {
107
        return this.client.apis['kubeflow.org'].v1beta2.namespaces('default').tfjobs;
108
109
110
111
    }

    public get containerName(): string {
        return 'tensorflow';
112
    }
113
114
115
}

class PyTorchOperatorClientV1Alpha2 extends KubeflowOperatorClient {
116
117
118
119
120
121
122
123
124
125
    /**
     * 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 {
126
        return this.client.apis['kubeflow.org'].v1alpha2.namespaces('default').pytorchjobs;
127
128
129
130
131
132
133
    }

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

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

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

153
154
155
156
157
158
159
160
161
162
163
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 {
164
        return this.client.apis['kubeflow.org'].v1beta2.namespaces('default').pytorchjobs;
165
166
167
168
169
170
171
    }

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

172
// tslint:enable: no-unsafe-any
173
export { KubeflowOperatorClient, GeneralK8sClient };