Skip to content

Commit

Permalink
Update variables reconciliation logic to reduce number of API calls (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
arybolovlev authored Apr 23, 2024
1 parent da17f0b commit fe8d4a5
Show file tree
Hide file tree
Showing 13 changed files with 329 additions and 212 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/ENHANCEMENTS-390-20240419-102720.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: ENHANCEMENTS
body: '`Workspace`: Update variables reconciliation logic to reduce the number of
API calls.'
time: 2024-04-19T10:27:20.790343+02:00
custom:
PR: "390"
6 changes: 6 additions & 0 deletions .changes/unreleased/NOTES-390-20240419-102736.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: NOTES
body: The `Workspace` CRD has been changed. Please follow the Helm chart instructions
on how to upgrade it.
time: 2024-04-19T10:27:36.644376+02:00
custom:
PR: "390"
46 changes: 46 additions & 0 deletions api/v1alpha2/workspace_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,52 @@

package v1alpha2

import (
"github.com/hashicorp/terraform-cloud-operator/internal/slice"
)

func (w *Workspace) IsCreationCandidate() bool {
return w.Status.WorkspaceID == ""
}

// AddOrUpdateVariableStatus adds a given variable to the status if it does not exist there; otherwise, it updates it.
func (s *WorkspaceStatus) AddOrUpdateVariableStatus(variable VariableStatus) {
for i, v := range s.Variables {
if v.Name == variable.Name && v.Category == variable.Category {
s.Variables[i].ID = variable.ID
s.Variables[i].VersionID = variable.VersionID
s.Variables[i].ValueID = variable.ValueID
s.Variables[i].Category = variable.Category
return
}
}

s.Variables = append(s.Variables, VariableStatus{
Name: variable.Name,
ID: variable.ID,
VersionID: variable.VersionID,
ValueID: variable.ValueID,
Category: variable.Category,
})
}

// GetVariableStatus returns a given variable from the status if it exists there; otherwise, nil.
func (s *WorkspaceStatus) GetVariableStatus(variable VariableStatus) *VariableStatus {
for _, v := range s.Variables {
if v.Name == variable.Name && v.Category == variable.Category {
return &v
}
}

return nil
}

// DeleteVariableStatus deletes a given variable from the status.
func (s *WorkspaceStatus) DeleteVariableStatus(variable VariableStatus) {
for i, v := range s.Variables {
if v.Name == variable.Name && v.Category == variable.Category {
s.Variables = slice.RemoveFromSlice(s.Variables, i)
return
}
}
}
17 changes: 17 additions & 0 deletions api/v1alpha2/workspace_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,19 @@ type RunStatus struct {
OutputRunID string `json:"outputRunID,omitempty"`
}

type VariableStatus struct {
// Name of the variable.
Name string `json:"name"`
// ID of the variable.
ID string `json:"id"`
// VersionID is a hash of the variable on the TFC end.
VersionID string `json:"versionID"`
// ValueID is a hash of the variable on the CRD end.
ValueID string `json:"valueID"`
// Category of the variable.
Category string `json:"category"`
}

// WorkspaceStatus defines the observed state of Workspace.
type WorkspaceStatus struct {
// Workspace ID that is managed by the controller.
Expand All @@ -624,6 +637,10 @@ type WorkspaceStatus struct {
//+kubebuilder:validation:Pattern:="^\\d{1}\\.\\d{1,2}\\.\\d{1,2}$"
//+optional
TerraformVersion string `json:"terraformVersion,omitempty"`
// Workspace variables.
//
//+optional
Variables []VariableStatus `json:"variables,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down
20 changes: 20 additions & 0 deletions api/v1alpha2/zz_generated.deepcopy.go

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

8 changes: 8 additions & 0 deletions charts/terraform-cloud-operator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ For a more detailed explanation, please refer to the [FAQ](../../docs/faq.md#gen

#### Upgrade recommendations

- `2.3.0` to `2.4.0`

- The `Workspace` CRD has been changed:

```console
$ kubectl replace -f https://raw.githubusercontent.com/hashicorp/terraform-cloud-operator/v2.4.0/charts/terraform-cloud-operator/crds/app.terraform.io_workspaces.yaml
```

- `2.2.0` to `2.3.0`

- The `Workspace` CRD has been changed:
Expand Down
8 changes: 8 additions & 0 deletions charts/terraform-cloud-operator/README.md.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ For a more detailed explanation, please refer to the [FAQ](../../docs/faq.md#gen

#### Upgrade recommendations

- `2.3.0` to `2.4.0`

- The `Workspace` CRD has been changed:

```console
$ kubectl replace -f https://raw.githubusercontent.com/hashicorp/terraform-cloud-operator/v2.4.0/charts/terraform-cloud-operator/crds/app.terraform.io_workspaces.yaml
```

- `2.2.0` to `2.3.0`

- The `Workspace` CRD has been changed:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,34 @@ spec:
description: Workspace last update timestamp.
format: int64
type: integer
variables:
description: Workspace variables.
items:
properties:
category:
description: Category of the variable.
type: string
id:
description: ID of the variable.
type: string
name:
description: Name of the variable.
type: string
valueID:
description: ValueID is a hash of the variable on the CRD end.
type: string
versionID:
description: VersionID is a hash of the variable on the TFC
end.
type: string
required:
- category
- id
- name
- valueID
- versionID
type: object
type: array
workspaceID:
description: Workspace ID that is managed by the controller.
type: string
Expand Down
28 changes: 28 additions & 0 deletions config/crd/bases/app.terraform.io_workspaces.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,34 @@ spec:
description: Workspace last update timestamp.
format: int64
type: integer
variables:
description: Workspace variables.
items:
properties:
category:
description: Category of the variable.
type: string
id:
description: ID of the variable.
type: string
name:
description: Name of the variable.
type: string
valueID:
description: ValueID is a hash of the variable on the CRD end.
type: string
versionID:
description: VersionID is a hash of the variable on the TFC
end.
type: string
required:
- category
- id
- name
- valueID
- versionID
type: object
type: array
workspaceID:
description: Workspace ID that is managed by the controller.
type: string
Expand Down
5 changes: 2 additions & 3 deletions controllers/workspace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"os"
"strconv"

"github.com/go-logr/logr"
tfc "github.com/hashicorp/go-tfe"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -22,9 +24,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"

"github.com/go-logr/logr"

tfc "github.com/hashicorp/go-tfe"
appv1alpha2 "github.com/hashicorp/terraform-cloud-operator/api/v1alpha2"
"github.com/hashicorp/terraform-cloud-operator/version"
)
Expand Down
Loading

0 comments on commit fe8d4a5

Please sign in to comment.