Skip to content

Commit 382cbab

Browse files
committed
Add 'file-based-vcap-services' app feature functionality
1 parent 17ad24a commit 382cbab

File tree

8 files changed

+278
-119
lines changed

8 files changed

+278
-119
lines changed

app/models/runtime/process_model.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ def revisions_enabled?
178178
end
179179

180180
delegate :service_binding_k8s_enabled, to: :app
181+
delegate :file_based_vcap_services_enabled, to: :app
181182

182183
def package_hash
183184
# this caches latest_package for performance reasons

app/presenters/system_environment/system_env_presenter.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@
44
class SystemEnvPresenter
55
def initialize(app_or_process)
66
@service_binding_k8s_enabled = app_or_process.service_binding_k8s_enabled
7+
@file_based_vcap_services_enabled = app_or_process.file_based_vcap_services_enabled
78
@service_bindings = app_or_process.service_bindings
89
end
910

1011
def system_env
1112
return { SERVICE_BINDING_ROOT: '/etc/cf-service-bindings' } if @service_binding_k8s_enabled
13+
return { VCAP_SERVICES_FILE_PATH: '/etc/cf-service-bindings/vcap_services' } if @file_based_vcap_services_enabled
1214

15+
vcap_services
16+
end
17+
18+
def vcap_services
1319
{ VCAP_SERVICES: service_binding_env_variables }
1420
end
1521

lib/cloud_controller/diego/service_binding_files_builder.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,24 @@ def self.build(app_or_process)
1010
end
1111

1212
def initialize(app_or_process)
13+
@app_or_process = app_or_process
1314
@service_binding_k8s_enabled = app_or_process.service_binding_k8s_enabled
15+
@file_based_vcap_services = app_or_process.file_based_vcap_services_enabled
1416
@service_bindings = app_or_process.service_bindings
1517
end
1618

1719
def build
20+
if @service_binding_k8s_enabled
21+
build_service_binding_k8s
22+
elsif @file_based_vcap_services
23+
vcap_services = SystemEnvPresenter.new(@app_or_process).vcap_services[:VCAP_SERVICES]
24+
build_vcap_service_file(vcap_services)
25+
end
26+
end
27+
28+
private
29+
30+
def build_service_binding_k8s
1831
return nil unless @service_binding_k8s_enabled
1932

2033
service_binding_files = {}
@@ -45,7 +58,15 @@ def build
4558
service_binding_files.values
4659
end
4760

48-
private
61+
def build_vcap_service_file(vcap_services)
62+
path = 'vcap_services'
63+
vcap_services_string = Oj.dump(vcap_services, mode: :compat)
64+
total_bytesize = vcap_services_string.bytesize + path.bytesize
65+
66+
raise IncompatibleBindings.new("Bindings exceed the maximum allowed bytesize of #{MAX_ALLOWED_BYTESIZE}: #{total_bytesize}") if total_bytesize > MAX_ALLOWED_BYTESIZE
67+
68+
[::Diego::Bbs::Models::File.new(path: path, content: vcap_services_string)]
69+
end
4970

5071
def binding_naming_convention
5172
/^[a-z0-9\-.]{1,253}$/

spec/request/apps_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,6 +1615,20 @@
16151615

16161616
it_behaves_like 'permissions for single object endpoint', ALL_PERMISSIONS
16171617
end
1618+
1619+
context 'when file-based VCAP service bindings are enabled' do
1620+
let(:app_model_response_object) do
1621+
r = super()
1622+
r[:system_env_json] = { VCAP_SERVICES_FILE_PATH: '/etc/cf-service-bindings/vcap_services' }
1623+
r
1624+
end
1625+
1626+
before do
1627+
app_model.update(file_based_vcap_services_enabled: true)
1628+
end
1629+
1630+
it_behaves_like 'permissions for single object endpoint', ALL_PERMISSIONS
1631+
end
16181632
end
16191633

16201634
context 'when VCAP_SERVICES contains potentially sensitive information' do

spec/unit/lib/cloud_controller/diego/app_recipe_builder_spec.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,23 @@ module Diego
6767

6868
it 'includes volume mounted files' do
6969
lrp = builder.build_app_lrp
70-
expect(lrp.volume_mounted_files).not_to be_empty
70+
expect(lrp.volume_mounted_files.size).to be > 1
71+
end
72+
end
73+
end
74+
75+
shared_examples 'file-based VCAP service bindings' do
76+
context 'when file-based VCAP service bindings are enabled' do
77+
before do
78+
app = process.app
79+
app.update(file_based_vcap_services_enabled: true)
80+
VCAP::CloudController::ServiceBinding.make(service_instance: ManagedServiceInstance.make(space: app.space), app: app)
81+
end
82+
83+
it 'includes the vcap_services file' do
84+
lrp = builder.build_app_lrp
85+
expect(lrp.volume_mounted_files.size).to eq(1)
86+
expect(lrp.volume_mounted_files[0].path).to eq('vcap_services')
7187
end
7288
end
7389
end
@@ -933,6 +949,7 @@ module Diego
933949
end
934950

935951
include_examples 'k8s service bindings'
952+
include_examples 'file-based VCAP service bindings'
936953
end
937954

938955
context 'when the lifecycle_type is "cnb"' do
@@ -1026,6 +1043,7 @@ module Diego
10261043
end
10271044

10281045
include_examples 'k8s service bindings'
1046+
include_examples 'file-based VCAP service bindings'
10291047
end
10301048

10311049
context 'when the lifecycle_type is "docker"' do
@@ -1371,6 +1389,7 @@ module Diego
13711389
end
13721390

13731391
include_examples 'k8s service bindings'
1392+
include_examples 'file-based VCAP service bindings'
13741393
end
13751394
end
13761395

0 commit comments

Comments
 (0)