Adding Default Resources to a Project
Last updated on
note
Before proceeding, ensure that the Harness Solutions Factory has been deployed into your account and that you've completed Your First Harness Project customizations lab
Throughout the lab, ensure you are only making changes in your custom-harness-template-library repository
Add a new Kubernetes Connector using Delegate
- Open the
custom-template-libraryin VSCode andReopen in Container - Create a new branch called
feature/infrastructure-connectors - Create a new file called
harness_connectors.tfin the root of theharness-projectdirectory
resource "harness_platform_connector_kubernetes" "k8s_dev_infra" {
identifier = "k8s_dev_infra"
name = "K8s Dev Infra"
org_id = data.harness_platform_organization.selected.id
project_id = data.harness_platform_project.selected.id
description = "Kubernetes Connector"
tags = local.common_tags_tuple
inherit_from_delegate {
delegate_selectors = [var.k8s_dev_infra_delegate]
}
}
- Open the file
harness-project/variables.tfto add the following variable.
variable "k8s_dev_infra_delegate" {
type = string
description = "Provide the delegate selector to use for the Kubernetes connector"
default = "dev"
}
- Open the file
harness-project/terraform.tfvars.exampleto add the following helper example
# Provide the delegate selector to use for the Kubernetes connector
k8s_dev_infra_delegate = "dev"
- In terminal, change directory to the new folder
cd harness-project, open the fileterraform.tfvars.exampleand save as a new file (CTL+SHIFT+S on windows or CMD+SHIFT+S on Mac). The file should be namedterraform.tfvars- For local testing, you will need a terraform provider configuration. Run the command
mise provider
- For local testing, you will need a terraform provider configuration. Run the command
- The **
providers.tfincludes the two variablesharness_platform_urlandharness_platform_account. Theharness-projectmandates those in thevariables.tf. Remove or Comment out the entries from theproviders.tf - Modify the
terraform.tfvarsto set the variables:
| Variable | Description |
|---|---|
harness_platform_account | Should be your Harness account number |
harness_platform_key | Personal Access Token with permissions to the provided account |
organization_id | For the purposes of this test, use the workshop organization Lab |
project_name | Provide a new name for a test project - Lab Exercise |
- In terminal, run the command
mise deploy:dryrun. You should see a Plan for 24 resources to be added. This will run the commandstofu init; tofu fmt; tofu planusing Mise
# Example Output
Plan: 24 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ organization_identifier = "Lab"
+ project_identifier = (known after apply)
+ project_url = (known after apply)
- Run a
mise deployto execute the full deployment of the codebase against your account. This will run the commandstofu init; tofu fmt; tofu plan; tofu applyusing Mise
# Example Output
Apply complete! Resources: 24 added, 0 changed, 0 destroyed.
Outputs:
organization_identifier = "Lab"
project_identifier = "Lab_Exercise"
project_url = "<https://app.harness.io/ng/account/uZuUmmrnT4qQRx5XF0ZtkQ/all/orgs/Lab/projects/Lab_Exercise/overview>"
- Navigate to your new project in your Harness Account to verify the results.
- Commit your code locally and push the branch
Add a new default Infrastructure Definition
- Create a new file called
harness_infradefs.tfin the root of theharness-projectdirectory
resource "harness_platform_infrastructure" "infrastructure" {
depends_on = [
harness_platform_environment.environments
]
identifier = "infrastructure"
name = "K8s Dev Infra"
org_id = data.harness_platform_organization.selected.id
project_id = data.harness_platform_project.selected.id
env_id = "dev"
type = "KubernetesDirect"
deployment_type = "Kubernetes"
yaml = <<-EOT
infrastructureDefinition:
name: K8s Dev Infra
identifier: infrastructure
description: ""
tags:
${indent(4, yamlencode(local.common_tags))}
orgIdentifier: ${data.harness_platform_organization.selected.id}
projectIdentifier: ${data.harness_platform_project.selected.id}
environmentRef: dev
deploymentType: Kubernetes
type: KubernetesDirect
spec:
connectorRef: ${harness_platform_connector_kubernetes.k8s_dev_infra.id}
namespace: ${var.k8s_dev_infra_namespace}
releaseName: release-<+INFRA_KEY>
allowSimultaneousDeployments: true
EOT
tags = local.common_tags_tuple
}
- Open the file
harness-project/variables.tfto add the following variable.
variable "k8s_dev_infra_namespace" {
type = string
description = "Provide the name of the infrastructure definition to add"
default = "infrastructure"
}
- Open the file
harness-project/terraform.tfvars.exampleto add the following helper example
# Provide the name of the infrastructure definition to add
k8s_dev_infra_namespace = "infrastructure"
- Open the file
harness-project/terraform.tfvarsto add the following value for testing
# Provide the name of the infrastructure definition to add
k8s_dev_infra_namespace = "infrastructure"
- In terminal, run the command
mise deploy:dryrun. You should see a Plan for 1 resource to be added. This will run the commandstofu init; tofu fmt; tofu planusing Mise
# Example Output
Plan: 1 to add, 0 to change, 0 to destroy.
- Run a
mise deployto execute the full deployment of the codebase against your account. This will run the commandstofu init; tofu fmt; tofu plan; tofu applyusing Mise
# Example Output
Plan: 1 to add, 0 to change, 0 to destroy.
harness_platform_infrastructure.infrastructure: Creating...
harness_platform_infrastructure.infrastructure: Creation complete after 1s [id=infrastructure]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
- Navigate to your new project to verify the results. We have just added a new Infrastructure Definition in the Environment
Dev - Clean-up your new project by deleting the resources
mise teardown. This will destroy the resources, remove the state files and providers, and then remove the **providers.tf - Commit your code locally and push the branch
Rollout changes to your projects
- Navigate to the
Solutions Factoryproject - Open the IACM Workspaces and select the
Lab_Custom-Projectworkspace - Switch the Configuration tab for the workspace and change the branch to use your new branch
feature/infrastructure-connectors - Click
Drift Check. The results will show that two new items will be added. - Navigate back to the workspace and run
Provision. Wait for the execution to complete to verify that everything has worked. - Navigate to the
Harness Platform Managementorganization, go to the Code Repository module, and clickcustom-template-library - Create and merge a new Pull-Request and delete the branch
- Navigate to IACM Workspace, select the workspace
Lab_Custom-Projectand in the Configuration tab change the branch to use your new branchmain - Run the pipeline
Bulk Workspace Managementby navigating to the pipelines in theSolutions Factoryproject and filtering your pipelines by typingiacmin the pipeline search bar- WORKSPACE_SOURCE =
custom - WORKSPACE_TYPE =
harness-project - WORKSPACE_STATUS =
any - WORKSPACE_CHANGE_SOURCE =
no - WORKSPACE_ACTION =
apply
- WORKSPACE_SOURCE =
- After the
Bulk Workspace Managementcompletes, it will have started new executions of theProvision Workspacepipeline for each matching IACM Workspace. Approve the changes to the existing workspaces and verify the changes have been applied.- Each project will have the new connector and infrastructure definition
- The
Custom Projectshould have no changes applied
In your local VSCode repository, check out branch main to pull your changes and delete your local branch feature/infrastructure-connectors