Skip to content

Commit eb13386

Browse files
schnatterernihussmann
authored andcommitted
Move createImagePullSecret() to trait
For easier reuse. Using a trait instead of a util might also help us in the future. For example, we can automate the setting of image values or extract all images used. An abstract class might have worked as well, but this way we can later add more traits, if needed.
1 parent 57767e0 commit eb13386

File tree

6 files changed

+106
-38
lines changed

6 files changed

+106
-38
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ abstract class Feature {
88
boolean install() {
99
if (isEnabled()) {
1010
log.info("Installing Feature ${getClass().getSimpleName()}")
11+
12+
if (this instanceof FeatureWithImage) {
13+
(this as FeatureWithImage).createImagePullSecret()
14+
}
15+
1116
enable()
1217
return true
1318
} else {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.cloudogu.gitops
2+
3+
import com.cloudogu.gitops.utils.K8sClient
4+
import org.slf4j.Logger
5+
import org.slf4j.LoggerFactory
6+
7+
/**
8+
* A feature that relies on container images running inside the kubernetes cluster.
9+
*/
10+
trait FeatureWithImage {
11+
12+
final Logger log = LoggerFactory.getLogger(this.class)
13+
14+
void createImagePullSecret() {
15+
if (config.registry['createImagePullSecrets'] && config.registry['twoRegistries']) {
16+
log.trace("Creating image pull secret 'proxy-registry' in namespace ${namespace}" as String)
17+
k8sClient.createNamespace(namespace)
18+
k8sClient.createImagePullSecret('proxy-registry', namespace, config.registry['proxyUrl'] as String,
19+
config.registry['proxyUsername'] as String,
20+
config.registry['proxyPassword'] as String)
21+
}
22+
}
23+
24+
abstract String getNamespace()
25+
abstract K8sClient getK8sClient()
26+
abstract Map getConfig()
27+
}

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

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

33
import com.cloudogu.gitops.Feature
4+
import com.cloudogu.gitops.FeatureWithImage
45
import com.cloudogu.gitops.config.Configuration
56
import com.cloudogu.gitops.features.deployment.DeploymentStrategy
67
import com.cloudogu.gitops.utils.AirGappedUtils
@@ -18,15 +19,16 @@ import java.nio.file.Path
1819
@Slf4j
1920
@Singleton
2021
@Order(400)
21-
class ExternalSecretsOperator extends Feature {
22+
class ExternalSecretsOperator extends Feature implements FeatureWithImage {
2223

2324
static final String HELM_VALUES_PATH = 'applications/cluster-resources/secrets/external-secrets/values.ftl.yaml'
24-
static final String NAMESPACE = 'secrets'
2525

26-
private Map config
26+
String namespace = 'secrets'
27+
Map config
28+
K8sClient k8sClient
29+
2730
private FileSystemUtils fileSystemUtils
2831
private DeploymentStrategy deployer
29-
private K8sClient k8sClient
3032
private AirGappedUtils airGappedUtils
3133

3234
ExternalSecretsOperator(
@@ -51,12 +53,6 @@ class ExternalSecretsOperator extends Feature {
5153
@Override
5254
void enable() {
5355

54-
if (config.registry['createImagePullSecrets'] && config.registry['twoRegistries']) {
55-
k8sClient.createImagePullSecret('proxy-registry', NAMESPACE, config.registry['proxyUrl'] as String,
56-
config.registry['proxyUsername'] as String,
57-
config.registry['proxyPassword'] as String)
58-
}
59-
6056
def helmConfig = config['features']['secrets']['externalSecrets']['helm']
6157
def helmValuesYaml = templateToMap(HELM_VALUES_PATH, [
6258
config: config,
@@ -81,7 +77,7 @@ class ExternalSecretsOperator extends Feature {
8177
"external-secrets",
8278
'.',
8379
externalSecretsVersion,
84-
NAMESPACE,
80+
namespace,
8581
'external-secrets',
8682
tmpHelmValues, DeploymentStrategy.RepoType.GIT
8783
)
@@ -91,7 +87,7 @@ class ExternalSecretsOperator extends Feature {
9187
"externalsecretsoperator",
9288
helmConfig['chart'] as String,
9389
helmConfig['version'] as String,
94-
NAMESPACE,
90+
namespace,
9591
'external-secrets',
9692
tmpHelmValues
9793
)

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

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

33
import com.cloudogu.gitops.Feature
4+
import com.cloudogu.gitops.FeatureWithImage
45
import com.cloudogu.gitops.config.Configuration
56
import com.cloudogu.gitops.features.deployment.DeploymentStrategy
67
import com.cloudogu.gitops.utils.AirGappedUtils
@@ -19,18 +20,19 @@ import java.nio.file.Path
1920
@Slf4j
2021
@Singleton
2122
@Order(200)
22-
class Mailhog extends Feature {
23+
class Mailhog extends Feature implements FeatureWithImage {
2324

2425
static final String HELM_VALUES_PATH = "applications/cluster-resources/mailhog-helm-values.ftl.yaml"
25-
static final String NAMESPACE = 'monitoring'
2626

27-
private Map config
27+
String namespace = 'monitoring'
28+
Map config
29+
K8sClient k8sClient
30+
2831
private String username
2932
private String password
3033
private FileSystemUtils fileSystemUtils
3134
private DeploymentStrategy deployer
3235
private AirGappedUtils airGappedUtils
33-
private K8sClient k8sClient
3436

3537
Mailhog(
3638
Configuration config,
@@ -57,12 +59,6 @@ class Mailhog extends Feature {
5759
@Override
5860
void enable() {
5961

60-
if (config.registry['createImagePullSecrets'] && config.registry['twoRegistries']) {
61-
k8sClient.createImagePullSecret('proxy-registry', NAMESPACE, config.registry['proxyUrl'] as String,
62-
config.registry['proxyUsername'] as String,
63-
config.registry['proxyPassword'] as String)
64-
}
65-
6662
String bcryptMailhogPassword = BCrypt.hashpw(password, BCrypt.gensalt(4))
6763
def tmpHelmValues = new TemplatingEngine().replaceTemplate(fileSystemUtils.copyToTempDir(HELM_VALUES_PATH).toFile(), [
6864
mail : [
@@ -94,7 +90,7 @@ class Mailhog extends Feature {
9490
'mailhog',
9591
'.',
9692
mailhogVersion,
97-
NAMESPACE,
93+
namespace,
9894
'mailhog',
9995
tmpHelmValues, DeploymentStrategy.RepoType.GIT)
10096
} else {
@@ -103,7 +99,7 @@ class Mailhog extends Feature {
10399
'mailhog',
104100
helmConfig['chart'] as String,
105101
helmConfig['version'] as String,
106-
NAMESPACE,
102+
namespace,
107103
'mailhog',
108104
tmpHelmValues)
109105
}

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

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

33
import com.cloudogu.gitops.Feature
4+
import com.cloudogu.gitops.FeatureWithImage
45
import com.cloudogu.gitops.config.Configuration
56
import com.cloudogu.gitops.features.deployment.DeploymentStrategy
67
import com.cloudogu.gitops.utils.*
@@ -15,15 +16,16 @@ import java.nio.file.Path
1516
@Slf4j
1617
@Singleton
1718
@Order(500)
18-
class Vault extends Feature {
19+
class Vault extends Feature implements FeatureWithImage {
1920
static final String VAULT_START_SCRIPT_PATH = '/applications/cluster-resources/secrets/vault/dev-post-start.ftl.sh'
2021
static final String HELM_VALUES_PATH = 'applications/cluster-resources/secrets/vault/values.ftl.yaml'
21-
static final String NAMESPACE = 'secrets'
22-
23-
private Map config
22+
23+
String namespace = 'secrets'
24+
Map config
25+
K8sClient k8sClient
26+
2427
private FileSystemUtils fileSystemUtils
2528
private Path tmpHelmValues
26-
private K8sClient k8sClient
2729
private DeploymentStrategy deployer
2830
private AirGappedUtils airGappedUtils
2931

@@ -51,13 +53,6 @@ class Vault extends Feature {
5153
@Override
5254
void enable() {
5355
// Note that some specific configuration steps are implemented in ArgoCD
54-
55-
if (config.registry['createImagePullSecrets'] && config.registry['twoRegistries']) {
56-
k8sClient.createImagePullSecret('proxy-registry', NAMESPACE, config.registry['proxyUrl'] as String,
57-
config.registry['proxyUsername'] as String,
58-
config.registry['proxyPassword'] as String)
59-
}
60-
6156
def helmConfig = config['features']['secrets']['vault']['helm']
6257

6358
def yaml = new YamlSlurper().parseText(
@@ -145,7 +140,7 @@ class Vault extends Feature {
145140
"vault",
146141
'.',
147142
vaultVersion,
148-
'secrets',
143+
namespace,
149144
'vault',
150145
tmpHelmValues, DeploymentStrategy.RepoType.GIT
151146
)
@@ -155,7 +150,7 @@ class Vault extends Feature {
155150
'vault',
156151
helmConfig['chart'] as String,
157152
helmConfig['version'] as String,
158-
'secrets',
153+
namespace,
159154
'vault',
160155
tmpHelmValues
161156
)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.cloudogu.gitops
2+
3+
import com.cloudogu.gitops.utils.K8sClient
4+
import com.cloudogu.gitops.utils.K8sClientForTest
5+
import org.junit.jupiter.api.Test
6+
7+
class FeatureTest {
8+
Map config = [
9+
registry: [
10+
createImagePullSecrets: false
11+
],
12+
application: [
13+
namePrefix: "foo-"
14+
]
15+
]
16+
K8sClientForTest k8sClient = new K8sClientForTest(config)
17+
18+
@Test
19+
void 'Image pull secrets are create automatically'() {
20+
config['registry']['createImagePullSecrets'] = true
21+
config['registry']['twoRegistries'] = true
22+
config['registry']['proxyUrl'] = 'proxy-url'
23+
config['registry']['proxyUsername'] = 'proxy-user'
24+
config['registry']['proxyPassword'] = 'proxy-pw'
25+
26+
Feature feature = new FeatureWithImageForTest()
27+
feature.config = config
28+
feature.k8sClient = k8sClient
29+
feature.namespace = 'my-ns'
30+
31+
feature.install()
32+
33+
k8sClient.commandExecutorForTest.assertExecuted(
34+
'kubectl create secret docker-registry proxy-registry -n foo-my-ns' +
35+
' --docker-server proxy-url --docker-username proxy-user --docker-password proxy-pw')
36+
}
37+
38+
class FeatureWithImageForTest extends Feature implements FeatureWithImage {
39+
40+
String namespace
41+
Map config
42+
K8sClient k8sClient
43+
44+
@Override
45+
boolean isEnabled() {
46+
return true
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)