Skip to content

Commit 43642f7

Browse files
authored
Merge pull request #298 from cloudogu/bug/multiTenant-namespaces
fixing hardcoded namespaces for central Argocd, smaller fix for ArgocdApplication Generator
2 parents 18f3982 + 28af814 commit 43642f7

File tree

7 files changed

+91
-19
lines changed

7 files changed

+91
-19
lines changed

argocd/cluster-resources/argocd/misc.ftl.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ apiVersion: argoproj.io/v1alpha1
44
kind: Application
55
metadata:
66
name: <#if config.multiTenant.useDedicatedInstance>${namePrefix}misc<#else>misc</#if>
7-
namespace: <#if config.multiTenant.useDedicatedInstance>argocd<#else>${namePrefix}argocd</#if>
7+
namespace: <#if config.multiTenant.useDedicatedInstance>${config.multiTenant.centralArgocdNamespace}<#else>${namePrefix}argocd</#if>
88
spec:
99
project: <#if config.multiTenant.useDedicatedInstance>${tenantName}<#else>cluster-resources</#if>
1010
destination:

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,11 @@ class Config {
484484
return new LinkedHashSet<>(dedicatedNamespaces + tenantNamespaces)
485485
}
486486
}
487+
488+
@JsonIgnore
489+
String getTenantName(){
490+
return namePrefix.replaceAll(/-$/, "")
491+
}
487492
}
488493

