Files
oc-k8s/oc-k8s.sh
2026-01-06 08:23:16 +01:00

426 lines
12 KiB
Bash
Executable File

#!/bin/bash
REPOS=(
"oc-auth"
"oc-catalog"
"oc-datacenter"
"oc-front"
"oc-monitord"
"oc-peer"
"oc-shared"
"oc-scheduler"
"oc-schedulerd"
"oc-workflow"
"oc-workspace"
)
main_replace_db() {
FILES=$(ls $1 | grep .json)
RELEASE=${2:-dev}
DB_NAME=${3:-opencloud}
POD_NAME=$(kubectl get pods --all-namespaces -o=name | grep $RELEASE-mongodb-*)
main_delete_db
main_install_db
}
main_delete_db() {
FILES=$(ls $1 | grep .json)
RELEASE=${2:-dev}
DB_NAME=${3:-opencloud}
POD_NAME=$(kubectl get pods --all-namespaces -o=name | grep $RELEASE-mongodb-*)
kubectl exec ${POD_NAME/pod\//}: -- mongosh --eval "db.getSiblingDB('$DB_NAME').dropDatabase()"
}
main_install_db() {
FILES=$(ls $1 | grep .json)
RELEASE=${2:-dev}
DB_NAME=${3:-opencloud}
POD_NAME=$(kubectl get pods --all-namespaces -o=name | grep $RELEASE-mongodb-*)
for file in "${FILES[@]}"; do
echo "ADD file $file in collection ${file/.json/} : ${POD_NAME/pod\//}"
kubectl cp $file ${POD_NAME/pod\//}:/tmp/$file
kubectl exec ${POD_NAME/pod\//}: -- mongoimport --db $DB_NAME --collection ${file/.json/} --file /tmp/$file --jsonArray
done
}
main_install() {
main_install_k3s
main_install_kind ${@:1}
main_install_helm
}
main_install_k3s() {
sudo /usr/local/bin/k3s-uninstall.sh | true
sudo rm -rf /etc/rancher /var/lib/rancher ~/.kube | true
curl -sfL https://get.k3s.io | sh -s - --write-kubeconfig-mode 644
sudo systemctl status k3s
}
main_install_helm() {
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash # install helm
helm version
}
main_install_kind() {
ARCH=${1:-linux-amd64} # linux-amd64 linux-arm64 darwin-amd64 darwin-arm64 windows-amd64.exe
VERSION=${2:-v0.30.0}
if [[ "$ARCH" =~ *windows* ]]; then
ARCH=${ARCH}.exe
fi
curl -Lo kind-linux-amd64 https://kind.sigs.k8s.io/dl/${VERSION}/kind-${ARCH}
}
# values template
main_create_values() {
set -euo pipefail
if [[ -z "${1:-}" ]]; then
echo "Error: No RELEASE PROVIDED."
main_help_values
exit 1
fi
TEMPLATE_FILE=./opencloud/values.yaml.template
ENV_FILE=${2:-}
OUTPUT_FILE="./opencloud/values/$1-values.yaml"
# Load environment variables from env file
if [[ -f "$ENV_FILE" ]]; then
set -a
source "$ENV_FILE"
set +a
fi
export RELEASE=$1
# Process the template
awk '
{
line = $0
# match ${VAR:-default} patterns
while (match(line, /\$\{([A-Za-z_][A-Za-z0-9_]*):-([^}]+)\}/, arr)) {
varname = arr[1]
defaultval = arr[2]
# get environment value or default
cmd = "bash -c '\''echo ${" varname ":-" defaultval "}'\''"
cmd | getline value
close(cmd)
line = substr(line, 1, RSTART-1) value substr(line, RSTART+RLENGTH)
}
print line
}' "$TEMPLATE_FILE" > "$OUTPUT_FILE"
echo "Rendered $OUTPUT_FILE from $TEMPLATE_FILE using $ENV_FILE"
}
# HELM SERVICE
main_create_helm() {
RELEASE_NAME=${1:-dev}
RELEASE_NAMESPACE=${1:-dev}
main_delete_helm "${1:-dev}" | true
helm dependency update
helm dependency build
kubectl delete sc longhorn-nor1
#kubectl apply -f ./opencloud/templates/sc-longhorn-nor1.yaml
#kubectl label storageclass longhorn-nor1 app.kubernetes.io/managed-by=Helm
#kubectl annotate storageclass longhorn-nor1 \
# meta.helm.sh/release-name=${RELEASE_NAMESPACE} \
# meta.helm.sh/release-namespace=${RELEASE_NAMESPACE}
ulimit -n 1000000
helm install ${RELEASE_NAME} opencloud -n ${RELEASE_NAMESPACE} --create-namespace -f ./opencloud/values/${RELEASE_NAME}-values.yaml --debug
kind get kubeconfig --name opencloud > ./deployed_config
kind export logs ./kind-logs
}
main_upgrade_helm() {
RELEASE_NAME=${1:-dev}
RELEASE_NAMESPACE=${1:-dev}
helm upgrade ${RELEASE_NAME} opencloud -n ${RELEASE_NAMESPACE} --create-namespace -f ./opencloud/values/${RELEASE_NAME}-values.yaml
}
main_delete_helm() {
RELEASE_NAME=${1:-dev}
RELEASE_NAMESPACE=${1:-dev}
helm uninstall ${RELEASE_NAME} -n ${RELEASE_NAMESPACE}
kubectl delete namespace ${RELEASE_NAMESPACE} &
export KUBECONFIG=$(realpath ~/.kube/config)
}
# CLUSTER SERVICE
build_service() {
local repo_url="https://cloud.o-forge.io/core/$1.git"
local branch=${2:-main}
local target=${3:-all}
local repo_name=$(basename "$repo_url" .git)
server=$(grep 'server:' ~/.kube/config | awk '{print $2}')
host=$(ip -4 addr show $(ip route | awk '/default/ {print $5}') | awk '/inet / {print $2}' | cut -d/ -f1)
port=6443
ca=$(kubectl config view --raw --minify -o jsonpath='{.clusters[0].cluster.certificate-authority-data}')
cert=$(kubectl config view --raw --minify -o jsonpath='{.users[0].user.client-certificate-data}')
key=$(kubectl config view --raw --minify -o jsonpath='{.users[0].user.client-key-data}')
echo "Processing repository: $repo_name"
if [ ! -d "$1" ]; then
echo "Cloning repository: $repo_name"
git clone "$repo_url"
if [ $? -ne 0 ]; then
echo "Error cloning $repo_url"
exit 1
fi
fi
echo "Repository '$repo_name' now exists. Pulling latest changes..."
cd "$repo_name" && git checkout $branch && git pull
echo "Running 'make $target' in $repo_name"
export HOST="${2:-http://beta.opencloud.com/}" && export KUBERNETES_SERVICE_HOST=$host && export KUBERNETES_SERVICE_PORT=$port \
&& export KUBE_CA=$ca && export KUBE_CERT=$cert && export KUBE_DATA=$key && make "$target"
if [ $? -ne 0 ]; then
echo "Error: make $target failed in $dir"
exit 1
fi
cd ..
}
main_build_services() {
branch=${1:-main}
target=${2:-all}
# docker system prune -af
cd ..
# Iterate through each repository in the list
for repo in "${REPOS[@]}"; do
build_service "$repo" "$branch" "$target"
done
echo "All repositories processed successfully."
}
# CLUSTER CONTROLLER
main_delete_cluster() {
kind delete cluster --name opencloud | true
}
main_create_cluster() {
main_delete_cluster | true
kubectl apply -f https://raw.githubusercontent.com/longhorn/longhorn/v1.9.0/deploy/longhorn.yaml
cat <<EOF | kind create cluster --name opencloud --config=-
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
kubeadmConfigPatches:
- |
kind: InitConfiguration
nodeRegistration:
kubeletExtraArgs:
node-labels: "ingress-ready=true"
extraPortMappings:
- containerPort: 30950
hostPort: 80
protocol: TCP
- containerPort: 30951
hostPort: 443
protocol: TCP
containerdConfigPatches:
- |-
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."locahost:5000"]
endpoint = ["http://dev-docker-registry-ui-registry-server.opencloud.svc.cluster.local:5000"]
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."dev-docker-registry-ui-registry-server.opencloud.svc.cluster.local:5000"]
endpoint = ["http://dev-docker-registry-ui-registry-server.opencloud.svc.cluster.local:5000"]
[plugins."io.containerd.grpc.v1.cri".registry.configs."dev-docker-registry-ui-registry-server.opencloud.svc.cluster.local:5000".tls]
insecure_skip_verify = true
cert_file = ""
key_file = ""
ca_file = ""
EOF
echo "[WARNING] New cluster shoulw be merged into your current config !"
# Export the kind cluster kubeconfig to a temporary file
kind get kubeconfig --name opencloud > /tmp/kind-opencloud.kubeconfig
# Merge the temporary kubeconfig with your existing one safely
KUBECONFIG=~/.kube/config:/tmp/kind-opencloud.kubeconfig kubectl config view --flatten --merge --minify > /tmp/merged-kubeconfig.yaml
# Replace the original kubeconfig safely
mv /tmp/merged-kubeconfig.yaml ~/.kube/config
chmod 600 ~/.kube/config
# Verify the contexts
kubectl config get-contexts
# Switch to the new kind cluster context
kubectl config use-context kind-opencloud
}
main_help_k3s() {
echo "
Cluster commands: oc-k8s <action> k3s
install - Install k3s
help - Show this help message
Usage:
oc-k8s install k3s
oc-k8s help values
"
}
main_help_kind() {
echo "
Cluster commands: oc-k8s <action> kind
install - Install kind
help - Show this help message
Usage:
oc-k8s install kind [arch] [version]
arch - Arch of OS (required)
kind_version - version of kind (required)
oc-k8s help values
"
}
main_help_values() {
echo "
Cluster commands: oc-k8s <action> values
create - Create a new values release yaml
help - Show this help message
Usage:
oc-k8s create values [release] [env_file (optionnal)]
release - Release values name (required)
env_file - env to map (optionnal)
oc-k8s help values
"
}
main_help_db() {
echo "
Cluster commands: oc-k8s <action> cluster
create - Add datas in db
replace - Replace datas in db
delete - Delete datas in db
help - Show this help message
Usage:
oc-k8s create db [file_path] [release] [db_name]
file_path - Datas folder files path (required)
release - Release values name (default: dev)
db_name - db name (default: opencloud)
oc-k8s replace db [file_path] [release] [db_name]
file_path - Datas folder files path (required)
release - Release values name (default: dev)
db_name - db name (default: opencloud)
oc-k8s delete db [file_path] [release] [db_name]
file_path - Datas folder files path (required)
release - Release values name (default: dev)
db_name - db name (default: opencloud)
oc-k8s help db
"
}
main_help_cluster() {
echo "
Cluster commands: oc-k8s <action> cluster
create - Create a new kind cluster named 'opencloud'
delete - Delete the kind cluster named 'opencloud'
help - Show this help message
Usage:
oc-k8s create cluster
oc-k8s delete cluster
oc-k8s help cluster
"
}
main_help_services() {
echo "
Service commands: oc-k8s <action> services
build - Build all opencloud services
help - Show this help message
Usage:
oc-k8s build services [branch] [target]
branch - Git branch to build (default: main)
target - make target (default: all)
oc-k8s help services
"
}
main_help_helm() {
echo "
Helm commands: oc-k8s <action> helm
install - Install Helm
create - Install a helm release for the given environment (default: dev)
delete - Uninstall a helm release for the given environment (default: dev)
help - Show this help message
Usage:
oc-k8s install helm
oc-k8s create helm [env]
env - environnement selected (default: dev)
oc-k8s upgrade helm [env]
env - environnement selected (default: dev)
oc-k8s delete helm [env]
env - environnement selected (default: dev)
oc-k8sh help helm
"
}
main_help_all() {
echo "
Main commands: oc-k8s <action>
install - Install opencloud dependancies [arch] [version]
start - Start opencloud k8s
stop - Stop opencloud k8s
Usage:
oc-k8s install [arch] [version]
arch - Arch of OS (required)
kind_version - version of kind (required)
oc-k8s start [env] [branch] [target]
env - environnement selected (default: dev)
branch - Git branch to build (default: main)
target - make target (default: all)
oc-k8s stop
"
main_help_cluster
main_help_services
main_help_helm
main_help_values
main_help_k3s
main_help_kind
main_help_db
}
main_start() {
sudo sysctl -w fs.inotify.max_user_instances=256
sudo /etc/init.d/apache2 stop
sudo nginx -s stop
main_create_cluster
main_build_services "${@:2}"
cd ./oc-k8s
main_create_helm $1
}
main_stop() {
main_delete_helm "${@:1}" | true
main_delete_cluster "${@:1}" | true
}
if declare -f main_${1} > /dev/null; then
main_${1} "${@:2}"
elif declare -f main_${1}_${2} > /dev/null; then
main_${1}_${2} "${@:3}"
else
echo "Function does not exist"
main_help_all
fi