Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ mainBranch: 'main'

## Stages
The GitOps-build-lib supports builds on multiple stages. A stage is defined by a name and contains a namespace (used to
generate the resources) and a deployment-flag.
generate the resources) and a deployment-flag. If no stages is passed into the gitops-config by the user, the default
is set to:

```groovy
def gitopsConfig = [
Expand All @@ -287,8 +288,9 @@ def gitopsConfig = [
]
```

The defaults above can be overwritten by providing an entry for 'stages' within your config.
If it is set to deploy directly it will commit and push to your desired `gitops-folder` and therefore triggers a deployment. If it is set to false
it will create a PR on your `gitops-folder`. **Remember** there are [ipmortant](#important-note) conventions regarding namespaces and the folder structure.
it will create a PR on your `gitops-folder`. **Remember** there are [important](#important-note) conventions regarding namespaces and the folder structure.

### Namespaces

Expand Down
53 changes: 37 additions & 16 deletions test/com/cloudogu/gitopsbuildlib/DeployViaGitopsTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ class DeployViaGitopsTest extends BasePipelineTest {

Map gitopsConfig(Map stages, Map deployments) {
return [
scmmCredentialsId : 'scmManagerCredentials',
scmmConfigRepoUrl : 'configRepositoryUrl',
scmmPullRequestBaseUrl: 'http://scmm-scm-manager/scm',
scmmPullRequestRepo : 'fluxv1/gitops',
cesBuildLibRepo : 'cesBuildLibRepo',
cesBuildLibVersion : 'cesBuildLibVersion',
scmmCredentialsId : 'scmManagerCredentials',
scmmConfigRepoUrl : 'configRepositoryUrl',
scmmPullRequestBaseUrl : 'http://scmm-scm-manager/scm',
scmmPullRequestRepo : 'fluxv1/gitops',
cesBuildLibRepo : 'cesBuildLibRepo',
cesBuildLibVersion : 'cesBuildLibVersion',
cesBuildLibCredentialsId: 'cesBuildLibCredentialsId',
application : 'application',
mainBranch : 'main',
deployments : deployments,
validators : [
application : 'application',
mainBranch : 'main',
deployments : deployments,
validators : [
kubeval : [
validator: new Kubeval(deployViaGitops),
enabled : true,
Expand All @@ -73,14 +73,14 @@ class DeployViaGitopsTest extends BasePipelineTest {
]
]
],
stages : stages
stages : stages
]
}

def plainDeployment = [
sourcePath: 'k8s',
plain : [
updateImages : [
plain : [
updateImages: [
[deploymentFilename: "deployment.yaml",
containerName : 'application',
imageName : 'newImageName']
Expand All @@ -95,7 +95,7 @@ class DeployViaGitopsTest extends BasePipelineTest {
def multipleStages = [
staging : [deployDirectly: true],
production: [deployDirectly: false],
qa : []
qa : [deployDirectly: false]
]

@BeforeAll
Expand Down Expand Up @@ -211,6 +211,29 @@ spec:
deployViaGitops([cesBuildLibRepo: 'abc', cesBuildLibCredentialsId: 'testuser'])
}

@Test
void 'default stages defined as staging and production'() {
deployViaGitops.metaClass.deploy = { Map actualGitOpsConfig ->
assertThat(actualGitOpsConfig.stages.containsKey('staging')).isEqualTo(true)
assertThat(actualGitOpsConfig.stages.containsKey('production')).isEqualTo(true)
assertThat(actualGitOpsConfig.stages.staging.deployDirectly).isEqualTo(true)
assertThat(actualGitOpsConfig.stages.production.deployDirectly).isEqualTo(false)
}

deployViaGitops([:])
}

@Test
void 'stages definition gets overwritten rather than merged'() {
deployViaGitops.metaClass.deploy = { Map actualGitOpsConfig ->
assertThat(actualGitOpsConfig.stages.containsKey('staging')).isEqualTo(true)
assertThat(actualGitOpsConfig.stages.containsKey('production')).isEqualTo(false)
assertThat(actualGitOpsConfig.stages.staging.deployDirectly).isEqualTo(true)
}

deployViaGitops(gitopsConfig(singleStages, plainDeployment))
}

@Test
void 'default validator can be disabled'() {

Expand Down Expand Up @@ -327,9 +350,7 @@ spec:

ArgumentCaptor<String> argumentCaptor2 = ArgumentCaptor.forClass(String.class)
verify(git).commit(argumentCaptor2.capture(), eq('staging'), anyString())
println("before argument get value")
assertThat(argumentCaptor2.getValue()).isEqualTo('[staging] #0001 backend/k8s-gitops@1234abcd')
println("after argument get value")

argumentCaptor2 = ArgumentCaptor.forClass(String.class)
verify(git).commit(argumentCaptor2.capture(), eq('production'), anyString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ class HelmKubevalTest {
]
)
assertThat(dockerMock.actualImages[0]).isEqualTo('img')
//assertThat(scriptMock.actualShArgs[0]).isEqualTo('git clone chartRepo/namespace/repoPath target/chart || true')
//assertThat(scriptMock.actualShArgs[1]).isEqualTo('git --git-dir=target/chart/.git --work-tree=target/chart checkout version')
//assertThat(scriptMock.actualShArgs[1]).isEqualTo('helm kubeval target/chart/chartPath -v 1.5')
//assertThat(scriptMock.actualShArgs[2]).isEqualTo('rm -rf target/chart')
assertThat(scriptMock.actualShArgs[0]).isEqualTo('helm kubeval target/chart/chartPath -v 1.5')
assertThat(scriptMock.actualShArgs[1]).isEqualTo('rm -rf target/chart')
}

@Test
Expand Down
27 changes: 18 additions & 9 deletions vars/deployViaGitops.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ List getMandatoryFields() {
Map getDefaultConfig() {

return [
cesBuildLibRepo : 'https://github.com/cloudogu/ces-build-lib',
cesBuildLibVersion: '1.45.0',
cesBuildLibRepo : 'https://github.com/cloudogu/ces-build-lib',
cesBuildLibVersion : '1.45.0',
cesBuildLibCredentialsId: '',
mainBranch : 'main',
deployments : [
mainBranch : 'main',
deployments : [
sourcePath: 'k8s',
],
validators : [
kubeval : [
validators : [
kubeval : [
validator: new Kubeval(this),
enabled : true,
config : [
Expand All @@ -44,7 +44,7 @@ Map getDefaultConfig() {
k8sSchemaVersion: '1.18.1'
]
],
yamllint: [
yamllint : [
validator: new Yamllint(this),
enabled : true,
config : [
Expand All @@ -54,6 +54,10 @@ Map getDefaultConfig() {
profile: 'relaxed'
]
]
],
stages : [
staging : [deployDirectly: true],
production: [deployDirectly: false],
]
]
}
Expand All @@ -69,7 +73,12 @@ void call(Map gitopsConfig) {
def mergeMaps(Map a, Map b) {
return b.inject(a.clone()) { map, entry ->
if (map[entry.key] instanceof Map && entry.value instanceof Map) {
map[entry.key] = mergeMaps(map[entry.key], entry.value)

// due to the stages being the definition of the environment its not a merge but overwriting
if (entry.key == 'stages')
map[entry.key] = entry.value
else
map[entry.key] = mergeMaps(map[entry.key], entry.value)
} else {
map[entry.key] = entry.value
}
Expand Down Expand Up @@ -117,7 +126,7 @@ def validateDeploymentConfig(Map gitopsConfig) {

protected initCesBuildLib(cesBuildLibRepo, cesBuildLibVersion, credentialsId) {
Map retrieverParams = [$class: 'GitSCMSource', remote: cesBuildLibRepo]
if(credentialsId?.trim()) {
if (credentialsId?.trim()) {
retrieverParams << [credentialsId: credentialsId]
}

Expand Down