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

'use strict';

6
7
export type KubernetesStorageKind = 'nfs' | 'azureStorage' | 'pvc';
import {MethodNotImplementedError} from '../../common/errors';
8

9
10
11
export abstract class KubernetesClusterConfig {
    public readonly storage?: KubernetesStorageKind;
    public readonly apiVersion: string;
12
    public readonly namespace?: string;
13

14
    constructor(apiVersion: string, storage?: KubernetesStorageKind, namespace?: string) {
15
        this.storage = storage;
16
        this.apiVersion = apiVersion;
17
        this.namespace = namespace
18
19
    }

20
    public get storageType(): KubernetesStorageKind {
21
22
23
24
25
26
27
28
        throw new MethodNotImplementedError();
    }
}

export class StorageConfig {
    public readonly storage?: KubernetesStorageKind;

    constructor(storage?: KubernetesStorageKind) {
29
30
31
32
        this.storage = storage;
    }
}

33
export class KubernetesClusterConfigNFS extends KubernetesClusterConfig {
34
    public readonly nfs: NFSConfig;
35
36

    constructor(
37
38
39
40
41
42
        apiVersion: string,
        nfs: NFSConfig,
        storage?: KubernetesStorageKind,
        namespace?: string
    ) {
        super(apiVersion, storage, namespace);
43
44
        this.nfs = nfs;
    }
45

46
    public get storageType(): KubernetesStorageKind {
47
48
49
50
        return 'nfs';
    }

    public static getInstance(jsonObject: object): KubernetesClusterConfigNFS {
51
52
        const kubernetesClusterConfigObjectNFS: KubernetesClusterConfigNFS = <KubernetesClusterConfigNFS>jsonObject;

53
54
55
        return new KubernetesClusterConfigNFS(
            kubernetesClusterConfigObjectNFS.apiVersion,
            kubernetesClusterConfigObjectNFS.nfs,
56
57
            kubernetesClusterConfigObjectNFS.storage,
            kubernetesClusterConfigObjectNFS.namespace
58
59
        );
    }
60
61
}

62
export class KubernetesClusterConfigAzure extends KubernetesClusterConfig {
63
    public readonly keyVault: KeyVaultConfig;
64
    public readonly azureStorage: AzureStorage;
65
    public readonly uploadRetryCount: number | undefined;
66

67
    constructor(
68
69
70
71
72
73
74
75
76
        apiVersion: string,
        keyVault: KeyVaultConfig,
        azureStorage: AzureStorage,
        storage?: KubernetesStorageKind,
        uploadRetryCount?: number,
        namespace?: string,

    ) {
        super(apiVersion, storage, namespace);
SparkSnail's avatar
SparkSnail committed
77
78
        this.keyVault = keyVault;
        this.azureStorage = azureStorage;
79
        this.uploadRetryCount = uploadRetryCount;
80
    }
81

82
    public get storageType(): KubernetesStorageKind {
83
84
85
86
        return 'azureStorage';
    }

    public static getInstance(jsonObject: object): KubernetesClusterConfigAzure {
87
88
        const kubernetesClusterConfigObjectAzure: KubernetesClusterConfigAzure = <KubernetesClusterConfigAzure>jsonObject;

89
90
91
92
        return new KubernetesClusterConfigAzure(
            kubernetesClusterConfigObjectAzure.apiVersion,
            kubernetesClusterConfigObjectAzure.keyVault,
            kubernetesClusterConfigObjectAzure.azureStorage,
93
            kubernetesClusterConfigObjectAzure.storage,
94
95
            kubernetesClusterConfigObjectAzure.uploadRetryCount,
            kubernetesClusterConfigObjectAzure.namespace
96
97
98
99
        );
    }
}

100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
export class KubernetesClusterConfigPVC extends KubernetesClusterConfig {
    public readonly pvc: PVCConfig;

    constructor(
        apiVersion: string,
        pvc: PVCConfig,
        storage?: KubernetesStorageKind,
        namespace?: string,
    ) {
        super(apiVersion, storage, namespace);
        this.pvc = pvc;
    }

