Skip to content

Instantly share code, notes, and snippets.

@gabihodoroaga
Last active November 10, 2021 20:42
Show Gist options
  • Save gabihodoroaga/eb0f9fc0d2681f20239fef22d7479f2e to your computer and use it in GitHub Desktop.
Save gabihodoroaga/eb0f9fc0d2681f20239fef22d7479f2e to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
echo "setup variables..."
ZONE=us-central1-a
CLUSTER_NAME=demo-gce-cluster
PROJECT_ID=`gcloud config list --format 'value(core.project)' 2>/dev/null`
REGISTRY="gcr.io/$PROJECT_ID"
GCP_USER=`gcloud config list --format 'value(core.account)' 2>/dev/null`
echo "download files..."
curl https://codeload.github.com/gist/a5644451e1d309ad40b1312eb2369fe6/zip/70d0ec9fd78834ba4662ac7ee1ca46584d3d026c \
--output deploy-ingress-gce.zip
unzip deploy-ingress-gce.zip
mv a5644451e1d309ad40b1312eb2369fe6-70d0ec9fd78834ba4662ac7ee1ca46584d3d026c \
deploy-ingress-gce
echo "create cluster..."
gcloud container clusters \
create $CLUSTER_NAME \
--zone $ZONE --machine-type "n2-standard-2" \
--enable-ip-alias \
--num-nodes=1
gcloud container clusters get-credentials $CLUSTER_NAME \
--zone $ZONE
kubectl create clusterrolebinding one-binding-to-rule-them-all \
--clusterrole=cluster-admin --user=${GCP_USER}
NODE_PORT=`kubectl get svc default-http-backend -n kube-system -o yaml \
| grep "nodePort:" | cut -f2- -d:`
echo $NODE_PORT
echo "disable native plugin..."
gcloud container clusters update ${CLUSTER_NAME} --zone=${ZONE} \
--update-addons=HttpLoadBalancing=DISABLED
echo "waiting for old GLBC service and pod to be removed..."
set +e
while true; do
kubectl get svc -n kube-system | grep default-http-backend &>/dev/null
if [[ $? -eq 1 ]];
then
break
fi
sleep 10
done
# Wait till old glbc pod is removed
while true; do
kubectl get pod -n kube-system | grep default-backend &>/dev/null
if [[ $? -eq 1 ]];
then
break
fi
sleep 10
done
set -e
echo "create new services"
kubectl apply -f deploy-ingress-gce/rbac.yaml
sed "s/\[NODE_PORT\]/$NODE_PORT/" deploy-ingress-gce/default-http-backend.yaml.tpl \
> deploy-ingress-gce/default-http-backend.yaml
kubectl create -f deploy-ingress-gce/default-http-backend.yaml
gcloud iam service-accounts create glbc-service-account \
--display-name "Service Account for GLBC"
gcloud projects add-iam-policy-binding ${PROJECT_ID} \
--member serviceAccount:glbc-service-account@${PROJECT_ID}.iam.gserviceaccount.com \
--role roles/compute.admin
gcloud iam service-accounts keys create key.json \
--iam-account glbc-service-account@${PROJECT_ID}.iam.gserviceaccount.com
kubectl create secret generic glbc-gcp-key \
--from-file=key.json
kubectl apply -f deploy-ingress-gce/theia.yaml
echo "waiting for theia-golang to be ready"
kubectl wait --for=condition=available --timeout=60s deployment/theia-golang
THEIA_NODE_PORT=$(kubectl get service theia-golang-service \
-o=jsonpath='{.spec.ports[?(@.name=="ide")].nodePort}')
NODE_ADDRESS=$(kubectl get nodes \
-o jsonpath='{.items[0].status.addresses[?(@.type=="ExternalIP")].address}')
GKE_NETWORK_TAG=$(gcloud compute instances describe \
$(kubectl get nodes -o jsonpath='{.items[0].metadata.name}') \
--zone=$ZONE --format="value(tags.items[0])")
# this must be your current address - not the cloud shell address
SHELL_IP_ADDRESS=$(curl http://ifconfig.me)
gcloud compute firewall-rules create allow-theia-golang \
--direction=INGRESS --priority=1000 --network=default \
--action=ALLOW --rules=tcp:$THEIA_NODE_PORT \
--source-ranges=$SHELL_IP_ADDRESS \
--target-tags=$GKE_NETWORK_TAG
NETWORK_NAME=$(basename $(gcloud container clusters \
describe $CLUSTER_NAME --project $PROJECT_ID --zone=$ZONE \
--format='value(networkConfig.network)'))
SUBNETWORK_NAME=$(basename $(gcloud container clusters \
describe $CLUSTER_NAME --project $PROJECT_ID \
--zone=$ZONE --format='value(networkConfig.subnetwork)'))
NETWORK_TAGS=$(gcloud compute instances describe \
$(kubectl get nodes -o jsonpath='{.items[0].metadata.name}') \
--zone=$ZONE --format="value(tags.items[0])")
sed "s/\[PROJECT\]/$PROJECT_ID/" deploy-ingress-gce/gce.conf.tpl | \
sed "s/\[NETWORK\]/$NETWORK_NAME/" | \
sed "s/\[SUBNETWORK\]/$SUBNETWORK_NAME/" | \
sed "s/\[CLUSTER_NAME\]/$CLUSTER_NAME/" | \
sed "s/\[NETWORK_TAGS\]/$NETWORK_TAGS/" | \
sed "s/\[ZONE\]/$ZONE/" > deploy-ingress-gce/gce.conf
kubectl cp deploy-ingress-gce/gce.conf \
$(kubectl get pods --selector=app=theia-golang \
--output=jsonpath={.items..metadata.name}):/home/project
kubectl exec $(kubectl get pods --selector=app=theia-golang \
--output=jsonpath={.items..metadata.name}) -- \
git clone https://github.com/kubernetes/ingress-gce.git \
/home/project/ingress-gce
echo http://$NODE_ADDRESS:$THEIA_NODE_PORT
echo "Done. Open a browser at the above address."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment