From 8ffdf1ecc838522459082b3a9f8de1628bd0e716 Mon Sep 17 00:00:00 2001 From: Alan Moran Date: Wed, 21 Aug 2024 11:49:20 +0200 Subject: [PATCH] WIP --- src/autoscaler/Makefile | 4 +- src/autoscaler/metricsforwarder/Makefile | 26 +++------- src/autoscaler/metricsforwarder/config.sh | 52 +++++++++++++++++++ .../metricsforwarder/config/config.go | 13 +++-- .../metricsforwarder/config/config_test.go | 35 ++++--------- src/autoscaler/mta.yaml | 8 ++- 6 files changed, 88 insertions(+), 50 deletions(-) create mode 100755 src/autoscaler/metricsforwarder/config.sh diff --git a/src/autoscaler/Makefile b/src/autoscaler/Makefile index b29f66ddab..3e37b453a9 100644 --- a/src/autoscaler/Makefile +++ b/src/autoscaler/Makefile @@ -155,6 +155,8 @@ clean: mta-deploy: mta-build build-extension-file $(MAKE) -f metricsforwarder/Makefile set-security-group $(MAKE) -f metricsforwarder/Makefile stop-metricsforwarder-vm + $(MAKE) -f metricsforwarder/Makefile create-config-user-provided-service + @echo "Deploying with extension file: $(EXTENSION_FILE)" @cf deploy mta_archives/*.mtar -f -e $(EXTENSION_FILE) @@ -170,7 +172,7 @@ mta-logs: .PHONY: mta-build mta-build: mta-build-clean cf-build - $(MAKE) -f metricsforwarder/Makefile fetch-config + $(MAKE) -f metricsforwarder/Makefile fetch-certs mbt build mta-build-clean: diff --git a/src/autoscaler/metricsforwarder/Makefile b/src/autoscaler/metricsforwarder/Makefile index 5b69047ef2..da9cb53723 100644 --- a/src/autoscaler/metricsforwarder/Makefile +++ b/src/autoscaler/metricsforwarder/Makefile @@ -2,23 +2,17 @@ PR_NUMBER ?= $(shell gh pr view --json number --jq '.number') DEPLOYMENT_NAME ?= autoscaler-$(PR_NUMBER) SYSTEM_DOMAIN ?=autoscaler.app-runtime-interfaces.ci.cloudfoundry.org METIRCSFORWARDER_VM := $(shell bosh -d $(DEPLOYMENT_NAME) vms --json | jq '.Tables | .[] | .Rows | .[] | select(.instance|test("metricsforwarder")) | .instance') -POSTGRES_ADDRESS := $(DEPLOYMENT_NAME)-postgres.tcp.$(SYSTEM_DOMAIN) -LOG_CACHE_IP := $(shell bosh -d cf vms --json | jq -r '.Tables | .[] | .Rows | .[] | select(.instance|test("log-cache")) | .ips' ) MAKEFILE_DIR := $(dir $(lastword $(MAKEFILE_LIST))) -POSTGRES_EXTERNAL_PORT := $(or $(PR_NUMBER),5432) +.PHONY: create-config-user-provided-service +create-config-user-provided-service: + cf cups config -p '$(shell ${MAKEFILE_DIR}/config.sh)' || echo "config service already exists" + cf uups config -p '$(shell ${MAKEFILE_DIR}/config.sh)' -.PHONY: fetch-config -fetch-config: start-metricsforwarder-vm - # how to define variables in deployment name +.PHONY: fetch-certs +fetch-certs: start-metricsforwarder-vm mkdir -p build/assets/certs/policy_db build/assets/certs/storedprocedure_db build/assets/certs/syslog_client - echo "POSTGRES ADDRESS: $(POSTGRES_ADDRESS)" - echo "LOG_CACHE IP: $(LOG_CACHE_IP)" - - @echo "Pulling metricforwarder config from $(METIRCSFORWARDER_VM)..." - bosh -d $(DEPLOYMENT_NAME) scp $(METIRCSFORWARDER_VM):/var/vcap/jobs/metricsforwarder/config/metricsforwarder.yml build/assets/metricsforwarder.yml - @echo "Pulling policy db certs from $(METIRCSFORWARDER_VM)..." bosh -d $(DEPLOYMENT_NAME) scp $(METIRCSFORWARDER_VM):/var/vcap/jobs/metricsforwarder/config/certs/policy_db/ca.crt build/assets/certs/policy_db/. bosh -d $(DEPLOYMENT_NAME) scp $(METIRCSFORWARDER_VM):/var/vcap/jobs/metricsforwarder/config/certs/policy_db/crt build/assets/certs/policy_db/. @@ -34,14 +28,6 @@ fetch-config: start-metricsforwarder-vm bosh -d $(DEPLOYMENT_NAME) scp $(METIRCSFORWARDER_VM):/var/vcap/jobs/metricsforwarder/config/certs/syslog_client/client.crt build/assets/certs/syslog_client/. bosh -d $(DEPLOYMENT_NAME) scp $(METIRCSFORWARDER_VM):/var/vcap/jobs/metricsforwarder/config/certs/syslog_client/client.key build/assets/certs/syslog_client/. - @echo "Build metricsforwarder config yaml" - cp build/assets/metricsforwarder.yml build/metricsforwarder.yml - - sed -i'' -e 's|\/var\/vcap\/jobs\/metricsforwarder\/config|\/home\/vcap\/app/assets|g' build/metricsforwarder.yml - sed -i'' -e 's|$(DEPLOYMENT_NAME).autoscalerpostgres.service.cf.internal:5432|$(POSTGRES_ADDRESS):$(POSTGRES_EXTERNAL_PORT)|g' build/metricsforwarder.yml - - - PHONY: set-security-group set-security-group: $(eval ORG := $(shell cf target |grep "org\:" |cut -d':' -f2 | xargs)) diff --git a/src/autoscaler/metricsforwarder/config.sh b/src/autoscaler/metricsforwarder/config.sh new file mode 100755 index 0000000000..5fea7296ef --- /dev/null +++ b/src/autoscaler/metricsforwarder/config.sh @@ -0,0 +1,52 @@ +#!/usr/bin/env bash + +export PR_NUMBER="$(gh pr view --json number --jq '.number')" +export DEPLOYMENT_NAME="autoscaler-${PR_NUMBER}" +export SYSTEM_DOMAIN="autoscaler.app-runtime-interfaces.ci.cloudfoundry.org" +export POSTGRES_ADDRESS="${DEPLOYMENT_NAME}-postgres.tcp.${SYSTEM_DOMAIN}" +export POLICY_DB_PASSWORD="$(credhub get -n /bosh-autoscaler/${DEPLOYMENT_NAME}/database_password --quiet)" +export STOREPROCEDURE_DB_PASSWORD="$(credhub get -n /bosh-autoscaler/${DEPLOYMENT_NAME}/database_password --quiet)" +export METRICSFORWARDER_HEALTH_PASSWORD="$(credhub get -n /bosh-autoscaler/${DEPLOYMENT_NAME}/autoscaler_metricsforwarder_health_password --quiet)" +export POSTGRES_EXTERNAL_PORT="${PR_NUMBER:-5432}" + +echo '{ "metricsforwarder": { + "cache_cleanup_interval": "6h", + "cache_ttl": "900s", + "cred_helper_impl": "default", + "health": { + "password": "'"${METRICSFORWARDER_HEALTH_PASSWORD}"'", + "username": "metricsforwarder" + }, + "logging": { + "level": "debug" + }, + "syslog": { + "server_address": "log-cache.service.cf.internal", + "port": 6067, + "tls": { + "ca_file": "/home/vcap/app/assets/certs/syslog_client/ca.crt", + "cert_file": "/home/vcap/app/assets/certs/syslog_client/client.crt", + "key_file": "/home/vcap/app/assets/certs/syslog_client/client.key" + } + }, + "db": { + "policy_db": { + "url": "postgres://postgres:'"${POLICY_DB_PASSWORD}@${POSTGRES_ADDRESS}:${POSTGRES_EXTERNAL_PORT}"'/autoscaler?application_name=metricsforwarder&sslmode=verify-full&sslrootcert=/home/vcap/app/assets/certs/policy_db/ca.crt&sslcert=/home/vcap/app/assets/certs/policy_db/crt&sslkey=/home/vcap/app/assets/certs/policy_db/key", + "max_open_connections": 100, + "max_idle_connections": 10, + "connection_max_lifetime": "60s" + }, + "storedprocedure_db": { + "url": "postgres://postgres:'"${STOREPROCEDURE_DB_PASSWORD}@${POSTGRES_ADDRESS}:${POSTGRES_EXTERNAL_PORT}"'/autoscaler?application_name=metricsforwarder&sslmode=verify-full&sslrootcert=/home/vcap/app/assets/certs/storedprocedure_db/ca.crt&sslcert=/home/vcap/app/assets/certs/storedprocedure_db/crt&sslkey=/home/vcap/app/assets/certs/storedprocedure_db/key", + "max_open_connections": 20, + "max_idle_connections": 10, + "connection_max_lifetime": "60s" + } + }, + "policy_poller_interval": "60s", + "rate_limit": { + "valid_duration": "1s", + "max_amount": 10 + } + } +}' diff --git a/src/autoscaler/metricsforwarder/config/config.go b/src/autoscaler/metricsforwarder/config/config.go index 127ef173a0..8dd16daff5 100644 --- a/src/autoscaler/metricsforwarder/config/config.go +++ b/src/autoscaler/metricsforwarder/config/config.go @@ -25,6 +25,7 @@ var ErrReadYaml = errors.New("failed to read config file") var ErrReadJson = errors.New("failed to read vcap_services json") var ErrReadEnvironment = errors.New("failed to read environment variables") var ErrReadVCAPEnvironment = errors.New("failed to read VCAP environment variables") +var ErrMetricsforwarderConfigNotFound = errors.New("Configuration error: metricsforwarder config service not found") const ( DefaultMetronAddress = "127.0.0.1:3458" @@ -102,6 +103,10 @@ func decodeYamlFile(filepath string, c *Config) error { func readConfigFromVCAP(appEnv *cfenv.App, c *Config) error { configVcapService, err := appEnv.Services.WithName("config") + if err != nil { + return fmt.Errorf("%w: %w", ErrMetricsforwarderConfigNotFound, err) + } + data := configVcapService.Credentials["metricsforwarder"] rawJSON, err := json.Marshal(data) @@ -152,14 +157,14 @@ func LoadConfig(filepath string) (*Config, error) { conf.Server.Port = appEnv.Port - err = readDbFromVCAP(appEnv, &conf) + err = readConfigFromVCAP(appEnv, &conf) if err != nil { - return &conf, err + return nil, err } - err = readConfigFromVCAP(appEnv, &conf) + err = readDbFromVCAP(appEnv, &conf) if err != nil { - return nil, err + return &conf, err } } diff --git a/src/autoscaler/metricsforwarder/config/config_test.go b/src/autoscaler/metricsforwarder/config/config_test.go index 06a445fba4..eec0ce5b53 100644 --- a/src/autoscaler/metricsforwarder/config/config_test.go +++ b/src/autoscaler/metricsforwarder/config/config_test.go @@ -53,7 +53,7 @@ var _ = Describe("Config", func() { When("PORT env variable is set to a number ", func() { BeforeEach(func() { - vcapServicesJson = "{}" + vcapServicesJson = `{ "user-provided": [ { "name": "config" } ] }` port = "3333" }) @@ -62,31 +62,18 @@ var _ = Describe("Config", func() { }) }) + When("VCAP_SERVICES is empty", func() { + BeforeEach(func() { + vcapServicesJson = "{}" + }) + + It("should error with config service not found", func() { + Expect(err).To(MatchError(MatchRegexp("Configuration error: metricsforwarder config service not found"))) + }) + }) + When("VCAP_SERVICES has service config", func() { BeforeEach(func() { - // VCAP_SERVICES={"user-provided":[ - //{"label":"user-provided", - // "name":"config", - // "tags":[], - // "instance_guid":"444c838e-17d9-429d-a1ea-660904db9841", - // "instance_name":"config", - // "binding_guid":"2cb523a1-773a-4fa4-ba05-3a76cc488ff7", - // "binding_name":null, - // "credentials":{ - // "db":null, - // "logging":{"level":"info"}, - // "policy_poller_interval":"60s", - // "rate_limit":{"max_amount":10,"valid_duration":"1s"}, - // "syslog":{ - // "port":6067, - // "server_address":"log-cache.service.cf.internal", - // "tls":{"ca_file":"/var/vcap/jobs/metricsforwarder/config/certs/syslog_client/ca.crt","cert_file":"/var/vcap/jobs/metricsforwarder/config/certs/syslog_client/client.crt","key_file":"/var/vcap/jobs/metricsforwarder/config/certs/syslog_client/client.key"} - // }, - // } - //}, - // "syslog_drain_url":null, - // "volume_mounts":[]}]} - // vcapServicesJson = `{ "user-provided": [ { "label":"user-provided", diff --git a/src/autoscaler/mta.yaml b/src/autoscaler/mta.yaml index eff830b6c9..a66e7a5904 100644 --- a/src/autoscaler/mta.yaml +++ b/src/autoscaler/mta.yaml @@ -9,10 +9,16 @@ modules: - name: metricsforwarder type: binary path: build + requires: + - name: config parameters: memory: 1G disk-quota: 1G instances: 1 stack: cflinuxfs4 - command: ./metricsforwarder -c metricsforwarder.yml + command: ./metricsforwarder routes: + +resources: +- name: config + type: org.cloudfoundry.existing-service