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

4
export type KubernetesStorageKind = 'nfs' | 'azureStorage' | 'pvc';
5
import {MethodNotImplementedError} from 'common/errors';
6

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

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

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

export class StorageConfig {
    public readonly storage?: KubernetesStorageKind;

    constructor(storage?: KubernetesStorageKind) {
27
28
29
30
        this.storage = storage;
    }
}

31
export class KubernetesClusterConfigNFS extends KubernetesClusterConfig {
32
    public readonly nfs: NFSConfig;
33
34

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

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

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

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

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

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

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

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

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

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

98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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';
    }
114

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

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

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

158
159
160
161
162
163
164
165
166
167
168
169
/**
 * 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
170
171
172
173
/**
 * 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
 */
174
175
export class KeyVaultConfig {
    // The vault-name to specify vault
chicm-ms's avatar
chicm-ms committed
176
    public readonly vaultName: string;
177
    // The name to specify private key
chicm-ms's avatar
chicm-ms committed
178
    public readonly name: string;
SparkSnail's avatar
SparkSnail committed
179

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

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

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

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

208
    // Memory
209
    public readonly memoryMB: number;
210

211
    // Docker image
212
    public readonly image: string;
213

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

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

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

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

234
export class KubernetesTrialConfig {
235
    public readonly codeDir: string;
236
237
238
239

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