Skip to content

Commit

Permalink
Merge branch 'improvement/2488-make-csc-upgrade-able' into q/2.5
Browse files Browse the repository at this point in the history
  • Loading branch information
bert-e committed May 6, 2020
2 parents ccc81c0 + e12ce23 commit f5d4ccd
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 75 deletions.
4 changes: 4 additions & 0 deletions buildchain/buildchain/salt_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ def _get_parts(self) -> Iterator[str]:
Path('salt/metalk8s/addons/dex/ca/advertised.sls'),
Path('salt/metalk8s/addons/dex/certs/init.sls'),
Path('salt/metalk8s/addons/dex/certs/server.sls'),
Path('salt/metalk8s/addons/dex/config/dex.yaml'),
Path('salt/metalk8s/addons/dex/deployed/chart.sls'),
Path('salt/metalk8s/addons/dex/deployed/init.sls'),
Path('salt/metalk8s/addons/dex/deployed/namespace.sls'),
Expand Down Expand Up @@ -269,6 +270,9 @@ def _get_parts(self) -> Iterator[str]:
Path('salt/metalk8s/addons/prometheus-adapter/deployed/chart.sls'),
Path('salt/metalk8s/addons/prometheus-adapter/deployed/init.sls'),

Path('salt/metalk8s/addons/prometheus-operator/config/alertmanager.yaml'),
Path('salt/metalk8s/addons/prometheus-operator/config/grafana.yaml'),
Path('salt/metalk8s/addons/prometheus-operator/config/prometheus.yaml'),
Path('salt/metalk8s/addons/prometheus-operator/deployed/'
'alertmanager-configuration-secret.sls'),
Path('salt/metalk8s/addons/prometheus-operator/deployed/chart.sls'),
Expand Down
27 changes: 20 additions & 7 deletions charts/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#!jinja | metalk8s_kubernetes
{{%- from "metalk8s/repo/macro.sls" import build_image_name with context %}}
{csc_defaults}
{configlines}
{{% raw %}}
Expand Down Expand Up @@ -212,15 +213,27 @@ def main():
doc=doc
)
)
if args.service_configs:
import_csc_yaml = '\n'.join(
("{{% import_yaml 'metalk8s/addons/{0}/config/{1}.yaml' as "
"{1}_defaults with context %}}").format(
args.name, service_config[0]
) for service_config in args.service_configs
)

config = '\n'.join(
("{{%- set {} = salt.metalk8s_service_configuration"
".get_service_conf('{}', '{}') %}}").format(
service_config[0], args.namespace, service_config[1]
) for service_config in args.service_configs
) if args.service_configs else ''
config = '\n'.join(
("{{%- set {0} = salt.metalk8s_service_configuration"
".get_service_conf('{1}', '{2}', {0}_defaults) %}}").format(
service_config[0], args.namespace, service_config[1]
) for service_config in args.service_configs
)
else:
import_csc_yaml = ''
config = ''

sys.stdout.write(START_BLOCK.format(configlines=config).lstrip())
sys.stdout.write(START_BLOCK.format(
csc_defaults=import_csc_yaml, configlines=config).lstrip()
)
sys.stdout.write('\n')

stream = io.StringIO()
Expand Down
26 changes: 18 additions & 8 deletions salt/_modules/metalk8s_service_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import yaml

import salt
from salt.exceptions import CommandExecutionError


