├── README.md ├── cloudbuild.yaml └── kubernetes ├── deployments └── helloworld.yaml └── services └── helloworld.yaml /README.md: -------------------------------------------------------------------------------- 1 | # helloworld-infrastructure-qa 2 | 3 | This repo holds the Kubernetes configuration files for the helloworld application. 4 | 5 | ## Target Environment 6 | 7 | QA 8 | 9 | ## Usage 10 | 11 | Changes pushed to the master branch should trigger the following actions: 12 | 13 | - recursive application of the configuration files located under the kubernetes directory 14 | - issue a pull request to the [helloworld-infrastructure-production](https://github.com/kelseyhightower/helloworld-infrastructure-production) repo which updates the container image for the helloworld deployment to match the container image deployed to QA. 15 | 16 | See the [cloudbuild.yaml](cloudbuild.yaml) file for more details. 17 | -------------------------------------------------------------------------------- /cloudbuild.yaml: -------------------------------------------------------------------------------- 1 | # 2 | # User-defined substitutions: 3 | # _CLOUDSDK_COMPUTE_ZONE 4 | # _CLOUDSDK_CONTAINER_CLUSTER 5 | # _GITHUB_USERNAME 6 | # _KMS_KEY 7 | # _KMS_KEYRING 8 | # 9 | 10 | steps: 11 | # Generate a kubeconfig file 12 | - name: 'gcr.io/cloud-builders/gcloud' 13 | env: 14 | - 'CLOUDSDK_COMPUTE_ZONE=${_CLOUDSDK_COMPUTE_ZONE}' 15 | - 'CLOUDSDK_CONTAINER_CLUSTER=${_CLOUDSDK_CONTAINER_CLUSTER}' 16 | - 'KUBECONFIG=/kube/config' 17 | entrypoint: 'sh' 18 | args: 19 | - '-c' 20 | - | 21 | CLUSTER=$$(gcloud config get-value container/cluster) 22 | PROJECT=$$(gcloud config get-value core/project) 23 | ZONE=$$(gcloud config get-value compute/zone) 24 | 25 | gcloud container clusters get-credentials "$${CLUSTER}" \ 26 | --project "$${PROJECT}" \ 27 | --zone "$${ZONE}" 28 | volumes: 29 | - name: 'kube' 30 | path: /kube 31 | 32 | # Retrieve and decrypt the GitHub Hub configuration. 33 | - name: 'gcr.io/cloud-builders/gcloud' 34 | entrypoint: 'sh' 35 | args: 36 | - '-c' 37 | - | 38 | gsutil cp gs://${PROJECT_ID}-helloworld-configs/hub.enc hub.enc 39 | 40 | gcloud kms decrypt \ 41 | --ciphertext-file=hub.enc \ 42 | --plaintext-file=/config/hub \ 43 | --location=global \ 44 | --keyring=${_KMS_KEYRING} \ 45 | --key=${_KMS_KEY} 46 | volumes: 47 | - name: 'config' 48 | path: /config 49 | 50 | # Update the Kubernetes deployment config. 51 | - name: 'gcr.io/cloud-builders/gcloud' 52 | env: 53 | - 'KUBECONFIG=/kube/config' 54 | entrypoint: 'sh' 55 | args: 56 | - '-c' 57 | - | 58 | kubectl apply --recursive -f kubernetes 59 | volumes: 60 | - name: 'kube' 61 | path: /kube 62 | 63 | # Create a pull request on the ${_GITHUB_USERNAME}/helloworld-infrastructure-production repo 64 | # to update the helloworld deployment configuration. 65 | - name: 'gcr.io/hightowerlabs/hub' 66 | env: 67 | - 'HUB_CONFIG=/config/hub' 68 | - 'KUBECONFIG=/kube/config' 69 | entrypoint: 'sh' 70 | args: 71 | - '-c' 72 | - | 73 | # Extract the container image from the qa helloworld deployment configuration file. 74 | CONTAINER_IMAGE=$(kubectl apply --dry-run \ 75 | -f kubernetes/deployments/helloworld.yaml \ 76 | -o jsonpath='{.spec.template.spec.containers[?(@.name == "helloworld")].image}') 77 | 78 | ACTIVE_ACCOUNT=$(gcloud auth list --filter=status:ACTIVE --format="value(account)") 79 | 80 | hub config --global credential.https://github.com.helper /usr/local/bin/hub-credential-helper 81 | hub config --global hub.protocol https 82 | hub config --global user.email "$${ACTIVE_ACCOUNT}" 83 | hub config --global user.name "Google Container Builder" 84 | 85 | 86 | # Clone the ${_GITHUB_USERNAME}/helloworld-infrastructure-production repo 87 | # and patch the helloworld deployment container image. 88 | hub clone "https://github.com/${_GITHUB_USERNAME}/helloworld-infrastructure-production.git" 89 | 90 | cat < patch.yaml 91 | spec: 92 | template: 93 | spec: 94 | containers: 95 | - name: helloworld 96 | image: $${CONTAINER_IMAGE} 97 | EOF 98 | 99 | kubectl patch --local -o yaml \ 100 | -f helloworld-infrastructure-production/kubernetes/deployments/helloworld.yaml \ 101 | -p "$(cat patch.yaml)" \ 102 | > helloworld.yaml 103 | 104 | mv helloworld.yaml helloworld-infrastructure-production/kubernetes/deployments/helloworld.yaml 105 | 106 | # Change to the helloworld-infrastructure-production directory 107 | # commit the local changes, then issue a pull request. 108 | cd helloworld-infrastructure-production 109 | 110 | git checkout -b update-deployment-${BUILD_ID} 111 | 112 | hub add kubernetes/deployments/helloworld.yaml 113 | 114 | hub commit -F- <