Skip to content

Commit 2f12020

Browse files
authored
Namespace isolation: Specify Prometheus NamespaceSelectors (#247)
* removed obsolete namespace lists from Argo and Prometheus. Adding active namespaces to Config. Getting active namespaces from all features with own namespace. * fixing tests * fixing tests
1 parent 4f92e23 commit 2f12020

File tree

14 files changed

+176
-91
lines changed

14 files changed

+176
-91
lines changed

applications/cluster-resources/monitoring/prometheus-stack-helm-values.ftl.yaml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,11 @@ prometheusOperator:
8383
releaseNamespace: false
8484
additional:
8585
<#-- Note that the quotes in the final YAML here are created by groovy, not Freemarker-->
86+
<#if namespaces?has_content>
8687
<#list namespaces as namespace>
87-
- ${namespace}
88+
- ${namespace}
8889
</#list>
90+
</#if>
8991
</#if>
9092
<#if podResources == true>
9193
resources:
@@ -272,8 +274,17 @@ prometheus:
272274
</#if>
273275
# Find podMonitors, serviceMonitor, etc. in all namespaces
274276
serviceMonitorNamespaceSelector:
275-
matchLabels: {}
276-
# With this, we don't need the label "release: kube-prometheus-stack" on the service monitor
277+
matchExpressions:
278+
- key: kubernetes.io/metadata.name
279+
operator: In
280+
values:
281+
<#if namespaces?has_content>
282+
<#list namespaces as namespace>
283+
- ${namespace}
284+
</#list>
285+
<#else>
286+
{}
287+
</#if>
277288
serviceMonitorSelectorNilUsesHelmValues: false
278289
podMonitorNamespaceSelector:
279290
matchLabels: {}

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.cloudogu.gitops
22

3-
3+
import com.cloudogu.gitops.config.Config
44
import groovy.util.logging.Slf4j
55
import jakarta.inject.Singleton
66

@@ -9,16 +9,21 @@ import jakarta.inject.Singleton
99
class Application {
1010

1111
final List<Feature> features
12+
final Config config
1213

13-
Application(
14+
Application(Config config,
1415
List<Feature> features
1516
) {
17+
this.config=config
1618
// Order is important. Enforced by @Order-Annotation on the Singletons
1719
this.features = features
20+
1821
}
1922

2023
def start() {
2124
log.debug("Starting Application")
25+
26+
setNamespaceListToConfig(config)
2227
features.forEach(feature -> {
2328
feature.install()
2429
})
@@ -28,4 +33,31 @@ class Application {
2833
List<Feature> getFeatures() {
2934
return features
3035
}
36+
37+
void setNamespaceListToConfig(Config config) {
38+
List<String> namespaces = []
39+
String namePrefix = config.application.namePrefix;
40+
41+
if(config.registry.internal || config.scmm.internal || config.jenkins.internal){
42+
namespaces.add(namePrefix + "default")
43+
}
44+
45+
if (config.features.argocd.active) {
46+
namespaces.addAll(Arrays.asList(
47+
namePrefix + "argocd",
48+
namePrefix + "example-apps-staging",
49+
namePrefix + "example-apps-production"
50+
))
51+
}
52+
53+
//iterates over all FeatureWithImages and gets their namespaces
54+
namespaces.addAll(this.features
55+
.collect { it.activeNamespaceFromFeature }
56+
.findAll { it }
57+
.unique()
58+
.collect { "${namePrefix}${it}".toString() })
59+
60+
log.debug("Active namespaces retrieved: {}", namespaces);
61+
config.application.activeNamespaces = namespaces
62+
}
3163
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,15 @@ abstract class Feature {
4646
return false
4747
}
4848
}
49-
49+
50+
String getActiveNamespaceFromFeature() {
51+
//using reflection to get all subclasses implementing a own namespace
52+
if (this.metaClass.hasProperty(this, 'namespace')) {
53+
return isEnabled() ? this.getProperty('namespace') : null
54+
}
55+
return null
56+
}
57+
5058
abstract boolean isEnabled()
5159

5260
/*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ trait FeatureWithImage {
2424
k8sClient.createImagePullSecret('proxy-registry', namespace, url, user, password)
2525
}
2626
}
27-
27+
2828
abstract String getNamespace()
2929
abstract K8sClient getK8sClient()
3030
abstract Config getConfig()

src/main/groovy/com/cloudogu/gitops/cli/GitopsPlaygroundCli.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import com.cloudogu.gitops.utils.K8sClient
1717
import groovy.util.logging.Slf4j
1818
import groovy.yaml.YamlSlurper
1919
import io.micronaut.context.ApplicationContext
20+
import jakarta.inject.Provider
2021
import org.slf4j.LoggerFactory
2122
import picocli.CommandLine
2223

src/main/groovy/com/cloudogu/gitops/cli/GitopsPlaygroundCliMainScripted.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class GitopsPlaygroundCliMainScripted {
8686

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

89-
context.registerSingleton(new Application([
89+
context.registerSingleton(new Application(config,[
9090
new Registry(config, fileSystemUtils, k8sClient, helmStrategy),
9191
new ScmManager(config, executor, fileSystemUtils, helmStrategy),
9292
new Jenkins(config, executor, fileSystemUtils, new GlobalPropertyManager(jenkinsApiClient),

src/main/groovy/com/cloudogu/gitops/config/Config.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ class Config {
249249
static class ApplicationSchema {
250250
Boolean runningInsideK8s = false
251251
String namePrefixForEnvVars = ''
252+
List<String> activeNamespaces = []
252253
String internalKubernetesApiUrl = ''
253254
String localHelmChartFolder = System.getenv('LOCAL_HELM_CHART_FOLDER')
254255

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

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class PrometheusStack extends Feature implements FeatureWithImage {
7575
remote: config.application.remote,
7676
skipCrds : config.application.skipCrds,
7777
namespaceIsolation: config.application.namespaceIsolation,
78-
namespaces : namespaceList,
78+
namespaces : config.application.activeNamespaces,
7979
mail : [
8080
active : config.features.mail.active,
8181
smtpAddress : config.features.mail.smtpAddress,
@@ -97,22 +97,22 @@ class PrometheusStack extends Feature implements FeatureWithImage {
9797
k8sClient.createSecret(
9898
'generic',
9999
'prometheus-metrics-creds-scmm',
100-
'monitoring',
100+
namespace,
101101
new Tuple2('password', config.application.password)
102102
)
103103

104104
k8sClient.createSecret(
105105
'generic',
106106
'prometheus-metrics-creds-jenkins',
107-
'monitoring',
107+
namespace,
108108
new Tuple2('password', config.jenkins.metricsPassword),
109109
)
110110

111111
if (config.features.mail.smtpUser || config.features.mail.smtpPassword) {
112112
k8sClient.createSecret(
113113
'generic',
114114
'grafana-email-secret',
115-
'monitoring',
115+
namespace,
116116
new Tuple2('user', config.features.mail.smtpUser),
117117
new Tuple2('password', config.features.mail.smtpPassword)
118118
)
@@ -121,7 +121,7 @@ class PrometheusStack extends Feature implements FeatureWithImage {
121121
if (config.application.namespaceIsolation || config.application.netpols) {
122122
ScmmRepo clusterResourcesRepo = scmmRepoProvider.getRepo('argocd/cluster-resources')
123123
clusterResourcesRepo.cloneRepo()
124-
for (String currentNamespace : namespaceList) {
124+
for (String currentNamespace : config.application.activeNamespaces) {
125125

126126
if(config.application.namespaceIsolation) {
127127
def rbacYaml = new TemplatingEngine().template(new File(RBAC_NAMESPACE_ISOLATION_TEMPLATE),
@@ -175,27 +175,6 @@ class PrometheusStack extends Feature implements FeatureWithImage {
175175
}
176176
}
177177

178-
protected List getNamespaceList() {
179-
def namespaces = []
180-
def namePrefix = config.application.namePrefix
181-
if (config.features.argocd.active) {
182-
namespaces.addAll("${namePrefix}argocd", "${namePrefix}example-apps-staging", "${namePrefix}example-apps-production")
183-
}
184-
if (config.features.monitoring.active) { // Ignore mailhog here, because it does not expose metrics
185-
namespaces.addAll("${namePrefix}monitoring")
186-
}
187-
if (config.features.secrets.active) {
188-
namespaces.addAll("${namePrefix}secrets")
189-
}
190-
if (config.features.ingressNginx.active) {
191-
namespaces.addAll("${namePrefix}ingress-nginx")
192-
}
193-
if (config.registry.internal || config.scmm.internal || config.jenkins.internal) {
194-
namespaces.addAll("${namePrefix}default")
195-
}
196-
return namespaces
197-
}
198-
199178
private Map getScmmConfiguration() {
200179
// Note that URI.resolve() seems to throw away the existing path. So we create a new URI object.
201180
URI uri = new URI("${scmmUri}/api/v2/metrics/prometheus")

src/main/groovy/com/cloudogu/gitops/utils/K8sClient.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.cloudogu.gitops.utils
22

3+
import com.cloudogu.gitops.config.Config
34
import groovy.json.JsonBuilder
45
import groovy.json.JsonSlurper
5-
import com.cloudogu.gitops.config.Config
66
import groovy.transform.Immutable
77
import groovy.util.logging.Slf4j
88
import jakarta.inject.Provider
@@ -24,7 +24,7 @@ class K8sClient {
2424
) {
2525
this.fileSystemUtils = fileSystemUtils
2626
this.commandExecutor = commandExecutor
27-
this.configProvider = configProvider;
27+
this.configProvider = configProvider
2828
}
2929

3030
String getInternalNodeIp() {
@@ -417,7 +417,7 @@ class K8sClient {
417417

418418
Kubectl namespace(String namespace) {
419419
if (namespace) {
420-
this.command += ['-n', configProvider.get().application.namePrefix + namespace]
420+
this.command += ['-n', K8sClient.this.configProvider.get().application.namePrefix + namespace]
421421
}
422422
return this
423423
}

0 commit comments

Comments
 (0)