From 0ffa6e076ccd8fcd86169390612a02bd1c726100 Mon Sep 17 00:00:00 2001 From: Yan Date: Thu, 9 Aug 2018 15:27:46 +0800 Subject: [PATCH] Unify redis configuration for harbor components (#5564) this commit is to specrate the redis_url into host,port,pwd and index for different components, and make it possible to set external redis server. --- make/common/templates/jobservice/config.yml | 1 - make/common/templates/registry/config.yml | 5 +- make/common/templates/registry/config_ha.yml | 5 +- make/common/templates/ui/env | 2 +- make/harbor.cfg | 20 ++++- make/prepare | 77 ++++++++++++-------- 6 files changed, 71 insertions(+), 39 deletions(-) diff --git a/make/common/templates/jobservice/config.yml b/make/common/templates/jobservice/config.yml index 65d3b9541..70f279440 100644 --- a/make/common/templates/jobservice/config.yml +++ b/make/common/templates/jobservice/config.yml @@ -18,7 +18,6 @@ worker_pool: #Additional config if use 'redis' backend redis_pool: #redis://[arbitrary_username:password@]ipaddress:port/database_index - #or ipaddress:port[,weight,password,database_index] redis_url: $redis_url namespace: "harbor_job_service_namespace" #Logger for job diff --git a/make/common/templates/registry/config.yml b/make/common/templates/registry/config.yml index 92a4834cc..6190baf78 100644 --- a/make/common/templates/registry/config.yml +++ b/make/common/templates/registry/config.yml @@ -13,8 +13,9 @@ storage: delete: enabled: true redis: - addr: $redis_url - db: 1 + addr: $redis_host:$redis_port + password: $redis_password + db: $redis_db_index_reg http: addr: :5000 secret: placeholder diff --git a/make/common/templates/registry/config_ha.yml b/make/common/templates/registry/config_ha.yml index 2f6d76d55..39f50bd7e 100644 --- a/make/common/templates/registry/config_ha.yml +++ b/make/common/templates/registry/config_ha.yml @@ -13,8 +13,9 @@ storage: delete: enabled: true redis: - addr: $redis_url - db: 1 + addr: $redis_host:$redis_port + password: $redis_password + db: $redis_db_index_reg http: addr: :5000 secret: placeholder diff --git a/make/common/templates/ui/env b/make/common/templates/ui/env index 7ec4ad559..e4d44bffd 100644 --- a/make/common/templates/ui/env +++ b/make/common/templates/ui/env @@ -5,6 +5,6 @@ JOBSERVICE_SECRET=$jobservice_secret GODEBUG=netdns=cgo ADMINSERVER_URL=$adminserver_url UAA_CA_ROOT=/etc/ui/certificates/uaa_ca.pem -_REDIS_URL=$redis_url +_REDIS_URL=$redis_host:$redis_port,100,$redis_password SYNC_REGISTRY=false CHART_CACHE_DRIVER=$chart_cache_driver diff --git a/make/harbor.cfg b/make/harbor.cfg index 9fac5f3b8..b22596f47 100644 --- a/make/harbor.cfg +++ b/make/harbor.cfg @@ -140,9 +140,23 @@ db_user = postgres ##### End of Harbor DB configuration####### -#The redis server address. Only needed in HA installation. -#address:port[,weight,password,db_index] -redis_url = redis:6379 +##########Redis server configuration.############ + +#Redis connection address +redis_host = redis + +#Redis connection port +redis_port = 6379 + +#Redis connection password +redis_password = + +#Redis connection db index +#db_index 1,2,3 is for registry, jobservice and chartmuseum. +#db_index 0 is for UI, it's unchangeable +redis_db_index = 1,2,3 + +##########Redis server configuration.############ ##########Clair DB configuration############ diff --git a/make/prepare b/make/prepare index c24688f15..1b8e7fe44 100755 --- a/make/prepare +++ b/make/prepare @@ -21,7 +21,8 @@ if sys.version_info[:3][0] == 3: DATA_VOL = "/data" -def validate(conf, args): +def validate(conf, args): + if args.ha_mode: db_host = rcp.get("configuration", "db_host") if db_host == "mysql": @@ -32,9 +33,6 @@ def validate(conf, args): msg = 'Is the Harbor Docker Registry configured to use shared storage (e.g. NFS, Ceph etc.)? [yes/no]:' if raw_input(msg).lower() != "yes": raise Exception("Error: In HA mode, shared storage configuration for Docker Registry in harbor.cfg is required. Refer to HA installation guide for details.") - redis_url = rcp.get("configuration", "redis_url") - if redis_url is None or len(redis_url) < 1: - raise Exception("Error: In HA mode, redis_url in harbor.cfg needs to point to a Redis cluster.") if args.notary_mode: raise Exception("Error: HA mode doesn't support Notary currently") if args.clair_mode: @@ -80,6 +78,18 @@ def validate(conf, args): if storage_provider_config == "": raise Exception("Error: no provider configurations are provided for provider %s" % storage_provider_name) + redis_host = rcp.get("configuration", "redis_host") + if redis_host is None or len(redis_host) < 1: + raise Exception("Error: redis_host in harbor.cfg needs to point to an endpoint of Redis server or cluster.") + + redis_port = rcp.get("configuration", "redis_port") + if len(redis_port) < 1: + raise Exception("Error: redis_port in harbor.cfg needs to point to the port of Redis server or cluster.") + + redis_db_index = rcp.get("configuration", "redis_db_index").strip() + if len(redis_db_index.split(",")) != 3: + raise Exception("Error invalid value for redis_db_index: %s. please set it as 1,2,3" % redis_db_index) + #To meet security requirement #By default it will change file mode to 0600, and make the owner of the file to 10000:10000 def mark_file(path, mode=0o600, uid=10000, gid=10000): @@ -282,10 +292,22 @@ secret_key = get_secret_key(secretkey_path) log_rotate_count = rcp.get("configuration", "log_rotate_count") log_rotate_size = rcp.get("configuration", "log_rotate_size") -if rcp.has_option("configuration", "redis_url"): - redis_url = rcp.get("configuration", "redis_url") +redis_host = rcp.get("configuration", "redis_host") +redis_port = rcp.get("configuration", "redis_port") +redis_password = rcp.get("configuration", "redis_password") +redis_db_index = rcp.get("configuration", "redis_db_index") + +db_indexs = redis_db_index.split(',') +redis_db_index_reg = db_indexs[0] +redis_db_index_js = db_indexs[1] +redis_db_index_chart = db_indexs[2] + +#redis://[arbitrary_username:password@]ipaddress:port/database_index +redis_url_js = '' +if len(redis_password) > 0: + redis_url_js = "redis://anonymous:%s@%s:%s/%s" % (redis_password, redis_host, redis_port, redis_db_index_js) else: - redis_url = "" + redis_url_js = "redis://%s:%s/%s" % (redis_host, redis_port, redis_db_index_js) if rcp.has_option("configuration", "skip_reload_env_pattern"): skip_reload_env_pattern = rcp.get("configuration", "skip_reload_env_pattern") @@ -430,14 +452,16 @@ render(os.path.join(templates_dir, "adminserver", "env"), # set cache for chart repo server # default set 'memory' mode, if redis is configured then set to 'redis' chart_cache_driver = "memory" -if len(redis_url) > 0: +if len(redis_host) > 0: chart_cache_driver = "redis" render(os.path.join(templates_dir, "ui", "env"), ui_conf_env, ui_secret=ui_secret, jobservice_secret=jobservice_secret, - redis_url = redis_url, + redis_host=redis_host, + redis_port=redis_port, + redis_password=redis_password, adminserver_url = adminserver_url, chart_cache_driver = chart_cache_driver ) @@ -459,14 +483,20 @@ render(os.path.join(templates_dir, "registry", registry_config_file_ha), storage_provider_info=storage_provider_info, public_url=public_url, ui_url=ui_url, - redis_url=redis_url) + redis_host=redis_host, + redis_port=redis_port, + redis_password=redis_password, + redis_db_index_reg=redis_db_index_reg) render(os.path.join(templates_dir, "registry", registry_config_file), registry_conf, storage_provider_info=storage_provider_info, public_url=public_url, ui_url=ui_url, - redis_url=redis_url) + redis_host=redis_host, + redis_port=redis_port, + redis_password=redis_password, + redis_db_index_reg=redis_db_index_reg) render(os.path.join(templates_dir, "db", "env"), db_conf_env, @@ -481,8 +511,8 @@ render(os.path.join(templates_dir, "jobservice", "env"), render(os.path.join(templates_dir, "jobservice", "config.yml"), jobservice_conf, max_job_workers=max_job_workers, - redis_url=redis_url) - + redis_url=redis_url_js) + render(os.path.join(templates_dir, "log", "logrotate.conf"), log_rotate_config, log_rotate_count=log_rotate_count, @@ -669,23 +699,10 @@ if args.chart_mode: os.makedirs(chartm_config_dir) # process redis info - cache_store = "" - cache_redis_password = "" - cache_redis_addr = "" - cache_redis_db_index = 0 - if redis_url and redis_url.strip(): - cache_store = "redis" - segments = redis_url.split(',', 3) - for index, r_cfg in enumerate(segments): - # the addr:port - if index == 0: - cache_redis_addr = r_cfg - # the password if existing - elif index == 2: - cache_redis_password = r_cfg - # the database index if existing - elif index == 3: - cache_redis_db_index = r_cfg + cache_store = "redis" + cache_redis_password = redis_password + cache_redis_addr = redis_host+":"+redis_port + cache_redis_db_index = redis_db_index_chart # process storage info #default using local file system