diff --git a/make/photon/prepare/commands/prepare.py b/make/photon/prepare/commands/prepare.py index 66ebd8dfc..6c464a186 100644 --- a/make/photon/prepare/commands/prepare.py +++ b/make/photon/prepare/commands/prepare.py @@ -22,6 +22,7 @@ from utils.redis import prepare_redis from utils.internal_tls import prepare_tls from utils.trivy_adapter import prepare_trivy_adapter from utils.portal import prepare_portal +from utils.exporter import prepare_exporter from g import (config_dir, input_config_path, private_key_pem_path, root_crt_path, secret_key_dir, old_private_key_pem_path, old_crt_path) @@ -62,6 +63,9 @@ def prepare(conf, with_notary, with_trivy, with_chartmuseum): old_private_key_pem_path=old_private_key_pem_path, old_crt_path=old_crt_path) + if config_dict['metric'].enabled: + prepare_exporter(config_dict) + if with_notary: prepare_notary(config_dict, nginx_confd_dir, SSL_CERT_PATH, SSL_CERT_KEY_PATH) diff --git a/make/photon/prepare/g.py b/make/photon/prepare/g.py index 66a116831..45234c157 100644 --- a/make/photon/prepare/g.py +++ b/make/photon/prepare/g.py @@ -61,4 +61,5 @@ INTERNAL_NO_PROXY_DN = { 'notary-server', 'notary-signer', 'trivy-adapter', + 'exporter', } diff --git a/make/photon/prepare/models.py b/make/photon/prepare/models.py index 6ab26adf9..c72fb9b17 100644 --- a/make/photon/prepare/models.py +++ b/make/photon/prepare/models.py @@ -4,7 +4,7 @@ from pathlib import Path from shutil import copytree, rmtree from g import internal_tls_dir, DEFAULT_GID, DEFAULT_UID, PG_GID, PG_UID -from utils.misc import check_permission, owner_can_read, get_realpath +from utils.misc import check_permission, owner_can_read, get_realpath, port_number_valid class InternalTLS: @@ -137,3 +137,7 @@ class Metric: self.enabled = enabled self.port = port self.path = path + + def validate(self): + if not port_number_valid(self.port): + raise Exception('Port number in metrics is not valid') \ No newline at end of file diff --git a/make/photon/prepare/templates/docker_compose/docker-compose.yml.jinja b/make/photon/prepare/templates/docker_compose/docker-compose.yml.jinja index 1969b3d73..d0db51ddf 100644 --- a/make/photon/prepare/templates/docker_compose/docker-compose.yml.jinja +++ b/make/photon/prepare/templates/docker_compose/docker-compose.yml.jinja @@ -532,6 +532,27 @@ services: env_file: ./common/config/chartserver/env {% endif %} +{% if metric.enabled %} + exporter: + image: goharbor/harbor-exporter:{{version}} + container_name: harbor-exporter + env_file: + - ./common/config/exporter/env + restart: always + networks: + - harbor + dns_search: . + depends_on: + - core + {% if external_database == False %} + - postgresql + {% endif %} + logging: + driver: "syslog" + options: + syslog-address: "tcp://127.0.0.1:1514" + tag: "exporter" +{% endif %} networks: harbor: external: false diff --git a/make/photon/prepare/templates/exporter/env.jinja b/make/photon/prepare/templates/exporter/env.jinja new file mode 100644 index 000000000..69aa95ee6 --- /dev/null +++ b/make/photon/prepare/templates/exporter/env.jinja @@ -0,0 +1,22 @@ +HARBOR_EXPORTER_PORT=8080 +HARBOR_EXPORTER_METRICS_PATH=/metrics +HARBOR_EXPORTER_METRICS_ENABLED=true +HARBOR_EXPORTER_MAX_REQUESTS=30 +HARBOR_METRIC_NAMESPACE=harbor +HARBOR_METRIC_SUBSYSTEM=exporter +HARBOR_SERVICE_HOST=core +{%if internal_tls.enabled %} +HARBOR_SERVICE_PORT=8443 +HARBOR_SERVICE_SCHEME=https +{% else %} +HARBOR_SERVICE_PORT=8080 +HARBOR_SERVICE_SCHEME=http +{% endif %} +HARBOR_DATABASE_HOST={{harbor_db_host}} +HARBOR_DATABASE_PORT={{harbor_db_port}} +HARBOR_DATABASE_USERNAME={{harbor_db_username}} +HARBOR_DATABASE_PASSWORD={{harbor_db_password}} +HARBOR_DATABASE_DBNAME={{harbor_db_name}} +HARBOR_DATABASE_SSLMODE={{harbor_db_sslmode}} +HARBOR_DATABASE_MAX_IDLE_CONNS={{harbor_db_max_idle_conns}} +HARBOR_DATABASE_MAX_OPEN_CONNS={{harbor_db_max_open_conns}} \ No newline at end of file diff --git a/make/photon/prepare/templates/nginx/nginx.http.conf.jinja b/make/photon/prepare/templates/nginx/nginx.http.conf.jinja index de2ff12c0..da601eea3 100644 --- a/make/photon/prepare/templates/nginx/nginx.http.conf.jinja +++ b/make/photon/prepare/templates/nginx/nginx.http.conf.jinja @@ -216,12 +216,17 @@ http { upstream registry_metrics { server registry:5001; } + + upstream harbor_exporter { + server exporter:8080; + } + server { listen 9090; location = /metrics { if ($arg_comp = core) { proxy_pass http://core_metrics; } if ($arg_comp = registry) { proxy_pass http://registry_metrics; } - proxy_pass http://core_metrics; + proxy_pass http://harbor_exporter; } } {% endif %} diff --git a/make/photon/prepare/templates/nginx/nginx.https.conf.jinja b/make/photon/prepare/templates/nginx/nginx.https.conf.jinja index dafbbd68b..cc162e0b8 100644 --- a/make/photon/prepare/templates/nginx/nginx.https.conf.jinja +++ b/make/photon/prepare/templates/nginx/nginx.https.conf.jinja @@ -248,12 +248,17 @@ http { upstream registry_metrics { server registry:{{ metric.port }}; } + + upstream harbor_exporter { + server exporter:8080; + } + server { listen 9090; location = {{ metric.path }} { if ($arg_comp = core) { proxy_pass http://core_metrics; } if ($arg_comp = registry) { proxy_pass http://registry_metrics; } - proxy_pass http://core_metrics; + proxy_pass http://harbor_exporter; } } {% endif %} diff --git a/make/photon/prepare/utils/exporter.py b/make/photon/prepare/utils/exporter.py new file mode 100644 index 000000000..2b16234ac --- /dev/null +++ b/make/photon/prepare/utils/exporter.py @@ -0,0 +1,16 @@ +import os +from g import config_dir, templates_dir, DEFAULT_GID, DEFAULT_UID +from utils.jinja import render_jinja +from utils.misc import prepare_dir + +EXPORTER_CONFIG_DIR = os.path.join(config_dir, "exporter") +EXPORTER_CONF_ENV = os.path.join(config_dir, "exporter", "env") +EXPORTER_ENV_TEMPLATE_PATH = os.path.join(templates_dir, "exporter", "env.jinja") + +def prepare_exporter(config_dict): + prepare_dir(EXPORTER_CONFIG_DIR, uid=DEFAULT_UID, gid=DEFAULT_GID) + + render_jinja( + EXPORTER_ENV_TEMPLATE_PATH, + EXPORTER_CONF_ENV, + **config_dict) diff --git a/make/photon/prepare/utils/misc.py b/make/photon/prepare/utils/misc.py index c40ae9327..f5082d0e7 100644 --- a/make/photon/prepare/utils/misc.py +++ b/make/photon/prepare/utils/misc.py @@ -163,3 +163,6 @@ def get_realpath(path: str) -> Path: if os.path.isdir(host_root_dir): return host_root_dir.joinpath(path.lstrip('/')) return Path(path) + +def port_number_valid(port:int): + return 0 < port < 65535