Skip to content

Commit

Permalink
Deploy Mailhog and Prometheus stack imperatively
Browse files Browse the repository at this point in the history
Advantage:
* Can be used from all GitOps operators in playground
* Uniform way, sam method of deployment as for Jenkins, SCMM, Registry, External Secrets, Vault
  • Loading branch information
schnatterer committed Oct 27, 2022
1 parent 517b0e4 commit ef07e1c
Show file tree
Hide file tree
Showing 15 changed files with 256 additions and 263 deletions.

This file was deleted.

This file was deleted.

4 changes: 4 additions & 0 deletions src/main/groovy/com/cloudogu/gitops/Application.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.cloudogu.gitops

import com.cloudogu.gitops.features.ExternalSecretsOperator
import com.cloudogu.gitops.features.Mailhog
import com.cloudogu.gitops.features.PrometheusStack
import com.cloudogu.gitops.features.argocd.ArgoCD
import groovy.util.logging.Slf4j

Expand Down Expand Up @@ -28,6 +30,8 @@ class Application {
private List<Feature> registerFeatures() {
List<Feature> features = []
features.add(new ArgoCD(config))
features.add(new Mailhog(config))
features.add(new PrometheusStack(config))
features.add(new ExternalSecretsOperator(config))
return features
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/groovy/com/cloudogu/gitops/Feature.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ abstract class Feature {
/*
* Hooks for enabling or disabling a feature. Both optional, because not always needed.
*/
void enable() {}
void disable() {}
protected void enable() {}
protected void disable() {}
}
Original file line number Diff line number Diff line change
@@ -1,47 +1,60 @@
package com.cloudogu.gitops.features.argocd
package com.cloudogu.gitops.features

import com.cloudogu.gitops.Feature
import com.cloudogu.gitops.utils.FileSystemUtils
import com.cloudogu.gitops.utils.HelmClient
import groovy.util.logging.Slf4j
import org.springframework.security.crypto.bcrypt.BCrypt

@Slf4j
class Mailhog extends Feature {

static final String MAILHOG_YAML_PATH = "applications/system/application-mailhog-helm.yaml"
static final String HELM_VALUES_PATH = "system/mailhog-helm-values.yaml"

private Map config
private boolean remoteCluster
private String username
private String password
private String tmpGitRepoDir
private FileSystemUtils fileSystemUtils
HelmClient helmClient

Mailhog(Map config, String tmpGitRepoDir, FileSystemUtils fileSystemUtils = new FileSystemUtils()) {
Mailhog(Map config, FileSystemUtils fileSystemUtils = new FileSystemUtils(),
HelmClient helmClient = new HelmClient()) {
this.config = config
this.remoteCluster = config.application["remote"]
this.username = config.application["username"]
this.password = config.application["password"]
this.tmpGitRepoDir = tmpGitRepoDir
this.fileSystemUtils = fileSystemUtils
this.helmClient = helmClient
}

@Override
boolean isEnabled() {
config.features["argocd"]["active"]
true
}

@Override
void enable() {
def tmpHelmValues = fileSystemUtils.copyToTempDir(HELM_VALUES_PATH)
def tmpHelmValuesFolder = tmpHelmValues.parent.toString()
def tmpHelmValuesFile = tmpHelmValues.fileName.toString()

if (!remoteCluster) {
log.debug("Setting mailhog service.type to NodePort since it is not running in a remote cluster")
fileSystemUtils.replaceFileContent(tmpGitRepoDir, MAILHOG_YAML_PATH, "LoadBalancer", "NodePort")
fileSystemUtils.replaceFileContent(tmpHelmValuesFolder, tmpHelmValuesFile,
"LoadBalancer", "NodePort")
}

log.debug("Setting new mailhog credentials")
String bcryptMailhogPassword = BCrypt.hashpw(password, BCrypt.gensalt(4))
String from = "fileContents: \"admin:\$2a\$04\$bM4G0jXB7m7mSv4UT8IuIe3.Bj6i6e2A13ryA0ln.hpyX7NeGQyG.\""
String to = "fileContents: \"$username:$bcryptMailhogPassword\""
fileSystemUtils.replaceFileContent(tmpGitRepoDir, MAILHOG_YAML_PATH, from, to)

fileSystemUtils.replaceFileContent(tmpHelmValuesFolder, tmpHelmValuesFile, from, to)

helmClient.addRepo('codecentric', 'https://codecentric.github.io/helm-charts')
helmClient.upgrade('mailhog', 'codecentric/mailhog', '5.0.1',
[namespace: 'monitoring',
values: "${tmpHelmValues.toString()}"])
}
}
Original file line number Diff line number Diff line change
@@ -1,63 +1,65 @@
package com.cloudogu.gitops.features.argocd
package com.cloudogu.gitops.features

import com.cloudogu.gitops.Feature
import com.cloudogu.gitops.utils.HelmClient
import com.cloudogu.gitops.utils.K8sClient
import com.cloudogu.gitops.utils.FileSystemUtils
import groovy.util.logging.Slf4j

@Slf4j
class PrometheusStack extends Feature {

static final String STACK_YAML_PATH = "applications/system/application-kube-prometheus-stack-helm.yaml"
static final String HELM_VALUES_PATH = "system/monitoring/prometheus-stack-helm-values.yaml"

private Map config
private boolean remoteCluster
private String username
private String password
private String tmpGitRepoDir
private FileSystemUtils fileSystemUtils
private K8sClient k8sClient
HelmClient helmClient

PrometheusStack(Map config, String tmpGitRepoDir, FileSystemUtils fileSystemUtils = new FileSystemUtils(),
K8sClient k8sClient = new K8sClient()) {
PrometheusStack(Map config, FileSystemUtils fileSystemUtils = new FileSystemUtils(),
K8sClient k8sClient = new K8sClient(), HelmClient helmClient = new HelmClient()) {
this.config = config
this.username = config.application["username"]
this.password = config.application["password"]
this.remoteCluster = config.application["remote"]
this.tmpGitRepoDir = tmpGitRepoDir
this.fileSystemUtils = fileSystemUtils
this.k8sClient = k8sClient
this.helmClient = helmClient
}

@Override
boolean isEnabled() {
return config.features["metrics"] && config.features["argocd"]["active"]
return config.features["metrics"]
}

@Override
void enable() {
def tmpHelmValues = fileSystemUtils.copyToTempDir(HELM_VALUES_PATH)
def tmpHelmValuesFolder = tmpHelmValues.parent.toString()
def tmpHelmValuesFile = tmpHelmValues.fileName.toString()

if (remoteCluster) {
log.debug("Setting grafana service.type to LoadBalancer since it is running in a remote cluster")
fileSystemUtils.replaceFileContent(tmpGitRepoDir, STACK_YAML_PATH, "NodePort", "LoadBalancer")
fileSystemUtils.replaceFileContent(tmpHelmValuesFolder, tmpHelmValuesFile, "NodePort", "LoadBalancer")
}

k8sClient.applyYaml(fileSystemUtils.rootDir + "/system/metrics/grafana/dashboards/")

if (username != null && username != "admin") {
log.debug("Setting grafana username")
fileSystemUtils.replaceFileContent(tmpGitRepoDir, STACK_YAML_PATH,
fileSystemUtils.replaceFileContent(tmpHelmValuesFolder, tmpHelmValuesFile,
'adminUser: admin', "adminUser: $username")
}
if (password != null && password != "admin") {
log.debug("Setting grafana password")
fileSystemUtils.replaceFileContent(tmpGitRepoDir, STACK_YAML_PATH,
fileSystemUtils.replaceFileContent(tmpHelmValuesFolder, tmpHelmValuesFile,
"adminPassword: admin", "adminPassword: $password")
}
}

@Override
void disable() {
String prometheusStack = tmpGitRepoDir + '/' + STACK_YAML_PATH
new File(prometheusStack).delete()
helmClient.addRepo('prometheus-community', 'https://prometheus-community.github.io/helm-charts')
helmClient.upgrade('kube-prometheus-stack', 'prometheus-community/kube-prometheus-stack', '19.2.2',
[namespace: 'monitoring',
values: "${tmpHelmValues.toString()}"])
}
}
20 changes: 6 additions & 14 deletions src/main/groovy/com/cloudogu/gitops/features/argocd/ArgoCD.groovy
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package com.cloudogu.gitops.features.argocd

import com.cloudogu.gitops.utils.GitClient
import com.cloudogu.gitops.Feature
import com.cloudogu.gitops.utils.GitClient
import groovy.util.logging.Slf4j

@Slf4j
class ArgoCD extends Feature {

private Map config
private GitClient git

private List<Feature> subFeatures = []

private File controlAppTmpDir

ArgoCD(Map config, GitClient gitClient = new GitClient(config)) {
Expand All @@ -19,7 +18,6 @@ class ArgoCD extends Feature {

controlAppTmpDir = File.createTempDir("gitops-playground-control-app")
controlAppTmpDir.deleteOnExit()
subFeatures = createSubFeatures(config)
}

@Override
Expand All @@ -33,22 +31,16 @@ class ArgoCD extends Feature {
String scmmRepoTarget = "argocd/control-app"

git.clone(localSrcDir, scmmRepoTarget, controlAppTmpDir.absolutePath)
// .foreach{} syntax leads to GroovyCastException: Cannot cast object 'com.cloudogu.gitops.features.argocd.ArgoCD$_enable_closure1@441697fd' with class 'com.cloudogu.gitops.features.argocd.ArgoCD$_enable_closure1' to class 'java.util.function.Consumer'
for (subFeature in subFeatures) {
subFeature.install()
}

new ArgoCDNotifications(config, controlAppTmpDir.absolutePath).install()

if (!config.features["vault"]) {
new File(controlAppTmpDir.absolutePath + '/' + "applications/secrets").delete()
}
if (!config.features["metrics"]) {
new File(controlAppTmpDir.absolutePath + '/' + "applications/monitoring").delete()
}
git.commitAndPush(scmmRepoTarget, controlAppTmpDir.absolutePath)
}

List createSubFeatures(Map config) {
subFeatures += new ArgoCDNotifications(config, controlAppTmpDir.absolutePath)
subFeatures += new Mailhog(config, controlAppTmpDir.absolutePath)
subFeatures += new PrometheusStack(config, controlAppTmpDir.absolutePath)
git.commitAndPush(scmmRepoTarget, controlAppTmpDir.absolutePath)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CommandExecutor {
}

private String getOutput(Process proc, String command) {
proc.waitForOrKill(10000)
proc.waitForOrKill(60000)
// err must be defined first because calling proc.text closes the output stream
String err = proc.err.text
String out = proc.text
Expand Down
13 changes: 12 additions & 1 deletion src/main/groovy/com/cloudogu/gitops/utils/FileSystemUtils.groovy
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//file:noinspection GrMethodMayBeStatic - it's not static to be able to hook in for testing
package com.cloudogu.gitops.utils

import groovy.io.FileType
import groovy.util.logging.Slf4j
import org.apache.commons.io.FileUtils

import java.nio.file.Files
import java.nio.file.Path

@Slf4j
class FileSystemUtils {

Expand Down Expand Up @@ -112,7 +116,14 @@ class FileSystemUtils {
}

void createDirectory(String directory) {
log.debug("Creating folder: " + directory)
log.trace("Creating folder: " + directory)
new File(directory).mkdirs()
}

Path copyToTempDir(String filePath) {
def sourcePath = Path.of(filePath)
def destDir = File.createTempDir("gitops-playground-").toPath()
def destPath = destDir.resolve(sourcePath.fileName)
return Files.copy(sourcePath, destPath)
}
}
Loading

0 comments on commit ef07e1c

Please sign in to comment.