53 lines
2.0 KiB
YAML
53 lines
2.0 KiB
YAML
name: Setup K8S Environment
|
|
description: Prepare the environment for A13labs CI/CD pipelines
|
|
inputs:
|
|
vault-master-password:
|
|
description: "The master password for the vault"
|
|
required: true
|
|
ansible-user:
|
|
description: "The user to use for Ansible"
|
|
required: true
|
|
k8s-target:
|
|
description: "The target host to deploy the K8S applications"
|
|
default: "microk8s"
|
|
|
|
runs:
|
|
using: "composite"
|
|
steps:
|
|
- name: Copy Kube Config from remote host
|
|
shell: bash
|
|
env:
|
|
VAULT_MASTER_PASSWORD: ${{ inputs.vault-master-password }}
|
|
ANSIBLE_USER: ${{ inputs.ansible-user }}
|
|
SSH_AUTH_SOCK: /tmp/ssh_agent.sock
|
|
K8S_TARGET: ${{ inputs.k8s-target }}
|
|
run: |
|
|
mkdir -p ~/.kube
|
|
ansible -i ansible/inventory/hosts -u $ANSIBLE_USER -m fetch -a "src=/home/$ANSIBLE_USER/.kube/config dest=~/.kube/config flat=yes" $K8S_TARGET
|
|
|
|
- name: Terraform Init
|
|
shell: bash
|
|
env:
|
|
VAULT_MASTER_PASSWORD: ${{ inputs.vault-master-password }}
|
|
run: |
|
|
cd terraform/k8sapps
|
|
sectool exec terraform init
|
|
|
|
- name: Terraform Apply
|
|
shell: bash
|
|
env:
|
|
VAULT_MASTER_PASSWORD: ${{ inputs.vault-master-password }}
|
|
ANSIBLE_USER: ${{ inputs.ansible-user }}
|
|
SSH_AUTH_SOCK: /tmp/ssh_agent.sock
|
|
K8S_TARGET: ${{ inputs.k8s-target }}
|
|
run: |
|
|
# Get Hostname and IP address of the target
|
|
ANSIBLE_HOST=$(ansible -i ansible/inventory/hosts -u $ANSIBLE_USER -m shell -a "hostname" $K8S_TARGET | grep -oP '(\w+\.){2}\w+' | head -1)
|
|
IP_ADDR=$(ansible -i ansible/inventory/hosts -u $ANSIBLE_USER -m shell -a "curl -s ifconfig.me" $K8S_TARGET | grep -oP '(\d+\.){3}\d+')
|
|
|
|
# Forward the k8s API server port to localhost
|
|
ssh -f -N -L 16443:localhost:16443 $ANSIBLE_USER@$ANSIBLE_HOST
|
|
|
|
# Override the kube config file to use localhost
|
|
sed -i "s/server: https:\/\/$IP_ADDR:16443/server: https:\/\/localhost:16443/g" ~/.kube/config
|