From b2e5683d8546230a2a5f19b540965fe42416f9c7 Mon Sep 17 00:00:00 2001 From: Nick Carboni Date: Wed, 10 Jun 2020 11:24:28 -0400 Subject: [PATCH] Conditionally add the messaging environment vars to worker containers If we don't provide a value for MESSAGING_TYPE we shouldn't assume that the kafka secret is present. If we try to deploy a container with env vars referencing the secret the container won't be created. https://github.com/ManageIQ/manageiq-pods/issues/541 --- .../object_definition.rb | 13 +++++++-- spec/lib/container_orchestrator_spec.rb | 28 +++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/lib/container_orchestrator/object_definition.rb b/lib/container_orchestrator/object_definition.rb index c5d6a8dd136..b87ba26dd3c 100644 --- a/lib/container_orchestrator/object_definition.rb +++ b/lib/container_orchestrator/object_definition.rb @@ -64,8 +64,6 @@ def default_environment {:name => "GUID", :value => MiqServer.my_guid}, {:name => "MEMCACHED_SERVER", :value => ENV["MEMCACHED_SERVER"]}, {:name => "MEMCACHED_SERVICE_NAME", :value => ENV["MEMCACHED_SERVICE_NAME"]}, - {:name => "MESSAGING_PORT", :value => ENV["MESSAGING_PORT"]}, - {:name => "MESSAGING_TYPE", :value => ENV["MESSAGING_TYPE"]}, {:name => "WORKER_HEARTBEAT_FILE", :value => Rails.root.join("tmp", "worker.hb").to_s}, {:name => "WORKER_HEARTBEAT_METHOD", :value => "file"}, {:name => "DATABASE_HOSTNAME", @@ -77,7 +75,16 @@ def default_environment {:name => "DATABASE_USER", :valueFrom => {:secretKeyRef=>{:name => "postgresql-secrets", :key => "username"}}}, {:name => "ENCRYPTION_KEY", - :valueFrom => {:secretKeyRef=>{:name => "app-secrets", :key => "encryption-key"}}}, + :valueFrom => {:secretKeyRef=>{:name => "app-secrets", :key => "encryption-key"}}} + ] + messaging_environment + end + + def messaging_environment + return [] unless ENV["MESSAGING_TYPE"].present? + + [ + {:name => "MESSAGING_PORT", :value => ENV["MESSAGING_PORT"]}, + {:name => "MESSAGING_TYPE", :value => ENV["MESSAGING_TYPE"]}, {:name => "MESSAGING_HOSTNAME", :valueFrom => {:secretKeyRef=>{:name => "kafka-secrets", :key => "hostname"}}}, {:name => "MESSAGING_PASSWORD", diff --git a/spec/lib/container_orchestrator_spec.rb b/spec/lib/container_orchestrator_spec.rb index 3447865601a..e11865e2139 100644 --- a/spec/lib/container_orchestrator_spec.rb +++ b/spec/lib/container_orchestrator_spec.rb @@ -56,6 +56,34 @@ end end + describe "#default_environment (private)" do + it "doesn't include messaging env vars when MESSAGING_TYPE is not set" do + env = subject.send(:default_environment) + expect(env).not_to include(hash_including(:name => "MESSAGING_TYPE")) + expect(env).not_to include(hash_including(:name => "MESSAGING_PORT")) + expect(env).not_to include(hash_including(:name => "MESSAGING_HOSTNAME")) + expect(env).not_to include(hash_including(:name => "MESSAGING_PASSWORD")) + expect(env).not_to include(hash_including(:name => "MESSAGING_USERNAME")) + end + + context "when MESSAGING_TYPE is set" do + before { stub_const("ENV", ENV.to_h.merge("MESSAGING_TYPE" => "kafka", "MESSAGING_PORT" => "9092")) } + + it "sets the messaging env vars" do + expect(subject.send(:default_environment)).to include( + {:name => "MESSAGING_PORT", :value => "9092"}, + {:name => "MESSAGING_TYPE", :value => "kafka"}, + {:name => "MESSAGING_HOSTNAME", + :valueFrom => {:secretKeyRef=>{:name => "kafka-secrets", :key => "hostname"}}}, + {:name => "MESSAGING_PASSWORD", + :valueFrom => {:secretKeyRef=>{:name => "kafka-secrets", :key => "password"}}}, + {:name => "MESSAGING_USERNAME", + :valueFrom => {:secretKeyRef=>{:name => "kafka-secrets", :key => "username"}}} + ) + end + end + end + context "with stub connections" do let(:apps_connection_stub) { double("AppsConnection") } let(:kube_connection_stub) { double("KubeConnection") }