Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep resources with ownerReference in stack prune #159

Merged
merged 7 commits into from
Sep 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/k8s/stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ def keep_resource!(resource)
# @param resource [K8s::Resource]
# @return [Boolean]
def keep_resource?(resource)
@keep_resources["#{resource.kind}:#{resource.metadata.name}@#{resource.metadata.namespace}"] == resource.metadata&.annotations&.dig(@checksum_annotation)
keep_annotation = @keep_resources["#{resource.kind}:#{resource.metadata.name}@#{resource.metadata.namespace}"]
return false unless keep_annotation

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should check the owner reference count here before trying to check anything else. if there's owners, keep it.

Copy link
Contributor Author

@kke kke Sep 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the keep_resource? won't be called when pruning with keep_resources: false (stack.delete / K8s::Stack.delete) (so implemented in the prune conditional monster)

keep_annotation == resource.metadata&.annotations&.dig(@checksum_annotation)
end

# Delete all stack resources that were not applied
Expand Down Expand Up @@ -158,6 +161,8 @@ def prune(client, keep_resources:, skip_forbidden: true)

if resource_label != name
# apiserver did not respect labelSelector
elsif resource.metadata&.ownerReferences && !resource.metadata.ownerReferences.empty?
logger.info "Server resource #{resource.apiVersion}:#{resource.apiKind}/#{resource.metadata.name} in namespace #{resource.metadata.namespace} has ownerReferences and will be kept"
elsif keep_resources && keep_resource?(resource)
# resource is up-to-date
else
Expand Down
26 changes: 26 additions & 0 deletions spec/k8s/stack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,32 @@
end
end

context "which has extra resources with owner references" do
let(:resources) {
subject.resources
}

before do
extra_resource = subject.prepare_resource(subject.resources.pop)
extra_resource.metadata.ownerReferences = [
{
:apiVersion=>"certmanager.k8s.io/v1alpha1",
:kind=>"Certificate",
:name=>"kontena-lens"
}
]
returned_resources = resources.dup
returned_resources = returned_resources.map { |r| subject.prepare_resource(r) unless r.nil? }
allow(client).to receive(:get_resources).with([K8s::Resource, K8s::Resource]).and_return(returned_resources)
allow(client).to receive(:list_resources).with(labelSelector: { 'k8s.kontena.io/stack' => 'whoami' }, skip_forbidden: true).and_return(returned_resources + [extra_resource])
end

it "keeps the resource" do
expect(client).not_to receive(:delete_resource)
subject.apply(client, prune: true)
end
end

context "which has extra resources on server" do
let(:resources) {
subject.resources
Expand Down