kubeflowApiClient.ts 5.37 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
// tslint:disable: no-unsafe-any no-any completed-docs
12
class TFOperatorClientV1Alpha2 extends KubernetesCRDClient {
13
14
15
16
17
18
19
20
21
22
    /**
     * 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 {
23
        return this.client.apis['kubeflow.org'].v1alpha2.namespaces('default').tfjobs;
24
25
26
27
    }

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

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 {
42
        return this.client.apis['kubeflow.org'].v1beta1.namespaces('default').tfjobs;
43
44
45
46
    }

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

50
51
52
53
54
55
56
57
58
59
60
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 {
61
        return this.client.apis['kubeflow.org'].v1beta2.namespaces('default').tfjobs;
62
63
64
65
    }

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

69
class PyTorchOperatorClientV1Alpha2 extends KubernetesCRDClient {
70
71
72
73
74
75
76
77
78
79
    /**
     * 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 {
80
        return this.client.apis['kubeflow.org'].v1alpha2.namespaces('default').pytorchjobs;
81
82
83
84
85
86
87
    }

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

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

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

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

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

126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
 * KubeflowOperator Client
 */
class KubeflowOperatorClientFactory {
    /**
     * Factory method to generate operator client
     */
    // tslint:disable-next-line:function-name
    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();
                    }
                    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}`);
        }
    }
}

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