Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions frontend/packages/kube-types/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tmp/
63 changes: 63 additions & 0 deletions frontend/packages/kube-types/.tools/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import * as process from 'process';

export const TEMP_DIR = 'tmp';
export const SRC_DIR = 'src';

export const buildTempPath = (...segments) => [TEMP_DIR, ...segments].join('/');
export const buildSrcPath = (...segments) => [SRC_DIR, ...segments].join('/');

const OPENSHIFT_PREFIX = 'OS';

export type Config = {
name: string;
url: string;
replaceMapping?: { [key: string]: string };
dateAsString?: boolean;
};

export const getOpenshiftConfig = (): Config => ({
name: 'openshift',
url:
process.env.OPENSHIFT_API_SPEC_URL ||
'https://raw.githubusercontent.com/openshift/origin/master/api/swagger-spec/openshift-openapi-spec.json',
dateAsString: true,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed some package names from generated object names because the names would be too long (as discussed in #1773).

There are some conflicts between the different packages so I had to leave some in form of short prefixes.

Please give me some feedback what are your thoughts on this. And which packages should be namespaced and what prefixes to use.

replaceMapping: {
ComGithubOpenshiftApiApps: OPENSHIFT_PREFIX,
ComGithubOpenshiftApiAuthorization: OPENSHIFT_PREFIX,
ComGithubOpenshiftApiBuild: OPENSHIFT_PREFIX,
ComGithubOpenshiftApiImage: OPENSHIFT_PREFIX,
ComGithubOpenshiftApiNetwork: OPENSHIFT_PREFIX,
ComGithubOpenshiftApiOauth: OPENSHIFT_PREFIX,
ComGithubOpenshiftApiProject: '',
ComGithubOpenshiftApiQuota: OPENSHIFT_PREFIX,
ComGithubOpenshiftApiRoute: '',
ComGithubOpenshiftApiSecurity: OPENSHIFT_PREFIX,
ComGithubOpenshiftApiTemplate: '',
ComGithubOpenshiftApiUser: OPENSHIFT_PREFIX,
IoK8sApiAdmissionregistration: '',
IoK8sApiApps: '',
IoK8sApiAuthentication: '',
IoK8sApiAuthorization: '',
IoK8sApiAutoscaling: '',
IoK8sApiBatch: '',
IoK8sApiCertificates: '',
IoK8sApiCore: '',
IoK8sApiEvents: '',
IoK8sApiExtensions: 'Extensions',
IoK8sApimachineryPkgApisMeta: '',
IoK8sApiNetworking: '',
IoK8sApiPolicy: '',
IoK8sApiRbac: '',
IoK8sApiScheduling: '',
IoK8sApiStorage: '',
IoK8sKubeAggregatorPkgApisApiregistration: 'APIRegistration',
},
});

export const getKubevirtConfig = (): Config => ({
name: 'kubevirt',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also kubevirt and openshift have some conflicts between them, but I have not done anything about that because they live in separate folders and the user can import them separately.

url:
process.env.KUBEVIRT_API_SPEC_URL ||
'https://raw.githubusercontent.com/kubevirt/kubevirt/master/api/openapi-spec/swagger.json',
dateAsString: true,
});
53 changes: 53 additions & 0 deletions frontend/packages/kube-types/.tools/generate-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* eslint-disable no-console */
import { removeSync, emptyDirSync, ensureDirSync } from 'fs-extra';
import * as process from 'process';
import { generateAndMapTypes } from './generators/configured-generator';
import { getKubevirtConfig, getOpenshiftConfig, SRC_DIR, TEMP_DIR } from './config';

type Options = {
openshift: boolean,
kubevirt: boolean,
}

const getOptions = (): Options => {
const argsSet = new Set(process.argv);
const options = {
openshift: false,
kubevirt: false,
};

if(argsSet.has('all')){
Object.keys(options).forEach( key => {
options[key] = true;
});
}else {
options.openshift = argsSet.has('openshift');
options.kubevirt = argsSet.has('kubevirt');
}
return options;
};

const generate = async (options: Options) => {
let exitCode = 0;
try {
emptyDirSync(TEMP_DIR);
ensureDirSync(SRC_DIR);

if(options.openshift){
await generateAndMapTypes(getOpenshiftConfig());
}
if(options.kubevirt){
await generateAndMapTypes(getKubevirtConfig());
}

} catch (e) {
console.error('ERROR: kube-types package in inconsistent state (please revert the changes)');
console.error(e);
exitCode = 1;
} finally {
removeSync(TEMP_DIR);
}
return exitCode;
};

generate(getOptions()).then( exitCode => process.exit(exitCode));
144 changes: 144 additions & 0 deletions frontend/packages/kube-types/.tools/generators/configured-generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import { FileType, generateTypes } from './generic-generator';
import { Config } from '../config';

const filterStartRegex = /^(import|export function|})/;

