-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
655 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import is from '@sindresorhus/is'; | ||
import yaml from 'js-yaml'; | ||
|
||
import { logger } from '../../logger'; | ||
import { PackageFile, PackageDependency, ExtractConfig } from '../common'; | ||
|
||
const isValidChartName = (name: string): boolean => { | ||
return name.match(/[!@#$%^&*(),.?":{}/|<>A-Z]/) === null; | ||
}; | ||
|
||
export function extractPackageFile( | ||
content: string, | ||
fileName: string, | ||
config: ExtractConfig | ||
): PackageFile { | ||
let deps = []; | ||
let doc; | ||
const aliases: Record<string, string> = {}; | ||
try { | ||
doc = yaml.safeLoad(content, { json: true }); | ||
} catch (err) { | ||
logger.debug({ err, fileName }, 'Failed to parse helmfile helmfile.yaml'); | ||
return null; | ||
} | ||
if (!(doc && is.array(doc.releases))) { | ||
logger.debug({ fileName }, 'helmfile.yaml has no releases'); | ||
return null; | ||
} | ||
|
||
if (doc.repositories) { | ||
for (let i = 0; i < doc.repositories.length; i += 1) { | ||
aliases[doc.repositories[i].name] = doc.repositories[i].url; | ||
} | ||
} | ||
logger.debug({ aliases }, 'repositories discovered.'); | ||
|
||
deps = doc.releases.map(dep => { | ||
let depName = dep.chart; | ||
let repoName = null; | ||
|
||
// If starts with ./ is for sure a local path | ||
if (dep.chart.startsWith('./')) { | ||
return { | ||
depName, | ||
skipReason: 'local-chart', | ||
} as PackageDependency; | ||
} | ||
|
||
if (dep.chart.includes('/')) { | ||
const v = dep.chart.split('/'); | ||
repoName = v.shift(); | ||
depName = v.join('/'); | ||
} else { | ||
repoName = dep.chart; | ||
} | ||
|
||
const res: PackageDependency = { | ||
depName, | ||
currentValue: dep.version, | ||
registryUrls: [aliases[repoName]] | ||
.concat([config.aliases[repoName]]) | ||
.filter(Boolean), | ||
}; | ||
|
||
// If version is null is probably a local chart | ||
if (!res.currentValue) { | ||
res.skipReason = 'local-chart'; | ||
} | ||
|
||
// By definition on helm the chart name should be lowecase letter + number + - | ||
// However helmfile support templating of that field | ||
if (!isValidChartName(res.depName)) { | ||
res.skipReason = 'unsupported-chart-type'; | ||
} | ||
|
||
// Skip in case we cannot locate the registry | ||
if (is.emptyArray(res.registryUrls)) { | ||
res.skipReason = 'unknown-registry'; | ||
} | ||
|
||
return res; | ||
}); | ||
|
||
return { deps, datasource: 'helm' } as PackageFile; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { extractPackageFile } from './extract'; | ||
export { updateDependency } from './update'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import _ from 'lodash'; | ||
import yaml from 'js-yaml'; | ||
import is from '@sindresorhus/is'; | ||
|
||
import { logger } from '../../logger'; | ||
import { Upgrade } from '../common'; | ||
|
||
// Return true if the match string is found at index in content | ||
function matchAt(content: string, index: number, match: string): boolean { | ||
return content.substring(index, index + match.length) === match; | ||
} | ||
|
||
// Replace oldString with newString at location index of content | ||
function replaceAt( | ||
content: string, | ||
index: number, | ||
oldString: string, | ||
newString: string | ||
): string { | ||
logger.debug(`Replacing ${oldString} with ${newString} at index ${index}`); | ||
return ( | ||
content.substr(0, index) + | ||
newString + | ||
content.substr(index + oldString.length) | ||
); | ||
} | ||
|
||
export function updateDependency( | ||
fileContent: string, | ||
upgrade: Upgrade | ||
): string | null { | ||
logger.trace({ config: upgrade }, 'updateDependency()'); | ||
if (!upgrade || !upgrade.depName || !upgrade.newValue) { | ||
logger.debug('Failed to update dependency, invalid upgrade'); | ||
return fileContent; | ||
} | ||
const doc = yaml.safeLoad(fileContent, { json: true }); | ||
if (!doc || !is.array(doc.releases)) { | ||
logger.debug('Failed to update dependency, invalid helmfile.yaml file'); | ||
return fileContent; | ||
} | ||
const { depName, newValue } = upgrade; | ||
const oldVersion = doc.releases.filter( | ||
dep => dep.chart.split('/')[1] === depName | ||
)[0].version; | ||
doc.releases = doc.releases.map(dep => | ||
dep.chart.split('/')[1] === depName ? { ...dep, version: newValue } : dep | ||
); | ||
const searchString = `${oldVersion}`; | ||
const newString = `${newValue}`; | ||
let newFileContent = fileContent; | ||
|
||
let searchIndex = newFileContent.indexOf('releases') + 'releases'.length; | ||
for (; searchIndex < newFileContent.length; searchIndex += 1) { | ||
// First check if we have a hit for the old version | ||
if (matchAt(newFileContent, searchIndex, searchString)) { | ||
logger.trace(`Found match at index ${searchIndex}`); | ||
// Now test if the result matches | ||
newFileContent = replaceAt( | ||
newFileContent, | ||
searchIndex, | ||
searchString, | ||
newString | ||
); | ||
} | ||
} | ||
// Compare the parsed yaml structure of old and new | ||
if (!_.isEqual(doc, yaml.safeLoad(newFileContent, { json: true }))) { | ||
logger.trace(`Mismatched replace: ${newFileContent}`); | ||
newFileContent = fileContent; | ||
} | ||
|
||
return newFileContent; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
test/manager/helmfile/__snapshots__/extract.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`lib/manager/helmfile/extract extractPackageFile() skip chart that does not have specified version 1`] = ` | ||
Object { | ||
"datasource": "helm", | ||
"deps": Array [ | ||
Object { | ||
"currentValue": undefined, | ||
"depName": "example", | ||
"registryUrls": Array [ | ||
"https://kubernetes-charts.storage.googleapis.com/", | ||
], | ||
"skipReason": "local-chart", | ||
}, | ||
], | ||
} | ||
`; | ||
|
||
exports[`lib/manager/helmfile/extract extractPackageFile() skip chart with special character in the name 1`] = ` | ||
Object { | ||
"datasource": "helm", | ||
"deps": Array [ | ||
Object { | ||
"currentValue": "1.0.0", | ||
"depName": "example/example", | ||
"registryUrls": Array [ | ||
"https://kiwigrid.github.io", | ||
], | ||
"skipReason": "unsupported-chart-type", | ||
}, | ||
Object { | ||
"currentValue": "1.0.0", | ||
"depName": "example?example", | ||
"registryUrls": Array [ | ||
"https://kiwigrid.github.io", | ||
], | ||
"skipReason": "unsupported-chart-type", | ||
}, | ||
], | ||
} | ||
`; | ||
|
||
exports[`lib/manager/helmfile/extract extractPackageFile() skip chart with unknown repository 1`] = ` | ||
Object { | ||
"datasource": "helm", | ||
"deps": Array [ | ||
Object { | ||
"currentValue": "1.0.0", | ||
"depName": "example", | ||
"registryUrls": Array [], | ||
"skipReason": "unknown-registry", | ||
}, | ||
], | ||
} | ||
`; | ||
|
||
exports[`lib/manager/helmfile/extract extractPackageFile() skip if repository details are not specified 1`] = ` | ||
Object { | ||
"datasource": "helm", | ||
"deps": Array [ | ||
Object { | ||
"currentValue": "1.0.0", | ||
"depName": "example", | ||
"registryUrls": Array [], | ||
"skipReason": "unknown-registry", | ||
}, | ||
], | ||
} | ||
`; | ||
|
||
exports[`lib/manager/helmfile/extract extractPackageFile() skip local charts 1`] = ` | ||
Object { | ||
"datasource": "helm", | ||
"deps": Array [ | ||
Object { | ||
"depName": "./charts/example", | ||
"skipReason": "local-chart", | ||
}, | ||
], | ||
} | ||
`; | ||
|
||
exports[`lib/manager/helmfile/extract extractPackageFile() skip templetized release with invalid characters 1`] = ` | ||
Object { | ||
"datasource": "helm", | ||
"deps": Array [ | ||
Object { | ||
"currentValue": "1.0.0", | ||
"depName": "{{\`{{ .Release.Name }}\`}}", | ||
"registryUrls": Array [ | ||
"https://kubernetes-charts.storage.googleapis.com/", | ||
], | ||
"skipReason": "unsupported-chart-type", | ||
}, | ||
Object { | ||
"currentValue": "1.0.0", | ||
"depName": "example", | ||
"registryUrls": Array [ | ||
"https://kubernetes-charts.storage.googleapis.com/", | ||
], | ||
}, | ||
], | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`lib/manager/helmfile/extract updateDependency() upgrades dependency if chart is repeated 1`] = ` | ||
" | ||
repositories: | ||
- name: kiwigrid | ||
url: https://kiwigrid.github.io | ||
releases: | ||
- name: fluentd-elasticsearch-internal | ||
version: 5.3.1 | ||
chart: kiwigrid/fluentd-elasticsearch | ||
- name: nginx-ingress | ||
version: 1.3.0 | ||
chart: stable/nginx-ingress | ||
- name: fluentd-elasticsearch-external | ||
version: 5.3.1 | ||
chart: kiwigrid/fluentd-elasticsearch | ||
" | ||
`; | ||
|
||
exports[`lib/manager/helmfile/extract updateDependency() upgrades dependency if valid upgrade 1`] = ` | ||
" | ||
repositories: | ||
- name: kiwigrid | ||
url: https://kiwigrid.github.io | ||
releases: | ||
- name: fluentd-elasticsearch | ||
version: 5.3.1 | ||
chart: kiwigrid/fluentd-elasticsearch | ||
" | ||
`; | ||
|
||
exports[`lib/manager/helmfile/extract updateDependency() upgrades dependency if version field comes before name field 1`] = ` | ||
" | ||
repositories: | ||
- name: kiwigrid | ||
url: https://kiwigrid.github.io | ||
releases: | ||
- version: 5.3.1 | ||
name: fluentd-elasticsearch | ||
chart: kiwigrid/fluentd-elasticsearch | ||
" | ||
`; |
Oops, something went wrong.