Expand All @@ -18,6 +19,7 @@ def __virtual__():
def get_service_conf(
namespace,
configmap_name,
default_csc,
apiVersion=None,
kind=None,
**kwargs
Expand All @@ -27,6 +29,7 @@ def get_service_conf(
Arguments:
configmap_name: the ConfigMap name
namespace: the Namespace where the ConfigMap is stored
default_csc: the CSC imported from YAML file
Returns:
A dict of a specific service configuration
Expand All @@ -35,13 +38,21 @@ def get_service_conf(
.. code-block:: bash
salt-call metalk8s_service_configuration.get_service_conf "metalk8s-monitoring" "metalk8s-prometheus-config"
salt-call metalk8s_service_configuration.get_service_conf
"metalk8s-monitoring" "metalk8s-prometheus-config"
'{"apiVersion":"addons.metalk8s.scality.com","kind":"PrometheusConfig","spec":{"deployment":{"replicas":1}}}'
"""

if not configmap_name:
raise Exception(
raise CommandExecutionError(
'Expected a ConfigMap name but got {}'.format(configmap_name)
)
if not isinstance(default_csc, dict):
raise CommandExecutionError(
'Expected default CSC for ConfigMap {} but got {}'.format(
configmap_name, default_csc
)
)

try:
manifest = __salt__[
Expand Down Expand Up @@ -83,10 +94,6 @@ def get_service_conf(
'Expected `config.yaml` as yaml in the ConfigMap {} but got {}'
.format(configmap_name, config)
)

if not apiVersion and not kind:
return config

if apiVersion and config['apiVersion'] != apiVersion:
raise CommandExecutionError(
'Expected value {} for key apiVersion, got {}'.format(
Expand All @@ -99,6 +106,9 @@ def get_service_conf(
kind, config['kind']
)
)
merged_config = salt.utils.dictupdate.merge(
default_csc, config, strategy='recurse',
merge_lists=True
)

#Todo: Need a full schema validation of the ConfigMap data portion
return config
return merged_config
16 changes: 16 additions & 0 deletions salt/metalk8s/addons/dex/config/dex.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
# Configuration of the Dex (OIDC) service
apiVersion: addons.metalk8s.scality.com
kind: DexConfig
spec:
# Configure the Dex Deployment
deployment:
replicas: 2
localuserstore:
enabled: true
userlist:
- email: "[email protected]"
hash: "$2a$10$2b2cU8CPhOTaGrs1HRQuAueS7JTT5ZHsHSzYiFPm1leZck7Mc8T4W"
username: "admin"
userID: "08a8684b-db88-4b73-90a9-3cd1661f5466"
connectors: []
3 changes: 2 additions & 1 deletion salt/metalk8s/addons/dex/deployed/chart.sls
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!jinja | metalk8s_kubernetes

{%- from "metalk8s/repo/macro.sls" import build_image_name with context %}
{%- set dex = salt.metalk8s_service_configuration.get_service_conf('metalk8s-auth', 'metalk8s-dex-config') %}
{% import_yaml 'metalk8s/addons/dex/config/dex.yaml' as dex_defaults with context %}
{%- set dex = salt.metalk8s_service_configuration.get_service_conf('metalk8s-auth', 'metalk8s-dex-config', dex_defaults) %}
{% raw %}
Expand Down
13 changes: 1 addition & 12 deletions salt/metalk8s/addons/dex/deployed/service-configuration.sls
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,7 @@ Create dex-config ConfigMap:
config.yaml: |-
apiVersion: addons.metalk8s.scality.com
kind: DexConfig
spec:
deployment:
replicas: 2
localuserstore:
enabled: true
userlist:
- email: "[email protected]"
hash: "$2a$10$2b2cU8CPhOTaGrs1HRQuAueS7JTT5ZHsHSzYiFPm1leZck7Mc8T4W"
username: "admin"
userID: "08a8684b-db88-4b73-90a9-3cd1661f5466"
connectors: []

spec: {}

{%- else %}

Expand Down
26 changes: 26 additions & 0 deletions salt/metalk8s/addons/prometheus-operator/config/alertmanager.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
# Configuration of the Alertmanager service
apiVersion: addons.metalk8s.scality.com
kind: AlertmanagerConfig
spec:
# Configure the Alertmanager Deployment
deployment:
replicas: 1
notification:
config:
global:
resolve_timeout: 5m
templates: []
route:
group_by: ['job']
group_wait: 30s
group_interval: 5m
repeat_interval: 12h
receiver: 'null'
routes:
- match:
alertname: Watchdog
receiver: 'null'
receivers:
- name: 'null'
inhibit_rules: []
8 changes: 8 additions & 0 deletions salt/metalk8s/addons/prometheus-operator/config/grafana.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Configuration of the Grafana service
apiVersion: addons.metalk8s.scality.com
kind: GrafanaConfig
spec:
# Configure the Grafana Deployment
deployment:
replicas: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Configuration of the Prometheus service
apiVersion: addons.metalk8s.scality.com
kind: PrometheusConfig
spec:
# Configure the Prometheus Deployment
deployment:
replicas: 1
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{% import_yaml 'metalk8s/addons/prometheus-operator/config/alertmanager.yaml' as alertmanager_defaults with context %}

{%- set alertmanager = salt.metalk8s_service_configuration.get_service_conf(
'metalk8s-monitoring', 'metalk8s-alertmanager-config'
'metalk8s-monitoring', 'metalk8s-alertmanager-config', alertmanager_defaults
)
%}

Expand Down
11 changes: 7 additions & 4 deletions salt/metalk8s/addons/prometheus-operator/deployed/chart.sls
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#!jinja | metalk8s_kubernetes

{%- from "metalk8s/repo/macro.sls" import build_image_name with context %}
{%- set grafana = salt.metalk8s_service_configuration.get_service_conf('metalk8s-monitoring', 'metalk8s-grafana-config') %}
{%- set prometheus = salt.metalk8s_service_configuration.get_service_conf('metalk8s-monitoring', 'metalk8s-prometheus-config') %}
{%- set alertmanager = salt.metalk8s_service_configuration.get_service_conf('metalk8s-monitoring', 'metalk8s-alertmanager-config') %}
{% import_yaml 'metalk8s/addons/prometheus-operator/config/grafana.yaml' as grafana_defaults with context %}
{% import_yaml 'metalk8s/addons/prometheus-operator/config/prometheus.yaml' as prometheus_defaults with context %}
{% import_yaml 'metalk8s/addons/prometheus-operator/config/alertmanager.yaml' as alertmanager_defaults with context %}
{%- set grafana = salt.metalk8s_service_configuration.get_service_conf('metalk8s-monitoring', 'metalk8s-grafana-config', grafana_defaults) %}
{%- set prometheus = salt.metalk8s_service_configuration.get_service_conf('metalk8s-monitoring', 'metalk8s-prometheus-config', prometheus_defaults) %}
{%- set alertmanager = salt.metalk8s_service_configuration.get_service_conf('metalk8s-monitoring', 'metalk8s-alertmanager-config', alertmanager_defaults) %}

{% raw %}

Expand Down Expand Up @@ -40382,7 +40385,7 @@ spec:
template:
metadata:
annotations:
checksum/config: 7bdc765fb193e66e7a094a5fb0ffe34218370368d820cffbaf93cae218ebe61f
checksum/config: 822028727d07edab6b024280685ef2510fcbae564ca11bea646dde763fd9559c
checksum/dashboards-json-config: 01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b
checksum/sc-dashboard-provider-config: 424200eb6040b7b1ec58add370935c65963d856f8ece2725caaa8390a3b54eee
checksum/secret: 90b18138547156baaa1588680e3842aabcb31605c7f7fb8baa7eacc2b6d4822b
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ Create grafana-config ConfigMap:
config.yaml: |-
apiVersion: addons.metalk8s.scality.com
kind: GrafanaConfig
spec:
deployment:
replicas: 1
spec: {}

{%- else %}

metalk8s-grafana-config ConfigMap already exist:
Expand All @@ -63,9 +62,8 @@ Create prometheus-config ConfigMap:
config.yaml: |-
apiVersion: addons.metalk8s.scality.com
kind: PrometheusConfig
spec:
deployment:
replicas: 1
spec: {}

{%- else %}

metalk8s-prometheus-config ConfigMap already exist:
Expand All @@ -87,27 +85,7 @@ Create alertmanager-config ConfigMap:
config.yaml: |-
apiVersion: addons.metalk8s.scality.com
kind: AlertmanagerConfig
spec:
deployment:
replicas: 1
notification:
config:
global:
resolve_timeout: 5m
templates: []
route:
group_by: ['job']
group_wait: 30s
group_interval: 5m
repeat_interval: 12h
receiver: 'null'
routes:
- match:
alertname: Watchdog
receiver: 'null'
receivers:
- name: 'null'
inhibit_rules: []
spec: {}

{%- else %}

Expand Down
2 changes: 1 addition & 1 deletion tests/post/features/service_configuration.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Feature: Cluster and Services Configurations
Given the Kubernetes API is available
And pods with label 'app.kubernetes.io/name=dex' are 'Ready'
And we have 2 running pod labeled 'app.kubernetes.io/name=dex' in namespace 'metalk8s-auth'
And we have a 'metalk8s-dex-config' CSC in namespace 'metalk8s-auth' with 'spec.deployment.replicas' equal to '2'
And we have a 'metalk8s-dex-config' CSC in namespace 'metalk8s-auth'
When we update 'metalk8s-dex-config' CSC in namespace 'metalk8s-auth' 'spec.deployment.replicas' to '3'
And we apply the 'metalk8s.addons.dex.deployed' salt state
Then we have '3' at 'status.available_replicas' for 'dex' Deployment in namespace 'metalk8s-auth'
Expand Down
32 changes: 18 additions & 14 deletions tests/post/steps/test_service_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@
from tests import utils


# Constants {{{


DEFAULT_CSC_CONFIG_YAML = """
apiVersion: addons.metalk8s.scality.com
kind: DexConfig
spec:
deployment:
replicas: {replicas}
"""


# }}}

# Fixtures {{{


Expand Down Expand Up @@ -42,15 +56,12 @@ def test_service_config_propagation(host):


@given(parsers.parse(
"we have a '{name}' CSC in namespace '{namespace}' with "
"'{path}' equal to '{value}'"))
"we have a '{name}' CSC in namespace '{namespace}'"))
def check_csc_configuration(
k8s_client,
csc,
name,
namespace,
path,
value
):
csc_response = csc.get(name, namespace)

Expand All @@ -61,12 +72,6 @@ def check_csc_configuration(
)

csc_obj = csc.load(csc_response, name, namespace)
response_value = utils.get_dict_element(csc_obj, path)

assert literal_eval(value) == response_value, (
"Expected value {} for key {} in ConfigMap {} found in namespace {}, "
"got {}".format(value, path, name, namespace, response_value)
)
return dict(csc_obj=csc_obj)


Expand All @@ -88,14 +93,13 @@ def update_service_configuration(
value
):

full_csc = csc.get(name, namespace)
csc_obj = utils.set_dict_element(
csc.load(full_csc, name, namespace), path, literal_eval(value)
csc_from_yaml = yaml.safe_load(
DEFAULT_CSC_CONFIG_YAML.format(replicas=value)
)
patch = {
'data': {
'config.yaml': yaml.safe_dump(
csc_obj, default_flow_style=False
csc_from_yaml, default_flow_style=False
)
}
}
Expand Down

0 comments on commit f5d4ccd

Please sign in to comment.