@@ -13,6 +13,7 @@ import groovy.transform.CompileStatic
1313import groovy.util.logging.Slf4j
1414import io.micronaut.core.annotation.Order
1515import jakarta.inject.Singleton
16+ import java.nio.file.Path
1617
1718@Slf4j
1819@Singleton
@@ -39,9 +40,9 @@ class Monitoring extends Feature implements FeatureWithImage {
3940 GitRepoFactory scmRepoProvider ,
4041 GitHandler gitHandler
4142 ) {
42- this . deployer = deployer
4343 this . config = config
4444 this . fileSystemUtils = fileSystemUtils
45+ this . deployer = deployer
4546 this . k8sClient = k8sClient
4647 this . airGappedUtils = airGappedUtils
4748 this . scmRepoProvider = scmRepoProvider
@@ -66,30 +67,23 @@ class Monitoring extends Feature implements FeatureWithImage {
6667 addHelmValuesData(' jenkins' , jenkinsConfigurationMetrics())
6768 addHelmValuesData(' uid' , uid)
6869
69- // Create secret imperatively here instead of values.yaml, because we don't want it to show in git repo
70+ // Create secrets imperatively here instead of values.yaml, because we don't want credentials to be visible in the Git repo
7071 setupMonitoringSecrets()
72+ createMonitoringCrd()
73+
74+ GitRepo clusterResourcesRepo = scmRepoProvider. getRepo(' argocd/cluster-resources' , this . gitHandler. resourcesScm)
75+ clusterResourcesRepo. cloneRepo()
7176
7277 if (config. application. namespaceIsolation || config. application. netpols) {
73- GitRepo clusterResourcesRepo = scmRepoProvider. getRepo(' argocd/cluster-resources' , this . gitHandler. resourcesScm)
74- clusterResourcesRepo. cloneRepo()
7578 if (config. application. namespaceIsolation) { generateNamespaceIsolationRBAC(clusterResourcesRepo) }
7679 if (config. application. netpols) { generateNetpols(clusterResourcesRepo) }
77- clusterResourcesRepo. commitAndPush(' Adding namespace-isolated RBAC and network policies if enabled.' )
7880 }
7981
80- deployHelmChart( ' prometheusstack ' , ' kube-prometheus-stack ' , namespace, config . features. monitoring . helm, HELM_VALUES_PATH , config)
81- }
82+ // Remove dashboards for features that are not enabled
83+ cleanupUnusedDashboards(clusterResourcesRepo)
8284
83- private static URI baseUriJenkins (Config config ) {
84- if (config. jenkins. internal) {
85- return new URI (" http://jenkins.${ config.application.namePrefix} jenkins.svc.cluster.local/" )
86- }
87- String urlString = config. jenkins?. url?. strip() ?: ' '
88- if (! urlString) {
89- throw new IllegalArgumentException (' config.jenkins.url must be set when config.jenkins.internal = false' )
90- }
91- URI url = URI . create(urlString)
92- return url. toString(). endsWith(' /' ) ? url : URI . create(url. toString() + ' /' )
85+ clusterResourcesRepo. commitAndPush(' Update Prometheus dashboards, RBAC and network policies.' )
86+ deployHelmChart(' monitoring' , ' kube-prometheus-stack' , namespace, config. features. monitoring. helm, HELM_VALUES_PATH , config)
9387 }
9488
9589 private void setupMonitoringSecrets () {
@@ -153,6 +147,26 @@ class Monitoring extends Feature implements FeatureWithImage {
153147 ]
154148 }
155149
150+ protected void createMonitoringCrd () {
151+ if (! config. application. skipCrds) {
152+ def serviceMonitorCrdYaml
153+ if (config. application. mirrorRepos) {
154+ serviceMonitorCrdYaml = Path . of(
155+ " ${ config.application.localHelmChartFolder} /${ config.features.monitoring.helm.chart} /charts/crds/crds/crd-servicemonitors.yaml"
156+ ). toString()
157+ } else {
158+ serviceMonitorCrdYaml =
159+ " https://raw.githubusercontent.com/prometheus-community/helm-charts/" +
160+ " kube-prometheus-stack-${ config.features.monitoring.helm.version} /" +
161+ " charts/kube-prometheus-stack/charts/crds/crds/crd-servicemonitors.yaml"
162+ }
163+
164+ log. debug(" Applying ServiceMonitor CRD; Argo CD fails if it is not there. Chicken-egg-problem.\n " +
165+ " Applying from path ${ serviceMonitorCrdYaml} " )
166+ k8sClient. applyYaml(serviceMonitorCrdYaml)
167+ }
168+ }
169+
156170 private Map jenkinsConfigurationMetrics () {
157171 URI uri = baseUriJenkins(config). resolve(' prometheus' )
158172 return [
@@ -163,15 +177,60 @@ class Monitoring extends Feature implements FeatureWithImage {
163177 ]
164178 }
165179
180+ private static URI baseUriJenkins (Config config ) {
181+ if (config. jenkins. internal) {
182+ return new URI (" http://jenkins.${ config.application.namePrefix} jenkins.svc.cluster.local/" )
183+ }
184+ def urlString = config. jenkins?. url?. strip() ?: " "
185+ if (! urlString) {
186+ throw new IllegalArgumentException (" config.jenkins.url must be set when config.jenkins.internal = false" )
187+ }
188+ def url = URI . create(urlString)
189+ return url. toString(). endsWith(" /" ) ? url : URI . create(url. toString() + " /" )
190+ }
191+
166192 private String findValidOpenShiftUid () {
167193 String uidRange = k8sClient. getAnnotation(' namespace' , namespace, ' openshift.io/sa.scc.uid-range' )
168194
169195 if (uidRange) {
170196 log. debug(" found UID=${ uidRange} " )
171197 String uid = uidRange. split(' /' )[0 ]
172198 return uid
199+ } else {
200+ throw new RuntimeException (" Could not find a valid UID! Really running on OpenShift?" )
201+ }
202+ }
203+
204+ protected void cleanupUnusedDashboards (GitRepo clusterResourcesRepo ) {
205+ String repoRoot = clusterResourcesRepo. getAbsoluteLocalRepoTmpDir()
206+ String dashboardRoot = " ${ repoRoot} /apps/prometheusstack/misc/dashboard"
207+
208+ if (! config. features. ingress. active) {
209+ fileSystemUtils. deleteFile(" ${ dashboardRoot} /traefik-dashboard.yaml" )
210+ fileSystemUtils. deleteFile(" ${ dashboardRoot} /traefik-dashboard-requests-handling.yaml" )
211+ }
212+
213+ if (! config. jenkins. active) {
214+ fileSystemUtils. deleteFile(" ${ dashboardRoot} /jenkins-dashboard.yaml" )
215+ }
216+
217+ if (! config. scm. scmManager?. url) {
218+ fileSystemUtils. deleteFile(" ${ dashboardRoot} /scmm-dashboard.yaml" )
173219 }
220+ }
221+
222+ @Override
223+ String getNamespace () {
224+ return namespace
225+ }
174226
175- throw new NoSuchElementException (' Could not find a valid UID! Really running on openshift?' )
227+ @Override
228+ K8sClient getK8sClient () {
229+ return k8sClient
230+ }
231+
232+ @Override
233+ Config getConfig () {
234+ return config
176235 }
177- }
236+ }
0 commit comments