Skip to content

Commit

Permalink
Merge pull request #509 from weaveworks/add-cloud
Browse files Browse the repository at this point in the history
Add cloud block
  • Loading branch information
Chanwit Kaewkasi authored Feb 3, 2023
2 parents 0a2765d + fb1cd48 commit 9c939d5
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 4 deletions.
36 changes: 36 additions & 0 deletions api/v1alpha1/cloud_spec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package v1alpha1

import (
. "github.com/onsi/gomega"
"strings"
"testing"
)

// CloudSpec defines the desired state of Terraform Cloud
func TestCloudSpec(t *testing.T) {
g := NewGomegaWithT(t)
cloudSpec := &CloudSpec{
Organization: "test-org",
Workspaces: &CloudWorkspacesSpec{
Name: "dev",
Tags: []string{"test-tag", "test-tag-2"},
},
Hostname: "app.terraform.io",
Token: "test-token",
}

fixture := strings.TrimLeft(`
terraform {
cloud {
organization = "test-org"
workspaces {
name = "dev"
tags = ["test-tag", "test-tag-2"]
}
hostname = "app.terraform.io"
token = "test-token"
}
}
`, "\n")
g.Expect(cloudSpec.ToHCL()).To(Equal(fixture))
}
64 changes: 64 additions & 0 deletions api/v1alpha1/terraform_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1alpha1

