Skip to content

Commit

Permalink
changed to Typed Config, renamed Schema to Config, removed Config Pro…
Browse files Browse the repository at this point in the history
…vider
  • Loading branch information
nihussmann committed Nov 12, 2024
1 parent 1f6b889 commit 46195af
Show file tree
Hide file tree
Showing 79 changed files with 1,613 additions and 1,546 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ RUN cp /tmp/.gitconfig /dist/home/.gitconfig
COPY scripts/jenkins/plugins /jenkins
RUN /jenkins/download-plugins.sh /dist/gitops/jenkins-plugins

COPY src/main/groovy/com/cloudogu/gitops/config/schema/Schema.groovy /tmp/
COPY src/main/groovy/com/cloudogu/gitops/config/Config.groovy /tmp/
COPY scripts/downloadHelmCharts.sh /tmp/
RUN cd /dist/gitops && /tmp/downloadHelmCharts.sh /tmp/Schema.groovy
RUN cd /dist/gitops && /tmp/downloadHelmCharts.sh /tmp/Config.groovy

WORKDIR /tmp
# Prepare local files for later stages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ grafana:
repository: ${sidecarImageObject.repository}
tag: ${sidecarImageObject.tag}
</#if>
<#if mail.active??>
<#if mail.active?? && mail.active>
notifiers:
notifiers.yaml:
notifiers:
Expand Down Expand Up @@ -244,7 +244,7 @@ grafana:
env:
GF_SMTP_ENABLED: true
GF_SMTP_FROM_ADDRESS: ${monitoring.grafanaEmailFrom}
GF_SMTP_HOST: ${mail.smtpAddress}<#if mail.smtpPort?has_content>:${mail.smtpPort}</#if>
GF_SMTP_HOST: ${mail.smtpAddress}<#if mail.smtpPort?has_content>:${mail.smtpPort?c}</#if>
<#else>
env:
GF_SMTP_ENABLED: true
Expand Down
2 changes: 1 addition & 1 deletion argocd/argocd/applications/argocd.ftl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ metadata:
annotations:
# Only app with the sync-status-unknown alert, so that we only get one alert when SCM is not reachable.
# Otherwise, there would be a spam wave by every application everytime the SCM is not reachable.
<#if mail.active??>
<#if mail.active?? && mail.active>
notifications.argoproj.io/subscribe.on-sync-status-unknown.email: ${argocd.emailToAdmin}
</#if>
# finalizer disabled, because otherwise everything under this Application would be deleted as well, if this Application is deleted by accident
Expand Down
6 changes: 3 additions & 3 deletions argocd/argocd/argocd/values.ftl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ argo-cd:
# secrets are created dynamically in groovy, so they are not stored in git
secret:
create: false
enabled: <#if mail.active??>true<#else>false</#if>
enabled: <#if mail.active == true>true<#else>false</#if>
argocdUrl: https://localhost:9092
<#if mail.active??>
<#if mail.active?? && mail.active>
notifiers:
service.email: |
<#if mail.smtpAddress?has_content>
host: ${mail.smtpAddress}
<#if mail.smtpPort?has_content>port: ${mail.smtpPort}</#if>
<#if mail.smtpPort??>port: ${mail.smtpPort?c}</#if>
<#if mail.smtpUser?has_content>username: $email-username</#if>
<#if mail.smtpPassword?has_content>password: $email-password</#if>
<#else>
Expand Down
2 changes: 1 addition & 1 deletion argocd/argocd/projects/argocd.ftl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ metadata:
name: argocd
namespace: ${namePrefix}argocd
annotations:
<#if mail.active??>
<#if mail.active?? && mail.active>
notifications.argoproj.io/subscribe.email: ${argocd.emailToAdmin}
</#if>
spec:
Expand Down
2 changes: 1 addition & 1 deletion argocd/argocd/projects/cluster-resources.ftl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
# In a real-world scenario these might to into a separate namespace
namespace: ${namePrefix}argocd
annotations:
<#if mail.active??>
<#if mail.active?? && mail.active>
notifications.argoproj.io/subscribe.email: ${argocd.emailToAdmin}
</#if>
spec:
Expand Down
2 changes: 1 addition & 1 deletion argocd/argocd/projects/default.ftl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ metadata:
name: default
namespace: ${namePrefix}argocd
annotations:
<#if mail.active??>
<#if mail.active?? && mail.active>
notifications.argoproj.io/subscribe.email: ${argocd.emailToAdmin}
</#if>
spec:
Expand Down
2 changes: 1 addition & 1 deletion argocd/argocd/projects/example-apps.ftl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ metadata:
name: example-apps
namespace: ${namePrefix}argocd
annotations:
<#if mail.active??>
<#if mail.active?? && mail.active>
notifications.argoproj.io/subscribe.email: ${argocd.emailToUser}
</#if>
spec:
Expand Down
2 changes: 1 addition & 1 deletion scm-manager/values.ftl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ extraEnv: |
- name: SCM_WEBAPP_INITIALPASSWORD
value: "${password}"
<#if !remote>
<#if remote?? && !remote>
service:
nodePort: 9091
type: NodePort
Expand Down
13 changes: 6 additions & 7 deletions scripts/downloadHelmCharts.sh
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
charts=( 'kube-prometheus-stack' 'external-secrets' 'vault' 'mailhog' 'ingress-nginx' 'certManager')
CONFIG="${1:-src/main/groovy/com/cloudogu/gitops/config/schema/Schema.groovy}"
charts=( 'kube-prometheus-stack' 'external-secrets' 'vault' 'mailhog' 'ingress-nginx' 'cert-manager')
CONFIG="${1:-src/main/groovy/com/cloudogu/gitops/config/Config.groovy}"

