Skip to content
Merged
Show file tree
Hide file tree
Changes from 46 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
8e158ce
all: add conditional set-me in config
Ajarmar Jan 23, 2024
c623d21
fix failing test and add unit tests for conditional-set-me
Ajarmar Feb 27, 2024
7c36189
update resources
Ajarmar Apr 22, 2024
6dd35d0
fix changes to rclone config
Ajarmar Apr 23, 2024
ae46d18
fix failing tests related to swift
Ajarmar Apr 23, 2024
a7de0f1
update migration library to account for conditional set-mes
Ajarmar Apr 23, 2024
6522ff2
account for azure changes
Ajarmar Apr 23, 2024
c790e0d
fix new conditional
Ajarmar Jun 4, 2024
1a3d492
use new method of private copy
Ajarmar Jun 4, 2024
6b246b4
netpol IPs should be a list
Ajarmar Jun 4, 2024
a2d2cd4
update resources again
Ajarmar Jun 4, 2024
e23c520
only ask abort once during validation
Ajarmar Jun 4, 2024
1ab2f46
simplify init with conditional set-me
Ajarmar Jun 4, 2024
4b1fa45
schema conditionals for letsencrypt
Ajarmar Jun 4, 2024
8a333c8
schema conditionals for trivy netpols
Ajarmar Jun 4, 2024
dd323a7
schema conditionals for kured netpols
Ajarmar Jun 4, 2024
6b354c2
schema conditionals for letsencrypt netpols
Ajarmar Jun 4, 2024
9ee793d
schema conditionals for falco netpols
Ajarmar Jun 4, 2024
8607060
schema conditionals and tests for externalDns netpols
Ajarmar Jun 4, 2024
49e5a6e
schema conditionals and updated tests for coredns netpols
Ajarmar Jun 4, 2024
1ab633a
schema conditionals for externaldns txtownerid
Ajarmar Jun 4, 2024
c8f43de
remove example for txtOwnerId
Ajarmar Jun 5, 2024
3183afb
schema conditionals for opsGenieHeartbeat
Ajarmar Jun 5, 2024
6612e61
schema conditionals for slack alerts
Ajarmar Jun 5, 2024
12fee49
schema conditionals for objectStorageSwift netpols
Ajarmar Jun 5, 2024
f4c5b57
schema conditionals for harbor netpols
Ajarmar Jun 5, 2024
a54fbfa
schema conditionals for monitoring externalDataSources
Ajarmar Jun 10, 2024
acd68fc
schema conditionals for opensearch plugin netpols
Ajarmar Jun 10, 2024
fccafaa
schema conditionals for rclone objectStorage netpols
Ajarmar Jun 10, 2024
689d1ee
schema conditionals and updated tests for rclone swift
Ajarmar Jun 11, 2024
b69d2ad
add missing config schema for rclone swift netpols
Ajarmar Jun 11, 2024
fb56f10
schema conditionals for rclone secondaryurl netpols
Ajarmar Jun 11, 2024
7b87089
schema conditionals for ingressnginx override netpols
Ajarmar Jun 11, 2024
1745796
schema conditionals for dex netpols
Ajarmar Jun 11, 2024
364fa87
add tags for all conditional set-me tests
Ajarmar Jun 11, 2024
30d1044
fix broken comment
Ajarmar Jun 11, 2024
3ab2e33
remove unnecessary override config
Ajarmar Jun 11, 2024
689cac7
don't test coredns serviceip since it is always set
Ajarmar Jun 11, 2024
c2e3bfd
fix failing tests
Ajarmar Jun 11, 2024
bbeb61c
only set openstack netpol ips on openstack envs
Ajarmar Jun 12, 2024
2161551
update resources for update-ips test
Ajarmar Jun 12, 2024
b85231f
fix failing schema tests
Ajarmar Jun 12, 2024
192e57f
bash cleanup
Ajarmar Jun 14, 2024
c9a211a
remove unused function
Ajarmar Jun 14, 2024
4840881
update conditional for objectStorageSwift
Ajarmar Jun 14, 2024
d689108
fix swift test
Ajarmar Jun 17, 2024
e25dba6
less duplicated code
Ajarmar Jun 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 42 additions & 11 deletions bin/common.bash
Original file line number Diff line number Diff line change
Expand Up @@ -344,24 +344,48 @@ validate_version() {
# future.
validate_config() {
log_info "Validating $1 config"

check_conditionals() {
merged_config="${1}"
template_config="${2}"

# Loop all lines in ${template_config} and checks if same option has conditional set-me in ${merged_config}
options="$(yq_read_block "${template_config}" "set-me-if-*")"
for opt in ${options}; do
opt_value="$(yq4 "${opt}" "${merged_config}")"
opt_value_no_list="$(yq4 "[.] | flatten | .[0]" <<< "${opt_value}")"

if [[ "${opt_value_no_list}" =~ ^set-me-if-.*$ ]]; then
required_condition="$(sed -rn 's/^set-me-if-(.*)/\1/p' <<< "${opt_value_no_list}")"
if [[ "$(yq4 "${required_condition}" "${merged_config}")" == "true" ]]; then
# If the option is a list, set the first element in the list
if [[ "$(yq4 "${opt} | tag" "${merged_config}")" == "!!seq" ]]; then
yq4 "${opt}[0] = \"set-me\"" -i "${merged_config}"
yq4 "${opt}[0] = \"set-me\"" -i "${template_config}"
log_info "Set-me condition matched for ${opt}"
else
yq4 "${opt} = \"set-me\"" -i "${merged_config}"
yq4 "${opt} = \"set-me\"" -i "${template_config}"
log_info "Set-me condition matched for ${opt}"
fi
fi
fi
done
}

validate() {
merged_config="${1}"
template_config="${2}"

# Loop all lines in ${template_config} and warns if same option is not available in ${merged_config}
options=$(yq_read_block "${template_config}" "set-me")
maybe_exit="false"
for opt in ${options}; do
compare=$(diff <(yq4 -oj "${opt}" "${template_config}") <(yq4 -oj "${opt}" "${merged_config}") || true)
if [[ -z "${compare}" ]]; then
log_warning "WARN: ${opt} is not set in config"
maybe_exit="true"
fi
done

if ${maybe_exit} && ! ${CK8S_AUTO_APPROVE}; then
ask_abort
fi
}

schema_validate() {
Expand All @@ -376,29 +400,30 @@ validate_config() {
sed -r 's/^.*_(..-config\.yaml): fail: (.*)/\1: \2/; / failed validation$/q' < "${schema_validation_result}"
grep -oP '(?<=fail: )[^:]+' "${schema_validation_result}" | sort -u |
while read -r jpath; do
echo -n ".$jpath = "
yq4 -oj ".$jpath" "${merged_config}"
if [[ $jpath != "(root)" ]]; then
echo -n ".$jpath = "
yq4 -oj ".$jpath" "${merged_config}"
fi
done
maybe_exit="true"
fi

if ${maybe_exit} && ! ${CK8S_AUTO_APPROVE}; then
ask_abort
fi
}

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

maybe_exit="false"
if [[ $1 == "sc" ]]; then
check_config "${config_template_path}/common-config.yaml" \
"${config_template_path}/sc-config.yaml" \
"${config_template_path}/secrets.yaml"
yq_merge "${config_template_path}/common-config.yaml" \
"${config_template_path}/sc-config.yaml" \
> "${template_file}"
check_conditionals "${config[config_file_sc]}" "${template_file}"
validate "${config[config_file_sc]}" "${template_file}"
schema_validate "${config[config_file_sc]}" "${config_template_path}/schemas/config.yaml"
check_conditionals "${secrets[secrets_file]}" "${config_template_path}/secrets.yaml"
validate "${secrets[secrets_file]}" "${config_template_path}/secrets.yaml"
schema_validate "${secrets[secrets_file]}" "${config_template_path}/schemas/secrets.yaml"
elif [[ $1 == "wc" ]]; then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: could you extract the common parts for sc/wc and run them after the if-statements?

Something like this?

Suggested change
if [[ $1 == "sc" ]]; then
check_config "${config_template_path}/common-config.yaml" \
"${config_template_path}/sc-config.yaml" \
"${config_template_path}/secrets.yaml"
yq_merge "${config_template_path}/common-config.yaml" \
"${config_template_path}/sc-config.yaml" \
> "${template_file}"
check_conditionals "${config[config_file_sc]}" "${template_file}"
validate "${config[config_file_sc]}" "${template_file}"
schema_validate "${config[config_file_sc]}" "${config_template_path}/schemas/config.yaml"
check_conditionals "${secrets[secrets_file]}" "${config_template_path}/secrets.yaml"
validate "${secrets[secrets_file]}" "${config_template_path}/secrets.yaml"
schema_validate "${secrets[secrets_file]}" "${config_template_path}/schemas/secrets.yaml"
elif [[ $1 == "wc" ]]; then
if [[ $1 == "sc" ]]; then
check_config "${config_template_path}/common-config.yaml" \
"${config_template_path}/sc-config.yaml" \
"${config_template_path}/secrets.yaml"
yq_merge "${config_template_path}/common-config.yaml" \
"${config_template_path}/sc-config.yaml" \
> "${template_file}"
config_to_validate="${config[config_file_sc]}"
elif [[ $1 == "wc" ]]; then
...
else
...
fi
check_conditionals "${config_to_validate}" "${template_file}"
validate "${config_to_validate}" "${template_file}"
schema_validate "${config_to_validate}" "${config_template_path}/schemas/config.yaml"
check_conditionals "${secrets[secrets_file]}" "${config_template_path}/secrets.yaml"
validate "${secrets[secrets_file]}" "${config_template_path}/secrets.yaml"
schema_validate "${secrets[secrets_file]}" "${config_template_path}/schemas/secrets.yaml"

Expand All @@ -408,14 +433,20 @@ validate_config() {
yq_merge "${config_template_path}/common-config.yaml" \
"${config_template_path}/wc-config.yaml" \
> "${template_file}"
check_conditionals "${config[config_file_wc]}" "${template_file}"
validate "${config[config_file_wc]}" "${template_file}"
schema_validate "${config[config_file_wc]}" "${config_template_path}/schemas/config.yaml"
check_conditionals "${secrets[secrets_file]}" "${config_template_path}/secrets.yaml"
validate "${secrets[secrets_file]}" "${config_template_path}/secrets.yaml"
schema_validate "${secrets[secrets_file]}" "${config_template_path}/schemas/secrets.yaml"
else
log_error "ERROR: usage validate_config <sc|wc>"
exit 1
fi

if ${maybe_exit} && ! ${CK8S_AUTO_APPROVE}; then
ask_abort
fi
}

validate_sops_config() {
Expand Down
12 changes: 0 additions & 12 deletions bin/init.bash
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,6 @@ generate_sops_config() {
sops_config_write_fingerprints "${fingerprint}"
}

# Only writes value if it is set to "set-me*"
# Usage: replace_set_me <file> <field> <value>
replace_set_me(){
if [[ $# -ne 3 ]]; then
log_error "ERROR: number of args in replace_set_me must be 3. #=[$#]"
exit 1
fi
if [[ $(yq4 "${2}" "${1}") =~ ^set-me.* ]]; then
yq4 --inplace "${2} = ${3}" "${1}"
fi
}

# Usage: generate_default_config <default_config>
generate_default_config() {
if [[ $# -ne 1 ]]; then
Expand Down
27 changes: 14 additions & 13 deletions config/common-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -583,11 +583,11 @@ ingressNginx:

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

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

## Enable node port allocation
## ref: https://kubernetes.io/docs/concepts/services-networking/service/#load-balancer-nodeport-allocation
Expand Down Expand Up @@ -670,7 +670,7 @@ issuers:
enabled: true
prod:
## Mail through which letsencrypt can contact you.
email: set-me
email: set-me-if-(.issuers.letsencrypt.enabled)
## Solvers, sets a default http01 when empty.
solvers: []
# - selector:
Expand All @@ -687,7 +687,7 @@ issuers:
# key: secretKey
staging:
## Mail through which letsencrypt can contact you.
email: set-me
email: set-me-if-(.issuers.letsencrypt.enabled)
## Solvers, sets a default http01 when empty.
solvers: []

Expand Down Expand Up @@ -1024,14 +1024,14 @@ networkPolicies:
ingressUsingHostNetwork: set-me
trivy:
ips:
- set-me
- set-me-if-(.trivy.enabled)
port: 443

kured:
enabled: true
notificationSlack:
ips:
- set-me-if-kured.notification.slack.enabled
- set-me-if-(.kured.enabled and .kured.notification.slack.enabled)
ports:
- 443

Expand All @@ -1043,7 +1043,7 @@ networkPolicies:
# letsencrypt ip addresses
letsencrypt:
ips:
- set-me
- set-me-if-(.networkPolicies.certManager.enabled)
# Configure this if DNS-01 challenges are enabled in cert-manager
dns01:
ips: []
Expand All @@ -1053,20 +1053,20 @@ networkPolicies:
ingressOverride:
enabled: set-me
ips:
- set-me-if-enabled
- set-me-if-(.networkPolicies.ingressNginx.ingressOverride.enabled)

falco:
enabled: true
plugins:
ips:
- set-me
- set-me-if-(.falco.enabled and .networkPolicies.falco.enabled)
ports:
- 443

externalDns:
enabled: false
ips:
- set-me-if-externalDns.enabled
- set-me-if-(.externalDns.enabled and .networkPolicies.externalDns.enabled)
ports:
- 443

Expand All @@ -1091,9 +1091,10 @@ networkPolicies:
enabled: true
externalDns:
ips:
- set-me
- set-me-if-(.networkPolicies.coredns.enabled)
serviceIp:
ips: set-me
ips:
- set-me-if-(.networkPolicies.coredns.enabled)

dnsAutoscaler:
enabled: true
Expand Down Expand Up @@ -1131,7 +1132,7 @@ externalDns:
# Example: https://kubernetes-sigs.github.io/external-dns/v0.14.1/tutorials/aws/
enabled: false
provider: aws
txtOwnerId: set-me-if-externalDns.enabled
txtOwnerId: set-me-if-(.externalDns.enabled)
sources:
crd: false
ingress: true
Expand Down
1 change: 0 additions & 1 deletion config/flavors/prod/sc-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ alerts:
alertTo: opsgenie
opsGenieHeartbeat:
enabled: true
name: set-me

prometheus:
retention:
Expand Down
2 changes: 0 additions & 2 deletions config/providers/baremetal/common-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ ingressNginx:
useHostPort: true
service:
enabled: false
type: set-me-if-ingressNginx.controller.service.enabled
annotations: set-me-if-ingressNginx.controller.service.enabled
allocateLoadBalancerNodePorts: true
networkPolicies:
global:
Expand Down
2 changes: 0 additions & 2 deletions config/providers/exoscale/common-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ ingressNginx:
service:
enabled: false
allocateLoadBalancerNodePorts: true
type: set-me-if-ingressNginx.controller.service.enabled
annotations: set-me-if-ingressNginx.controller.service.enabled
networkPolicies:
global:
externalLoadBalancer: true
Expand Down
2 changes: 0 additions & 2 deletions config/providers/safespring/common-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ ingressNginx:
service:
enabled: false
allocateLoadBalancerNodePorts: true
type: set-me-if-ingressNginx.controller.service.enabled
annotations: set-me-if-ingressNginx.controller.service.enabled
externalTrafficPolicy:
local: false
opa:
Expand Down
2 changes: 0 additions & 2 deletions config/providers/upcloud/common-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ ingressNginx:
service:
enabled: false
allocateLoadBalancerNodePorts: true
type: set-me-if-ingressNginx.controller.service.enabled
annotations: set-me-if-ingressNginx.controller.service.enabled
externalTrafficPolicy:
local: false
opa:
Expand Down
27 changes: 13 additions & 14 deletions config/sc-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1097,9 +1097,9 @@ alerts:
opsGenieHeartbeat:
enabled: false
url: https://api.eu.opsgenie.com/v2/heartbeats
name: set-me-if-enabled
name: set-me-if-(.alerts.opsGenieHeartbeat.enabled)
slack:
channel: set-me-if-enabled
channel: set-me-if-(.alerts.alertTo == "slack")
# Alertmanager templating: https://prometheus.io/docs/alerting/notifications/
customTemplate: {}
## Example:
Expand Down Expand Up @@ -1182,7 +1182,7 @@ networkPolicies:
global:
objectStorageSwift:
ips:
- "set-me-if-enabled"
- set-me-if-(.harbor.persistence.type == "swift" or .thanos.objectStorage.type == "swift")
ports:
- 5000
scApiserver:
Expand All @@ -1199,12 +1199,12 @@ networkPolicies:
# For replication, added to core and jobservice
registries:
ips:
- "set-me"
- set-me-if-(.harbor.enabled and .networkPolicies.harbor.enabled)
ports:
- 443
jobservice:
ips:
- "set-me"
- set-me-if-(.harbor.enabled and .networkPolicies.harbor.enabled)
ports:
- 443
database:
Expand Down Expand Up @@ -1244,7 +1244,7 @@ networkPolicies:
trivy:
# IP to trivy vulnerability database
ips:
- "set-me"
- set-me-if-(.harbor.enabled and .networkPolicies.harbor.enabled)
ports:
- 443
monitoring:
Expand All @@ -1254,9 +1254,9 @@ networkPolicies:
externalDataSources:
enabled: false
ips:
- "set-me-if-externalDataSources.enabled"
- set-me-if-(.networkPolicies.monitoring.enabled and .networkPolicies.monitoring.grafana.externalDataSources.enabled)
ports:
- "set-me-if-externalDataSources.enabled"
- set-me-if-(.networkPolicies.monitoring.enabled and .networkPolicies.monitoring.grafana.externalDataSources.enabled)
# loading dashboards from grafana website
externalDashboardProvider:
ips:
Expand All @@ -1270,7 +1270,7 @@ networkPolicies:
enabled: true
plugins:
ips:
- "set-me"
- set-me-if-(.networkPolicies.opensearch.enabled)
ports:
- 443

Expand All @@ -1283,20 +1283,19 @@ networkPolicies:
sync:
objectStorage:
ips:
- set-me-if-objectStorage.sync.enabled
- set-me-if-(.objectStorage.sync.enabled and .objectStorage.type == "s3")
ports:
- 443
objectStorageSwift:
ips:
- set-me-if-objectStorage.sync.enabled-and-any-target-use-swift-as-destination
- set-me-if-(.objectStorage.sync.enabled and (.harbor.persistence.type == "swift" or .thanos.objectStorage.type == "swift"))
ports:
- 5000
secondaryUrl:
ips:
- set-me-if-objectStorage.sync.secondaryUrl-has-an-url
- set-me-if-(.objectStorage.sync.secondaryUrl != null and .objectStorage.sync.secondaryUrl != "")
ports:
- 443

s3Exporter:
enabled: true

Expand All @@ -1313,7 +1312,7 @@ networkPolicies:
# Ip to connector, e.g. Google, LDAP, ...
connectors:
ips:
- "set-me"
- set-me-if-(.networkPolicies.dex.enabled)
ports:
- 443

Expand Down
Loading