Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce field to set annotations on tfstate secret #1429

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions api/v1alpha2/terraform_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ type TerraformSpec struct {
// +optional
TFState *TFStateSpec `json:"tfstate,omitempty"`

// annotations to add to the tfstate secret
// +optional
TFStateAnnotations map[string]string `json:"tfstateAnnotations,omitempty"`

// Targets specify the resource, module or collection of resources to target.
// +optional
Targets []string `json:"targets,omitempty"`
Expand Down
7 changes: 7 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.

5 changes: 5 additions & 0 deletions charts/tofu-controller/crds/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10441,6 +10441,11 @@ spec:
Defaults to `0s` which will behave as though `LockTimeout` was not set
type: string
type: object
tfstateAnnotations:
additionalProperties:
type: string
description: annotations to add to the tfstate secret
type: object
values:
description: |-
Values map to the Terraform variable "values", which is an object of arbitrary values.
Expand Down
5 changes: 5 additions & 0 deletions config/crd/bases/infra.contrib.fluxcd.io_terraforms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10441,6 +10441,11 @@ spec:
Defaults to `0s` which will behave as though `LockTimeout` was not set
type: string
type: object
tfstateAnnotations:
additionalProperties:
type: string
description: annotations to add to the tfstate secret
type: object
values:
description: |-
Values map to the Terraform variable "values", which is an object of arbitrary values.
Expand Down
32 changes: 32 additions & 0 deletions controllers/tf_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,38 @@ func (r *TerraformReconciler) patchStatus(ctx context.Context, objectKey types.N
return r.Status().Patch(ctx, &terraform, patch, statusOpts)
}

func (r *TerraformReconciler) patchAnnotationsToTfstateSecret(ctx context.Context, terraform infrav1.Terraform) error {
if len(terraform.Spec.TFStateAnnotations) == 0 {
// skip if no annotations are provided
return nil
}
secretSuffix := terraform.Name
if terraform.Spec.BackendConfig != nil && terraform.Spec.BackendConfig.SecretSuffix != "" {
secretSuffix = terraform.Spec.BackendConfig.SecretSuffix
}

secret := &corev1.Secret{}
err := r.Client.Get(ctx, types.NamespacedName{
Namespace: terraform.Namespace,
Name: fmt.Sprintf(`tfstate-%s-%s`, terraform.WorkspaceName(), secretSuffix),
}, secret)
if err != nil {
return err
}

// set annotations to the secret
for key, value := range terraform.Spec.TFStateAnnotations {
secret.Annotations[key] = value
}

err = r.Client.Update(ctx, secret)
if err != nil {
return err
}

return nil
}

func (r *TerraformReconciler) IndexBy(kind string) func(o client.Object) []string {
return func(o client.Object) []string {
terraform, ok := o.(*infrav1.Terraform)
Expand Down
5 changes: 5 additions & 0 deletions controllers/tf_controller_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ func (r *TerraformReconciler) reconcile(ctx context.Context, runnerClient runner
return &terraform, err
}

if err := r.patchAnnotationsToTfstateSecret(ctx, terraform); err != nil {
log.Error(err, "unable to set annotations to tfstate secret")
// not a critical error, continue
}

if err := r.patchStatus(ctx, objectKey, terraform.Status); err != nil {
log.Error(err, "unable to update status after applying")
return &terraform, err
Expand Down
24 changes: 24 additions & 0 deletions docs/References/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -1789,6 +1789,18 @@ TFStateSpec
</tr>
<tr>
<td>
<code>tfstateAnnotations</code><br>
<em>
map[string]string
</em>
</td>
<td>
<em>(Optional)</em>
<p>annotations to add to the tfstate secret</p>
</td>
</tr>
<tr>
<td>
<code>targets</code><br>
<em>
[]string
Expand Down Expand Up @@ -2352,6 +2364,18 @@ TFStateSpec
</tr>
<tr>
<td>
<code>tfstateAnnotations</code><br>
<em>
map[string]string
</em>
</td>
<td>
<em>(Optional)</em>
<p>annotations to add to the tfstate secret</p>
</td>
</tr>
<tr>
<td>
<code>targets</code><br>
<em>
[]string
Expand Down