import (
"bytes"
"fmt"
"strings"
"time"
Expand Down Expand Up @@ -93,6 +94,9 @@ type TerraformSpec struct {
// +optional
BackendConfigsFrom []BackendConfigsReference `json:"backendConfigsFrom,omitempty"`

// +optional
Cloud *CloudSpec `json:"cloud,omitempty"`

// +optional
// +kubebuilder:default:=default
Workspace string `json:"workspace,omitempty"`
Expand Down Expand Up @@ -235,6 +239,28 @@ type TerraformSpec struct {
Enterprise *apiextensionsv1.JSON `json:"enterprise,omitempty"`
}

type CloudSpec struct {
// +required
Organization string `json:"organization"`

// +required
Workspaces *CloudWorkspacesSpec `json:"workspaces"`

// +optional
Hostname string `json:"hostname,omitempty"`

// +optional
Token string `json:"token,omitempty"`
}

type CloudWorkspacesSpec struct {
// +optional
Name string `json:"name"`

// +optional
Tags []string `json:"tags,omitempty"`
}

type Webhook struct {
// +kubebuilder:validation:Enum=post-planning
// +kubebuilder:default:=post-planning
Expand Down Expand Up @@ -825,6 +851,44 @@ func (in *TerraformSpec) GetAlwaysCleanupRunnerPod() bool {
return *in.AlwaysCleanupRunnerPod
}

func (c *CloudSpec) IsValid() bool {
if c.Organization == "" {
return false
}

if c.Workspaces == nil {
return false
}

if c.Workspaces.Name == "" && c.Workspaces.Tags == nil {
return false
}

return true
}

func (c *CloudSpec) ToHCL() string {
var buf bytes.Buffer
buf.WriteString("terraform {\n")
buf.WriteString(" cloud {\n")
buf.WriteString(fmt.Sprintf(" organization = %q\n", c.Organization))
buf.WriteString(fmt.Sprintf(" workspaces {\n"))
if c.Workspaces.Name != "" {
buf.WriteString(fmt.Sprintf(" name = %q\n", c.Workspaces.Name))
}
if len(c.Workspaces.Tags) > 0 {
tags := "[\"" + strings.Join(c.Workspaces.Tags, "\", \"") + "\"]"
buf.WriteString(fmt.Sprintf(" tags = %s\n", tags))
}
buf.WriteString(fmt.Sprintf(" }\n"))
buf.WriteString(fmt.Sprintf(" hostname = %q\n", c.Hostname))
buf.WriteString(fmt.Sprintf(" token = %q\n", c.Token))
buf.WriteString(fmt.Sprintf(" }\n"))
buf.WriteString(fmt.Sprintf("}\n"))

return buf.String()
}

// trimString takes in a string and an integer limit, and returns a new string with a maximum length of limit characters.
// If the length of the input string is greater than limit, the returned string will be truncated to limit characters
// and "..." will be appended to the end.
Expand Down
45 changes: 45 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion charts/tf-controller/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ apiVersion: v2
name: tf-controller
description: The Helm chart for Weave GitOps Terraform Controller
type: application
version: 0.10.0
version: 0.10.1
appVersion: "v0.13.1"
2 changes: 1 addition & 1 deletion charts/tf-controller/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Weave GitOps Terraform Controller

![Version: 0.10.0](https://img.shields.io/badge/Version-0.10.0-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: v0.13.1](https://img.shields.io/badge/AppVersion-v0.13.1-informational?style=flat-square)
![Version: 0.10.1](https://img.shields.io/badge/Version-0.10.1-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: v0.13.1](https://img.shields.io/badge/AppVersion-v0.13.1-informational?style=flat-square)

The Helm chart for Weave GitOps Terraform Controller

Expand Down
21 changes: 21 additions & 0 deletions charts/tf-controller/templates/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,27 @@ spec:
type: string
type: object
x-kubernetes-map-type: atomic
cloud:
properties:
hostname:
type: string
organization:
type: string
token:
type: string
workspaces:
properties:
name:
type: string
tags:
items:
type: string
type: array
type: object
required:
- organization
- workspaces
type: object
dependsOn:
items:
description: NamespacedObjectReference contains enough information
Expand Down
21 changes: 21 additions & 0 deletions config/crd/bases/infra.contrib.fluxcd.io_terraforms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,27 @@ spec:
type: string
type: object
x-kubernetes-map-type: atomic
cloud:
properties:
hostname:
type: string
organization:
type: string
token:
type: string
workspaces:
properties:
name:
type: string
tags:
items:
type: string
type: array
type: object
required:
- organization
- workspaces
type: object
dependsOn:
items:
description: NamespacedObjectReference contains enough information
Expand Down
21 changes: 19 additions & 2 deletions controllers/tf_controller_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import (
)

func (r *TerraformReconciler) backendCompletelyDisable(terraform infrav1.Terraform) bool {
if terraform.Spec.Cloud != nil {
return true
}

return terraform.Spec.BackendConfig != nil && terraform.Spec.BackendConfig.Disable == true
}

Expand Down Expand Up @@ -94,7 +98,7 @@ terraform {
} else if DisableTFK8SBackend && terraform.Spec.BackendConfig == nil {
backendConfig = `
terraform {
backend "local" { }
backend "local" { }
}`
} else if terraform.Spec.BackendConfig == nil {
// TODO must be tested in cluster only
Expand All @@ -116,7 +120,20 @@ terraform {
}

if r.backendCompletelyDisable(terraform) {
log.Info("BackendConfig is completely disabled")
log.Info("backendConfig is completely disabled. When Spec.Cloud is not nil, backendConfig is disabled by default too.")
if terraform.Spec.Cloud != nil {
log.Info("Spec.Cloud is not nil. backendConfig is disabled by default.")
writeBackendConfigReply, err := runnerClient.WriteBackendConfig(ctx,
&runner.WriteBackendConfigRequest{
DirPath: workingDir,
BackendConfig: []byte(terraform.Spec.Cloud.ToHCL()),
})
if err != nil {
log.Error(err, "write cloud config error")
return terraform, tfInstance, tmpDir, err
}
log.Info(fmt.Sprintf("write cloud config: %s", writeBackendConfigReply.Message))
}
} else {
writeBackendConfigReply, err := runnerClient.WriteBackendConfig(ctx,
&runner.WriteBackendConfigRequest{
Expand Down

0 comments on commit 9c939d5

Please sign in to comment.