Skip to content

Commit

Permalink
feat: talos_patches makejinja func and move certain patches to `pat…
Browse files Browse the repository at this point in the history
…ches` directory (#1509)
  • Loading branch information
onedr0p authored Jun 4, 2024
1 parent 6fd9c17 commit f397a7d
Show file tree
Hide file tree
Showing 15 changed files with 143 additions and 148 deletions.
20 changes: 19 additions & 1 deletion bootstrap/scripts/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@
import validation


# Return the filename of a path without the j2 extension
def basename(value: str) -> str:
return Path(value).stem


# Return a list of files in the talos patches directory
def talos_patches(value: str) -> list[str]:
path = Path(f'bootstrap/templates/kubernetes/bootstrap/talos/patches/{value}')
if not path.is_dir():
return []
return [str(f) for f in path.glob('*.yaml.j2') if f.is_file()]


# Return the nth host in a CIDR range
def nthhost(value: str, query: int) -> str:
value = IPNetwork(value)
try:
Expand Down Expand Up @@ -50,7 +64,11 @@ def __init__(self, data: dict[str, Any], config: makejinja.config.Config):


def filters(self) -> makejinja.plugin.Filters:
return [nthhost]
return [basename, nthhost]


def functions(self) -> makejinja.plugin.Functions:
return [talos_patches]


def path_filters(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Talos Patching

This directory contains Kustomization patches that are added to the talhelper configuration file.

<https://www.talos.dev/v1.7/talos-guides/configuration/patching/>

## Patch Directories

Under this `patches` directory, there are several sub-directories that can contain patches that are added to the talhelper configuration file.
Each directory is optional and therefore might not created by default.

- `global/`: patches that are applied to both the controller and worker configurations
- `controller/`: patches that are applied to the controller configurations
- `worker/`: patches that are applied to the worker configurations
- `${node-hostname}/`: patches that are applied to the node with the specified name
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
machine:
features:
kubernetesTalosAPIAccess:
enabled: true
allowedRoles:
- os:admin
allowedKubernetesNamespaces:
- system-upgrade
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
cluster:
allowSchedulingOnControlPlanes: true
controllerManager:
extraArgs:
bind-address: 0.0.0.0
coreDNS:
disabled: true
proxy:
disabled: true
scheduler:
extraArgs:
bind-address: 0.0.0.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- op: remove
path: /cluster/apiServer/admissionControl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cluster:
etcd:
extraArgs:
listen-metrics-urls: http://0.0.0.0:2381
advertisedSubnets:
- #{ bootstrap_node_network }#
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cluster:
discovery:
registries:
kubernetes:
disabled: false
service:
disabled: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
machine:
files:
- op: create
path: /etc/cri/conf.d/20-customization.part
content: |-
[plugins."io.containerd.grpc.v1.cri"]
enable_unprivileged_ports = true
enable_unprivileged_icmp = true
[plugins."io.containerd.grpc.v1.cri".containerd]
discard_unpacked_layers = false
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
discard_unpacked_layers = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
machine:
network:
disableSearchDomain: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
machine:
features:
hostDNS:
enabled: true
resolveMemberNames: true
forwardKubeDNSToHost: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
machine:
kubelet:
extraArgs:
rotate-server-certificates: true
nodeIP:
validSubnets:
- #{ bootstrap_node_network }#
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
machine:
kubelet:
extraMounts:
- destination: /var/openebs/local
type: bind
source: /var/openebs/local
options:
- bind
- rshared
- rw
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
machine:
sysctls:
fs.inotify.max_queued_events: "65536"
fs.inotify.max_user_watches: "524288"
fs.inotify.max_user_instances: "8192"
net.core.rmem_max: "2500000"
net.core.wmem_max: "2500000"
164 changes: 29 additions & 135 deletions bootstrap/templates/kubernetes/bootstrap/talos/talconfig.yaml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ nodes:
ip: "#{ bootstrap_controller_vip }#"
#% endif %#
#% endif %#
#% if bootstrap_user_patches %#
patches:
- "@./patches/node_#{ item.name }#.yaml"
#% endif %#
#% if item.manifests %#
extraManifests:
#% for manifest in item.manifests %#
Expand All @@ -110,73 +106,29 @@ nodes:
#% endif %#
#% endfor %#
#% endif %#
#% for file in talos_patches('%s' % (item.name)) %#
#% if loop.index == 1 %#
patches:
#% endif %#
- "@./patches/#{ item.name }#/#{ file | basename }#"
#% endfor %#
#% endfor %#

# Global patches
patches:
# Configure containerd
- |-
machine:
files:
- op: create
path: /etc/cri/conf.d/20-customization.part
content: |-
[plugins."io.containerd.grpc.v1.cri"]
enable_unprivileged_ports = true
enable_unprivileged_icmp = true
[plugins."io.containerd.grpc.v1.cri".containerd]
discard_unpacked_layers = false
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
discard_unpacked_layers = false
# Disable search domain everywhere
- |-
machine:
network:
disableSearchDomain: true
# Enable cluster discovery
- |-
cluster:
discovery:
registries:
kubernetes:
disabled: false
service:
disabled: false
# Configure kubelet
- |-
machine:
kubelet:
extraArgs:
rotate-server-certificates: true
nodeIP:
validSubnets:
- #{ bootstrap_node_network }#
# Enable host dns
- |-
machine:
features:
hostDNS:
enabled: true
resolveMemberNames: true
forwardKubeDNSToHost: false
#% if bootstrap_dns_servers | length %#
# Force nameserver
- |-
- # Force nameserver
|-
machine:
network:
nameservers:
#% for item in bootstrap_dns_servers %#
- #{ item }#
#% endfor %#
#% endif %#

#% if bootstrap_ntp_servers | length %#
# Configure NTP
- |-
- # Configure NTP
|-
machine:
time:
disabled: false
Expand All @@ -185,33 +137,9 @@ patches:
- #{ item }#
#% endfor %#
#% endif %#

# Custom sysctl settings
- |-
machine:
sysctls:
fs.inotify.max_queued_events: "65536"
fs.inotify.max_user_watches: "524288"
fs.inotify.max_user_instances: "8192"
net.core.rmem_max: "2500000"
net.core.wmem_max: "2500000"
# Mount openebs-hostpath in kubelet
- |-
machine:
kubelet:
extraMounts:
- destination: /var/openebs/local
type: bind
source: /var/openebs/local
options:
- bind
- rshared
- rw
#% if bootstrap_secureboot.enabled and bootstrap_secureboot.encrypt_disk_with_tpm %#
# Encrypt system disk with TPM
- |-
- # Encrypt system disk with TPM
|-
machine:
systemDiskEncryption:
ephemeral:
Expand All @@ -225,60 +153,26 @@ patches:
- slot: 0
tpm: {}
#% endif %#
#% for file in talos_patches('global') %#
- "@./patches/global/#{ file | basename }#"
#% endfor %#

#% if bootstrap_user_patches %#
# User specified global patches
- "@./patches/global.yaml"
#% endif %#

#% for file in talos_patches('controller') %#
#% if loop.index == 1 %#
# Controller patches
controlPlane:
patches:
# Cluster configuration
- |-
cluster:
allowSchedulingOnControlPlanes: true
controllerManager:
extraArgs:
bind-address: 0.0.0.0
coreDNS:
disabled: true
proxy:
disabled: true
scheduler:
extraArgs:
bind-address: 0.0.0.0
# ETCD configuration
- |-
cluster:
etcd:
extraArgs:
listen-metrics-urls: http://0.0.0.0:2381
advertisedSubnets:
- #{ bootstrap_node_network }#
# Disable default API server admission plugins.
- |-
- op: remove
path: /cluster/apiServer/admissionControl
#% endif %#
- "@./patches/controller/#{ file | basename }#"
#% endfor %#

# Enable K8s Talos API Access
- |-
machine:
features:
kubernetesTalosAPIAccess:
enabled: true
allowedRoles:
- os:admin
allowedKubernetesNamespaces:
- system-upgrade
#% if bootstrap_user_patches %#
# User specified controlPlane patches
- "@./patches/controlPlane.yaml"
#% endif %#
#% if ((bootstrap_user_patches) and (bootstrap_node_inventory | selectattr('controller', 'equalto', False) | list | length)) %#
#% if (bootstrap_node_inventory | selectattr('controller', 'equalto', False) | list | length) and (talos_patches('worker') | length) %#
#% for file in talos_patches('worker') %#
#% if loop.index == 1 %#
# Worker patches
worker:
patches:
# User specified worker patches
- "@./patches/worker.yaml"
#% endif %#
- "@./patches/worker/#{ file | basename }#"
#% endfor %#
#% endif %#
12 changes: 0 additions & 12 deletions config.sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,6 @@ bootstrap_secureboot:
# See: https://www.talos.dev/v1.6/talos-guides/install/bare-metal-platforms/secureboot/#disk-encryption-with-tpm
encrypt_disk_with_tpm: false

# (Optional) Add includes for user provided patches to generated talconfig.yaml.
# See: https://github.com/budimanjojo/talhelper/blob/179ba9ed42f70069c7842109bea24f769f7af6eb/example/extraKernelArgs-patch.yaml
# Patches are applied in this order. (global overrides cp/worker which overrides node-specific).
# Create these files to allow talos:bootstrap-genconfig to complete (empty files are ok).
# kubernetes/bootstrap/talos/patches/node_<name>.yaml # Patches for individual nodes
# kubernetes/bootstrap/talos/patches/controlPlane.yaml # Patches for controlplane nodes
# kubernetes/bootstrap/talos/patches/worker.yaml # Patches for worker nodes
# kubernetes/bootstrap/talos/patches/global.yaml # Patches for ALL nodes
bootstrap_user_patches: false

#
# 2. (Required) Flux details - Flux is used to manage the cluster configuration.
#
Expand Down Expand Up @@ -219,6 +209,4 @@ bootstrap_cloudflare:
# # IMPORTANT: I am looking for people to help maintain IPv6 support since I cannot test it.
# # Ref: https://github.com/onedr0p/cluster-template/issues/1148
# # Keep in mind that Cilium does not currently support IPv6 L2 announcements.
# # Make sure you set cluster.pod_cidr and cluster.service_cidr
# # to a valid dual stack CIDRs, e.g. "10.42.0.0/16,fd00:10:244::/64"
# dual_stack_ipv4_first: false

0 comments on commit f397a7d

Please sign in to comment.