|
| 1 | +import { |
| 2 | + Configs, |
| 3 | + KubernetesObject, |
| 4 | + kubernetesObjectResult, |
| 5 | + Result, |
| 6 | +} from 'kpt-functions'; |
| 7 | +import { ChildProcess, spawn } from 'child_process'; |
| 8 | +import { Writable } from 'stream'; |
| 9 | + |
| 10 | +const SCHEMA_LOCATION = 'schema_location'; |
| 11 | +const ADDITIONAL_SCHEMA_LOCATIONS = 'additional_schema_locations'; |
| 12 | +const IGNORE_MISSING_SCHEMAS = 'ignore_missing_schemas'; |
| 13 | +const SKIP_KINDS = 'skip_kinds'; |
| 14 | +const STRICT = 'strict'; |
| 15 | + |
| 16 | +type Feedback = FeedbackItem[]; |
| 17 | + |
| 18 | +interface FeedbackItem { |
| 19 | + filename: string; |
| 20 | + kind: string; |
| 21 | + status: 'valid' | 'invalid'; |
| 22 | + errors: string[]; |
| 23 | +} |
| 24 | + |
| 25 | +export async function kubeval(configs: Configs): Promise<void> { |
| 26 | + const schemaLocation = configs.getFunctionConfigValue(SCHEMA_LOCATION); |
| 27 | + const additionalSchemaLocationsStr = configs.getFunctionConfigValue( |
| 28 | + ADDITIONAL_SCHEMA_LOCATIONS |
| 29 | + ); |
| 30 | + const additionalSchemaLocations = additionalSchemaLocationsStr |
| 31 | + ? additionalSchemaLocationsStr.split(',') |
| 32 | + : []; |
| 33 | + const ignoreMissingSchemas = JSON.parse( |
| 34 | + configs.getFunctionConfigValue(IGNORE_MISSING_SCHEMAS) || 'false' |
| 35 | + ); |
| 36 | + const skipKindsStr = configs.getFunctionConfigValue(SKIP_KINDS); |
| 37 | + const skipKinds = skipKindsStr ? skipKindsStr.split(',') : []; |
| 38 | + const strict = JSON.parse(configs.getFunctionConfigValue(STRICT) || 'false'); |
| 39 | + |
| 40 | + const results: Result[] = []; |
| 41 | + |
| 42 | + for (const object of configs.getAll()) { |
| 43 | + await runKubeval( |
| 44 | + object, |
| 45 | + results, |
| 46 | + schemaLocation, |
| 47 | + additionalSchemaLocations, |
| 48 | + ignoreMissingSchemas, |
| 49 | + skipKinds, |
| 50 | + strict |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + if (results.length > 0) { |
| 55 | + configs.addResults(...results); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +async function runKubeval( |
| 60 | + object: KubernetesObject, |
| 61 | + results: Result[], |
| 62 | + schemaLocation?: string, |
| 63 | + additionalSchemaLocations?: string[], |
| 64 | + ignoreMissingSchemas?: boolean, |
| 65 | + skipKinds?: string[], |
| 66 | + strict?: boolean |
| 67 | +): Promise<void> { |
| 68 | + const args = ['--output', 'json']; |
| 69 | + |
| 70 | + if (schemaLocation) { |
| 71 | + args.push('--schema-location'); |
| 72 | + args.push(schemaLocation); |
| 73 | + } |
| 74 | + |
| 75 | + if (additionalSchemaLocations) { |
| 76 | + args.push('--additional-schema-locations'); |
| 77 | + args.push(additionalSchemaLocations.join(',')); |
| 78 | + } |
| 79 | + |
| 80 | + if (ignoreMissingSchemas) { |
| 81 | + args.push('--ignore-missing-schemas'); |
| 82 | + } |
| 83 | + |
| 84 | + if (skipKinds) { |
| 85 | + args.push('--skip-kinds'); |
| 86 | + args.push(skipKinds.join(',')); |
| 87 | + } |
| 88 | + |
| 89 | + if (strict) { |
| 90 | + args.push('--strict'); |
| 91 | + } |
| 92 | + |
| 93 | + const kubevalProcess = spawn('kubeval', args, { |
| 94 | + stdio: ['pipe', 'pipe', process.stderr], |
| 95 | + }); |
| 96 | + const serializedObject = JSON.stringify(object); |
| 97 | + await writeToStream(kubevalProcess.stdin, serializedObject); |
| 98 | + kubevalProcess.stdin.end(); |
| 99 | + const rawOutput = await readStdoutToString(kubevalProcess); |
| 100 | + try { |
| 101 | + const feedback = JSON.parse(rawOutput) as Feedback; |
| 102 | + |
| 103 | + for (const { status, errors } of feedback) { |
| 104 | + if (status !== 'valid') { |
| 105 | + for (const error of errors) { |
| 106 | + const [path, ...rest] = error.split(':'); |
| 107 | + let result; |
| 108 | + if (rest.length > 0) { |
| 109 | + result = kubernetesObjectResult( |
| 110 | + rest.join(':').trim(), |
| 111 | + object, |
| 112 | + { |
| 113 | + path, |
| 114 | + }, |
| 115 | + 'error' |
| 116 | + ); |
| 117 | + } else { |
| 118 | + result = kubernetesObjectResult(error, object, undefined, 'error'); |
| 119 | + } |
| 120 | + results.push(result); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } catch (error) { |
| 125 | + results.push( |
| 126 | + kubernetesObjectResult( |
| 127 | + 'Failed to parse raw kubeval output:\n' + |
| 128 | + error.message + |
| 129 | + '\n\n' + |
| 130 | + rawOutput, |
| 131 | + object |
| 132 | + ) |
| 133 | + ); |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +function writeToStream(stream: Writable, data: string): Promise<void> { |
| 138 | + return new Promise((resolve, reject) => |
| 139 | + stream.write(data, 'utf-8', err => (err ? reject(err) : resolve())) |
| 140 | + ); |
| 141 | +} |
| 142 | + |
| 143 | +function readStdoutToString(childProcess: ChildProcess): Promise<string> { |
| 144 | + return new Promise<string>(resolve => { |
| 145 | + let result = ''; |
| 146 | + childProcess.stdout!!.on('data', data => { |
| 147 | + result += data.toString(); |
| 148 | + }); |
| 149 | + childProcess.on('close', () => { |
| 150 | + resolve(result); |
| 151 | + }); |
| 152 | + }); |
| 153 | +} |
| 154 | + |
| 155 | +kubeval.usage = ` |
| 156 | +Validates configuration using kubeval. |
| 157 | +
|
| 158 | +Configured using a ConfigMap with the following keys: |
| 159 | +${SCHEMA_LOCATION}: Comma-seperated list of secondary base URLs used to download schemas. |
| 160 | +${ADDITIONAL_SCHEMA_LOCATIONS}: List of secondary base URLs used to download schemas. |
| 161 | +${IGNORE_MISSING_SCHEMAS}: Skip validation for resource definitions without a schema. |
| 162 | +${SKIP_KINDS}: Comma-separated list of case-sensitive kinds to skip when validating against schemas. |
| 163 | +${STRICT}: Disallow additional properties not in schema. |
| 164 | +`; |
0 commit comments