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

Commit 2e946eb

Browse files
matt-royalSannidhi Jalukar
authored andcommitted
Use dedicated methods to fetch k8s config values
- Add methods to VCAP::CloudController::Config - The methods raise an exception if no k8s api is configured [#169924422] Co-authored-by: Matt Royal <mroyal@pivotal.io> Co-authored-by: Sannidhi Jalukar <sjalukar@pivotal.io>
1 parent 98b3ed7 commit 2e946eb

9 files changed

Lines changed: 42 additions & 13 deletions

File tree

app/actions/app_delete.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def delete(apps, record_event: true)
5656
logger.info('Deleting associated kpack image')
5757
kpack_client.delete_image(
5858
app.guid,
59-
VCAP::CloudController::Config.config.get(:kubernetes, :kpack, :builder_namespace)
59+
VCAP::CloudController::Config.config.kpack_builder_namespace
6060
)
6161
end
6262
end

app/fetchers/kpack_buildpack_list_fetcher.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
module VCAP::CloudController
66
class KpackBuildpackListFetcher
77
def fetch_all(message)
8-
staging_namespace = VCAP::CloudController::Config.config.get(:kubernetes, :kpack, :builder_namespace)
8+
staging_namespace = VCAP::CloudController::Config.config.kpack_builder_namespace
99
default_builder = kpack_client.get_custom_builder('cf-default-builder', staging_namespace)
1010

1111
version_map = default_builder.status.builderMetadata.each.with_object({}) do |metadata, h|

app/models/runtime/domain.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def protocols
150150
private
151151

152152
def k8s_enabled?
153-
Config.config.get(:kubernetes, :host_url).present?
153+
Config.config.kubernetes_api_configured?
154154
end
155155

156156
def validate_change_owning_organization(organization)

lib/cloud_controller/backends/stagers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def validate_process(process)
2929

3030
def stager_for_build(build)
3131
if build.kpack_lifecycle?
32-
return Kpack::Stager.new(**@config.get(:kubernetes, :kpack))
32+
return Kpack::Stager.new(**@config.kpack_config)
3333
end
3434

3535
Diego::Stager.new(@config)

lib/cloud_controller/config.rb

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class Config
1818
class InvalidConfigPath < StandardError
1919
end
2020

21+
class KubernetesApiNotConfigured < StandardError
22+
end
23+
2124
class << self
2225
def load_from_file(file_name, context: :api)
2326
schema_class = schema_class_for_context(context)
@@ -34,9 +37,7 @@ def schema_class_for_context(context)
3437
const_get("VCAP::CloudController::ConfigSchemas::#{context.to_s.camelize}Schema")
3538
end
3639

37-
def kubernetes_api_configured?
38-
!!config.get(:kubernetes, :host_url)
39-
end
40+
delegate :kubernetes_api_configured?, to: :config
4041

4142
private
4243

@@ -124,8 +125,36 @@ def valid_config_path?(keys, some_schema)
124125
end
125126
end
126127

128+
def kubernetes_api_configured?
129+
get(:kubernetes, :host_url).present?
130+
end
131+
132+
def kubernetes_config
133+
ensure_k8s_api_configured!
134+
get(:kubernetes)
135+
end
136+
137+
def kpack_config
138+
ensure_k8s_api_configured!
139+
get(:kubernetes, :kpack)
140+
end
141+
142+
def kpack_builder_namespace
143+
ensure_k8s_api_configured!
144+
get(:kubernetes, :kpack, :builder_namespace)
145+
end
146+
147+
def kubernetes_workloads_namespace
148+
ensure_k8s_api_configured!
149+
get(:kubernetes, :workloads_namespace)
150+
end
151+
127152
private
128153

154+
def ensure_k8s_api_configured!
155+
raise KubernetesApiNotConfigured.new('No kubernetes API is configured') unless kubernetes_api_configured?
156+
end
157+
129158
def invalid_config_path!(keys)
130159
raise InvalidConfigPath.new(%("#{keys.join('.')}" is not a valid config key))
131160
end

lib/cloud_controller/dependency_locator.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ def statsd_client
369369
end
370370

371371
def kpack_client
372-
kubernetes_config = VCAP::CloudController::Config.config.get(:kubernetes)
372+
kubernetes_config = VCAP::CloudController::Config.config.kubernetes_config
373373
service_account_token = File.open(kubernetes_config[:service_account][:token_file]).read
374374
ca_crt = File.open(kubernetes_config[:ca_file]).read
375375
build_kube_client = Kubernetes::KubeClientBuilder.build({
@@ -390,7 +390,7 @@ def kpack_client
390390
end
391391

392392
def route_crd_client
393-
kubernetes_config = VCAP::CloudController::Config.config.get(:kubernetes)
393+
kubernetes_config = VCAP::CloudController::Config.config.kubernetes_config
394394
kube_client = Kubernetes::KubeClientBuilder.build({
395395
api_group_url: "#{kubernetes_config[:host_url]}/apis/networking.cloudfoundry.org",
396396
version: 'v1alpha1',

lib/cloud_controller/opi/stager_client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def get_lifecycle(staging_details, staging_guid, action_builder)
117117
end
118118

119119
def staging_completion_callback(staging_details)
120-
if !!config.get(:kubernetes, :host_url)
120+
if config.kubernetes_api_configured?
121121
port = 80
122122
scheme = 'http'
123123
else

lib/kubernetes/route_crd_client.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def create_route(route)
1010
route_crd_hash = {
1111
metadata: {
1212
name: route.guid,
13-
namespace: VCAP::CloudController::Config.config.get(:kubernetes, :workloads_namespace),
13+
namespace: VCAP::CloudController::Config.config.kubernetes_workloads_namespace,
1414
labels: {
1515
'app.kubernetes.io/name' => route.guid,
1616
'app.kubernetes.io/version' => '0.0.0',
@@ -56,7 +56,7 @@ def update_destinations(route)
5656
end
5757

5858
def delete_route(route)
59-
@client.delete_route(route.guid, VCAP::CloudController::Config.config.get(:kubernetes, :workloads_namespace))
59+
@client.delete_route(route.guid, VCAP::CloudController::Config.config.kubernetes_workloads_namespace)
6060
rescue Kubeclient::ResourceNotFoundError
6161
nil
6262
rescue Kubeclient::HttpError => e

spec/unit/actions/app_delete_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ module VCAP::CloudController
4949
end
5050

5151
it 'deletes the associated kpack Image' do
52-
build_namespace = VCAP::CloudController::Config.config.get(:kubernetes, :kpack, :builder_namespace)
52+
build_namespace = VCAP::CloudController::Config.config.kpack_builder_namespace
5353
expect(kpack_client).to receive(:delete_image).with(app.guid, build_namespace)
5454
app_delete.delete(app_dataset)
5555
end

0 commit comments

Comments
 (0)