Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions api/v1alpha1/workspace_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ type WorkspaceSpec struct {
// Specifies the agent pool name we wish to use.
// +optional
AgentPoolName string `json:"agentPoolName,omitempty"`
// Working directory within the module repository
// +optional
WorkingDirectory string `json:"workingDirectory,omitempty"`
}

// WorkspaceStatus defines the observed state of Workspace
Expand Down
3 changes: 3 additions & 0 deletions config/crd/bases/app.terraform.io_workspaces.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ spec:
- repo_identifier
- token_id
type: object
workingDirectory:
description: Working directory within the module repository
type: string
required:
- organization
- secretsMountPath
Expand Down
22 changes: 22 additions & 0 deletions workspacehelper/tfc_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ func (t *TerraformCloudClient) SetTerraformVersion(workspace, terraformVersion s
return nil
}

func (t *TerraformCloudClient) SetWorkingDirectory(workspace, workingDirectory string) error {
wsUpdateOptions := tfc.WorkspaceUpdateOptions{
WorkingDirectory: &workingDirectory,
}
_, err := t.Client.Workspaces.Update(context.TODO(), t.Organization, workspace, wsUpdateOptions)
if err != nil {
return err
}
return nil
}

// getAgentPoolID uses AgentPoolName to lookup and return AgentPoolID
func getAgentPoolID(specTFCAgentPoolName string, agentPools []*tfc.AgentPool) (*tfc.AgentPool, error) {
for _, agentPool := range agentPools {
Expand Down Expand Up @@ -198,6 +209,13 @@ func (t *TerraformCloudClient) CheckWorkspace(workspace string, instance *appv1a
}
}

if instance.Spec.WorkingDirectory != ws.WorkingDirectory {
err = t.SetWorkingDirectory(workspace, instance.Spec.WorkingDirectory)
if err != nil {
return nil, err
}
}

if instance.Spec.AgentPoolID != ws.AgentPoolID {
err := t.updateAgentPoolID(instance, ws)
if err != nil {
Expand Down Expand Up @@ -248,6 +266,10 @@ func (t *TerraformCloudClient) CreateWorkspace(workspace string, instance *appv1
options.ExecutionMode = tfc.String("agent")
}

if instance.Spec.WorkingDirectory != "" {
options.WorkingDirectory = &instance.Spec.WorkingDirectory
}

ws, err := t.Client.Workspaces.Create(context.TODO(), t.Organization, options)
if err != nil {
return "", err
Expand Down