Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(roles/k3s): add calico #196

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions roles/k3s/defaults/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,37 @@ k3s_etcd_snapshot_region: us-east-1
k3s_etcd_snapshot_folder: snapshots
k3s_etcd_snapshot_insecure: false
k3s_etcd_snapshot_timeout: 5m0s

# Enable this if you want to install calico
# https://docs.tigera.io/calico/latest/getting-started/kubernetes/k3s/multi-node-install#install-calico
# NOTE: enabling this will add the following args to the k3s server node:
# --flannel-backend=none
# --disable-network-policy
# NOTE: If you run rancher webhook you may run into this issue:
# https://ranchermanager.docs.rancher.com/reference-guides/rancher-webhook#eks-cluster-with-calico-cni
k3s_calico: false
k3s_calico_version: "v3.29.1"
# Change this if you want to use a different version of calico custom resources.
# The default calico custom resources will be used when this is omitted.
# https://docs.tigera.io/calico/latest/getting-started/kubernetes/k3s/multi-node-install#install-calico
# k3s_calico_custom_resources: |
# apiVersion: operator.tigera.io/v1
# kind: Installation
# metadata:
# name: default
# spec:
# # Configures Calico networking.
# calicoNetwork:
# # Note: The ipPools section cannot be modified post-install.
# ipPools:
# - blockSize: 26
# cidr: 192.168.0.0/16
# encapsulation: IPIP
# natOutgoing: Enabled
# nodeSelector: all()
# ---
# apiVersion: operator.tigera.io/v1
# kind: APIServer
# metadata:
# name: default
# spec: {}
66 changes: 66 additions & 0 deletions roles/k3s/tasks/server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,69 @@
src: /usr/local/bin/k3s
dest: /usr/local/bin/crictl
state: link

- name: Install Calico for k3s
when: k3s_calico
block:
- name: Check if custom Calico resources are defined
ansible.builtin.set_fact:
custom_resources_defined: "{{ k3s_calico_custom_resources is defined and k3s_calico_custom_resources | trim | length > 0 }}"

- name: Create calico directory
ansible.builtin.file:
path: "{{ k3s_server_location }}/server/calico"
state: directory
owner: root
group: root
mode: "0755"

- name: Download tigera-operator.yaml
ansible.builtin.get_url:
url: "https://raw.githubusercontent.com/projectcalico/calico/{{ k3s_calico_version }}/manifests/tigera-operator.yaml"
dest: "{{ k3s_server_location }}/server/calico/tigera-operator.{{ k3s_calico_version }}.yaml"
owner: root
group: root
mode: "0644"
register: download_tigera_operator

- name: Remove existing custom resources

Check failure on line 121 in roles/k3s/tasks/server.yaml

View workflow job for this annotation

GitHub Actions / job

ignore-errors

Use failed_when and specify error conditions instead of using ignore_errors.

Check failure on line 121 in roles/k3s/tasks/server.yaml

View workflow job for this annotation

GitHub Actions / job

no-changed-when

Commands should not change things if nothing needs doing.
ansible.builtin.command:
cmd: k3s kubectl delete -f {{ k3s_server_location }}/server/calico/custom-resources.yaml
ignore_errors: true

- name: Apply tigera-operator.yaml with --force-conflicts

Check failure on line 126 in roles/k3s/tasks/server.yaml

View workflow job for this annotation

GitHub Actions / job

no-changed-when

Commands should not change things if nothing needs doing.

Check failure on line 126 in roles/k3s/tasks/server.yaml

View workflow job for this annotation

GitHub Actions / job

no-handler

Tasks that run when changed should likely be handlers.
ansible.builtin.command:
cmd: >
k3s kubectl apply --server-side --force-conflicts -f
{{ k3s_server_location }}/server/calico/tigera-operator.{{ k3s_calico_version }}.yaml
when: download_tigera_operator.changed
register: apply_tigera_operator
retries: 5
delay: 10
until: apply_tigera_operator is succeeded

- name: Write custom resources to file
ansible.builtin.copy:
dest: "{{ k3s_server_location }}/server/calico/custom-resources.yaml"
content: "{{ k3s_calico_custom_resources }}"
mode: "0644"
when: custom_resources_defined

- name: Download default custom-resources.yaml
ansible.builtin.get_url:
url: "https://raw.githubusercontent.com/projectcalico/calico/{{ k3s_calico_version }}/manifests/custom-resources.yaml"
dest: "{{ k3s_server_location }}/server/calico/custom-resources.yaml"
owner: root
group: root
mode: "0644"
when: not custom_resources_defined

- name: Apply custom-resources.yaml

Check failure on line 153 in roles/k3s/tasks/server.yaml

View workflow job for this annotation

GitHub Actions / job

no-changed-when

Commands should not change things if nothing needs doing.
ansible.builtin.command:
cmd: >
k3s kubectl apply --server-side -f
{{ k3s_server_location }}/server/calico/custom-resources.yaml
register: apply_custom_resources
retries: 5
delay: 10
until: apply_custom_resources is succeeded
2 changes: 1 addition & 1 deletion roles/k3s/templates/k3s-server.service.j2
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ After=network-online.target
Type=notify
ExecStartPre=-/sbin/modprobe br_netfilter
ExecStartPre=-/sbin/modprobe overlay
ExecStart=/usr/local/bin/k3s server --data-dir {{ k3s_server_location }} {{ k3s_server_extra_args | default("") }}
ExecStart=/usr/local/bin/k3s server --data-dir {{ k3s_server_location }} {{ k3s_server_extra_args | default("") }}{% if k3s_calico %} --flannel-backend=none --disable-network-policy{% endif %}{{ "" }}

KillMode=process
Delegate=yes
Expand Down
Loading