Skip to content
This repository was archived by the owner on Jun 2, 2021. It is now read-only.

Commit 54e9048

Browse files
matt-royalpiyalibanerjeeJaskanwal Pawar
committed
Choose config schema based on its contents
- Use the kubernetes config schema if the kubernetes.host_url key has a value. Otherwise use the VM config schema. - Most contexts inherit from the Base config schema, and then add additional config options for the VM or Kubernetes configuration. Some contexts only exist in VM deployments, so they don't use Base - Before we were just validating against parent schema and child schema separately; however, splitting the schemas up without merging breaks how we access config objects via `Config.#get` - The `delegate` definitions in each of the child schemas was also incorrect and fixed that - Change the cloud_controller.yml used in test so it represent a VMs deployment, instead of a hybrid of VMs and k8s. - Removed Kpack::Stager logic to use blobstores, since that's no longer supported and it was causing spec failures - Deleted some now unused test fixtures [#175806825] Co-authored-by: Matt Royal <mroyal@vmware.com> Co-authored-by: Piyali Banerjee <pbanerjee@pivotal.io> Co-authored-by: Jaskanwal Pawar <jpawar@pivotal.io>
1 parent 8f44194 commit 54e9048

44 files changed

Lines changed: 1803 additions & 1415 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

config/cloud_controller.yml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -293,16 +293,16 @@ opi:
293293
url: http://opi.service.cf.internal
294294
opi_staging: false
295295

296-
kubernetes:
297-
host_url: https://master.default.svc.cluster-domain.example
298-
service_account:
299-
token_file: "spec/fixtures/service_accounts/k8s.token"
300-
ca_file: "spec/fixtures/certs/kubernetes_ca.crt"
301-
workloads_namespace: 'cf-workloads'
302-
kpack:
303-
builder_namespace: 'cf-workloads-staging'
304-
registry_service_account_name: 'fake-registry-service-account'
305-
registry_tag_base: 'gcr.io/fake-image-repository'
296+
#kubernetes:
297+
# host_url: https://master.default.svc.cluster-domain.example
298+
# service_account:
299+
# token_file: "spec/fixtures/service_accounts/k8s.token"
300+
# ca_file: "spec/fixtures/certs/kubernetes_ca.crt"
301+
# workloads_namespace: 'cf-workloads'
302+
# kpack:
303+
# builder_namespace: 'cf-workloads-staging'
304+
# registry_service_account_name: 'fake-registry-service-account'
305+
# registry_tag_base: 'gcr.io/fake-image-repository'
306306

307307
directories:
308308
tmpdir: /tmp

lib/cloud_controller/config.rb

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@
55
require 'cloud_controller/index_stopper'
66
require 'cloud_controller/backends/instances_reporters'
77
require 'repositories/service_event_repository'
8-
require 'cloud_controller/config_schemas/api_schema'
9-
require 'cloud_controller/config_schemas/clock_schema'
10-
require 'cloud_controller/config_schemas/migrate_schema'
11-
require 'cloud_controller/config_schemas/route_syncer_schema'
12-
require 'cloud_controller/config_schemas/worker_schema'
13-
require 'cloud_controller/config_schemas/deployment_updater_schema'
14-
require 'cloud_controller/config_schemas/rotatate_database_key_schema'
8+
require 'cloud_controller/config_schemas/kubernetes/api_schema'
9+
require 'cloud_controller/config_schemas/vms/api_schema'
10+
require 'cloud_controller/config_schemas/kubernetes/clock_schema'
11+
require 'cloud_controller/config_schemas/vms/clock_schema'
12+
require 'cloud_controller/config_schemas/kubernetes/migrate_schema'
13+
require 'cloud_controller/config_schemas/vms/migrate_schema'
14+
require 'cloud_controller/config_schemas/vms/route_syncer_schema'
15+
require 'cloud_controller/config_schemas/kubernetes/worker_schema'
16+
require 'cloud_controller/config_schemas/vms/worker_schema'
17+
require 'cloud_controller/config_schemas/kubernetes/deployment_updater_schema'
18+
require 'cloud_controller/config_schemas/vms/deployment_updater_schema'
19+
require 'cloud_controller/config_schemas/vms/rotate_database_key_schema'
1520
require 'utils/hash_utils'
1621
require 'cloud_controller/internal_api'
1722

@@ -25,25 +30,41 @@ class KubernetesApiNotConfigured < StandardError
2530

2631
class << self
2732
def load_from_file(file_name, context: :api, secrets_hash: {})
28-
schema_class = schema_class_for_context(context)
29-
config_from_file = schema_class.from_file(file_name, secrets_hash: secrets_hash)
30-
hash = merge_defaults(config_from_file)
33+
config = VCAP::CloudController::YAMLConfig.safe_load_file(file_name)
34+
config = deep_symbolize_keys_except_in_arrays(config)
35+
secrets_hash = deep_symbolize_keys_except_in_arrays(secrets_hash)
36+
config = config.deep_merge(secrets_hash)
37+
38+
schema_class = schema_class_for_context(context, config)
39+
schema_class.validate(config)
40+
41+
hash = merge_defaults(config)
3142
@instance = new(hash, context: context)
3243
end
3344

3445
def config
3546
@instance
3647
end
3748

38-
def schema_class_for_context(context)
39-
const_get("VCAP::CloudController::ConfigSchemas::#{context.to_s.camelize}Schema")
49+
def schema_class_for_context(context, config)
50+
module_name = config.dig(:kubernetes, :host_url).present? ? 'Kubernetes' : 'Vms'
51+
const_get("VCAP::CloudController::ConfigSchemas::#{module_name}::#{context.to_s.camelize}Schema")
4052
end
4153

4254
delegate :kubernetes_api_configured?, to: :config
4355

4456
private
4557

46-
def merge_defaults(config)
58+
def deep_symbolize_keys_except_in_arrays(hash)
59+
return hash unless hash.is_a? Hash
60+
61+
hash.each.with_object({}) do |(k, v), new_hash|
62+
new_hash[k.to_sym] = deep_symbolize_keys_except_in_arrays(v)
63+
end
64+
end
65+
66+
def merge_defaults(orig_config)
67+
config = orig_config.dup
4768
config[:db] ||= {}
4869
ensure_config_has_database_parts(config)
4970
sanitize(config)
@@ -85,7 +106,7 @@ def valid_in_userinfo?(value)
85106

86107
def initialize(config_hash, context: :api)
87108
@config_hash = config_hash
88-
@schema_class = self.class.schema_class_for_context(context)
109+
@schema_class = self.class.schema_class_for_context(context, config_hash)
89110
end
90111

91112
def configure_components
@@ -129,6 +150,8 @@ def valid_config_path?(keys, some_schema)
129150

130151
def kubernetes_api_configured?
131152
get(:kubernetes, :host_url).present?
153+
rescue InvalidConfigPath
154+
false
132155
end
133156

134157
def kubernetes_host_url

0 commit comments

Comments
 (0)