Skip to content

Commit

Permalink
skip sleep after a CRD write so the next iteration can immediately pr…
Browse files Browse the repository at this point in the history
…ocess CRD changes
  • Loading branch information
wr0ngway committed Sep 27, 2021
1 parent 9036e07 commit 5751ae4
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/kubetruth/etl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ETL

def initialize(dry_run: false)
@dry_run = dry_run
@wrote_crds = false
end

def kubeapi
Expand All @@ -28,6 +29,12 @@ def interruptible_sleep(interval)
end

def watch_crds_to_interrupt(&block)
if @wrote_crds
logger.info {"Skipping Poller sleep to handle the recent application of kubetruth CRDs"}
@wrote_crds = false
return
end

begin
begin
watcher = kubeapi.watch_project_mappings
Expand Down Expand Up @@ -256,11 +263,17 @@ def kube_apply(parsed_yml)
# work as there a bunch of fields we don't control, so we just rely on
# the server-side apply to do the right thing.
logger.info "Updating kubernetes resource #{ident}"
kubeapi.apply_resource(parsed_yml) unless @dry_run
unless @dry_run
applied_resource = kubeapi.apply_resource(parsed_yml)
@wrote_crds = true if kind == "ProjectMapping" && applied_resource.metadata&.resourceVersion != resource.metadata&.resourceVersion
end
end
rescue Kubeclient::ResourceNotFoundError
logger.info "Creating kubernetes resource #{ident}"
kubeapi.apply_resource(parsed_yml) unless @dry_run
unless @dry_run
kubeapi.apply_resource(parsed_yml)
@wrote_crds = true if kind == "ProjectMapping"
end
end
end

Expand Down
67 changes: 67 additions & 0 deletions spec/kubetruth/etl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,28 @@ class ForceExit < Exception; end

end

it "skips next sleep when crds get written" do
watcher = double()
expect(@kubeapi).to receive(:watch_project_mappings).and_return(watcher)
allow(watcher).to receive(:each)
allow(watcher).to receive(:finish)
expect(etl).to receive(:interruptible_sleep).and_raise(ForceExit)

count = 0
begin
etl.with_polling(0.2) do
if count == 0
etl.instance_variable_set(:@wrote_crds, true)
end
count += 1
end
rescue ForceExit
end

expect(etl.instance_variable_get(:@wrote_crds)).to eq(false)
expect(count).to eq(2)
end

end

describe "#load_config" do
Expand Down Expand Up @@ -246,6 +268,7 @@ class ForceExit < Exception; end
expect(@kubeapi).to receive(:apply_resource).with(parsed_yml)
etl.kube_apply(parsed_yml)
expect(Logging.contents).to match(/Creating kubernetes resource/)
expect(etl.instance_variable_get(:@wrote_crds)).to eq(false)
end

it "calls to kube to update existing resource" do
Expand All @@ -265,6 +288,7 @@ class ForceExit < Exception; end
expect(@kubeapi).to receive(:apply_resource).with(parsed_yml)
etl.kube_apply(parsed_yml)
expect(Logging.contents).to match(/Updating kubernetes resource/)
expect(etl.instance_variable_get(:@wrote_crds)).to eq(false)
end

it "skips call to kube for existing resource not under management" do
Expand Down Expand Up @@ -324,6 +348,49 @@ class ForceExit < Exception; end
expect(Logging.contents).to match(/Creating kubernetes resource/)
end

it "registers project mapping writes on new resource" do
resource_yml = <<~EOF
apiVersion: v1
kind: ProjectMapping
metadata:
name: "pm1"
spec:
"scope": "override"
EOF
parsed_yml = YAML.load(resource_yml)
resource = Kubeclient::Resource.new(parsed_yml.merge(metadata: {resourceVersion: "123"}))

expect(etl.instance_variable_get(:@wrote_crds)).to eq(false)
expect(@kubeapi).to receive(:get_resource).and_raise(Kubeclient::ResourceNotFoundError.new(1, "", 2))
expect(@kubeapi).to receive(:apply_resource).and_return(resource)
etl.kube_apply(parsed_yml)
expect(etl.instance_variable_get(:@wrote_crds)).to eq(true)
end

it "registers project mapping writes on updated resource" do
resource_yml = <<~EOF
apiVersion: v1
kind: ProjectMapping
metadata:
name: "pm1"
spec:
"scope": "override"
EOF
parsed_yml = YAML.load(resource_yml)
resource = Kubeclient::Resource.new(parsed_yml.merge(metadata: {resourceVersion: "123"}))

expect(etl.instance_variable_get(:@wrote_crds)).to eq(false)
expect(@kubeapi).to receive(:get_resource).and_return(resource)
expect(@kubeapi).to receive(:apply_resource).and_return(resource)
etl.kube_apply(parsed_yml)
expect(etl.instance_variable_get(:@wrote_crds)).to eq(false)

resource.metadata.resourceVersion = "456"
expect(@kubeapi).to receive(:apply_resource).and_return(resource)
etl.kube_apply(parsed_yml)
expect(etl.instance_variable_get(:@wrote_crds)).to eq(true)
end

end

describe "#apply" do
Expand Down

0 comments on commit 5751ae4

Please sign in to comment.