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

Commit 53724a6

Browse files
Create a droplet in the kpack stager
- Previously for kpack apps, the droplet was created when the build controller would send a patch request to CAPI after a successful OCI Image build. We wanted to create the droplet when the Image CR is created so that the Image CR would have a droplet guid label that could be used by the image controller to know which droplet to update after a stack update. - Removed droplet creation from the BuildUpdate action - Non-kpack lifecycle app staging workflows should not have been changed or impacted by this change. [172542372] Co-authored-by: Piyali Banerjee <pbanerjee@pivotal.io> Co-authored-by: Nat Bennett <nbennett@pivotal.io>
1 parent 53892f5 commit 53724a6

6 files changed

Lines changed: 24 additions & 8 deletions

File tree

app/actions/build_update.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def update(build, message)
1515
if message.state == VCAP::CloudController::BuildModel::FAILED_STATE
1616
build.fail_to_stage!('StagerError', message.error)
1717
elsif message.state == VCAP::CloudController::BuildModel::STAGED_STATE
18-
droplet = VCAP::CloudController::DropletCreate.new.create_docker_droplet(build)
18+
droplet = build.droplet
1919
droplet.lock!
2020
droplet.docker_receipt_image = message.lifecycle.dig(:data, :image)
2121
droplet.process_types = message.lifecycle.dig(:data, :processTypes)

lib/cloud_controller/kpack/stager.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Stager
66
}.freeze
77
APP_GUID_LABEL_KEY = 'cloudfoundry.org/app_guid'.freeze
88
BUILD_GUID_LABEL_KEY = 'cloudfoundry.org/build_guid'.freeze
9+
DROPLET_GUID_LABEL_KEY = 'cloudfoundry.org/droplet_guid'.freeze
910
STAGING_SOURCE_LABEL_KEY = 'cloudfoundry.org/source_type'.freeze
1011

1112
def initialize(builder_namespace:, registry_service_account_name:, registry_tag_base:)
@@ -64,8 +65,9 @@ def image_resource(staging_details, builder_spec)
6465
name: staging_details.package.app.guid,
6566
namespace: builder_namespace,
6667
labels: {
67-
APP_GUID_LABEL_KEY.to_sym => staging_details.package.app.guid,
68-
BUILD_GUID_LABEL_KEY.to_sym => staging_details.staging_guid,
68+
APP_GUID_LABEL_KEY.to_sym => staging_details.package.app.guid,
69+
BUILD_GUID_LABEL_KEY.to_sym => staging_details.staging_guid,
70+
DROPLET_GUID_LABEL_KEY.to_sym => create_droplet_and_get_guid(staging_details),
6971
STAGING_SOURCE_LABEL_KEY.to_sym => 'STG'
7072
},
7173
annotations: {
@@ -154,5 +156,11 @@ def client
154156
def blobstore_url_generator
155157
::CloudController::DependencyLocator.instance.blobstore_url_generator
156158
end
159+
160+
def create_droplet_and_get_guid(staging_details)
161+
build = VCAP::CloudController::BuildModel.find(guid: staging_details.staging_guid)
162+
droplet = VCAP::CloudController::DropletCreate.new.create_docker_droplet(build)
163+
droplet.guid
164+
end
157165
end
158166
end

spec/request/builds_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,9 @@
583583
end
584584

585585
context 'updating state' do
586-
let(:build_model) { VCAP::CloudController::BuildModel.make(package: package_model, state: VCAP::CloudController::BuildModel::STAGING_STATE) }
586+
let(:build_model) { VCAP::CloudController::BuildModel.make(:kpack, package: package_model,
587+
state: VCAP::CloudController::BuildModel::STAGING_STATE)
588+
}
587589
let(:request) do
588590
{
589591
state: 'STAGED',

spec/support/fakes/blueprints.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,10 @@ module VCAP::CloudController
7878
BuildModel.blueprint(:kpack) do
7979
guid { Sham.guid }
8080
state { VCAP::CloudController::DropletModel::STAGING_STATE }
81-
app { AppModel.make }
81+
app { AppModel.make(droplet_guid: guid) }
8282
kpack_lifecycle_data { KpackLifecycleDataModel.make(build: object.save) }
8383
package { PackageModel.make(app: app) }
84+
droplet { DropletModel.make(:docker, build: object.save) }
8485
end
8586

8687
BuildModel.blueprint(:buildpack) do

spec/unit/actions/build_update_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ module VCAP::CloudController
133133

134134
context 'when updating state' do
135135
let(:build) { BuildModel.make(:kpack) }
136+
136137
context 'when a build was successfully completed' do
137138
let(:body) do
138139
{
@@ -154,6 +155,7 @@ module VCAP::CloudController
154155
build_update.update(build, message)
155156

156157
expect(build.state).to eq('STAGED')
158+
expect(build.droplet.state).to eq('STAGED')
157159
expect(build.droplet.docker_receipt_image).to eq('some-fake-image:tag')
158160
expect(build.droplet.process_types).to eq({ 'foo' => 'foo start', 'bar' => 'bar start' })
159161
end

spec/unit/lib/cloud_controller/kpack/stager_spec.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ module Kpack
6363
before do
6464
allow(CloudController::DependencyLocator.instance).to receive(:k8s_api_client).and_return(client)
6565
allow(CloudController::DependencyLocator.instance).to receive(:blobstore_url_generator).and_return(blobstore_url_generator)
66-
6766
allow(client).to receive(:get_image).and_return(nil)
6867
allow(client).to receive(:get_custom_builder).and_return(default_builder_obj)
6968
end
@@ -87,8 +86,11 @@ module Kpack
8786
let(:package) do
8887
VCAP::CloudController::PackageModel.make(app: VCAP::CloudController::AppModel.make(:kpack))
8988
end
90-
let(:build) { VCAP::CloudController::BuildModel.make(:kpack) }
91-
89+
let!(:build) { VCAP::CloudController::BuildModel.make(:kpack) }
90+
let!(:droplet) { VCAP::CloudController::DropletModel.make(:docker, app: package.app) }
91+
before do
92+
allow_any_instance_of(VCAP::CloudController::DropletCreate).to receive(:create_docker_droplet).with(build).and_return(droplet)
93+
end
9294
it 'checks if the image exists' do
9395
allow(client).to receive(:create_image)
9496
expect(client).to receive(:get_image).with(package.app.guid, 'namespace').and_return(nil)
@@ -103,6 +105,7 @@ module Kpack
103105
name: package.app.guid,
104106
namespace: 'namespace',
105107
labels: {
108+
Stager::DROPLET_GUID_LABEL_KEY => droplet.guid,
106109
Stager::APP_GUID_LABEL_KEY => package.app.guid,
107110
Stager::BUILD_GUID_LABEL_KEY => build.guid,
108111
Stager::STAGING_SOURCE_LABEL_KEY => 'STG',

0 commit comments

Comments
 (0)