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

Add .tfstate.disablePlanLock #1379

Open
wants to merge 4 commits 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
10 changes: 10 additions & 0 deletions api/v1alpha2/terraform_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,16 @@ type TFStateSpec struct {
// +optional
// +kubebuilder:default:string="0s"
LockTimeout metav1.Duration `json:"lockTimeout,omitempty"`

// DisablePlanLock is an option to disableb Terraform state lock for a plan action
//
// By default terraform will lock state but in case you want to avoid the default behaviour,
// you need to set this vale to `true`
//
// Defaults to `false` which will lock Terraform state during the plan stage
// +optional

DisablePlanLock bool `json:"disablePlanLock,omitempty"`
}

type ForceUnlockEnum string
Expand Down
5 changes: 5 additions & 0 deletions charts/tofu-controller/crds/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10440,6 +10440,11 @@ spec:

Defaults to `0s` which will behave as though `LockTimeout` was not set
type: string
disablePlanLock:
default: true
description: |-
Disables state locking for a plan action. If set to true Terraform plan will be running win -lock=false parameter
type: boolean
type: object
values:
description: |-
Expand Down
8 changes: 6 additions & 2 deletions controllers/tf_controller_drift_detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ func (r *TerraformReconciler) detectDrift(ctx context.Context, terraform infrav1
planRequest.Out = ""
planRequest.Refresh = true
}

if terraform.Spec.TFState != nil {
if terraform.Spec.TFState.DisablePlanLock == true {
planRequest.DisablePlanLock = true
}
}
eventSent := false
planReply, err := runnerClient.Plan(ctx, planRequest)
if err != nil {
Expand All @@ -98,7 +102,7 @@ func (r *TerraformReconciler) detectDrift(ctx context.Context, terraform infrav1
r.event(ctx, terraform, revision, eventv1.EventSeverityError, msg, nil)
}

err = fmt.Errorf("error running Plan: %s", err)
err = fmt.Errorf("error running drift Plan: %s", err)
return infrav1.TerraformNotReady(
terraform,
revision,
Expand Down
3 changes: 3 additions & 0 deletions controllers/tf_controller_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ func (r *TerraformReconciler) plan(ctx context.Context, terraform infrav1.Terraf
log.Info(fmt.Sprintf("LockTimeout is set: %s", terraform.Spec.TFState.LockTimeout))
planRequest.LockTimeout = terraform.Spec.TFState.LockTimeout.Duration.String()
}
if terraform.Spec.TFState.DisablePlanLock == true {
planRequest.DisablePlanLock = true
}
}

planReply, err := runnerClient.Plan(ctx, planRequest)
Expand Down
10 changes: 10 additions & 0 deletions docs/References/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,16 @@ three seconds.</p>
<p>Defaults to <code>0s</code> which will behave as though <code>LockTimeout</code> was not set</p>
</td>
</tr>
<tr>
<td>
<code>disablePlanLock</code><br>
<em>
bool
</em>
</td>
<td>
</td>
</tr>
</tbody>
</table>
</div>
Expand Down
740 changes: 375 additions & 365 deletions runner/runner.pb.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions runner/runner.proto
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ message PlanRequest {
repeated string targets = 5;
string lockTimeout = 6;
string sourceRefRootDir = 7;
bool disablePlanLock = 8;
}

message PlanReply {
Expand Down
7 changes: 6 additions & 1 deletion runner/server_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
tfjson "github.com/hashicorp/terraform-json"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"sigs.k8s.io/controller-runtime"
controllerruntime "sigs.k8s.io/controller-runtime"
ctrl "sigs.k8s.io/controller-runtime"
)

Expand Down Expand Up @@ -127,6 +127,11 @@ func (r *TerraformRunnerServer) Plan(ctx context.Context, req *PlanRequest) (*Pl
planOpt = append(planOpt, tfexec.Destroy(req.Destroy))
}

if req.DisablePlanLock {
log.Info("Running plan with no lock")
planOpt = append(planOpt, tfexec.Lock(false))
}

if req.LockTimeout != "" {
planOpt = append(planOpt, tfexec.LockTimeout(req.LockTimeout))
}
Expand Down
Loading