Skip to content

Commit

Permalink
Refactor check-and-set logic into check_and_rewrite_coredns method
Browse files Browse the repository at this point in the history
  • Loading branch information
compumike committed Apr 1, 2021
1 parent 739841e commit 588a561
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions hairpin-proxy-controller/src/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,25 @@ def coredns_corefile_with_rewrite_rules(original_corefile, hosts)
cflines.join("\n")
end

def check_and_rewrite_coredns
@log.info("Polling all Ingress resources and CoreDNS configuration...")
hosts = fetch_ingress_hosts
cm = @k8s.api.resource("configmaps", namespace: "kube-system").get("coredns")

old_corefile = cm.data.Corefile
new_corefile = coredns_corefile_with_rewrite_rules(old_corefile, hosts)

if old_corefile.strip != new_corefile.strip
@log.info("Corefile has changed! New contents:\n#{new_corefile}\nSending updated ConfigMap to Kubernetes API server...")
cm.data.Corefile = new_corefile
@k8s.api.resource("configmaps", namespace: "kube-system").update_resource(cm)
end
end

def main_loop
@log.info("Starting main_loop with #{POLL_INTERVAL}s polling interval.")
loop do
@log.info("Polling all Ingress resources and CoreDNS configuration...")
hosts = fetch_ingress_hosts
cm = @k8s.api.resource("configmaps", namespace: "kube-system").get("coredns")

old_corefile = cm.data.Corefile
new_corefile = coredns_corefile_with_rewrite_rules(old_corefile, hosts)

if old_corefile.strip != new_corefile.strip
@log.info("Corefile has changed! New contents:\n#{new_corefile}\nSending updated ConfigMap to Kubernetes API server...")
cm.data.Corefile = new_corefile
@k8s.api.resource("configmaps", namespace: "kube-system").update_resource(cm)
end
check_and_rewrite_coredns

sleep(POLL_INTERVAL)
end
Expand Down

0 comments on commit 588a561

Please sign in to comment.