Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Decouple the code from the terraform version. #77

Merged
merged 4 commits into from
Oct 27, 2020
Merged
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,6 @@ tags
.vscode/*
.history
# End of https://www.gitignore.io/api/go,vim,emacs,visualstudiocode
.idea/

terraform-k8s
5 changes: 5 additions & 0 deletions deploy/crds/app.terraform.io_workspaces_crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ spec:
organization. This can either be the user assigned name of the
SSH Key, or the system assigned ID.
type: string
terraformVersion:
default: latest
description: Terraform version used for this workspace. The default
is `latest`.
type: string
variables:
description: Variables as inputs to module
items:
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ go 1.15

require (
github.com/go-logr/logr v0.1.0
github.com/hashicorp/go-tfe v0.7.0
github.com/hashicorp/terraform v0.12.25
github.com/hashicorp/go-tfe v0.10.3-0.20201021065942-225578ab1ee7 //pin to commit until new version is out
github.com/hashicorp/terraform v0.13.4
github.com/mitchellh/cli v1.1.1
github.com/operator-framework/operator-sdk v0.18.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.5.1
github.com/zclconf/go-cty v1.4.0
k8s.io/api v0.18.2
k8s.io/apimachinery v0.18.2
k8s.io/client-go v12.0.0+incompatible
Expand Down
70 changes: 70 additions & 0 deletions go.sum

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions pkg/apis/app/v1alpha1/workspace_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ type WorkspaceSpec struct {
// Outputs denote outputs wanted
// +optional
Outputs []*OutputSpec `json:"outputs,omitempty"`
// Terraform version used for this workspace. The default is `latest`.
// +optional
// +kubebuilder:default=latest
TerraformVersion string `json:"terraformVersion"`
}

// WorkspaceStatus defines the observed state of Workspace
Expand Down
57 changes: 45 additions & 12 deletions pkg/controller/workspace/tfc_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ func createTerraformConfig(address string, tfConfig *cliconfig.Config) (*tfc.Con

ua := fmt.Sprintf("terraform-k8s/%s", version.Version)
return &tfc.Config{
Address: address,
Token: fmt.Sprintf("%v", tfConfig.Credentials[host]["token"]),
Headers: http.Header{"User-Agent": []string{ua}},
Address: address,
Token: fmt.Sprintf("%v", tfConfig.Credentials[host]["token"]),
Headers: http.Header{"User-Agent": []string{ua}},
HTTPClient: httpClient,
}, nil
}
Expand Down Expand Up @@ -92,6 +92,17 @@ func (t *TerraformCloudClient) CheckOrganization() error {
return err
}

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

// CheckWorkspace looks for a workspace
func (t *TerraformCloudClient) CheckWorkspace(workspace string, instance *appv1alpha1.Workspace) (*tfc.Workspace, error) {
ws, err := t.Client.Workspaces.Read(context.TODO(), t.Organization, workspace)
Expand All @@ -118,23 +129,21 @@ func (t *TerraformCloudClient) CheckWorkspace(workspace string, instance *appv1a
}
}

return ws, err
}

func getTerraformVersion() *string {
tfVersion := os.Getenv("TF_VERSION")
if tfVersion == "" {
return &version.TerraformVersion
if instance.Spec.TerraformVersion != ws.TerraformVersion {
err = t.SetTerraformVersion(workspace, instance.Spec.TerraformVersion)
if err != nil {
return nil, err
}
}
return &tfVersion
return ws, err
}

// CreateWorkspace creates a Terraform Cloud Workspace that auto-applies
func (t *TerraformCloudClient) CreateWorkspace(workspace string, instance *appv1alpha1.Workspace) (string, error) {
options := tfc.WorkspaceCreateOptions{
AutoApply: &AutoApply,
Name: &workspace,
TerraformVersion: getTerraformVersion(),
TerraformVersion: &instance.Spec.TerraformVersion,
}

if instance.Spec.VCS != nil {
Expand Down Expand Up @@ -211,3 +220,27 @@ func (t *TerraformCloudClient) DeleteWorkspace(workspaceID string) error {
}
return nil
}

// CheckOutputs retrieves outputs for a run.
func (t *TerraformCloudClient) CheckOutputs(workspaceID string, runID string) ([]*appv1alpha1.OutputStatus, error) {
outputs := []*appv1alpha1.OutputStatus{}
if runID == "" {
return outputs, nil
}

st, err := t.Client.StateVersions.CurrentWithOptions(context.TODO(), workspaceID,
&tfc.StateVersionCurrentOptions{Include: "outputs"})
if err != nil {
return nil, err
}

for _, value := range st.Outputs {
if !value.Sensitive {
if value.Value != "" {
outputs = append(outputs, &appv1alpha1.OutputStatus{Key: value.Name, Value: value.Value})
}
}
}

return outputs, nil
}
14 changes: 0 additions & 14 deletions pkg/controller/workspace/tfc_org_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,3 @@ func TestOrganizationTerraformEnterpriseNotFound(t *testing.T) {
err = tfClient.CheckOrganization()
assert.Error(t, err)
}

func TestShouldGetTerraformVersionFromEnvVariable(t *testing.T) {
expected := "0.11.0"
os.Setenv("TF_VERSION", expected)
actual := getTerraformVersion()
os.Unsetenv("TF_VERSION")
assert.Equal(t, expected, *actual)
}

func TestShouldGetTerraformVersionFromOperator(t *testing.T) {
expected := "0.12.25"
actual := getTerraformVersion()
assert.Equal(t, expected, *actual)
}
184 changes: 0 additions & 184 deletions pkg/controller/workspace/tfc_output.go

This file was deleted.

Loading