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

22
23
export type KubernetesStorageKind = 'nfs' | 'azureStorage';
import { MethodNotImplementedError } from '../../common/errors';
24

25
// tslint:disable: completed-docs function-name
26
27
28
export abstract class KubernetesClusterConfig {
    public readonly storage?: KubernetesStorageKind;
    public readonly apiVersion: string;
29

30
31
    constructor(apiVersion: string, storage?: KubernetesStorageKind) {
        this.storage = storage;
32
        this.apiVersion = apiVersion;
33
34
    }

35
    public get storageType(): KubernetesStorageKind {
36
37
38
39
40
41
42
43
        throw new MethodNotImplementedError();
    }
}

export class StorageConfig {
    public readonly storage?: KubernetesStorageKind;

    constructor(storage?: KubernetesStorageKind) {
44
45
46
47
        this.storage = storage;
    }
}

48
export class KubernetesClusterConfigNFS extends KubernetesClusterConfig {
49
    public readonly nfs: NFSConfig;
50
51

    constructor(
52
            apiVersion: string,
53
54
55
56
            nfs: NFSConfig,
            storage?: KubernetesStorageKind
        ) {
        super(apiVersion, storage);
57
58
        this.nfs = nfs;
    }
59

60
    public get storageType(): KubernetesStorageKind {
61
62
63
64
        return 'nfs';
    }

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

67
68
69
70
71
72
        return new KubernetesClusterConfigNFS(
            kubernetesClusterConfigObjectNFS.apiVersion,
            kubernetesClusterConfigObjectNFS.nfs,
            kubernetesClusterConfigObjectNFS.storage
        );
    }
73
74
}

75
export class KubernetesClusterConfigAzure extends KubernetesClusterConfig {
76
    public readonly keyVault: KeyVaultConfig;
77
    public readonly azureStorage: AzureStorage;
78
    public readonly uploadRetryCount: number | undefined;
79

80
    constructor(
81
            apiVersion: string,
82
            keyVault: KeyVaultConfig,
83
            azureStorage: AzureStorage,
84
85
            storage?: KubernetesStorageKind,
            uploadRetryCount?: number
86
87
        ) {
        super(apiVersion, storage);
SparkSnail's avatar
SparkSnail committed
88
89
        this.keyVault = keyVault;
        this.azureStorage = azureStorage;
90
        this.uploadRetryCount = uploadRetryCount;
91
    }
92

93
    public get storageType(): KubernetesStorageKind {
94
95
96
97
        return 'azureStorage';
    }

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

100
101
102
103
        return new KubernetesClusterConfigAzure(
            kubernetesClusterConfigObjectAzure.apiVersion,
            kubernetesClusterConfigObjectAzure.keyVault,
            kubernetesClusterConfigObjectAzure.azureStorage,
104
105
            kubernetesClusterConfigObjectAzure.storage,
            kubernetesClusterConfigObjectAzure.uploadRetryCount
106
107
108
109
        );
    }
}

110
// tslint:disable-next-line:no-unnecessary-class
111
112
113
export class KubernetesClusterConfigFactory {

    public static generateKubernetesClusterConfig(jsonObject: object): KubernetesClusterConfig {
114
115
         const storageConfig: StorageConfig = <StorageConfig>jsonObject;
         switch (storageConfig.storage) {
116
117
            case 'azureStorage':
                return KubernetesClusterConfigAzure.getInstance(jsonObject);
118
119
            case 'nfs':
            case undefined:
120
                return KubernetesClusterConfigNFS.getInstance(jsonObject);
121
122
            default:
                throw new Error(`Invalid json object ${jsonObject}`);
123
124
         }
    }
125
126
127
128
129
130
}

/**
 * NFS configuration to store Kubeflow job related files
 */
export class NFSConfig {
131
    // IP Adress of NFS server
132
    public readonly server : string;
133
    // exported NFS path on NFS server
134
135
136
137
138
139
140
141
    public readonly path : string;

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

SparkSnail's avatar
SparkSnail committed
142
143
144
145
/**
 * 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
 */
146
147
export class KeyVaultConfig {
    // The vault-name to specify vault
SparkSnail's avatar
SparkSnail committed
148
    public readonly vaultName : string;
149
    // The name to specify private key
SparkSnail's avatar
SparkSnail committed
150
151
    public readonly name : string;

152
    constructor(vaultName : string, name : string) {
SparkSnail's avatar
SparkSnail committed
153
154
155
156
157
158
159
160
161
        this.vaultName = vaultName;
        this.name = name;
    }
}

/**
 * Azure Storage Service
 */
export class AzureStorage {
162
    // The azure share to storage files
SparkSnail's avatar
SparkSnail committed
163
    public readonly azureShare : string;
164

165
    // The account name of sotrage service
SparkSnail's avatar
SparkSnail committed
166
    public readonly accountName: string;
167
    constructor(azureShare : string, accountName: string) {
SparkSnail's avatar
SparkSnail committed
168
169
170
171
172
        this.azureShare = azureShare;
        this.accountName = accountName;
    }
}

173
/**
174
 * Trial job configuration for Kubernetes
175
 */
176
export class KubernetesTrialConfigTemplate {
177
    // CPU number
178
    public readonly cpuNum: number;
179

180
    // Memory
181
    public readonly memoryMB: number;
182

183
    // Docker image
184
    public readonly image: string;
185

186
187
188
    // Private registry config file path to download docker iamge
    public readonly privateRegistryAuthPath?: string;

189
    // Trail command
190
191
    public readonly command : string;

192
    // Required GPU number for trial job. The number should be in [0,100]
193
    public readonly gpuNum : number;
194
195

    constructor(command : string, gpuNum : number,
196
                cpuNum: number, memoryMB: number, image: string, privateRegistryAuthPath?: string) {
197
198
        this.command = command;
        this.gpuNum = gpuNum;
199
200
201
        this.cpuNum = cpuNum;
        this.memoryMB = memoryMB;
        this.image = image;
202
        this.privateRegistryAuthPath = privateRegistryAuthPath;
203
    }
204
205
}

206
export class KubernetesTrialConfig {
207
    public readonly codeDir: string;
208
209
210
211

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