Skip to content

Commit

Permalink
Autogenerate deployment YAML for cluster sharding
Browse files Browse the repository at this point in the history
  • Loading branch information
mgianluc committed Oct 24, 2023
1 parent 1b5cde2 commit 6d5d7fe
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ manifests: $(CONTROLLER_GEN) $(KUSTOMIZE) $(ENVSUBST) fmt generate ## Generate W
$(CONTROLLER_GEN) rbac:roleName=controller-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
MANIFEST_IMG=$(CONTROLLER_IMG)-$(ARCH) MANIFEST_TAG=$(TAG) $(MAKE) set-manifest-image
$(KUSTOMIZE) build config/default | $(ENVSUBST) > manifest/manifest.yaml
./scripts/extract_deployment.sh manifest/manifest.yaml manifest/deployment-shard.yaml

.PHONY: generate
generate: $(CONTROLLER_GEN) ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
Expand Down Expand Up @@ -203,8 +204,10 @@ fv: $(KUBECTL) $(GINKGO) ## Run Sveltos Controller tests using existing cluster

.PHONY: fv-sharding
fv-sharding: $(KUBECTL) $(GINKGO) ## Run Sveltos Controller tests using existing cluster
$(KUBECTL) patch cluster clusterapi-workload -n default --type json -p '[{ "op": "add", "path": "/metadata/annotations/sharding.projectsveltos.io~1key", "value": "shard1" }]'
$(KUBECTL) apply -f test/addon-controller-shard.yaml
$(KUBECTL) patch cluster clusterapi-workload -n default --type json -p '[{ "op": "add", "path": "/metadata/annotations/sharding.projectsveltos.io~1key", "value": "shard1" }]'
sed -e "s/{{.SHARD}}/shard1/g" manifest/deployment-shard.yaml > test/addon-controller-deployment-shard.yaml
$(KUBECTL) apply -f test/addon-controller-deployment-shard.yaml
rm -f test/addon-controller-deployment-shard.yaml
cd test/fv; $(GINKGO) -nodes $(NUM_NODES) --label-filter='FV' --v --trace --randomize-all

.PHONY: create-cluster
Expand Down
1 change: 1 addition & 0 deletions config/default/manager_auth_proxy_patch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ spec:
- "--health-probe-bind-address=:8081"
- "--metrics-bind-address=127.0.0.1:8080"
- "--report-mode=0"
- --shard-key=
- "--v=5"
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ kind: Deployment
metadata:
labels:
control-plane: addon-controller
name: addon-controller-shard1
name: addon-controller-{{.SHARD}}
namespace: projectsveltos
spec:
replicas: 1
Expand All @@ -22,8 +22,8 @@ spec:
- --health-probe-bind-address=:8081
- --metrics-bind-address=127.0.0.1:8080
- --report-mode=0
- --shard-key={{.SHARD}}
- --v=5
- --shard-key=shard1
command:
- /manager
image: projectsveltos/addon-controller-amd64:dev
Expand Down
1 change: 1 addition & 0 deletions manifest/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2135,6 +2135,7 @@ spec:
- --health-probe-bind-address=:8081
- --metrics-bind-address=127.0.0.1:8080
- --report-mode=0
- --shard-key=
- --v=5
command:
- /manager
Expand Down
48 changes: 48 additions & 0 deletions scripts/extract_deployment.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash

# Define the YAML file path
yaml_file=$1
output_file=$2

# Create a temporary directory to store the split sections
mkdir -p temp_yaml_sections

# Initialize variables for section counting and flag to track changes
section_count=0
in_section=false

# Iterate through the YAML file
while IFS= read -r line; do
if [[ $line == '---' ]]; then
# Start a new section
in_section=true
section_count=$((section_count + 1))
current_section_file="temp_yaml_sections/section_${section_count}.yaml"
continue
fi

if [[ $in_section == true ]]; then
# Replace "shard-key=" with "shard-key='shard1'"
if [[ $line == *"shard-key"* ]]; then
line=$(echo "$line" | sed "s/shard-key=/shard-key={{.SHARD}}/")
fi

# Replace "name" to contain shard info
if [[ $line == *"name: addon-controller"* ]]; then
line=$(echo "$line" | sed "s/addon-controller/addon-controller-{{.SHARD}}/")
fi

# Write the line to the current section file
echo "$line" >> "$current_section_file"
fi
done < "$yaml_file"

# Iterate through the split sections and print those with "kind: Deployment"
for section_file in temp_yaml_sections/*.yaml; do
if grep -q "kind: Deployment" "$section_file"; then
cat "$section_file" > $output_file
fi
done

# Remove the temporary directory
rm -r temp_yaml_sections

0 comments on commit 6d5d7fe

Please sign in to comment.