const fromToJSONRegex = /.*(FromJSON|ToJSON)/;

const thisFolderImportEndRegexOneLine = /from '\.\/?';$/;
const dateRegex = /(.*[^A-Za-z0-9])(Date)([^A-Za-z0-9].*)/;

const resolveMappings = (line, replaceMappingsRegex, replaceMapping) => {
let result = line;
const possibleMapping = replaceMappingsRegex && replaceMapping && replaceMappingsRegex.exec(line);
if (possibleMapping && possibleMapping[1]) {
possibleMapping.forEach((value, index) => {
if (index !== 0) {
result = result.replace(value, replaceMapping[value]);
}
});
}
return result;
};

const resolveDate = (line) => {
const possibleDate = dateRegex.exec(line);
if (possibleDate && possibleDate.length === 4) {
return `${possibleDate[1]}string${possibleDate[3]}`;
}
return line;
};

const getImports = (line) => {
const start = line.indexOf('{');
const end = line.indexOf('}');

return line
.substring(start < 0 ? 0 : start + 1, end < 0 ? line.length : end)
.split(',')
.map((s) => s.trim())
.filter((s) => s);
};

/**
* - removes global functions (..FromJSON(), ..toJSON())
* - removes all imports (e.g. runtime folder) except the ones from this folder
* - reorganizes imports to prevent circular dependencies
*/

export const processFile = (
{ content, filename }: FileType,
{ replaceMapping, dateAsString }: Config,
) => {
const replaceMappingsRegex =
replaceMapping && RegExp(`(${Object.keys(replaceMapping).join('|')})`);

let isInsideGlobalFunction = false;
let isInsideImport = false;

let firstImportLineNumber = null;

const filesToImport = [];
const filesToImportBuffer = [];

const resultContent = content
.split('\n')
.map((line, index) => {
const possibleIlegalStart = filterStartRegex.exec(line);
switch (possibleIlegalStart && possibleIlegalStart[1]) {
case 'import':
if (firstImportLineNumber == null) {
firstImportLineNumber = index;
}

if (line.endsWith(';')) {
if (line.match(thisFolderImportEndRegexOneLine)) {
filesToImport.push(...getImports(line));
}
return null;
}
isInsideImport = true;
return null;
case 'export function':
isInsideGlobalFunction = true;
return null;
case '}':
if (isInsideGlobalFunction) {
isInsideGlobalFunction = false;
return null;
}
if (isInsideImport) {
if (line.match(thisFolderImportEndRegexOneLine)) {
filesToImport.push(...filesToImportBuffer);
}
filesToImportBuffer.length = 0;
isInsideImport = false;
return null;
}
break;
default:
break;
}

if (isInsideGlobalFunction) {
return null;
}

if (isInsideImport) {
filesToImportBuffer.push(...getImports(line));
return null;
}

const result = resolveMappings(line, replaceMappingsRegex, replaceMapping);

return dateAsString ? resolveDate(result) : result;
})
.filter((line) => line != null);

const resolvedImports = filesToImport
.filter((imp) => !fromToJSONRegex.exec(imp))
.map((imp) => {
const mappedImport = resolveMappings(imp, replaceMappingsRegex, replaceMapping);
return `import { ${mappedImport} } from './${mappedImport}';`;
});

resultContent.splice(firstImportLineNumber, 0, ...resolvedImports);

return {
content: resultContent.join('\n'),
filename: resolveMappings(filename, replaceMappingsRegex, replaceMapping),
};
};