tmpRepoFile="$(mktemp)"

mkdir -p charts

for chart in "${charts[@]}"; do
chartDetails=$(grep -EA10 "${chart}.*:" "${APPLICATION_CONFIGURATOR_GROOVY}" \
| grep -m1 -EA5 'helm.*:' || true)
chartDetails=$(grep -m1 -EA3 "chart.*:.*${chart}" "${CONFIG}" || true)
if [[ -z "$chartDetails" ]]; then
echo "Did not find chart details for chart $chart in file ${APPLICATION_CONFIGURATOR_GROOVY} " >&2
echo "Did not find chart details for chart $chart in file ${CONFIG} " >&2
exit 1
fi
repo=$(echo "$chartDetails" | grep -oP "repoURL\s*:\s*'\K[^']+")
chart=$(echo "$chartDetails" | grep -oP "chart\s*:\s*'\K[^']+")
chart=$(echo "$chartDetails" | grep -oP "chart\s*:\s*'\K[^']+")
version=$(echo "$chartDetails" | grep -oP "version\s*:\s*'\K[^']+")

# avoid Error: failed to untar: a file or directory with the name charts/$chart already exists
Expand All @@ -26,7 +25,7 @@ for chart in "${charts[@]}"; do
# Note that keeping charts as tgx would need only 1/10 of storage
# But untaring them in groovy would need additional libraries.
# As layers of the image are compressed anyway, we'll do the untar process here, pragmatically

# Do a simple verification
helm template test "./charts/$chart" > /dev/null
done
14 changes: 8 additions & 6 deletions src/main/groovy/com/cloudogu/gitops/FeatureWithImage.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cloudogu.gitops

