Add prepare file for exporter

prepare env for exporter

Signed-off-by: DQ <dengq@vmware.com>
This commit is contained in:
DQ 2020-11-17 20:14:11 +08:00
parent dc0047c48c
commit f0db193895
9 changed files with 84 additions and 3 deletions

View File

@ -22,6 +22,7 @@ from utils.redis import prepare_redis
from utils.internal_tls import prepare_tls from utils.internal_tls import prepare_tls
from utils.trivy_adapter import prepare_trivy_adapter from utils.trivy_adapter import prepare_trivy_adapter
from utils.portal import prepare_portal 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, 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) 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_private_key_pem_path=old_private_key_pem_path,
old_crt_path=old_crt_path) old_crt_path=old_crt_path)
if config_dict['metric'].enabled:
prepare_exporter(config_dict)
if with_notary: if with_notary:
prepare_notary(config_dict, nginx_confd_dir, SSL_CERT_PATH, SSL_CERT_KEY_PATH) prepare_notary(config_dict, nginx_confd_dir, SSL_CERT_PATH, SSL_CERT_KEY_PATH)

View File

@ -61,4 +61,5 @@ INTERNAL_NO_PROXY_DN = {
'notary-server', 'notary-server',
'notary-signer', 'notary-signer',
'trivy-adapter', 'trivy-adapter',
'exporter',
} }

View File

@ -4,7 +4,7 @@ from pathlib import Path
from shutil import copytree, rmtree from shutil import copytree, rmtree
from g import internal_tls_dir, DEFAULT_GID, DEFAULT_UID, PG_GID, PG_UID 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: class InternalTLS:
@ -137,3 +137,7 @@ class Metric:
self.enabled = enabled self.enabled = enabled
self.port = port self.port = port
self.path = path self.path = path
def validate(self):
if not port_number_valid(self.port):
raise Exception('Port number in metrics is not valid')

View File

@ -532,6 +532,27 @@ services:
env_file: env_file:
./common/config/chartserver/env ./common/config/chartserver/env
{% endif %} {% 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: networks:
harbor: harbor:
external: false external: false

View File

@ -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}}

View File

@ -216,12 +216,17 @@ http {
upstream registry_metrics { upstream registry_metrics {
server registry:5001; server registry:5001;
} }
upstream harbor_exporter {
server exporter:8080;
}
server { server {
listen 9090; listen 9090;
location = /metrics { location = /metrics {
if ($arg_comp = core) { proxy_pass http://core_metrics; } if ($arg_comp = core) { proxy_pass http://core_metrics; }
if ($arg_comp = registry) { proxy_pass http://registry_metrics; } if ($arg_comp = registry) { proxy_pass http://registry_metrics; }
proxy_pass http://core_metrics; proxy_pass http://harbor_exporter;
} }
} }
{% endif %} {% endif %}

View File

@ -248,12 +248,17 @@ http {
upstream registry_metrics { upstream registry_metrics {
server registry:{{ metric.port }}; server registry:{{ metric.port }};
} }
upstream harbor_exporter {
server exporter:8080;
}
server { server {
listen 9090; listen 9090;
location = {{ metric.path }} { location = {{ metric.path }} {
if ($arg_comp = core) { proxy_pass http://core_metrics; } if ($arg_comp = core) { proxy_pass http://core_metrics; }
if ($arg_comp = registry) { proxy_pass http://registry_metrics; } if ($arg_comp = registry) { proxy_pass http://registry_metrics; }
proxy_pass http://core_metrics; proxy_pass http://harbor_exporter;
} }
} }
{% endif %} {% endif %}

View File

@ -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)

View File

@ -163,3 +163,6 @@ def get_realpath(path: str) -> Path:
if os.path.isdir(host_root_dir): if os.path.isdir(host_root_dir):
return host_root_dir.joinpath(path.lstrip('/')) return host_root_dir.joinpath(path.lstrip('/'))
return Path(path) return Path(path)
def port_number_valid(port:int):
return 0 < port < 65535