kubernetesConfig.ts 5.63 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';
import { MethodNotImplementedError } from '../../common/errors';
8

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

13
14
    constructor(apiVersion: string, storage?: KubernetesStorageKind) {
        this.storage = storage;
15
        this.apiVersion = apiVersion;
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
            apiVersion: string,
36
37
38
39
            nfs: NFSConfig,
            storage?: KubernetesStorageKind
        ) {
        super(apiVersion, storage);
40
41
        this.nfs = nfs;
    }
42

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

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

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

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

63
    constructor(
64
            apiVersion: string,
65
            keyVault: KeyVaultConfig,
66
            azureStorage: AzureStorage,
67
68
            storage?: KubernetesStorageKind,
            uploadRetryCount?: number
69
70
        ) {
        super(apiVersion, storage);
SparkSnail's avatar
SparkSnail committed
71
72
        this.keyVault = keyVault;
        this.azureStorage = azureStorage;
73
        this.uploadRetryCount = uploadRetryCount;
74
    }
75

76
    public get storageType(): KubernetesStorageKind {
77
78
79
80
        return 'azureStorage';
    }

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

83
84
85
86
        return new KubernetesClusterConfigAzure(
            kubernetesClusterConfigObjectAzure.apiVersion,
            kubernetesClusterConfigObjectAzure.keyVault,
            kubernetesClusterConfigObjectAzure.azureStorage,
87
88
            kubernetesClusterConfigObjectAzure.storage,
            kubernetesClusterConfigObjectAzure.uploadRetryCount
89
90
91
92
93
94
95
        );
    }
}

export class KubernetesClusterConfigFactory {

    public static generateKubernetesClusterConfig(jsonObject: object): KubernetesClusterConfig {
96
97
         const storageConfig: StorageConfig = <StorageConfig>jsonObject;
         switch (storageConfig.storage) {
98
99
            case 'azureStorage':
                return KubernetesClusterConfigAzure.getInstance(jsonObject);
100
101
            case 'nfs':
            case undefined:
102
                return KubernetesClusterConfigNFS.getInstance(jsonObject);
103
104
            default:
                throw new Error(`Invalid json object ${jsonObject}`);
105
106
         }
    }
107
108
109
110
111
112
}

/**
 * NFS configuration to store Kubeflow job related files
 */
export class NFSConfig {
113
    // IP Adress of NFS server
chicm-ms's avatar
chicm-ms committed
114
    public readonly server: string;
115
    // exported NFS path on NFS server
chicm-ms's avatar
chicm-ms committed
116
    public readonly path: string;
117

chicm-ms's avatar
chicm-ms committed
118
    constructor(server: string, path: string) {
119
120
121
122
123
        this.server = server;
        this.path = path;
    }
}

SparkSnail's avatar
SparkSnail committed
124
125
126
127
/**
 * 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
 */
128
129
export class KeyVaultConfig {
    // The vault-name to specify vault
chicm-ms's avatar
chicm-ms committed
130
    public readonly vaultName: string;
131
    // The name to specify private key
chicm-ms's avatar
chicm-ms committed
132
    public readonly name: string;
SparkSnail's avatar
SparkSnail committed
133

chicm-ms's avatar
chicm-ms committed
134
    constructor(vaultName: string, name: string) {
SparkSnail's avatar
SparkSnail committed
135
136
137
138
139
140
141
142
143
        this.vaultName = vaultName;
        this.name = name;
    }
}

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

147
    // The account name of sotrage service
SparkSnail's avatar
SparkSnail committed
148
    public readonly accountName: string;
chicm-ms's avatar
chicm-ms committed
149
    constructor(azureShare: string, accountName: string) {
SparkSnail's avatar
SparkSnail committed
150
151
152
153
154
        this.azureShare = azureShare;
        this.accountName = accountName;
    }
}

155
/**
156
 * Trial job configuration for Kubernetes
157
 */
158
export class KubernetesTrialConfigTemplate {
159
    // CPU number
160
    public readonly cpuNum: number;
161

162
    // Memory
163
    public readonly memoryMB: number;
164

165
    // Docker image
166
    public readonly image: string;
167

168
169
170
    // Private registry config file path to download docker iamge
    public readonly privateRegistryAuthPath?: string;

171
    // Trail command
chicm-ms's avatar
chicm-ms committed
172
    public readonly command: string;
173

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

chicm-ms's avatar
chicm-ms committed
177
    constructor(command: string, gpuNum: number,
178
                cpuNum: number, memoryMB: number, image: string, privateRegistryAuthPath?: string) {
179
180
        this.command = command;
        this.gpuNum = gpuNum;
181
182
183
        this.cpuNum = cpuNum;
        this.memoryMB = memoryMB;
        this.image = image;
184
        this.privateRegistryAuthPath = privateRegistryAuthPath;
185
    }
186
187
}

188
export class KubernetesTrialConfig {
189
    public readonly codeDir: string;
190
191
192
193

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