import com.cloudogu.gitops.config.Config
import com.cloudogu.gitops.utils.K8sClient
import org.slf4j.Logger
import org.slf4j.LoggerFactory
Expand All @@ -12,11 +13,12 @@ trait FeatureWithImage {
final Logger log = LoggerFactory.getLogger(this.class)

void createImagePullSecret() {
if (config.registry['createImagePullSecrets']) {
log.trace("Creating image pull secret 'proxy-registry' in namespace ${namespace}" as String)
String url = config.registry['proxyUrl'] ?: config.registry['url']
String user = config.registry['proxyUsername'] ?: config.registry['readOnlyUsername'] ?: config.registry['username']
String password = config.registry['proxyPassword'] ?: config.registry['readOnlyPassword'] ?: config.registry['password']
if (config.registry.createImagePullSecrets) {

log.trace("Creating image pull secret 'proxy-registry' in namespace ${namespace}")
String url = config.registry.proxyUrl ?: config.registry.url
String user = config.registry.proxyUsername ?: config.registry.readOnlyUsername ?: config.registry.username
String password = config.registry.proxyPassword ?: config.registry.readOnlyPassword ?: config.registry.password

k8sClient.createNamespace(namespace)
k8sClient.createImagePullSecret('proxy-registry', namespace, url, user, password)
Expand All @@ -25,5 +27,5 @@ trait FeatureWithImage {

abstract String getNamespace()
abstract K8sClient getK8sClient()
abstract Map getConfig()
abstract Config getConfig()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.node.ObjectNode
import io.micronaut.context.ApplicationContext
/**
* Generates the JSON Schema for the configuration file and prints it to docs/configuration.schema.json.
* Generates the JSON Config for the configuration file and prints it to docs/configuration.schema.json.
* Passing '-' as parameter prints the schema to stdout
* JsonSchemaGeneratorTest ensures that this is updated.
*
* @see com.cloudogu.gitops.config.schema.Schema
* @see com.cloudogu.gitops.config.Config
*/
class GenerateJsonSchema {
static void main(String[] args) {
Expand Down
28 changes: 13 additions & 15 deletions src/main/groovy/com/cloudogu/gitops/cli/GitopsPlaygroundCli.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import ch.qos.logback.classic.spi.ILoggingEvent
import ch.qos.logback.core.ConsoleAppender
import com.cloudogu.gitops.Application
import com.cloudogu.gitops.config.ApplicationConfigurator
import com.cloudogu.gitops.config.Configuration
import com.cloudogu.gitops.config.schema.JsonSchemaValidator
import com.cloudogu.gitops.config.schema.Schema
import com.cloudogu.gitops.config.Config
import com.cloudogu.gitops.destroy.Destroyer
import com.cloudogu.gitops.utils.CommandExecutor
import com.cloudogu.gitops.utils.FileSystemUtils
Expand All @@ -23,11 +22,12 @@ import picocli.CommandLine

import static com.cloudogu.gitops.config.ConfigConstants.APP_NAME
import static com.cloudogu.gitops.utils.MapUtils.deepMerge

/**
* Provides the entrypoint to the application as well as all config parameters.
* When changing parameters, make sure to update the Schema for the config file as well
* When changing parameters, make sure to update the Config for the config file as well
*
* @see com.cloudogu.gitops.config.schema.Schema
* @see Config
*/
@Slf4j
class GitopsPlaygroundCli {
Expand Down Expand Up @@ -96,14 +96,11 @@ class GitopsPlaygroundCli {

/** Can be used as a hook by child classes */
@SuppressWarnings('GrMethodMayBeStatic') // static methods cannot be overridden
protected Configuration register(Schema config, ApplicationContext context) {
protected void register(Config config, ApplicationContext context) {
context.registerSingleton(config)
def configuration = new Configuration(config.toMap())
context.registerSingleton(configuration)
return configuration
}

private static boolean confirm(String message, Schema config) {
private static boolean confirm(String message, Config config) {
if (config.application.yes) {
return true
}
Expand Down Expand Up @@ -162,9 +159,9 @@ class GitopsPlaygroundCli {
rootLogger.addAppender(appender)
}

private Schema readConfigs(String[] args) {
private Config readConfigs(String[] args) {
log.debug("Reading initial CLI params")
def cliParams = new Schema()
def cliParams = new Config()
new CommandLine(cliParams).parseArgs(args)

String configFilePath = cliParams.application.configFile
Expand Down Expand Up @@ -192,11 +189,12 @@ class GitopsPlaygroundCli {
}

log.debug("Writing CLI params into config")
def mergedConfig = Schema.fromMap(mergedConfigs)
Config mergedConfig = Config.fromMap(mergedConfigs)
//Schema newConfig = Config.fromMap(deepMerge(configToSet, config.toMap()))
new CommandLine(mergedConfig).parseArgs(args)

// TODO make it write to Schema directly? No more immutable then.
mergedConfig = applicationConfigurator.initAndValidateConfig(mergedConfig.toMap())
// TODO make it write to Config directly? No more immutable then.
mergedConfig = applicationConfigurator.initAndValidateConfig(mergedConfig)

log.debug("Actual config: ${mergedConfig.toYaml(true)}")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.cloudogu.gitops.cli

import com.cloudogu.gitops.Application
import com.cloudogu.gitops.config.Configuration
import com.cloudogu.gitops.config.schema.Schema
import com.cloudogu.gitops.config.Config
import com.cloudogu.gitops.dependencyinjection.HttpClientFactory
import com.cloudogu.gitops.dependencyinjection.JenkinsFactory
import com.cloudogu.gitops.dependencyinjection.RetrofitFactory
Expand Down Expand Up @@ -39,23 +38,22 @@ class GitopsPlaygroundCliMainScripted {

static class GitopsPlaygroundCliScripted extends GitopsPlaygroundCli {

@Override
protected Configuration register(Schema config, ApplicationContext context) {
def configuration = super.register(config, context)
protected void register(Config config, ApplicationContext context) {
super.register(config, context)

def fileSystemUtils = new FileSystemUtils()
def executor = new CommandExecutor()
def k8sClient = new K8sClient(executor, fileSystemUtils, new Provider<Configuration>() {
def k8sClient = new K8sClient(executor, fileSystemUtils, new Provider<Config>() {
@Override
Configuration get() {
return configuration
Config get() {
return config
}
})
def helmClient = new HelmClient(executor)

def httpClientFactory = new HttpClientFactory()

def scmmRepoProvider = new ScmmRepoProvider(configuration, fileSystemUtils)
def scmmRepoProvider = new ScmmRepoProvider(config, fileSystemUtils)
def retrofitFactory = new RetrofitFactory()

def insecureSslContextProvider = new Provider<HttpClientFactory.InsecureSslContext>() {
Expand All @@ -64,47 +62,46 @@ class GitopsPlaygroundCliMainScripted {
return httpClientFactory.insecureSslContext()
}
}
def httpClientScmm = retrofitFactory.okHttpClient(httpClientFactory.createLoggingInterceptor(), configuration, insecureSslContextProvider)
def retrofit = retrofitFactory.retrofit(configuration, httpClientScmm)
def httpClientScmm = retrofitFactory.okHttpClient(httpClientFactory.createLoggingInterceptor(), config, insecureSslContextProvider)
def retrofit = retrofitFactory.retrofit(config, httpClientScmm)
def repoApi = retrofitFactory.repositoryApi(retrofit)

def jenkinsConfiguration = new JenkinsConfigurationAdapter(configuration)
def jenkinsConfiguration = new JenkinsConfigurationAdapter(config)
JenkinsFactory jenkinsFactory = new JenkinsFactory(jenkinsConfiguration)
def jenkinsApiClient = jenkinsFactory.jenkinsApiClient(
httpClientFactory.okHttpClient(httpClientFactory.createLoggingInterceptor(), jenkinsConfiguration, insecureSslContextProvider))

context.registerSingleton(k8sClient)

if (configuration.config['application']['destroy']) {
if (config.application.destroy) {
context.registerSingleton(new Destroyer([
new ArgoCDDestructionHandler(configuration, k8sClient, scmmRepoProvider, helmClient, fileSystemUtils),
new ScmmDestructionHandler(configuration, retrofitFactory.usersApi(retrofit), retrofitFactory.repositoryApi(retrofit)),
new JenkinsDestructionHandler(new JobManager(jenkinsApiClient), configuration, new GlobalPropertyManager(jenkinsApiClient))
new ArgoCDDestructionHandler(config, k8sClient, scmmRepoProvider, helmClient, fileSystemUtils),
new ScmmDestructionHandler(config, retrofitFactory.usersApi(retrofit), retrofitFactory.repositoryApi(retrofit)),
new JenkinsDestructionHandler(new JobManager(jenkinsApiClient), config, new GlobalPropertyManager(jenkinsApiClient))
]))
} else {
def helmStrategy = new HelmStrategy(configuration, helmClient)
def helmStrategy = new HelmStrategy(config, helmClient)

def deployer = new Deployer(configuration, new ArgoCdApplicationStrategy(configuration, fileSystemUtils, scmmRepoProvider), helmStrategy)
def deployer = new Deployer(config, new ArgoCdApplicationStrategy(config, fileSystemUtils, scmmRepoProvider), helmStrategy)

def airGappedUtils = new AirGappedUtils(configuration, scmmRepoProvider, repoApi, fileSystemUtils, helmClient)
def airGappedUtils = new AirGappedUtils(config, scmmRepoProvider, repoApi, fileSystemUtils, helmClient)

context.registerSingleton(new Application([
new Registry(config, fileSystemUtils, k8sClient, helmStrategy),
new ScmManager(configuration, executor, fileSystemUtils, helmStrategy),
new Jenkins(configuration, executor, fileSystemUtils, new GlobalPropertyManager(jenkinsApiClient),
new ScmManager(config, executor, fileSystemUtils, helmStrategy),
new Jenkins(config, executor, fileSystemUtils, new GlobalPropertyManager(jenkinsApiClient),
new JobManager(jenkinsApiClient), new UserManager(jenkinsApiClient),
new PrometheusConfigurator(jenkinsApiClient)),
new Content(configuration, k8sClient),
new ArgoCD(configuration, k8sClient, helmClient, fileSystemUtils, scmmRepoProvider),
new IngressNginx(configuration, fileSystemUtils, deployer, k8sClient, airGappedUtils),
new CertManager(configuration, fileSystemUtils, deployer, k8sClient, airGappedUtils),
new Mailhog(configuration, fileSystemUtils, deployer, k8sClient, airGappedUtils),
new PrometheusStack(configuration, fileSystemUtils, deployer, k8sClient, airGappedUtils, scmmRepoProvider),
new ExternalSecretsOperator(configuration, fileSystemUtils, deployer, k8sClient, airGappedUtils),
new Vault(configuration, fileSystemUtils, k8sClient, deployer, airGappedUtils)
new Content(config, k8sClient),
new ArgoCD(config, k8sClient, helmClient, fileSystemUtils, scmmRepoProvider),
new IngressNginx(config, fileSystemUtils, deployer, k8sClient, airGappedUtils),
new CertManager(config, fileSystemUtils, deployer, k8sClient, airGappedUtils),
new Mailhog(config, fileSystemUtils, deployer, k8sClient, airGappedUtils),
new PrometheusStack(config, fileSystemUtils, deployer, k8sClient, airGappedUtils, scmmRepoProvider),
new ExternalSecretsOperator(config, fileSystemUtils, deployer, k8sClient, airGappedUtils),
new Vault(config, fileSystemUtils, k8sClient, deployer, airGappedUtils)
]))
}
return configuration
}
}
}
Loading

0 comments on commit 46195af

Please sign in to comment.