github.com/abayer/test-infra@v0.0.5/velodrome/config.py (about) 1 #!/usr/bin/env python 2 3 # Copyright 2017 The Kubernetes Authors. 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 import os 18 import string 19 import sys 20 21 import yaml 22 23 CONFIG = "config.yaml" 24 25 DEPLOYMENTS = { 26 "fetcher": "fetcher/deployment.yaml", 27 "transform": "transform/deployment.yaml", 28 "influxdb": "grafana-stack/influxdb.yaml", 29 "prometheus": "grafana-stack/prometheus.yaml", 30 "prometheus-config": "grafana-stack/prometheus-config.yaml", 31 "prober": "prober/blackbox.yaml", 32 "grafana": "grafana-stack/grafana.yaml", 33 "grafana-config": "grafana-stack/grafana-config.yaml", 34 "nginx": "grafana-stack/nginx.yaml", 35 "sqlproxy": "mysql/sqlproxy.yaml", 36 } 37 38 39 def main(): 40 if len(sys.argv) != 1: 41 print >> sys.stderr, "Too many arguments." 42 sys.exit(128) 43 44 with open(get_absolute_path(CONFIG)) as config_file: 45 config = yaml.load(config_file) 46 print_deployments(["sqlproxy", "prober"], {}) 47 for project_name, project in config['projects'].items(): 48 public_ip = project.get('nginx', {}).get('public-ip', '') or '' 49 print_deployments(["influxdb", "grafana", "nginx"], { 50 "PROJECT": project_name, 51 "NGINX_PUBLIC_IP": public_ip, 52 }) 53 if 'prometheus' in project: 54 print_deployments(["prometheus"], { 55 "PROJECT": project_name, 56 }) 57 patch_configuration("prometheus-config", 58 project['prometheus'], 59 {"PROJECT": project_name}) 60 if 'grafana' in project: 61 patch_configuration("grafana-config", 62 project['grafana'], 63 {"PROJECT": project_name}) 64 for repository, transforms in project['repositories'].items(): 65 print_deployments(["fetcher"], { 66 "GH_ORGANIZATION": repository.split("/")[0], 67 "GH_REPOSITORY": repository.split("/")[1], 68 "PROJECT": project_name, 69 }) 70 for metric, transformer in (transforms or {}).items(): 71 plugin = metric 72 if "plugin" in transformer: 73 plugin = transformer["plugin"] 74 args = [] 75 if "args" in transformer: 76 args = transformer["args"] 77 apply_transform(args, { 78 "GH_ORGANIZATION": repository.split("/")[0], 79 "GH_REPOSITORY": repository.split("/")[1], 80 "PROJECT": project_name, 81 "TRANSFORM_PLUGIN": plugin, 82 "TRANSFORM_METRIC": metric, 83 }) 84 85 86 def apply_transform(new_args, env): 87 with open(get_absolute_path(DEPLOYMENTS["transform"])) as fp: 88 config = yaml.load(fp) 89 config['spec']['template']['spec']['containers'][0]['args'] += new_args 90 print_deployment(yaml.dump(config, default_flow_style=False), env) 91 92 93 def patch_configuration(component, values, env): 94 with open(get_absolute_path(DEPLOYMENTS[component])) as fp: 95 config = yaml.load(fp) 96 # We want to fail if we have unknown keys in values 97 unknown_keys = set(values) - set(config['data']) 98 if unknown_keys: 99 raise ValueError("Unknown keys in config:", unknown_keys) 100 config['data'] = values 101 print_deployment(yaml.dump(config, default_flow_style=False), env) 102 103 104 def get_absolute_path(path): 105 return os.path.join(os.path.dirname(__file__), path) 106 107 108 def print_deployments(components, env): 109 for component in components: 110 with open(get_absolute_path(DEPLOYMENTS[component])) as fp: 111 print_deployment(fp.read(), env) 112 113 114 def print_deployment(deployment, env): 115 print string.Template(deployment).safe_substitute(**env), 116 print '---' 117 118 if __name__ == '__main__': 119 main()