Skip to content

Commit 588a561

Browse files
committed
Refactor check-and-set logic into check_and_rewrite_coredns method
1 parent 739841e commit 588a561

File tree

1 file changed

+16
-12
lines changed
  • hairpin-proxy-controller/src

1 file changed

+16
-12
lines changed

hairpin-proxy-controller/src/main.rb

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,25 @@ def coredns_corefile_with_rewrite_rules(original_corefile, hosts)
5353
cflines.join("\n")
5454
end
5555

56+
def check_and_rewrite_coredns
57+
@log.info("Polling all Ingress resources and CoreDNS configuration...")
58+
hosts = fetch_ingress_hosts
59+
cm = @k8s.api.resource("configmaps", namespace: "kube-system").get("coredns")
60+
61+
old_corefile = cm.data.Corefile
62+
new_corefile = coredns_corefile_with_rewrite_rules(old_corefile, hosts)
63+
64+
if old_corefile.strip != new_corefile.strip
65+
@log.info("Corefile has changed! New contents:\n#{new_corefile}\nSending updated ConfigMap to Kubernetes API server...")
66+
cm.data.Corefile = new_corefile
67+
@k8s.api.resource("configmaps", namespace: "kube-system").update_resource(cm)
68+
end
69+
end
70+
5671
def main_loop
5772
@log.info("Starting main_loop with #{POLL_INTERVAL}s polling interval.")
5873
loop do
59-
@log.info("Polling all Ingress resources and CoreDNS configuration...")
60-
hosts = fetch_ingress_hosts
61-
cm = @k8s.api.resource("configmaps", namespace: "kube-system").get("coredns")
62-
63-
old_corefile = cm.data.Corefile
64-
new_corefile = coredns_corefile_with_rewrite_rules(old_corefile, hosts)
65-
66-
if old_corefile.strip != new_corefile.strip
67-
@log.info("Corefile has changed! New contents:\n#{new_corefile}\nSending updated ConfigMap to Kubernetes API server...")
68-
cm.data.Corefile = new_corefile
69-
@k8s.api.resource("configmaps", namespace: "kube-system").update_resource(cm)
70-
end
74+
check_and_rewrite_coredns
7175

7276
sleep(POLL_INTERVAL)
7377
end

0 commit comments

Comments
 (0)