export const processAPIObject = (APIObj) => {
// to prevent error: "Multiple schemas found in content, returning only the first one"
APIObj.paths = {};
return APIObj;
};

export const generateAndMapTypes = async (config: Config) =>
generateTypes({
config,
processAPIObject,
processFile: (file) => processFile(file, config),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* eslint-disable no-console */
import { emptyDirSync, moveSync } from 'fs-extra';
import * as fs from 'fs';
import { download } from '../utils';
import { openapiGenerator } from './openapi-generator';
import { buildSrcPath, buildTempPath, Config } from '../config';

const GENERATED = 'generated';

export type FileType = {
content: string;
filename: string;
};

const checkFileDoesNotExist = (filename) => {
if (fs.existsSync(filename)) {
throw new Error(
`Not unique filename (${filename})! (Maybe wrong package name mapping/elimination?)`,
);
}
};

export const generateTypes = async ({
config: { name, url },
processAPIObject,
processFile,
}: {
config: Config;
processAPIObject?: Function;
processFile?: (a: FileType) => FileType;
}) => {
const openAPIObject: any = await download(url);
const generateFrom = processAPIObject ? processAPIObject(openAPIObject) : openAPIObject;

const apiFilename = buildTempPath(name, `${name}-types.json`);
const generatedDir = buildTempPath(name, GENERATED);

emptyDirSync(generatedDir);
fs.writeFileSync(apiFilename, JSON.stringify(generateFrom));
await openapiGenerator(apiFilename, generatedDir);

// copy files to src directory
emptyDirSync(buildSrcPath(name));

fs.readdirSync(buildTempPath(name, GENERATED, 'models')).forEach((filename) => {
const relativeTmpPathToFile = buildTempPath(name, GENERATED, 'models', filename);
if (processFile) {
const readContent = fs.readFileSync(relativeTmpPathToFile, 'utf8');
const { content: resultContent, filename: resultFilename } = processFile({
content: readContent,
filename,
});

const resultFullFilename = buildSrcPath(name, resultFilename);
checkFileDoesNotExist(resultFullFilename);
fs.writeFileSync(resultFullFilename, resultContent);
} else {
const resultFullFilename = buildSrcPath(name, filename);
checkFileDoesNotExist(resultFullFilename);
moveSync(relativeTmpPathToFile, resultFullFilename);
}
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* eslint-disable no-console */
import * as process from 'process';
import { spawn } from 'child_process';

export const openapiGenerator = async (input, outputFolder) => {
return new Promise((resolve, reject) => {
const genCommand = 'node_modules/.bin/openapi-generator';
const genArgs = [
'generate',
'-Dmodels ',
'-DsupportingFiles ', // index.js
'--generator-name',
'typescript-fetch',
'--input-spec',
input,
'--output',
outputFolder,
];

console.log(genCommand, genArgs.join(' '));

const genProc = spawn(genCommand, genArgs, { env: process.env });
genProc.stdout.pipe(process.stdout);
genProc.stderr.on('data', (errData) => console.error(String(errData)));

genProc.on('close', (code) => {
if (code === 0) {
resolve(code);
} else {
console.error(`openapi-generator exited with code ${code}`);
reject(code);
}
});
});
};
30 changes: 30 additions & 0 deletions frontend/packages/kube-types/.tools/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* eslint-disable no-console */
import * as https from 'https';
import * as http from 'http';

const getClient = (url: string) => {
return url.startsWith('https') ? https : http;
};

export const download = (url: string) => {
return new Promise((resolve, reject) => {
console.log('downloading', url);
const result = [];
getClient(url)
.get(url, (response) => {
// response.setEncoding('utf8');
if (response.statusCode < 200 || response.statusCode >= 300) {
reject(new Error(`statusCode=${response.statusCode}`));
}
response.on('data', (chunk) => result.push(chunk));
response.on('end', () => {
try {
resolve(JSON.parse(Buffer.concat(result).toString()));
} catch (e) {
reject(e);
}
});
})
.on('error', reject);
});
};
Loading