1 { config, options, pkgs, lib, ... }:
4 cfg = config.services.kubernetes.addons.dns;
11 options.services.kubernetes.addons.dns = {
12 enable = lib.mkEnableOption "kubernetes dns addon";
14 clusterIp = lib.mkOption {
15 description = "Dns addon clusterIP";
17 # this default is also what kubernetes users
19 lib.concatStringsSep "." (
20 lib.take 3 (lib.splitString "." config.services.kubernetes.apiserver.serviceClusterIpRange
23 defaultText = lib.literalMD ''
25 `config.${options.services.kubernetes.apiserver.serviceClusterIpRange}`.
30 clusterDomain = lib.mkOption {
31 description = "Dns cluster domain";
32 default = "cluster.local";
36 replicas = lib.mkOption {
37 description = "Number of DNS pod replicas to deploy in the cluster.";
42 reconcileMode = lib.mkOption {
44 Controls the addon manager reconciliation mode for the DNS addon.
46 Setting reconcile mode to EnsureExists makes it possible to tailor DNS behavior by editing the coredns ConfigMap.
48 See: <https://github.com/kubernetes/kubernetes/blob/master/cluster/addons/addon-manager/README.md>.
50 default = "Reconcile";
51 type = lib.types.enum [ "Reconcile" "EnsureExists" ];
54 coredns = lib.mkOption {
55 description = "Docker image to seed for the CoreDNS container.";
56 type = lib.types.attrs;
58 imageName = "coredns/coredns";
59 imageDigest = "sha256:a0ead06651cf580044aeb0a0feba63591858fb2e43ade8c9dea45a6a89ae7e5e";
60 finalImageTag = version;
61 sha256 = "0wg696920smmal7552a2zdhfncndn5kfammfa8bk8l7dz9bhk0y1";
65 corefile = lib.mkOption {
67 Custom coredns corefile configuration.
69 See: <https://coredns.io/manual/toc/#configuration>.
73 .:${toString ports.dns} {
75 health :${toString ports.health}
76 kubernetes ${cfg.clusterDomain} in-addr.arpa ip6.arpa {
78 fallthrough in-addr.arpa ip6.arpa
80 prometheus :${toString ports.metrics}
81 forward . /etc/resolv.conf
87 defaultText = lib.literalExpression ''
89 .:${toString ports.dns} {
91 health :${toString ports.health}
92 kubernetes ''${config.services.kubernetes.addons.dns.clusterDomain} in-addr.arpa ip6.arpa {
94 fallthrough in-addr.arpa ip6.arpa
96 prometheus :${toString ports.metrics}
97 forward . /etc/resolv.conf
108 config = lib.mkIf cfg.enable {
109 services.kubernetes.kubelet.seedDockerImages =
110 lib.singleton (pkgs.dockerTools.pullImage cfg.coredns);
112 services.kubernetes.addonManager.bootstrapAddons = {
114 apiVersion = "rbac.authorization.k8s.io/v1";
115 kind = "ClusterRole";
118 "addonmanager.kubernetes.io/mode" = "Reconcile";
119 k8s-app = "kube-dns";
120 "kubernetes.io/cluster-service" = "true";
121 "kubernetes.io/bootstrapping" = "rbac-defaults";
123 name = "system:coredns";
128 resources = [ "endpoints" "services" "pods" "namespaces" ];
129 verbs = [ "list" "watch" ];
133 resources = [ "nodes" ];
137 apiGroups = [ "discovery.k8s.io" ];
138 resources = [ "endpointslices" ];
139 verbs = [ "list" "watch" ];
145 apiVersion = "rbac.authorization.k8s.io/v1";
146 kind = "ClusterRoleBinding";
149 "rbac.authorization.kubernetes.io/autoupdate" = "true";
152 "addonmanager.kubernetes.io/mode" = "Reconcile";
153 k8s-app = "kube-dns";
154 "kubernetes.io/cluster-service" = "true";
155 "kubernetes.io/bootstrapping" = "rbac-defaults";
157 name = "system:coredns";
160 apiGroup = "rbac.authorization.k8s.io";
161 kind = "ClusterRole";
162 name = "system:coredns";
166 kind = "ServiceAccount";
168 namespace = "kube-system";
174 services.kubernetes.addonManager.addons = {
177 kind = "ServiceAccount";
180 "addonmanager.kubernetes.io/mode" = "Reconcile";
181 k8s-app = "kube-dns";
182 "kubernetes.io/cluster-service" = "true";
185 namespace = "kube-system";
194 "addonmanager.kubernetes.io/mode" = cfg.reconcileMode;
195 k8s-app = "kube-dns";
196 "kubernetes.io/cluster-service" = "true";
199 namespace = "kube-system";
202 Corefile = cfg.corefile;
207 apiVersion = "apps/v1";
211 "addonmanager.kubernetes.io/mode" = cfg.reconcileMode;
212 k8s-app = "kube-dns";
213 "kubernetes.io/cluster-service" = "true";
214 "kubernetes.io/name" = "CoreDNS";
217 namespace = "kube-system";
220 replicas = cfg.replicas;
222 matchLabels = { k8s-app = "kube-dns"; };
225 rollingUpdate = { maxUnavailable = 1; };
226 type = "RollingUpdate";
231 k8s-app = "kube-dns";
237 args = [ "-conf" "/etc/coredns/Corefile" ];
238 image = with cfg.coredns; "${imageName}:${finalImageTag}";
239 imagePullPolicy = "Never";
241 failureThreshold = 5;
247 initialDelaySeconds = 60;
248 successThreshold = 1;
254 containerPort = ports.dns;
259 containerPort = ports.dns;
264 containerPort = ports.metrics;
279 allowPrivilegeEscalation = false;
283 readOnlyRootFilesystem = true;
287 mountPath = "/etc/coredns";
288 name = "config-volume";
294 dnsPolicy = "Default";
296 "beta.kubernetes.io/os" = "linux";
298 serviceAccountName = "coredns";
301 effect = "NoSchedule";
302 key = "node-role.kubernetes.io/master";
305 key = "CriticalAddonsOnly";
320 name = "config-volume";
333 "prometheus.io/port" = toString ports.metrics;
334 "prometheus.io/scrape" = "true";
337 "addonmanager.kubernetes.io/mode" = "Reconcile";
338 k8s-app = "kube-dns";
339 "kubernetes.io/cluster-service" = "true";
340 "kubernetes.io/name" = "CoreDNS";
343 namespace = "kube-system";
346 clusterIP = cfg.clusterIp;
351 targetPort = ports.dns;
357 targetPort = ports.dns;
361 selector = { k8s-app = "kube-dns"; };
366 services.kubernetes.kubelet.clusterDns = lib.mkDefault [ cfg.clusterIp ];
369 meta.buildDocsInSandbox = false;