Skip to content

Commit

Permalink
Merge pull request #1652 from mantis-toboggan-md/workload-validation
Browse files Browse the repository at this point in the history
workload validation
  • Loading branch information
vincent99 authored Oct 30, 2020
2 parents 3a48f80 + 71585a5 commit c2a4ca6
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 4 deletions.
1 change: 1 addition & 0 deletions assets/translations/en-us.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,7 @@ validation:
flowOutput:
both: Requires "Output" or "Cluster Output" to be selected.
global: Requires "Cluster Output" to be selected.
invalidCron: Invalid cron schedule
k8s:
identifier:
emptyLabel: '"{key}" cannot have an empty key'
Expand Down
60 changes: 60 additions & 0 deletions models/workload.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,66 @@ export default {
return out;
},

customValidationRules() {
const out = [
{
nullable: false,
path: 'metadata.name',
required: true,
translationKey: 'generic.name',
type: 'dnsLabel',
},
{
nullable: false,
path: 'spec',
required: true,
type: 'object',
validators: ['containerImages'],
}
];

const type = this._type ? this._type : this.type;

switch (type) {
case WORKLOAD_TYPES.DEPLOYMENT:
case WORKLOAD_TYPES.REPLICA_SET:
out.push( {
nullable: false,
path: 'spec.replicas',
required: true,
type: 'number',
translationKey: 'workload.replicas'
});
break;
case WORKLOAD_TYPES.STATEFUL_SET:
out.push({
nullable: false,
path: 'spec.replicas',
required: true,
type: 'number',
translationKey: 'workload.replicas'
});
out.push({
nullable: false,
path: 'spec.serviceName',
required: true,
type: 'string',
translationKey: 'workload.serviceName'
});
break;
case WORKLOAD_TYPES.CRON_JOB:
out.push( {
nullable: false,
path: 'spec.schedule',
required: true,
type: 'string',
validators: ['cronSchedule'],
});
}

return out;
},

container() {
if (this.type === WORKLOAD_TYPES.CRON_JOB) {
// cronjob pod template is nested slightly different than other types
Expand Down
1 change: 1 addition & 0 deletions plugins/steve/resource-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,7 @@ export default {
type: fieldType,
} = rule;
let pathValue = get(data, path) || null;

const parsedRules = compact((validators || []));
let displayKey = path;

Expand Down
6 changes: 5 additions & 1 deletion utils/custom-validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { flowOutput } from '@/utils/validators/flow-output';
import { clusterIp, externalName, servicePort } from '@/utils/validators/service';
import { ruleGroups, groupsAreValid } from '@/utils/validators/prometheusrule';
import { interval, matching } from '@/utils/validators/monitoring-route';
import { containerImages } from '@/utils/validators/container-images';
import { cronSchedule } from '@/utils/validators/cron-schedule';

/**
* Custom validation functions beyond normal scalr types
Expand All @@ -16,5 +18,7 @@ export default {
ruleGroups,
interval,
servicePort,
matching
matching,
containerImages,
cronSchedule
};
18 changes: 18 additions & 0 deletions utils/validators/container-images.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export function containerImages(spec, getters, errors) {
let container;

if (spec.jobTemplate) {
// cronjob pod template is nested slightly different than other types
const { jobTemplate: { spec: { template: { spec: { containers = [] } } } } } = spec;

container = containers[0] || {};
} else {
const { template:{ spec:{ containers = [] } } } = spec;

container = containers[0] || {};
}

if (!container.image) {
errors.push(getters['i18n/t']('validation.required', { key: getters['i18n/t']('workload.container.image') }));
}
}
9 changes: 9 additions & 0 deletions utils/validators/cron-schedule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import cronstrue from 'cronstrue';

export function cronSchedule(schedule = '', getters, errors) {
try {
cronstrue.toString(schedule);
} catch (e) {
errors.push(getters['i18n/t']('validation.invalidCron'));
}
}
8 changes: 5 additions & 3 deletions utils/validators/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ export function validateLength(val, field, displayKey, getters, errors = []) {
} = field;
const len = val ? get(val, 'length') : 0;

if ( !nullable && required && isEmpty(val) ) {
errors.push(getters['i18n/t']('validation.required', { key: displayKey }));
if ( !nullable && required) {
if ((typeof val === 'object' && isEmpty(val)) || !val) {
errors.push(getters['i18n/t']('validation.required', { key: displayKey }));

return errors;
return errors;
}
}

if ( val === null ) {
Expand Down

0 comments on commit c2a4ca6

Please sign in to comment.