blob: 83050332eb1c6d007641327602740a6f379f60e4 [file] [log] [blame]
Sergiusz Bazanskidbfa9882020-06-06 01:21:45 +02001# Common cluster configuration.
2# This defines what Kubernetes resources are required to turn a bare k8s
3# deployment into a fully working cluster.
4# These assume that you're running on bare metal, and using the corresponding
5# NixOS deployment that we do.
6
7local kube = import "../../kube/kube.libsonnet";
8local policies = import "../../kube/policies.libsonnet";
9
10local calico = import "lib/calico.libsonnet";
11local certmanager = import "lib/cert-manager.libsonnet";
12local coredns = import "lib/coredns.libsonnet";
Serge Bazanski2414afe2021-05-24 15:08:06 +020013local identd = import "lib/identd.libsonnet";
Sergiusz Bazanskidbfa9882020-06-06 01:21:45 +020014local metallb = import "lib/metallb.libsonnet";
15local metrics = import "lib/metrics.libsonnet";
16local nginx = import "lib/nginx.libsonnet";
17local prodvider = import "lib/prodvider.libsonnet";
18local rook = import "lib/rook.libsonnet";
19local pki = import "lib/pki.libsonnet";
20
21{
22 Cluster(short, realm):: {
23 local cluster = self,
24 local cfg = cluster.cfg,
25
26 short:: short,
27 realm:: realm,
28 fqdn:: "%s.%s" % [cluster.short, cluster.realm],
29
30 cfg:: {
31 // Storage class used for internal services (like registry). This must
32 // be set to a valid storage class. This can either be a cloud provider class
33 // (when running on GKE &co) or a storage class created using rook.
34 storageClassNameRedundant: error "storageClassNameRedundant must be set",
35 },
36
37 // These are required to let the API Server contact kubelets.
38 crAPIServerToKubelet: kube.ClusterRole("system:kube-apiserver-to-kubelet") {
39 metadata+: {
40 annotations+: {
41 "rbac.authorization.kubernetes.io/autoupdate": "true",
42 },
43 labels+: {
44 "kubernetes.io/bootstrapping": "rbac-defaults",
45 },
46 },
47 rules: [
48 {
49 apiGroups: [""],
50 resources: ["nodes/%s" % r for r in [ "proxy", "stats", "log", "spec", "metrics" ]],
51 verbs: ["*"],
52 },
53 ],
54 },
55 crbAPIServer: kube.ClusterRoleBinding("system:kube-apiserver") {
56 roleRef: {
57 apiGroup: "rbac.authorization.k8s.io",
58 kind: "ClusterRole",
59 name: cluster.crAPIServerToKubelet.metadata.name,
60 },
61 subjects: [
62 {
63 apiGroup: "rbac.authorization.k8s.io",
64 kind: "User",
65 # A cluster API Server authenticates with a certificate whose CN is == to the FQDN of the cluster.
66 name: cluster.fqdn,
67 },
68 ],
69 },
70
71 // This ClusterRole is bound to all humans that log in via prodaccess/prodvider/SSO.
72 // It should allow viewing of non-sensitive data for debugability and openness.
73 crViewer: kube.ClusterRole("system:viewer") {
74 rules: [
75 {
76 apiGroups: [""],
77 resources: [
78 "nodes",
79 "namespaces",
80 "pods",
81 "configmaps",
82 "services",
83 ],
84 verbs: ["list"],
85 },
86 {
87 apiGroups: ["metrics.k8s.io"],
88 resources: [
89 "nodes",
90 "pods",
91 ],
92 verbs: ["list"],
93 },
94 {
95 apiGroups: ["apps"],
96 resources: [
97 "statefulsets",
98 ],
99 verbs: ["list"],
100 },
101 {
102 apiGroups: ["extensions"],
103 resources: [
104 "deployments",
105 "ingresses",
106 ],
107 verbs: ["list"],
108 }
109 ],
110 },
111 // This ClusterRole is applied (scoped to personal namespace) to all humans.
112 crFullInNamespace: kube.ClusterRole("system:admin-namespace") {
113 rules: [
114 {
115 apiGroups: ["", "extensions", "apps"],
116 resources: ["*"],
117 verbs: ["*"],
118 },
119 {
120 apiGroups: ["batch"],
121 resources: ["jobs", "cronjobs"],
122 verbs: ["*"],
123 },
Serge Bazanskif40c9242021-02-07 19:23:43 +0000124 {
125 apiGroups: ["networking.k8s.io"],
126 resources: ["ingresses"],
127 verbs: ["*"],
128 },
Patryk Jakuszewd0a0b182022-01-25 21:05:49 +0100129 {
130 apiGroups: ["certmanager.k8s.io"],
131 resources: ["certificates"],
132 verbs: ["*"],
133 },
Sergiusz Bazanskidbfa9882020-06-06 01:21:45 +0200134 ],
135 },
136 // This ClusterRoleBindings allows root access to cluster admins.
137 crbAdmins: kube.ClusterRoleBinding("system:admins") {
138 roleRef: {
139 apiGroup: "rbac.authorization.k8s.io",
140 kind: "ClusterRole",
141 name: "cluster-admin",
142 },
143 subjects: [
144 {
145 apiGroup: "rbac.authorization.k8s.io",
146 kind: "User",
147 name: user + "@hackerspace.pl",
148 } for user in [
149 "q3k",
150 "implr",
151 "informatic",
152 ]
153 ],
154 },
155
156 podSecurityPolicies: policies.Cluster {},
157
158 allowInsecureNamespaces: [
159 policies.AllowNamespaceInsecure("kube-system"),
160 policies.AllowNamespaceInsecure("metallb-system"),
161 ],
162
163 // Allow all service accounts (thus all controllers) to create secure pods.
164 crbAllowServiceAccountsSecure: kube.ClusterRoleBinding("policy:allow-all-secure") {
165 roleRef_: cluster.podSecurityPolicies.secureRole,
166 subjects: [
167 {
168 kind: "Group",
169 apiGroup: "rbac.authorization.k8s.io",
170 name: "system:serviceaccounts",
171 }
172 ],
173 },
174
175 // Calico network fabric
176 calico: calico.Environment {},
177
178 // CoreDNS for this cluster.
179 dns: coredns.Environment {
180 cfg+: {
181 cluster_domains: [
182 "cluster.local",
183 cluster.fqdn,
184 ],
185 },
186 },
187
188 // Metrics Server
189 metrics: metrics.Environment {},
190
191 // Metal Load Balancer
192 metallb: metallb.Environment {},
193
194 // Main nginx Ingress Controller
195 nginx: nginx.Environment {},
196
197 // Cert-manager (Let's Encrypt, CA, ...)
198 certmanager: certmanager.Environment {},
199
200 issuer: kube.ClusterIssuer("letsencrypt-prod") {
201 spec: {
202 acme: {
203 server: "https://acme-v02.api.letsencrypt.org/directory",
204 email: "bofh@hackerspace.pl",
205 privateKeySecretRef: {
206 name: "letsencrypt-prod"
207 },
208 http01: {},
209 },
210 },
211 },
212
Serge Bazanski2414afe2021-05-24 15:08:06 +0200213 // Ident service
214 identd: identd.Environment {},
215
Sergiusz Bazanskidbfa9882020-06-06 01:21:45 +0200216 // Rook Ceph storage operator.
217 rook: rook.Operator {
218 operator+: {
219 spec+: {
Serge Bazanski464fb042021-09-11 20:24:27 +0000220 // Downscaled because of b.hswaw.net/6.
Serge Bazanski4f0468f2021-09-11 12:22:53 +0000221 replicas: 0,
Sergiusz Bazanskidbfa9882020-06-06 01:21:45 +0200222 },
223 },
224 },
225
226 // TLS PKI machinery (compatibility with mirko)
227 pki: pki.Environment(cluster.short, cluster.realm),
228
229 // Prodvider
230 prodvider: prodvider.Environment {
231 cfg+: {
232 apiEndpoint: "kubernetes.default.svc.%s" % [cluster.fqdn],
233 },
234 },
235 },
236}