489494
static class ImagesSchema {

src/main/groovy/com/cloudogu/gitops/features/argocd/ArgoCD.groovy

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,9 @@ class ArgoCD extends Feature {
246246
'example-apps',
247247
ScmmRepo.createSCMBaseUrl(config)+'argocd/example-apps',
248248
namespace,
249-
'argocd/')
249+
namespace,
250+
'argocd/',
251+
config.application.getTenantName())
250252
.generate(tenantBootstrapInitializationAction.repo, 'applications')
251253
}
252254
}
@@ -345,14 +347,14 @@ class ArgoCD extends Feature {
345347
// Append new namespaces to existing ones from the secret.
346348
// `kubectl patch` can't merge list subfields, so we read, decode, merge, and update the secret.
347349
// This ensures all centrally managed namespaces are preserved.
348-
String base64Namespaces = k8sClient.getArgoCDNamespacesSecret('argocd-default-cluster-config', 'argocd')
350+
String base64Namespaces = k8sClient.getArgoCDNamespacesSecret('argocd-default-cluster-config', config.multiTenant.centralArgocdNamespace)
349351
byte[] decodedBytes = Base64.decoder.decode(base64Namespaces)
350352
String decoded = new String(decodedBytes, "UTF-8")
351353
def decodedList = decoded?.split(',') as List ?: []
352354
def activeList = config.application.namespaces.activeNamespaces?.flatten() as List ?: []
353355
def merged = (decodedList + activeList).unique().join(',')
354356
log.debug("Updating Central Argocd 'argocd-default-cluster-config' secret")
355-
k8sClient.patch('secret', 'argocd-default-cluster-config', 'argocd',
357+
k8sClient.patch('secret', 'argocd-default-cluster-config', config.multiTenant.centralArgocdNamespace,
356358
[stringData: ['namespaces': merged]])
357359
}
358360
}
@@ -446,19 +448,18 @@ class ArgoCD extends Feature {
446448
k8sClient.label('secret', repoTemplateSecretName, namespace,
447449
new Tuple2(' argocd.argoproj.io/secret-type', 'repo-creds'))
448450

449-
450451
if (config.multiTenant.useDedicatedInstance) {
451452
log.debug('Creating central repo credential secret that is used by argocd to access repos in SCM-Manager')
452453
// Create secret imperatively here instead of values.yaml, because we don't want it to show in git repo
453454
def centralRepoTemplateSecretName = 'argocd-repo-creds-central-scmm'
454455

455-
k8sClient.createSecret('generic', centralRepoTemplateSecretName, "argocd",
456+
k8sClient.createSecret('generic', centralRepoTemplateSecretName, config.multiTenant.centralArgocdNamespace,
456457
new Tuple2('url', config.multiTenant.centralScmUrl),
457458
new Tuple2('username', config.multiTenant.username),
458459
new Tuple2('password', config.multiTenant.password)
459460
)
460461

461-
k8sClient.label('secret', centralRepoTemplateSecretName, "argocd",
462+
k8sClient.label('secret', centralRepoTemplateSecretName, config.multiTenant.centralArgocdNamespace,
462463
new Tuple2(' argocd.argoproj.io/secret-type', 'repo-creds'))
463464
}
464465
}

src/main/groovy/com/cloudogu/gitops/kubernetes/argocd/ArgoApplication.groovy

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

3-
import com.cloudogu.gitops.config.Config
3+
44
import com.cloudogu.gitops.scmm.ScmmRepo
55
import com.cloudogu.gitops.utils.TemplatingEngine
66
import groovy.util.logging.Slf4j
@@ -14,28 +14,30 @@ class ArgoApplication {
1414

1515
String name
1616
String namespace
17+
String destinationNamespace
1718
String path
1819
String repoUrl
1920
String project
2021

2122
private final TemplatingEngine templater = new TemplatingEngine()
2223

23-
ArgoApplication(String name, String repoUrl, String namespace, String path, String project = 'default') {
24+
ArgoApplication(String name, String repoUrl, String namespace, String destinationNamespace, String path, String project = 'default') {
2425
this.name = name
26+
this.namespace = namespace
27+
this.destinationNamespace = destinationNamespace
2528
this.project = project
2629
this.repoUrl = repoUrl
27-
this.namespace = namespace
2830
this.path = path
2931
}
3032

3133
Map<String, Object> toTemplateParams() {
3234
return [
33-
name : name,
34-
namespace : namespace,
35-
project : project,
36-
path : path,
37-
namePrefix: new Config().application.namePrefix,
38-
repoUrl : repoUrl
35+
name : this.name,
36+
namespace : this.namespace,
37+
project : this.project,
38+
path : this.path,
39+
destinationNamespace: this.destinationNamespace,
40+
repoUrl : this.repoUrl
3941
]
4042
}
4143

src/test/groovy/com/cloudogu/gitops/config/schema/ConfigTest.groovy

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,10 @@ registry:
5252
assertThat(actualValues.application.namePrefix).isEqualTo(expectedValues.application.namePrefix)
5353
assertThat(actualValues.registry.internalPort).isEqualTo(expectedValues.registry.internalPort)
5454
}
55-
}
55+
56+
@Test
57+
void 'getting Tenantname from Config'(){
58+
testConfig.application.namePrefix='testprefix-'
59+
assertThat(testConfig.application.getTenantName()).isEqualTo("testprefix")
60+
}
61+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.cloudogu.gitops.kubernetes.rbac
2+
3+
import com.cloudogu.gitops.config.Config
4+
import com.cloudogu.gitops.kubernetes.argocd.ArgoApplication
5+
import com.cloudogu.gitops.scmm.ScmmRepo
6+
import com.cloudogu.gitops.utils.FileSystemUtils
7+
import groovy.yaml.YamlSlurper
8+
import org.junit.jupiter.api.Test
9+
10+
import static org.assertj.core.api.Assertions.assertThat
11+
12+
class ArgocdApplicationTest {
13+
14+
15+
Config config = Config.fromMap([
16+
scmm : [
17+
username: 'user',
18+
password: 'pass',
19+
protocol: 'http',
20+
host : 'localhost',
21+
provider: 'scm-manager',
22+
rootPath: 'scm'
23+
],
24+
application: [
25+
namePrefix: '',
26+
insecure : false,
27+
gitName : 'Test User',
28+
gitEmail : 'test@example.com'
29+
]
30+
])
31+
32+
33+
@Test
34+
void 'simple ArgoCD Application with common values'() {
35+
36+
ScmmRepo repo = new ScmmRepo(config, "my-repo", new FileSystemUtils())
37+
38+
new ArgoApplication(
39+
'example-apps',
40+
'testurl.com/argocd/example-apps',
41+
'testprefix-argocd',
42+
'testnamespace',
43+
'argocd/')
44+
.generate(repo, 'testsubfolder/test')
45+
46+
47+
File file = new File(repo.getAbsoluteLocalRepoTmpDir(), "testsubfolder/test/argocd-application-example-apps-testprefix-argocd.yaml")
48+
assertThat(file).exists()
49+
Map yaml = new YamlSlurper().parse(file) as Map
50+
51+
assertThat(yaml["metadata"]["name"]).isEqualTo('example-apps')
52+
assertThat(yaml["metadata"]["namespace"]).isEqualTo('testprefix-argocd')
53+
assertThat(yaml["spec"]["destination"]["namespace"]).isEqualTo('testnamespace')
54+
55+
assertThat(yaml["spec"]["source"]["path"]).isEqualTo('argocd/')
56+
assertThat(yaml["spec"]["source"]["repoURL"]).isEqualTo('testurl.com/argocd/example-apps')
57+
}
58+
}

templates/kubernetes/argocd/application.ftl.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ metadata:
99
spec:
1010
destination:
1111
server: https://kubernetes.default.svc
12-
namespace: ${namePrefix}argocd
13-
project: default
12+
namespace: ${destinationNamespace}
13+
project: ${project}
1414
source:
1515
path: ${path}
1616
repoURL: ${repoUrl}

0 commit comments

Comments
 (0)