1 { config, options, pkgs, lib, ... }:
7 cfg = config.services.kubernetes.addons.dns;
14 options.services.kubernetes.addons.dns = {
15 enable = mkEnableOption (lib.mdDoc "kubernetes dns addon");
17 clusterIp = mkOption {
18 description = lib.mdDoc "Dns addon clusterIP";
20 # this default is also what kubernetes users
22 concatStringsSep "." (
23 take 3 (splitString "." config.services.kubernetes.apiserver.serviceClusterIpRange
26 defaultText = literalMD ''
28 `config.${options.services.kubernetes.apiserver.serviceClusterIpRange}`.
33 clusterDomain = mkOption {
34 description = lib.mdDoc "Dns cluster domain";
35 default = "cluster.local";
40 description = lib.mdDoc "Number of DNS pod replicas to deploy in the cluster.";
45 reconcileMode = mkOption {
46 description = lib.mdDoc ''
47 Controls the addon manager reconciliation mode for the DNS addon.
49 Setting reconcile mode to EnsureExists makes it possible to tailor DNS behavior by editing the coredns ConfigMap.
51 See: <https://github.com/kubernetes/kubernetes/blob/master/cluster/addons/addon-manager/README.md>.
53 default = "Reconcile";
54 type = types.enum [ "Reconcile" "EnsureExists" ];
58 description = lib.mdDoc "Docker image to seed for the CoreDNS container.";
61 imageName = "coredns/coredns";
62 imageDigest = "sha256:4a6e0769130686518325b21b0c1d0688b54e7c79244d48e1b15634e98e40c6ef";
63 finalImageTag = version;
64 sha256 = "02r440xcdsgi137k5lmmvp0z5w5fmk8g9mysq5pnysq1wl8sj6mw";
69 description = lib.mdDoc ''
70 Custom coredns corefile configuration.
72 See: <https://coredns.io/manual/toc/#configuration>.
76 .:${toString ports.dns} {
78 health :${toString ports.health}
79 kubernetes ${cfg.clusterDomain} in-addr.arpa ip6.arpa {
81 fallthrough in-addr.arpa ip6.arpa
83 prometheus :${toString ports.metrics}
84 forward . /etc/resolv.conf
90 defaultText = literalExpression ''
92 .:${toString ports.dns} {
94 health :${toString ports.health}
95 kubernetes ''${config.services.kubernetes.addons.dns.clusterDomain} in-addr.arpa ip6.arpa {
97 fallthrough in-addr.arpa ip6.arpa
99 prometheus :${toString ports.metrics}
100 forward . /etc/resolv.conf
111 config = mkIf cfg.enable {
112 services.kubernetes.kubelet.seedDockerImages =
113 singleton (pkgs.dockerTools.pullImage cfg.coredns);
115 services.kubernetes.addonManager.bootstrapAddons = {
117 apiVersion = "rbac.authorization.k8s.io/v1";
118 kind = "ClusterRole";
121 "addonmanager.kubernetes.io/mode" = "Reconcile";
122 k8s-app = "kube-dns";
123 "kubernetes.io/cluster-service" = "true";
124 "kubernetes.io/bootstrapping" = "rbac-defaults";
126 name = "system:coredns";
131 resources = [ "endpoints" "services" "pods" "namespaces" ];
132 verbs = [ "list" "watch" ];
136 resources = [ "nodes" ];
143 apiVersion = "rbac.authorization.k8s.io/v1";
144 kind = "ClusterRoleBinding";
147 "rbac.authorization.kubernetes.io/autoupdate" = "true";
150 "addonmanager.kubernetes.io/mode" = "Reconcile";
151 k8s-app = "kube-dns";
152 "kubernetes.io/cluster-service" = "true";
153 "kubernetes.io/bootstrapping" = "rbac-defaults";
155 name = "system:coredns";
158 apiGroup = "rbac.authorization.k8s.io";
159 kind = "ClusterRole";
160 name = "system:coredns";
164 kind = "ServiceAccount";
166 namespace = "kube-system";
172 services.kubernetes.addonManager.addons = {
175 kind = "ServiceAccount";
178 "addonmanager.kubernetes.io/mode" = "Reconcile";
179 k8s-app = "kube-dns";
180 "kubernetes.io/cluster-service" = "true";
183 namespace = "kube-system";
192 "addonmanager.kubernetes.io/mode" = cfg.reconcileMode;
193 k8s-app = "kube-dns";
194 "kubernetes.io/cluster-service" = "true";
197 namespace = "kube-system";
200 Corefile = cfg.corefile;
205 apiVersion = "apps/v1";
209 "addonmanager.kubernetes.io/mode" = cfg.reconcileMode;
210 k8s-app = "kube-dns";
211 "kubernetes.io/cluster-service" = "true";
212 "kubernetes.io/name" = "CoreDNS";
215 namespace = "kube-system";
218 replicas = cfg.replicas;
220 matchLabels = { k8s-app = "kube-dns"; };
223 rollingUpdate = { maxUnavailable = 1; };
224 type = "RollingUpdate";
229 k8s-app = "kube-dns";
235 args = [ "-conf" "/etc/coredns/Corefile" ];
236 image = with cfg.coredns; "${imageName}:${finalImageTag}";
237 imagePullPolicy = "Never";
239 failureThreshold = 5;
245 initialDelaySeconds = 60;
246 successThreshold = 1;
252 containerPort = ports.dns;
257 containerPort = ports.dns;
262 containerPort = ports.metrics;
277 allowPrivilegeEscalation = false;
281 readOnlyRootFilesystem = true;
285 mountPath = "/etc/coredns";
286 name = "config-volume";
292 dnsPolicy = "Default";
294 "beta.kubernetes.io/os" = "linux";
296 serviceAccountName = "coredns";
299 effect = "NoSchedule";
300 key = "node-role.kubernetes.io/master";
303 key = "CriticalAddonsOnly";
318 name = "config-volume";
331 "prometheus.io/port" = toString ports.metrics;
332 "prometheus.io/scrape" = "true";
335 "addonmanager.kubernetes.io/mode" = "Reconcile";
336 k8s-app = "kube-dns";
337 "kubernetes.io/cluster-service" = "true";
338 "kubernetes.io/name" = "CoreDNS";
341 namespace = "kube-system";
344 clusterIP = cfg.clusterIp;
349 targetPort = ports.dns;
355 targetPort = ports.dns;
359 selector = { k8s-app = "kube-dns"; };
364 services.kubernetes.kubelet.clusterDns = mkDefault cfg.clusterIp;
367 meta.buildDocsInSandbox = false;