Fix cluster yaml issue to keep consistent with RKE

https://github.com/rancher/rancher/issues/14750
This commit is contained in:
loganhz 2018-07-26 14:23:24 +08:00
parent 9d47de4ed3
commit 1d43526193
2 changed files with 46 additions and 22 deletions

View File

@ -6,9 +6,7 @@ import {
import { satisfies } from 'shared/utils/parse-version';
import { sortVersions } from 'shared/utils/sort';
import { inject as service } from '@ember/service';
import {
camelToUnderline, underlineToCamel, removeEmpty, keysToCamel, validateEndpoint
} from 'shared/utils/util';
import { underlineToCamel, removeEmpty, keysToCamel, validateEndpoint } from 'shared/utils/util';
import C from 'shared/utils/constants';
import YAML from 'npm:yamljs';
import json2yaml from 'npm:json2yaml';
@ -18,6 +16,19 @@ import { isEmpty } from '@ember/utils';
import InputTextFile from 'ui/components/input-text-file/component';
import { scheduleOnce } from '@ember/runloop';
const EXCLUDED_KEYS = [];
function camelToUnderline(str, split = true) {
str = (str || '');
if ( str.indexOf('-') > -1 || str.endsWith('CloudProvider')) {
return str;
} else if ( split ) {
return (str || '').dasherize().split('-').join('_');
} else {
return (str || '').dasherize();
}
}
const NETWORKCHOICES = [
{
label: 'clusterNew.rke.network.flannel',
@ -321,10 +332,10 @@ export default InputTextFile.extend(ClusterDriver, {
return '';
}
config = removeEmpty(config);
config = removeEmpty(config, EXCLUDED_KEYS);
while ( JSON.stringify(config) !== JSON.stringify(removeEmpty(config)) ){
config = removeEmpty(config);
while ( JSON.stringify(config) !== JSON.stringify(removeEmpty(config, EXCLUDED_KEYS)) ){
config = removeEmpty(config, EXCLUDED_KEYS);
}
let yaml = json2yaml.stringify(config);
@ -465,10 +476,35 @@ export default InputTextFile.extend(ClusterDriver, {
});
},
findExcludedKeys(resourceFields) {
Object.keys(resourceFields).forEach((key) => {
const type = resourceFields[key].type;
if ( type.startsWith('map[') ) {
const t = type.slice(4, type.length - 1);
const s = get(this, 'globalStore').getById('schema', t.toLowerCase());
if ( s ) {
const underlineKey = camelToUnderline(key);
if ( EXCLUDED_KEYS.indexOf(underlineKey) === -1 ) {
EXCLUDED_KEYS.push(underlineKey);
}
}
}
});
},
getResourceFields(type) {
const schema = get(this, 'globalStore').getById('schema', type.toLowerCase());
let resourceFields = null;
return schema ? get(schema, 'resourceFields') : null;
if ( schema ) {
resourceFields = get(schema, 'resourceFields');
this.findExcludedKeys(resourceFields);
}
return resourceFields;
},
getFieldValue(field, type) {

View File

@ -390,26 +390,15 @@ function keyNotType(k) {
return Object.keys(k).filter((key) => key !== 'type').length > 0;
}
export function removeEmpty(obj){
export function removeEmpty(obj, excludedKeys = []){
return Object.keys(obj)
.filter((k) => !isEmpty(obj[k]) && (typeof obj[k] !== 'object' || keyNotType(obj[k])))
.reduce((newObj, k) => !isArray(obj[k]) && typeof obj[k] === 'object' ?
Object.assign(newObj, { [k]: removeEmpty(obj[k]) }) :
.reduce((newObj, k) => !isArray(obj[k]) && typeof obj[k] === 'object' && excludedKeys.indexOf(k) === -1 ?
Object.assign(newObj, { [k]: removeEmpty(obj[k], excludedKeys) }) :
Object.assign(newObj, { [k]: obj[k] }),
{});
}
export function camelToUnderline(str, split = true) {
str = (str || '');
if ( str.indexOf('-') > -1 ) {
return str;
} else if ( split ) {
return (str || '').dasherize().split('-').join('_');
} else {
return (str || '').dasherize();
}
}
export function underlineToCamel(str) {
return (str || '').split('_').map((t, i) => {
if ( i === 0 ) {
@ -449,7 +438,6 @@ var Util = {
arrayIntersect,
compareDisplayEndpoint,
camelToTitle,
camelToUnderline,
convertToMillis,
constructUrl,
download,