Introduction
The Terraform Enterprise environment has an agent pool with workspaces connected to it. The Terraform Operator for Kubernetes has been implemented and you want to bring the existing agent pool under the Terraform Operator for Kubernetes management.
Existing agent pool in Terraform Enterprise called example
The following Kubernetes manifest defines the AgentPool resource you want to create:
apiVersion: app.terraform.io/v1alpha2
kind: AgentPool
metadata:
name: example
namespace: orange
spec:
organization: test
token:
secretKeyRef:
name: terraformrc
key: token
name: example
agentTokens:
- name: example
agentDeployment:
replicas: 1
spec:
containers:
- name: tfc-agent
image: "hashicorp/tfc-agent:1.22.2"
# Enable autoscaling
autoscaling:
minReplicas: 2
maxReplicas: 5
targetWorkspaces:
- wildcardName: test*
cooldownPeriod:
scaleUpSeconds: 30
scaleDownSeconds: 30
Apply it using the command:
kubectl apply -f agentpool.yaml
agentpool.app.terraform.io/example created
The Terraform Operator show the follow error in the logs and no agents are starting
ERROR Reconcile Agent Pool {"agentpool": {"name":"example","namespace":"orange"},
"msg": "failed to create a new agent pool",
"error": "invalid attribute\n\nName is already taken within the organization"}
Expected Outcome
Adding the existing agent pool to the Terraform Operator management
Prerequisites
- Have the Terraform Operator for Kubernetes configured as described here
Procedure
- Get the agentpool id from terraform Enterprise.
- Go to the agent pool and click on the agent pool and the url should contain something like the following
apool-bN3GijECCj9YmVa8
. This is the ID we need
- Go to the agent pool and click on the agent pool and the url should contain something like the following
- Retrieve the metadata.generation of the AgentPool Resource
- To get the YAML output of the current
AgentPool
resource and extract themetadata.generation
value
- To get the YAML output of the current
kubectl -n <namespace> get agentpool <metadata.name> -o yaml |grep generation
example
kubectl -n orange get agentpool example -o yaml |grep generation
generation: 2
- Update your agentpool resource with the following
kubectl patch -n <namespace> agentpool <metadata.name> --type=merge --subresource status --patch 'status: {observedGeneration: <metadata.generation>, agentPoolID: <agentpool_id>}'
Example with the above data
kubectl patch -n orange agentpool example --type=merge --subresource status --patch 'status: {observedGeneration: 2, agentPoolID: apool-WGaTSkPAnPtZWXR3}'
- Now it should succeed and you should see the agents start and connect