Skip to content

Commit

Permalink
Merge pull request #2505 from Tommy12789/2456
Browse files Browse the repository at this point in the history
Adding NodeSelection for Controller Shards
  • Loading branch information
manno authored Jun 26, 2024
2 parents de3f46a + c301686 commit 71fcf47
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 67 deletions.
23 changes: 21 additions & 2 deletions .github/scripts/deploy-fleet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -euxo pipefail

shards=${SHARDS-""}
shards_json=${SHARDS-""}

function eventually {
for _ in $(seq 1 3); do
Expand All @@ -28,6 +28,25 @@ host=$(kubectl get node k3d-upstream-server-0 -o jsonpath='{.status.addresses[?(
ca=$( kubectl config view --flatten -o jsonpath='{.clusters[?(@.name == "k3d-upstream")].cluster.certificate-authority-data}' | base64 -d )
server="https://$host:6443"

# Constructing the shards settings dynamically
shards_settings=""
if [ -n "$shards_json" ]; then
index=0
for shard in $(echo "${shards_json}" | jq -c '.[]'); do
shard_id=$(echo "$shard" | jq -r '.id')
shards_settings="$shards_settings --set shards[$index].id=$shard_id"
node_selector=$(echo "$shard" | jq -r '.nodeSelector // empty')
if [ -n "$node_selector" ]; then
for key in $(echo "$node_selector" | jq -r 'keys[]'); do
value=$(echo "$node_selector" | jq -r --arg key "$key" '.[$key]')
escaped_key=$(echo "$key" | sed 's/\./\\./g')
shards_settings="$shards_settings --set shards[$index].nodeSelector.\"$escaped_key\"=$value"
done
fi
index=$((index + 1))
done
fi

eventually helm upgrade --install fleet-crd charts/fleet-crd \
--atomic \
-n cattle-fleet-system \
Expand All @@ -43,7 +62,7 @@ eventually helm upgrade --install fleet charts/fleet \
--set agentImage.imagePullPolicy=IfNotPresent \
--set apiServerCA="$ca" \
--set apiServerURL="$server" \
--set shards="{$shards}" \
$shards_settings \
--set debug=true --set debugLevel=1

# wait for controller and agent rollout
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/aks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ jobs:
run: |
export KUBECONFIG="$GITHUB_WORKSPACE/kubeconfig-fleet-ci"
echo "${{ steps.meta-fleet.outputs.tags }} ${{ steps.meta-fleet-agent.outputs.tags }}"
SHARDS="shard1,shard2,shard3" ./.github/scripts/deploy-fleet.sh \
SHARDS='[{"id":"shard0"},{"id":"shard1"},{"id":"shard2"}]' ./.github/scripts/deploy-fleet.sh \
${{ steps.meta-fleet.outputs.tags }} \
${{ steps.meta-fleet-agent.outputs.tags }}
-
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/e2e-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ jobs:
-
name: Deploy Fleet
run: |
SHARDS="shard1,shard2,shard3" ./.github/scripts/deploy-fleet.sh
SHARDS='[{"id":"shard0"},{"id":"shard1"},{"id":"shard2"}]' ./.github/scripts/deploy-fleet.sh
-
name: Create Zot certificates for OCI tests
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/gke.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ jobs:
name: Deploy Fleet
run: |
echo "${{ steps.meta-fleet.outputs.tags }} ${{ steps.meta-fleet-agent.outputs.tags }}"
SHARDS="shard1,shard2,shard3" ./.github/scripts/deploy-fleet.sh \
SHARDS='[{"id":"shard0"},{"id":"shard1"},{"id":"shard2"}]' ./.github/scripts/deploy-fleet.sh \
${{ steps.meta-fleet.outputs.tags }} \
${{ steps.meta-fleet-agent.outputs.tags }}
-
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nightly-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ jobs:
-
name: Deploy Fleet
run: |
SHARDS="shard1,shard2,shard3" ./.github/scripts/deploy-fleet.sh
SHARDS='[{"id":"shard0"},{"id":"shard1"},{"id":"shard2"}]' ./.github/scripts/deploy-fleet.sh
-
name: Create Zot certificates for OCI tests
if: ${{ matrix.test_type == 'e2e-nosecrets' }}
Expand Down
38 changes: 25 additions & 13 deletions charts/fleet/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
{{ $shards := list "" }}
{{ if .Values.shards }}
{{ $shards = concat $shards .Values.shards | uniq }}
{{ end }}
{{ range $shards }}
{{- $shards := list (dict "id" "" "nodeSelector" dict) -}}
{{- $uniqueShards := list -}}
{{- if .Values.shards -}}
{{- range .Values.shards -}}
{{- if not (has .id $uniqueShards) -}}
{{- $shards = append $shards . -}}
{{- $uniqueShards = append $uniqueShards .id -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{ range $shard := $shards }}
apiVersion: apps/v1
kind: Deployment
metadata:
name: "fleet-controller{{if . }}-shard-{{ . }}{{end}}"
name: "fleet-controller{{if $shard.id }}-shard-{{ $shard.id }}{{end}}"
spec:
selector:
matchLabels:
Expand All @@ -15,8 +22,8 @@ spec:
metadata:
labels:
app: fleet-controller
fleet.cattle.io/shard-id: "{{ . }}"
{{- if empty . }}
fleet.cattle.io/shard-id: "{{ $shard.id }}"
{{- if empty $shard.id }}
fleet.cattle.io/shard-default: "true"
{{- end }}
spec:
Expand Down Expand Up @@ -89,9 +96,9 @@ spec:
{{- end }}
command:
- fleetcontroller
{{- if . }}
{{- if $shard.id }}
- --shard-id
- {{ quote . }}
- {{ quote $shard.id }}
{{- end }}
{{- if not $.Values.metrics.enabled }}
- --disable-metrics
Expand All @@ -116,7 +123,7 @@ spec:
- mountPath: /tmp/pprof
name: pprof
{{- end }}
{{- if not . }} # Only deploy cleanup and agent management through sharding-less deployment
{{- if not $shard.id }} # Only deploy cleanup and agent management through sharding-less deployment
- env:
- name: NAMESPACE
valueFrom:
Expand Down Expand Up @@ -221,11 +228,16 @@ spec:
{{- end }}

serviceAccountName: fleet-controller
nodeSelector: {{ include "linux-node-selector" . | nindent 8 }}
nodeSelector: {{ include "linux-node-selector" $shard.id | nindent 8 }}
{{- if $.Values.nodeSelector }}
{{ toYaml $.Values.nodeSelector | indent 8 }}
{{- end }}
tolerations: {{ include "linux-node-tolerations" . | nindent 8 }}
{{- if $shard.nodeSelector -}}
{{- range $key, $value := $shard.nodeSelector }}
{{ $key | indent 8}}: {{ $value }}
{{- end }}
{{- end }}
tolerations: {{ include "linux-node-tolerations" $shard.id | nindent 8 }}
{{- if $.Values.tolerations }}
{{ toYaml $.Values.tolerations | indent 8 }}
{{- end }}
Expand Down
31 changes: 19 additions & 12 deletions charts/fleet/templates/deployment_gitjob.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
{{ $shards := list "" }}
{{ if .Values.shards }}
{{ $shards = concat $shards .Values.shards | uniq }}
{{ end }}
{{ range $shards }}
{{- $shards := list (dict "id" "" "nodeSelector" dict) -}}
{{- $uniqueShards := list -}}
{{- if .Values.shards -}}
{{- range .Values.shards -}}
{{- if not (has .id $uniqueShards) -}}
{{- $shards = append $shards . -}}
{{- $uniqueShards = append $uniqueShards .id -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{ range $shard := $shards }}
{{- if $.Values.gitops.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
name: "gitjob{{if . }}-shard-{{ . }}{{end}}"
name: "gitjob{{if $shard.id }}-shard-{{ $shard.id }}{{end}}"
spec:
selector:
matchLabels:
Expand All @@ -16,8 +23,8 @@ spec:
metadata:
labels:
app: "gitjob"
fleet.cattle.io/shard-id: "{{ . }}"
{{- if empty . }}
fleet.cattle.io/shard-id: "{{ $shard.id }}"
{{- if empty $shard.id }}
fleet.cattle.io/shard-default: "true"
{{- end }}
spec:
Expand All @@ -38,9 +45,9 @@ spec:
{{- if $.Values.debug }}
- --debug
{{- end }}
{{- if . }}
{{- if $shard.id }}
- --shard-id
- {{ quote . }}
- {{ quote $shard.id }}
{{- end }}
{{- if not $.Values.metrics.enabled }}
- --disable-metrics
Expand Down Expand Up @@ -77,11 +84,11 @@ spec:
{{- if $.Values.extraEnv }}
{{ toYaml $.Values.extraEnv | indent 12}}
{{- end }}
nodeSelector: {{ include "linux-node-selector" . | nindent 8 }}
nodeSelector: {{ include "linux-node-selector" $shard.id | nindent 8 }}
{{- if $.Values.nodeSelector }}
{{ toYaml $.Values.nodeSelector | indent 8 }}
{{- end }}
tolerations: {{ include "linux-node-tolerations" . | nindent 8 }}
tolerations: {{ include "linux-node-tolerations" $shard.id | nindent 8 }}
{{- if $.Values.tolerations }}
{{ toYaml $.Values.tolerations | indent 8 }}
{{- end }}
Expand Down
23 changes: 15 additions & 8 deletions charts/fleet/templates/service.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
{{- if .Values.metrics.enabled }}
{{ $shards := list "" }}
{{ if .Values.shards }}
{{ $shards = concat $shards .Values.shards | uniq }}
{{ end }}
{{ range $shards }}
{{- $shards := list (dict "id" "" "nodeSelector" dict) -}}
{{- $uniqueShards := list -}}
{{- if .Values.shards -}}
{{- range .Values.shards -}}
{{- if not (has .id $uniqueShards) -}}
{{- $shards = append $shards . -}}
{{- $uniqueShards = append $uniqueShards .id -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{ range $shard := $shards }}
apiVersion: v1
kind: Service
metadata:
name: "monitoring-fleet-controller{{if . }}-shard-{{ . }}{{end}}"
name: "monitoring-fleet-controller{{if $shard.id }}-shard-{{ $shard.id }}{{end}}"
labels:
app: fleet-controller
spec:
Expand All @@ -19,10 +26,10 @@ spec:
name: metrics
selector:
app: fleet-controller
{{- if empty . }}
{{- if empty $shard.id }}
fleet.cattle.io/shard-default: "true"
{{- else }}
fleet.cattle.io/shard-id: "{{ . }}"
fleet.cattle.io/shard-id: "{{ $shard.id }}"
{{- end }}
---
{{- end }}
Expand Down
23 changes: 15 additions & 8 deletions charts/fleet/templates/service_gitjob.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,22 @@ spec:
app: "gitjob"
---
{{- if .Values.metrics.enabled }}
{{ $shards := list "" }}
{{ if .Values.shards }}
{{ $shards = concat $shards .Values.shards | uniq }}
{{ end }}
{{ range $shards }}
{{- $shards := list (dict "id" "" "nodeSelector" dict) -}}
{{- $uniqueShards := list -}}
{{- if .Values.shards -}}
{{- range .Values.shards -}}
{{- if not (has .id $uniqueShards) -}}
{{- $shards = append $shards . -}}
{{- $uniqueShards = append $uniqueShards .id -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{ range $shard := $shards }}
apiVersion: v1
kind: Service
metadata:
name: "monitoring-gitjob{{if . }}-shard-{{ . }}{{end}}"
name: "monitoring-gitjob{{if $shard.id }}-shard-{{ $shard.id }}{{end}}"
labels:
app: gitjob
spec:
Expand All @@ -33,10 +40,10 @@ spec:
name: metrics
selector:
app: gitjob
{{- if empty . }}
{{- if empty $shard.id }}
fleet.cattle.io/shard-default: "true"
{{- else }}
fleet.cattle.io/shard-id: "{{ . }}"
fleet.cattle.io/shard-id: "{{ $shard.id }}"
{{- end }}
---
{{- end }}
Expand Down
15 changes: 0 additions & 15 deletions charts/fleet/templates/service_gitops_metrics.yaml

This file was deleted.

12 changes: 11 additions & 1 deletion charts/fleet/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ bootstrap:
branch: master
paths: ""


global:
cattle:
systemDefaultRegistry: ""
Expand Down Expand Up @@ -108,3 +107,14 @@ controller:
# extraEnv:
# - name: EXPERIMENTAL_OCI_STORAGE
# value: "true"

# shards:
# - id: shard0
# nodeSelector:
# kubernetes.io/hostname: k3d-upstream-server-0
# - id: shard1
# nodeSelector:
# kubernetes.io/hostname: k3d-upstream-server-1
# - id: shard2
# nodeSelector:
# kubernetes.io/hostname: k3d-upstream-server-2
24 changes: 22 additions & 2 deletions dev/setup-fleet
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
set -euxo pipefail

cluster_name=${1-upstream}
shards=${2-""}
shards_json=${2:-}

if [ ! -d ./charts/fleet ]; then
echo "please change the current directory to the fleet repo checkout"
Expand All @@ -16,10 +16,30 @@ host=$( docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end
ca=$( kubectl config view --flatten -o jsonpath='{.clusters[?(@.name == "k3d-'"$cluster_name"'")].cluster.certificate-authority-data}' | base64 -d )
server="https://$host:6443"
helm -n cattle-fleet-system upgrade --install --create-namespace --wait fleet-crd charts/fleet-crd

# Constructing the shards settings dynamically
shards_settings=""
if [ -n "$shards_json" ]; then
index=0
for shard in $(echo "${shards_json}" | jq -c '.[]'); do
shard_id=$(echo "$shard" | jq -r '.id')
shards_settings="$shards_settings --set shards[$index].id=$shard_id"
node_selector=$(echo "$shard" | jq -r '.nodeSelector // empty')
if [ -n "$node_selector" ]; then
for key in $(echo "$node_selector" | jq -r 'keys[]'); do
value=$(echo "$node_selector" | jq -r --arg key "$key" '.[$key]')
escaped_key=$(echo "$key" | sed 's/\./\\./g')
shards_settings="$shards_settings --set shards[$index].nodeSelector.\"$escaped_key\"=$value"
done
fi
index=$((index + 1))
done
fi

helm -n cattle-fleet-system upgrade --install --create-namespace --wait --reset-values \
--set apiServerCA="$ca" \
--set apiServerURL="$server" \
--set shards="{$shards}" \
$shards_settings \
--set debug=true --set debugLevel=1 fleet charts/fleet

# wait for controller and agent rollout
Expand Down
Loading

0 comments on commit 71fcf47

Please sign in to comment.