    public get storageType(): KubernetesStorageKind {
        return 'pvc';
    }
116

117
118
119
120
121
122
123
124
125
126
127
128
    public static getInstance(jsonObject: object): KubernetesClusterConfigPVC {
        const kubernetesClusterConfigObjectPVC: KubernetesClusterConfigPVC =
            <KubernetesClusterConfigPVC>jsonObject;
        return new KubernetesClusterConfigPVC(
            kubernetesClusterConfigObjectPVC.apiVersion,
            kubernetesClusterConfigObjectPVC.pvc,
            kubernetesClusterConfigObjectPVC.storage,
            kubernetesClusterConfigObjectPVC.namespace
        );
    }
}
export class KubernetesClusterConfigFactory {
129
    public static generateKubernetesClusterConfig(jsonObject: object): KubernetesClusterConfig {
130
131
        const storageConfig: StorageConfig = <StorageConfig>jsonObject;
        switch (storageConfig.storage) {
132
133
            case 'azureStorage':
                return KubernetesClusterConfigAzure.getInstance(jsonObject);
134
135
            case 'pvc':
                return KubernetesClusterConfigPVC.getInstance(jsonObject);
136
137
            case 'nfs':
            case undefined:
138
                return KubernetesClusterConfigNFS.getInstance(jsonObject);
139
140
            default:
                throw new Error(`Invalid json object ${jsonObject}`);
141
        }
142
    }
143
144
145
146
147
148
}

/**
 * NFS configuration to store Kubeflow job related files
 */
export class NFSConfig {
149
    // IP Adress of NFS server
chicm-ms's avatar
chicm-ms committed
150
    public readonly server: string;
151
    // exported NFS path on NFS server
chicm-ms's avatar
chicm-ms committed
152
    public readonly path: string;
153

chicm-ms's avatar
chicm-ms committed
154
    constructor(server: string, path: string) {
155
156
157
158
159
        this.server = server;
        this.path = path;
    }
}

160
161
162
163
164
165
166
167
168
169
170
171
/**
 * PVC configuration to store Kubernetes job related files
 */
export class PVCConfig {
    // Path of the mounted pvc
    public readonly path: string;

    constructor(path: string) {
        this.path = path;
    }
}

SparkSnail's avatar
SparkSnail committed
172
173
174
175
/**
 * KeyVault configuration to store the key of Azure Storage Service
 * Refer https://docs.microsoft.com/en-us/azure/key-vault/key-vault-manage-with-cli2
 */
176
177
export class KeyVaultConfig {
    // The vault-name to specify vault
chicm-ms's avatar
chicm-ms committed
178
    public readonly vaultName: string;
179
    // The name to specify private key
chicm-ms's avatar
chicm-ms committed
180
    public readonly name: string;
SparkSnail's avatar
SparkSnail committed
181

chicm-ms's avatar
chicm-ms committed
182
    constructor(vaultName: string, name: string) {
SparkSnail's avatar
SparkSnail committed
183
184
185
186
187
188
189
190
191
        this.vaultName = vaultName;
        this.name = name;
    }
}

/**
 * Azure Storage Service
 */
export class AzureStorage {
192
    // The azure share to storage files
chicm-ms's avatar
chicm-ms committed
193
    public readonly azureShare: string;
194

195
    // The account name of sotrage service
SparkSnail's avatar
SparkSnail committed
196
    public readonly accountName: string;
chicm-ms's avatar
chicm-ms committed
197
    constructor(azureShare: string, accountName: string) {
SparkSnail's avatar
SparkSnail committed
198
199
200
201
202
        this.azureShare = azureShare;
        this.accountName = accountName;
    }
}

203
/**
204
 * Trial job configuration for Kubernetes
205
 */
206
export class KubernetesTrialConfigTemplate {
207
    // CPU number
208
    public readonly cpuNum: number;
209

210
    // Memory
211
    public readonly memoryMB: number;
212

213
    // Docker image
214
    public readonly image: string;
215

216
217
218
    // Private registry config file path to download docker iamge
    public readonly privateRegistryAuthPath?: string;

219
    // Trail command
chicm-ms's avatar
chicm-ms committed
220
    public readonly command: string;
221

222
    // Required GPU number for trial job. The number should be in [0,100]
chicm-ms's avatar
chicm-ms committed
223
    public readonly gpuNum: number;
224

chicm-ms's avatar
chicm-ms committed
225
    constructor(command: string, gpuNum: number,
226
        cpuNum: number, memoryMB: number, image: string, privateRegistryAuthPath?: string) {
227
228
        this.command = command;
        this.gpuNum = gpuNum;
229
230
231
        this.cpuNum = cpuNum;
        this.memoryMB = memoryMB;
        this.image = image;
232
        this.privateRegistryAuthPath = privateRegistryAuthPath;
233
    }
234
235
}

236
export class KubernetesTrialConfig {
237
    public readonly codeDir: string;
238
239
240
241

    constructor(codeDir: string) {
        this.codeDir = codeDir;
    }
242
}