Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
bonzofenix committed Aug 21, 2024
1 parent ef8ebd0 commit 8ffdf1e
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 50 deletions.
4 changes: 3 additions & 1 deletion src/autoscaler/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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:
Expand Down
26 changes: 6 additions & 20 deletions src/autoscaler/metricsforwarder/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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/.
Expand All @@ -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))
Expand Down
52 changes: 52 additions & 0 deletions src/autoscaler/metricsforwarder/config.sh
Original file line number Diff line number Diff line change
@@ -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
}
}
}'
13 changes: 9 additions & 4 deletions src/autoscaler/metricsforwarder/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}

Expand Down
35 changes: 11 additions & 24 deletions src/autoscaler/metricsforwarder/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
})

Expand All @@ -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",
Expand Down
8 changes: 7 additions & 1 deletion src/autoscaler/mta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8ffdf1e

Please sign in to comment.