Skip to content

Commit 5c22973

Browse files
Thomas Michaelschnatterer
authored andcommitted
add feature validation
1 parent 9e82b1f commit 5c22973

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

src/main/groovy/com/cloudogu/gitops/Application.groovy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ class Application {
2626
log.debug("Starting Application")
2727

2828
setNamespaceListToConfig(config)
29+
30+
features.forEach(feature -> {
31+
feature.validate()
32+
})
2933
features.forEach(feature -> {
3034
feature.install()
3135
})

src/main/groovy/com/cloudogu/gitops/Feature.groovy

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,19 @@ abstract class Feature {
7171
}
7272

7373
abstract boolean isEnabled()
74-
74+
75+
76+
7577
/*
7678
* Hooks for enabling or disabling a feature. Both optional, because not always needed.
7779
*/
7880
protected void enable() {}
7981
protected void disable() {}
82+
83+
/*
84+
* Hook for special feature validation. Optional.
85+
* Feature should throw RuntimeException to stop immediately.
86+
*/
87+
protected void validate() { }
88+
8089
}

src/main/groovy/com/cloudogu/gitops/features/Content.groovy

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class Content extends Feature {
4343
protected Jenkins jenkins
4444
// set by lazy initialisation
4545
private TemplatingEngine templatingEngine
46+
// used to clone repos in validation phase
47+
private List<RepoCoordinate> cachedRepoCoordinates = new ArrayList<>()
4648

4749
Content(
4850
Config config, K8sClient k8sClient, ScmmRepoProvider repoProvider, ScmmApiClient scmmApiClient, Jenkins jenkins
@@ -66,6 +68,16 @@ class Content extends Feature {
6668
createContentRepos()
6769
}
6870

71+
@Override
72+
void validate() {
73+
try {
74+
cachedRepoCoordinates = cloneContentRepos()
75+
} catch (Exception e) {
76+
log.error('Error validate Content Feature', e)
77+
throw new RuntimeException('Feature content has problem. Please check configuration.')
78+
}
79+
}
80+
6981
void createImagePullSecrets() {
7082
if (config.registry.createImagePullSecrets) {
7183
String registryUsername = config.registry.readOnlyUsername ?: config.registry.username
@@ -93,19 +105,19 @@ class Content extends Feature {
93105
}
94106

95107
void createContentRepos() {
96-
List<RepoCoordinate> repoCoordinates = cloneContentRepos()
97-
pushTargetRepos(repoCoordinates)
108+
if (cachedRepoCoordinates.empty) {
109+
cachedRepoCoordinates = cloneContentRepos()
110+
}
111+
pushTargetRepos(cachedRepoCoordinates)
112+
cachedRepoCoordinates.clear()
98113
}
99114

100115
protected List<RepoCoordinate> cloneContentRepos() {
101116
List<RepoCoordinate> repoCoordinates = []
102117
def mergedReposFolder = File.createTempDir('gitops-playground-folder-based-content-repos-')
103118
mergedReposFolder.deleteOnExit()
104-
105-
106119
log.debug("Aggregating folder structure for all ${config.content.repos.size()} folder based-repos")
107120
config.content.repos.each { repoConfig ->
108-
109121
createRepoCoordinates(repoConfig, mergedReposFolder, repoCoordinates)
110122
}
111123
return repoCoordinates
@@ -127,7 +139,7 @@ class Content extends Feature {
127139
cloneToLocalFolder(repoConfig, repoTmpDir)
128140

129141
def contentRepoDir = new File(repoTmpDir, repoConfig.path)
130-
applyTemplatingIfNeeded(repoConfig, contentRepoDir)
142+
applyTemplatingIfApplicable(repoConfig, contentRepoDir)
131143

132144

133145
switch (repoConfig.type) {
@@ -143,7 +155,6 @@ class Content extends Feature {
143155
addRepoCoordinates(repoCoordinates, repoCoordinate)
144156
break
145157
}
146-
147158
log.debug("Finished cloning content repos. repoCoordinates=${repoCoordinates}")
148159
}
149160

@@ -166,11 +177,9 @@ class Content extends Feature {
166177
String repoName = contentRepoRepoDir.name
167178
def repoCoordinate = mergeRepoDirs(contentRepoRepoDir, namespace, repoName, mergedReposFolder, repoConfig)
168179
repoCoordinate.refIsTag = refIsTag
169-
170180
addRepoCoordinates(repoCoordinates, repoCoordinate)
171181
}
172182
}
173-
174183
}
175184

176185
private static RepoCoordinate createRepoCoordinateForTypeMirror(ContentRepositorySchema repoConfig, File repoTmpDir) {
@@ -205,7 +214,6 @@ class Content extends Feature {
205214
clonedContentRepo: target,
206215
repoConfig: repoConfig,
207216
)
208-
// addRepoCoordinates(repoCoordinates, repoCoordinate) // TODO: Thomas delete line
209217
return repoCoordinate
210218
}
211219

@@ -217,7 +225,7 @@ class Content extends Feature {
217225
}
218226
}
219227

220-
private void applyTemplatingIfNeeded(ContentRepositorySchema repoConfig, File srcPath) {
228+
private void applyTemplatingIfApplicable(ContentRepositorySchema repoConfig, File srcPath) {
221229
if (repoConfig.templating) {
222230
def engine = getTemplatingEngine()
223231
engine.replaceTemplates(srcPath, [
@@ -302,9 +310,9 @@ class Content extends Feature {
302310
break
303311
}
304312

305-
createJenkinsJobIfAppplicable(repoCoordinate, targetRepo)
313+
createJenkinsJobIfApplicable(repoCoordinate, targetRepo)
306314

307-
// cleaning
315+
// cleaning tmp folders
308316
repoCoordinate.clonedContentRepo.deleteDir()
309317
new File(targetRepo.absoluteLocalRepoTmpDir).deleteDir()
310318
} // no else needed
@@ -404,7 +412,7 @@ class Content extends Feature {
404412
}
405413
}
406414

407-
protected void createJenkinsJobIfAppplicable(RepoCoordinate repoCoordinate, ScmmRepo repo) {
415+
protected void createJenkinsJobIfApplicable(RepoCoordinate repoCoordinate, ScmmRepo repo) {
408416
if (repoCoordinate.repoConfig.createJenkinsJob && jenkins.isEnabled()) {
409417
if (existFileInSomeBranch(repo.absoluteLocalRepoTmpDir, 'Jenkinsfile')) {
410418
jenkins.createJenkinsjob(repoCoordinate.namespace, repoCoordinate.namespace)
@@ -521,10 +529,7 @@ class Content extends Feature {
521529
}
522530
}
523531
/**
524-
* INIT only for new and not for existing repos.
525-
* @param repo
526-
* @param repoCoordinate
527-
* @return
532+
* Checks whether the repo already exists and overwrite Mode matches.
528533
*/
529534
boolean isValidForPush(ScmmRepo repo, RepoCoordinate repoCoordinate) {
530535
def isRepoCreated = repo.create('', scmmApiClient)

0 commit comments

Comments
 (0)