Terraform Enterprise API scripts located in folder tfe-scripts helps create a new workspace with the initial configuration, folder bcgov
and user-defined configure variables, folder variables.
- Install
asdf. - Install required tools via
asdf(the versions are defined in.tool-versions).
make install_asdf_tools-
Create a new Team token via
Settings > Teams > Team API Token. -
Set environment variable
TFC_TOKENfor tfe scripts.
export TFC_TOKEN=<team-token>- Navigate to the Google Cloud Platform API Console.
- Select a project or create a new project.
- Navigate to IAM & Admin > Service Accounts page.
- Select a Service Account or create a new Service Account.
- The Service Account requires the roles
Storage Admin,Create Service Accounts,Delete Service Accounts, andService Account Key Admin.
- The Service Account requires the roles
- Create a Service Account key as JSON and download it named
credentials.jsonin the root directory.
make create_workspace org=<my-team> workspace=<my-workspace>- It skips creating a new one if the specified name of workspace already exists.
- It compresses the main TF script folder
bcgovand uploads to the workspace (non-VCS). - It sets placeholder variables from
variablesfolder.
-
Copy
example.valuesfile to.valuesfile. -
Set each value in
.valuesfile.credentials_file: the credentials file path of Google Cloud service accountkubernetes_host: the hostname of Openshift clusterkubernetes_token: the authentication token of Openshift cluster- Use
Service Account Tokensinstead ofSession Tokens, which is expiring within 24 hours by default.
- Use
kubernetes_namespaces: the list of Openshift namespaces to run jobsnamespace_apps: the list of namespace and app name pairs of Openshift cluster
-
Run TFE script to set the variables on TFC workspace.
make set_values org=<my-team> workspace=<my-workspace>make run org=<my-team> workspace=<my-workspace>- It sets
Apply MethodtoAuto applyon workspace creation to skip manual user confirmations after planning in terms of API-driven run workflow.
make sync_values org=<my-team> workspace=<my-workspace> # it downloads the current variable values from the workspace
make destroy org=<my-team> workspace=<my-workspace> # it destroys all resources created by the workspace
make delete_workspace org=<my-team> workspace=<my-workspace> # it deletes the workspacesync_valuescommand is useful to sync variablenamespace_appsvalue because it gets updated during deployment process.- It is highly recommended to run
destroycommand to remove resources before deleting a workspace.
If you are not familier with creating Role, Service Account, and Role Binding to assign the roles to the service account with oc, kubectl commands, there is a separate Terraform script to help create the OCP service account and produce its token.
- Navigate to the folder.
cd secret-admin-sa- Create a Terraform variable file based off the example file.
cp terraform.tfvars.example terraform.tfvars-
Set the values in the variable file.
kubernetes_token: the authentication token that has privileges to create resources,Role,Service Account, andRole Binding, on the target namespaces.kubernetes_host: the hostname of Openshift clusterkubernetes_role_namespaces: the array of OCP namespaces to create theRoles, andRole Bindings on.kubernetes_service_account_namespace: the OCP namespace to create theService Accounton.
-
Initialize Terrafrom and provision the resources.
terraform init
terraform apply -auto-approve- Once completed successfully, it will prompt the service account information.
Apply complete! Resources: ⬜ added, 0 changed, 0 destroyed.
Outputs:
service_account_name = <namespace>/<service_account_name>
service_account_token = <service_account_token>- The service account will have minimum privileges to support the main Terraform script.
- Note that it will create Terraform state data in your local machine.
- In the process of setting variable values
.values, it also creates Secret Objects containing Terraform Cloud workspace credentials in each Openshift namespace. - In Github tag push events, CI (CircleCI) publishes images encapsulating one of the TFC API script to provision a new app.
- There is
kubernetes job taskin a sub-directoryopenshift>deploy>job, which runs the image on Openshift cluster using the TFC workspace Secret Objet.- it requires one string argument includes namespace and app name separated by comma,
"namespace,app"incommandjob attribute.
- it requires one string argument includes namespace and app name separated by comma,
- It uses Sementic Versioning
SemVerin order to attach a meaning to a version number or the change. - It publishes 4(+) different versions of images based on
Git Tagto support flexibleversion rangeto users.- e.g.
git tag 1.2.3-rc2<image-url>:1<image-url>:1.2<image-url>:1.2.3<image-url>:1.2.3-rc2- Image tags above point at the same image after pushing them.
- e.g.