Skip to content

Commit 3929a92

Browse files
authored
Add conditional set-me in config (#1980)
1 parent 344adb1 commit 3929a92

File tree

15 files changed

+1048
-197
lines changed

15 files changed

+1048
-197
lines changed

bin/common.bash

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -344,24 +344,48 @@ validate_version() {
344344
# future.
345345
validate_config() {
346346
log_info "Validating $1 config"
347+
348+
check_conditionals() {
349+
merged_config="${1}"
350+
template_config="${2}"
351+
352+
# Loop all lines in ${template_config} and checks if same option has conditional set-me in ${merged_config}
353+
options="$(yq_read_block "${template_config}" "set-me-if-*")"
354+
for opt in ${options}; do
355+
opt_value="$(yq4 "${opt}" "${merged_config}")"
356+
opt_value_no_list="$(yq4 "[.] | flatten | .[0]" <<< "${opt_value}")"
357+
358+
if [[ "${opt_value_no_list}" =~ ^set-me-if-.*$ ]]; then
359+
required_condition="$(sed -rn 's/^set-me-if-(.*)/\1/p' <<< "${opt_value_no_list}")"
360+
if [[ "$(yq4 "${required_condition}" "${merged_config}")" == "true" ]]; then
361+
# If the option is a list, set the first element in the list
362+
if [[ "$(yq4 "${opt} | tag" "${merged_config}")" == "!!seq" ]]; then
363+
yq4 "${opt}[0] = \"set-me\"" -i "${merged_config}"
364+
yq4 "${opt}[0] = \"set-me\"" -i "${template_config}"
365+
log_info "Set-me condition matched for ${opt}"
366+
else
367+
yq4 "${opt} = \"set-me\"" -i "${merged_config}"
368+
yq4 "${opt} = \"set-me\"" -i "${template_config}"
369+
log_info "Set-me condition matched for ${opt}"
370+
fi
371+
fi
372+
fi
373+
done
374+
}
375+
347376
validate() {
348377
merged_config="${1}"
349378
template_config="${2}"
350379

351380
# Loop all lines in ${template_config} and warns if same option is not available in ${merged_config}
352381
options=$(yq_read_block "${template_config}" "set-me")
353-
maybe_exit="false"
354382
for opt in ${options}; do
355383
compare=$(diff <(yq4 -oj "${opt}" "${template_config}") <(yq4 -oj "${opt}" "${merged_config}") || true)
356384
if [[ -z "${compare}" ]]; then
357385
log_warning "WARN: ${opt} is not set in config"
358386
maybe_exit="true"
359387
fi
360388
done
361-
362-
if ${maybe_exit} && ! ${CK8S_AUTO_APPROVE}; then
363-
ask_abort
364-
fi
365389
}
366390

367391
schema_validate() {
@@ -376,46 +400,50 @@ validate_config() {
376400
sed -r 's/^.*_(..-config\.yaml): fail: (.*)/\1: \2/; / failed validation$/q' < "${schema_validation_result}"
377401
grep -oP '(?<=fail: )[^:]+' "${schema_validation_result}" | sort -u |
378402
while read -r jpath; do
379-
echo -n ".$jpath = "
380-
yq4 -oj ".$jpath" "${merged_config}"
403+
if [[ $jpath != "(root)" ]]; then
404+
echo -n ".$jpath = "
405+
yq4 -oj ".$jpath" "${merged_config}"
406+
fi
381407
done
382408
maybe_exit="true"
383409
fi
384-
385-
if ${maybe_exit} && ! ${CK8S_AUTO_APPROVE}; then
386-
ask_abort
387-
fi
388410
}
389411

390412
template_file=$(mktemp --suffix="-tpl.yaml")
391413
append_trap "rm ${template_file}" EXIT
392414

415+
maybe_exit="false"
393416
if [[ $1 == "sc" ]]; then
394417
check_config "${config_template_path}/common-config.yaml" \
395418
"${config_template_path}/sc-config.yaml" \
396419
"${config_template_path}/secrets.yaml"
397420
yq_merge "${config_template_path}/common-config.yaml" \
398421
"${config_template_path}/sc-config.yaml" \
399422
> "${template_file}"
400-
validate "${config[config_file_sc]}" "${template_file}"
401-
schema_validate "${config[config_file_sc]}" "${config_template_path}/schemas/config.yaml"
402-
validate "${secrets[secrets_file]}" "${config_template_path}/secrets.yaml"
403-
schema_validate "${secrets[secrets_file]}" "${config_template_path}/schemas/secrets.yaml"
423+
config_to_validate="${config[config_file_sc]}"
404424
elif [[ $1 == "wc" ]]; then
405425
check_config "${config_template_path}/common-config.yaml" \
406426
"${config_template_path}/wc-config.yaml" \
407427
"${config_template_path}/secrets.yaml"
408428
yq_merge "${config_template_path}/common-config.yaml" \
409429
"${config_template_path}/wc-config.yaml" \
410430
> "${template_file}"
411-
validate "${config[config_file_wc]}" "${template_file}"
412-
schema_validate "${config[config_file_wc]}" "${config_template_path}/schemas/config.yaml"
413-
validate "${secrets[secrets_file]}" "${config_template_path}/secrets.yaml"
414-
schema_validate "${secrets[secrets_file]}" "${config_template_path}/schemas/secrets.yaml"
431+
config_to_validate="${config[config_file_wc]}"
415432
else
416433
log_error "ERROR: usage validate_config <sc|wc>"
417434
exit 1
418435
fi
436+
437+
check_conditionals "${config_to_validate}" "${template_file}"
438+
validate "${config_to_validate}" "${template_file}"
439+
schema_validate "${config_to_validate}" "${config_template_path}/schemas/config.yaml"
440+
check_conditionals "${secrets[secrets_file]}" "${config_template_path}/secrets.yaml"
441+
validate "${secrets[secrets_file]}" "${config_template_path}/secrets.yaml"
442+
schema_validate "${secrets[secrets_file]}" "${config_template_path}/schemas/secrets.yaml"
443+
444+
if ${maybe_exit} && ! ${CK8S_AUTO_APPROVE}; then
445+
ask_abort
446+
fi
419447
}
420448

421449
validate_sops_config() {

bin/init.bash

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,6 @@ generate_sops_config() {
8686
sops_config_write_fingerprints "${fingerprint}"
8787
}
8888

89-
# Only writes value if it is set to "set-me*"
90-
# Usage: replace_set_me <file> <field> <value>
91-
replace_set_me(){
92-
if [[ $# -ne 3 ]]; then
93-
log_error "ERROR: number of args in replace_set_me must be 3. #=[$#]"
94-
exit 1
95-
fi
96-
if [[ $(yq4 "${2}" "${1}") =~ ^set-me.* ]]; then
97-
yq4 --inplace "${2} = ${3}" "${1}"
98-
fi
99-
}
100-
10189
# Usage: generate_default_config <default_config>
10290
generate_default_config() {
10391
if [[ $# -ne 1 ]]; then

config/common-config.yaml

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -583,11 +583,11 @@ ingressNginx:
583583

584584
## Type of service.
585585
## ref: https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types
586-
type: set-me
586+
type: set-me-if-(.ingressNginx.controller.service.enabled)
587587

588588
## Annotations to add to service
589589
## ref: https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/
590-
annotations: set-me
590+
annotations: set-me-if-(.ingressNginx.controller.service.enabled)
591591

592592
## Enable node port allocation
593593
## ref: https://kubernetes.io/docs/concepts/services-networking/service/#load-balancer-nodeport-allocation
@@ -670,7 +670,7 @@ issuers:
670670
enabled: true
671671
prod:
672672
## Mail through which letsencrypt can contact you.
673-
email: set-me
673+
email: set-me-if-(.issuers.letsencrypt.enabled)
674674
## Solvers, sets a default http01 when empty.
675675
solvers: []
676676
# - selector:
@@ -687,7 +687,7 @@ issuers:
687687
# key: secretKey
688688
staging:
689689
## Mail through which letsencrypt can contact you.
690-
email: set-me
690+
email: set-me-if-(.issuers.letsencrypt.enabled)
691691
## Solvers, sets a default http01 when empty.
692692
solvers: []
693693

@@ -1026,14 +1026,14 @@ networkPolicies:
10261026
ingressUsingHostNetwork: set-me
10271027
trivy:
10281028
ips:
1029-
- set-me
1029+
- set-me-if-(.trivy.enabled)
10301030
port: 443
10311031

10321032
kured:
10331033
enabled: true
10341034
notificationSlack:
10351035
ips:
1036-
- set-me-if-kured.notification.slack.enabled
1036+
- set-me-if-(.kured.enabled and .kured.notification.slack.enabled)
10371037
ports:
10381038
- 443
10391039

@@ -1045,7 +1045,7 @@ networkPolicies:
10451045
# letsencrypt ip addresses
10461046
letsencrypt:
10471047
ips:
1048-
- set-me
1048+
- set-me-if-(.networkPolicies.certManager.enabled)
10491049
# Configure this if DNS-01 challenges are enabled in cert-manager
10501050
dns01:
10511051
ips: []
@@ -1055,20 +1055,20 @@ networkPolicies:
10551055
ingressOverride:
10561056
enabled: set-me
10571057
ips:
1058-
- set-me-if-enabled
1058+
- set-me-if-(.networkPolicies.ingressNginx.ingressOverride.enabled)
10591059

10601060
falco:
10611061
enabled: true
10621062
plugins:
10631063
ips:
1064-
- set-me
1064+
- set-me-if-(.falco.enabled and .networkPolicies.falco.enabled)
10651065
ports:
10661066
- 443
10671067

10681068
externalDns:
10691069
enabled: false
10701070
ips:
1071-
- set-me-if-externalDns.enabled
1071+
- set-me-if-(.externalDns.enabled and .networkPolicies.externalDns.enabled)
10721072
ports:
10731073
- 443
10741074

@@ -1093,9 +1093,10 @@ networkPolicies:
10931093
enabled: true
10941094
externalDns:
10951095
ips:
1096-
- set-me
1096+
- set-me-if-(.networkPolicies.coredns.enabled)
10971097
serviceIp:
1098-
ips: set-me
1098+
ips:
1099+
- set-me-if-(.networkPolicies.coredns.enabled)
10991100

11001101
dnsAutoscaler:
11011102
enabled: true
@@ -1133,7 +1134,7 @@ externalDns:
11331134
# Example: https://kubernetes-sigs.github.io/external-dns/v0.14.1/tutorials/aws/
11341135
enabled: false
11351136
provider: aws
1136-
txtOwnerId: set-me-if-externalDns.enabled
1137+
txtOwnerId: set-me-if-(.externalDns.enabled)
11371138
sources:
11381139
crd: false
11391140
ingress: true

config/flavors/prod/sc-config.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ alerts:
55
alertTo: opsgenie
66
opsGenieHeartbeat:
77
enabled: true
8-
name: set-me
98

109
prometheus:
1110
retention:

config/providers/baremetal/common-config.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ ingressNginx:
99
useHostPort: true
1010
service:
1111
enabled: false
12-
type: set-me-if-ingressNginx.controller.service.enabled
13-
annotations: set-me-if-ingressNginx.controller.service.enabled
1412
allocateLoadBalancerNodePorts: true
1513
networkPolicies:
1614
global:

config/providers/exoscale/common-config.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ ingressNginx:
1414
service:
1515
enabled: false
1616
allocateLoadBalancerNodePorts: true
17-
type: set-me-if-ingressNginx.controller.service.enabled
18-
annotations: set-me-if-ingressNginx.controller.service.enabled
1917
networkPolicies:
2018
global:
2119
externalLoadBalancer: true

config/providers/safespring/common-config.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ ingressNginx:
3636
service:
3737
enabled: false
3838
allocateLoadBalancerNodePorts: true
39-
type: set-me-if-ingressNginx.controller.service.enabled
40-
annotations: set-me-if-ingressNginx.controller.service.enabled
4139
externalTrafficPolicy:
4240
local: false
4341
opa:

config/providers/upcloud/common-config.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ ingressNginx:
3232
service:
3333
enabled: false
3434
allocateLoadBalancerNodePorts: true
35-
type: set-me-if-ingressNginx.controller.service.enabled
36-
annotations: set-me-if-ingressNginx.controller.service.enabled
3735
externalTrafficPolicy:
3836
local: false
3937
opa:

config/sc-config.yaml

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,9 +1097,9 @@ alerts:
10971097
opsGenieHeartbeat:
10981098
enabled: false
10991099
url: https://api.eu.opsgenie.com/v2/heartbeats
1100-
name: set-me-if-enabled
1100+
name: set-me-if-(.alerts.opsGenieHeartbeat.enabled)
11011101
slack:
1102-
channel: set-me-if-enabled
1102+
channel: set-me-if-(.alerts.alertTo == "slack")
11031103
# Alertmanager templating: https://prometheus.io/docs/alerting/notifications/
11041104
customTemplate: {}
11051105
## Example:
@@ -1182,7 +1182,7 @@ networkPolicies:
11821182
global:
11831183
objectStorageSwift:
11841184
ips:
1185-
- "set-me-if-enabled"
1185+
- set-me-if-(.harbor.persistence.type == "swift" or .thanos.objectStorage.type == "swift")
11861186
ports:
11871187
- 5000
11881188
scApiserver:
@@ -1199,12 +1199,12 @@ networkPolicies:
11991199
# For replication, added to core and jobservice
12001200
registries:
12011201
ips:
1202-
- "set-me"
1202+
- set-me-if-(.harbor.enabled and .networkPolicies.harbor.enabled)
12031203
ports:
12041204
- 443
12051205
jobservice:
12061206
ips:
1207-
- "set-me"
1207+
- set-me-if-(.harbor.enabled and .networkPolicies.harbor.enabled)
12081208
ports:
12091209
- 443
12101210
database:
@@ -1244,7 +1244,7 @@ networkPolicies:
12441244
trivy:
12451245
# IP to trivy vulnerability database
12461246
ips:
1247-
- "set-me"
1247+
- set-me-if-(.harbor.enabled and .networkPolicies.harbor.enabled)
12481248
ports:
12491249
- 443
12501250
monitoring:
@@ -1254,9 +1254,9 @@ networkPolicies:
12541254
externalDataSources:
12551255
enabled: false
12561256
ips:
1257-
- "set-me-if-externalDataSources.enabled"
1257+
- set-me-if-(.networkPolicies.monitoring.enabled and .networkPolicies.monitoring.grafana.externalDataSources.enabled)
12581258
ports:
1259-
- "set-me-if-externalDataSources.enabled"
1259+
- set-me-if-(.networkPolicies.monitoring.enabled and .networkPolicies.monitoring.grafana.externalDataSources.enabled)
12601260
# loading dashboards from grafana website
12611261
externalDashboardProvider:
12621262
ips:
@@ -1270,7 +1270,7 @@ networkPolicies:
12701270
enabled: true
12711271
plugins:
12721272
ips:
1273-
- "set-me"
1273+
- set-me-if-(.networkPolicies.opensearch.enabled)
12741274
ports:
12751275
- 443
12761276

@@ -1283,20 +1283,19 @@ networkPolicies:
12831283
sync:
12841284
objectStorage:
12851285
ips:
1286-
- set-me-if-objectStorage.sync.enabled
1286+
- set-me-if-(.objectStorage.sync.enabled and .objectStorage.type == "s3")
12871287
ports:
12881288
- 443
12891289
objectStorageSwift:
12901290
ips:
1291-
- set-me-if-objectStorage.sync.enabled-and-any-target-use-swift-as-destination
1291+
- set-me-if-(.objectStorage.sync.enabled and (.harbor.persistence.type == "swift" or .thanos.objectStorage.type == "swift"))
12921292
ports:
12931293
- 5000
12941294
secondaryUrl:
12951295
ips:
1296-
- set-me-if-objectStorage.sync.secondaryUrl-has-an-url
1296+
- set-me-if-(.objectStorage.sync.secondaryUrl != null and .objectStorage.sync.secondaryUrl != "")
12971297
ports:
12981298
- 443
1299-
13001299
s3Exporter:
13011300
enabled: true
13021301

@@ -1313,7 +1312,7 @@ networkPolicies:
13131312
# Ip to connector, e.g. Google, LDAP, ...
13141313
connectors:
13151314
ips:
1316-
- "set-me"
1315+
- set-me-if-(.networkPolicies.dex.enabled)
13171316
ports:
13181317
- 443
13191318

0 commit comments

Comments
 (0)