blob: d6d93ca7316c9103e8239e5e108cb6f7b4a5bac8 [file] [log] [blame]
Ruslan Gustomiasov5d131b62019-08-21 11:51:26 +02001package com.mirantis.mk
2
3/**
4 *
5 * Functions to work with Helm
6 *
7 */
8
9/**
10 * Build index file for helm chart
Sergey Otpuschennikov50b248c2019-08-28 17:21:18 +040011 * @param extra_params additional params, e.g. --url repository_URL
12 * @param charts_dir path to a directory
Ruslan Gustomiasov5d131b62019-08-21 11:51:26 +020013 */
14
Sergey Otpuschennikov50b248c2019-08-28 17:21:18 +040015def helmRepoIndex(extra_params='', charts_dir='.'){
16 sh("helm repo index ${extra_params} ${charts_dir}")
17}
Mykyta Karpin3c78c0f2019-09-11 18:11:06 +030018
19/**
Sergey Otpuschennikov958f2872019-10-16 17:04:33 +040020 * Rebuild index file for helm chart repo
Sergey Otpuschennikovcd9b5262021-02-02 20:02:06 +040021 * @param absHelmRepoUrl if set to true, URLs to charts will be absolute
22 * @param helmRepoUrl repository with helm charts
23 * @param md5Remote md5 sum of index.yaml for check
Sergey Otpuschennikov958f2872019-10-16 17:04:33 +040024 */
25
Sergey Otpuschennikovcd9b5262021-02-02 20:02:06 +040026def helmMergeRepoIndex(helmRepoUrl, md5Remote='', absHelmRepoUrl=true) {
Sergey Otpuschennikov958f2872019-10-16 17:04:33 +040027 def common = new com.mirantis.mk.Common()
28
29 def helmRepoDir = '.'
Sergey Otpuschennikovcd9b5262021-02-02 20:02:06 +040030 def helmExtraParams = ''
31 if (absHelmRepoUrl) {
32 helmExtraParams = "--url ${helmRepoUrl}"
33 }
Sergey Otpuschennikov958f2872019-10-16 17:04:33 +040034
35 def indexRes = common.shCmdStatus("wget -O index-upstream.yaml ${helmRepoUrl}/index.yaml")
Sergey Otpuschennikovcd9b5262021-02-02 20:02:06 +040036
Sergey Otpuschennikov958f2872019-10-16 17:04:33 +040037 if (indexRes['status']){
38 if (indexRes['status'] == 8 && indexRes['stderr'].contains('ERROR 404') && !md5Remote) {
39 common.warningMsg("Index.yaml not found in ${helmRepoUrl} and will be fully regenerated")
40 } else {
41 error("Something went wrong during index.yaml download: ${indexRes['stderr']}")
42 }
43 } else {
44 if (md5Remote) {
45 def md5Local = sh(script: "md5sum index-upstream.yaml | cut -d ' ' -f 1", returnStdout: true).readLines()[0]
46 if (md5Local != md5Remote) {
47 error 'Target repository already exist, but upstream index.yaml broken or not found'
48 }
49 }
50 helmExtraParams += " --merge index-upstream.yaml"
51 }
Sergey Otpuschennikov892b4e72019-10-29 14:54:08 +040052 helmRepoIndex(helmExtraParams, helmRepoDir)
Sergey Otpuschennikov958f2872019-10-16 17:04:33 +040053}
54
55/**
Mykyta Karpin3c78c0f2019-09-11 18:11:06 +030056 * Generates version for helm chart based on information from git repository. Tries to search
57 * first parent git tag using pattern '[0-9]*-{tagSuffix}', if found that tag will be used
58 * in final version, if not found - version will be formed as '{defaultVersion}-{tagSuffix}'. Number
59 * of commits since last tag or sha of current commit can be added to version.
60 *
61 * @param repoDir string, path to a directory with git repository of helm charts
62 * @param devVersion Boolean, if set to true development version will be calculated e.g 0.1.0-mcp-{sha of current commit}
63 * @param increment Boolean, if set to true patch version will be incremented (e.g 0.1.0 -> 0.1.1)
64 * @param defaultVersion string, value of version which will be used in case no tags found. should be semver2 compatible
65 * @param tagSuffix string, suffix which will be used for finding tags in git repository, also if tag not found, it
66 * it will be added to {defaultVersion} e.g {defaultVersion}-{tagSuffix}
67 */
68
69def generateChartVersionFromGit(repoDir, devVersion = true, increment = false, defaultVersion = '0.1.0', tagSuffix = 'mcp') {
70 def common = new com.mirantis.mk.Common()
71 def git = new com.mirantis.mk.Git()
Mykyta Karpine7554842019-12-11 17:00:29 +020072 String initialVersion = "${defaultVersion}"
Mykyta Karpin3c78c0f2019-09-11 18:11:06 +030073 String countRange
74 String versionData
Mykyta Karpine7554842019-12-11 17:00:29 +020075 String tagPattern = "[0-9]*"
76 if (tagSuffix) {
77 tagPattern = "${tagPattern}-${tagSuffix}"
78 initialVersion = "${initialVersion}-${tagSuffix}"
79 }
Mykyta Karpin3c78c0f2019-09-11 18:11:06 +030080 dir(repoDir){
81 Map cmd = common.shCmdStatus("git describe --tags --first-parent --abbrev=0 --match ${tagPattern}")
82 String lastTag = cmd['stdout'].trim()
83
84 if (cmd['status'] != 0){
85 if (cmd['stderr'].contains('fatal: No names found, cannot describe anything')){
86 common.warningMsg("No parent git tag found, using initial version ${initialVersion}")
87 versionData = initialVersion
88 countRange = 'HEAD'
89 } else {
90 error("Something went wrong, cannot find git information ${cmd['stderr']}")
91 }
92 } else {
93 versionData = lastTag
94 countRange = "${lastTag}..HEAD"
95 }
96 List versionParts = versionData.tokenize('-')
97
Mykyta Karpine7554842019-12-11 17:00:29 +020098 if (!common.isSemVer(versionData)){
99 error "Version ${versionData} is not in semver2 format"
Mykyta Karpin3c78c0f2019-09-11 18:11:06 +0300100 }
Mykyta Karpine7554842019-12-11 17:00:29 +0200101 if (tagSuffix && versionParts.size() == 2 && versionParts[1] != tagSuffix){
102 error "Tag suffix ${tagSuffix} was specified but not found in ${versionData}"
103 }
104 String commitsSinceTag = sh(script: "git rev-list --count ${countRange}", returnStdout: true).trim()
105 String commitSha = sh(script: 'git rev-parse --short=7 HEAD', returnStdout: true).trim()
106
107 if (commitsSinceTag == '0'){
108 return versionData
109 }
110
111 if (devVersion){
112 versionParts.add(commitSha)
113 } else {
114 versionParts.add(commitsSinceTag)
115 }
116 // Patch version will be incremented e.g. 0.1.0 -> 0.1.1
117 if (increment) {
118 versionParts[0] = git.incrementVersion(versionParts[0])
119 }
120 return versionParts.join('-')
Mykyta Karpin3c78c0f2019-09-11 18:11:06 +0300121 }
Mykyta Karpin6f050b22019-09-24 13:57:20 +0300122}
123
124/**
125 * Takes a list of dependencies and a version, and sets a version for each dependency in requirements.yaml. If dependency isn't
126 * found in requirements.yaml or requirements.yaml does not exist - does nothing.
127 *
128 * @param chartPath string, path to a directory with helm chart
129 * @param dependencies list of hashes with names and versions of dependencies in format:
130 * [['name': 'chart-name1', 'version': '0.1.0-myversion'], ['name': 'chart-name2', 'version': '0.2.0-myversion']]
131 */
132
133def setChartDependenciesVersion(chartPath, List dependencies){
134 def common = new com.mirantis.mk.Common()
135 if (!dependencies){
136 error 'No list of target dependencies is specified'
137 }
138 def reqsFilePath = "${chartPath}/requirements.yaml"
139 def chartYaml = readYaml file: "${chartPath}/Chart.yaml"
140 def reqsUpdateNeeded = false
141 def reqsMap = [:]
142 if (fileExists(reqsFilePath)){
143 reqsMap = readYaml file: reqsFilePath
144 for (i in dependencies) {
145 for (item in reqsMap.get('dependencies', [])){
146 if (item['name'] == i['name']){
Mykyta Karpin882dd362019-09-25 11:27:55 +0300147 common.infoMsg("Set version ${i['version']} for dependency ${i['name']} in chart ${chartYaml['name']}")
Mykyta Karpin6f050b22019-09-24 13:57:20 +0300148 item['version'] = i['version']
149 reqsUpdateNeeded = true
150 }
151 }
152 }
153 }
Oleksandr Kononenkoc5b3f972021-10-21 14:31:44 +0300154 else {
155 common.warningMsg("requirements.yaml doesn't exist at path ${reqsFilePath} or chart doesn't contain ${dependencies}, nothing to set")
156 }
Mykyta Karpin6f050b22019-09-24 13:57:20 +0300157 if (reqsUpdateNeeded){
158 sh "rm ${reqsFilePath}"
159 writeYaml file: reqsFilePath, data: reqsMap
Mykyta Karpin6f050b22019-09-24 13:57:20 +0300160 }
Sergey Otpuschennikov958f2872019-10-16 17:04:33 +0400161}
Oleksandr Kononenkoc5b3f972021-10-21 14:31:44 +0300162
163/**
164 * Takes a list of dependencies and a version, and sets a version for each dependency in requirements.yaml. If dependency isn't
165 * found in requirements.yaml or requirements.yaml does not exist - does nothing.
166 *
167 * @param chartPath string, path to a directory with helm chart
168 * @param version string, new version chart that needed to set
169 */
170
171def setChartVersion(chartPath, version){
172 def chartFile = "${chartPath}/Chart.yaml"
173 def chartYaml = readYaml file: chartFile
174 chartYaml['version'] = version
175 sh "rm ${chartFile}"
176 writeYaml file: chartFile, data: chartYaml
177}