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

Validate if plan has no changes #854

Merged
merged 20 commits into from
May 20, 2024
Merged

Validate if plan has no changes #854

merged 20 commits into from
May 20, 2024

Conversation

alfespa17
Copy link
Member

@alfespa17 alfespa17 commented May 20, 2024

Correctly validate if a plan has no changes, if the plan has no changes then no other operation should run in Terrakube and the CLI complete the job sucessfully

user@pop-os:~/git/simple-terraform$ terraform init

Initializing Terraform Cloud...
Initializing modules...
- time_module in module

Initializing provider plugins...
- Finding latest version of hashicorp/random...
- Finding latest version of hashicorp/null...
- Finding latest version of hashicorp/time...
- Installing hashicorp/random v3.6.1...
- Installed hashicorp/random v3.6.1 (signed by HashiCorp)
- Installing hashicorp/null v3.2.2...
- Installed hashicorp/null v3.2.2 (signed by HashiCorp)
- Installing hashicorp/time v0.11.1...
- Installed hashicorp/time v0.11.1 (signed by HashiCorp)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform Cloud has been successfully initialized!

You may now begin working with Terraform Cloud. Try running "terraform plan" to
see any changes that are required for your infrastructure.

If you ever set or change modules or Terraform Settings, run "terraform init"
again to reinitialize your working directory.
user@pop-os:~/git/simple-terraform$ terraform apply

Running apply in Terraform Cloud. Output will stream here. Pressing Ctrl-C
will cancel the remote apply if it's still pending. If the apply started it
will stop streaming the logs, but will not stop the apply running remotely.

Preparing the remote apply...

To view this run in a browser, visit:
https://8080-azbuilder-terrakube-klg638fzzv3.ws-us114.gitpod.io/app/simple/auto-approve/runs/1

Waiting for the plan to start...

***************************************
Running Terraform PLAN
***************************************

Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # null_resource.next will be created
  + resource "null_resource" "next" {
      + id = (known after apply)
    }

  # null_resource.next2 will be created
  + resource "null_resource" "next2" {
      + id = (known after apply)
    }

  # null_resource.next3 will be created
  + resource "null_resource" "next3" {
      + id = (known after apply)
    }

  # null_resource.next4 will be created
  + resource "null_resource" "next4" {
      + id = (known after apply)
    }

  # null_resource.previous will be created
  + resource "null_resource" "previous" {
      + id = (known after apply)
    }

  # time_sleep.wait_30_seconds will be created
  + resource "time_sleep" "wait_30_seconds" {
      + create_duration = (known after apply)
      + id              = (known after apply)
    }

  # module.time_module.random_integer.time will be created
  + resource "random_integer" "time" {
      + id     = (known after apply)
      + max    = 5
      + min    = 1
      + result = (known after apply)
    }

Plan: 7 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + creation_time = (known after apply)
  + fake_data     = {
      + data     = "Hello World"
      + resource = {
          + resource1 = "fake"
        }
    }

Do you want to perform these actions in workspace "auto-approve"?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

module.time_module.random_integer.time: Creating...
null_resource.previous: Creating...
null_resource.previous: Creation complete after 0s [id=8638434822281483174]
module.time_module.random_integer.time: Creation complete after 0s [id=1]
time_sleep.wait_30_seconds: Creating...
time_sleep.wait_30_seconds: Creation complete after 1s [id=2024-05-20T23:34:11Z]
null_resource.next3: Creating...
null_resource.next2: Creating...
null_resource.next: Creating...
null_resource.next4: Creating...
null_resource.next4: Creation complete after 0s [id=7893297334135631849]
null_resource.next: Creation complete after 0s [id=6850444880568818972]
null_resource.next2: Creation complete after 0s [id=1936697189527663205]
null_resource.next3: Creation complete after 0s [id=7370101385154037294]

Apply complete! Resources: 7 added, 0 changed, 0 destroyed.

Outputs:

creation_time = "1s"
fake_data = {
  "data" = "Hello World"
  "resource" = {
    "resource1" = "fake"
  }
}
user@pop-os:~/git/simple-terraform$ terraform apply

Running apply in Terraform Cloud. Output will stream here. Pressing Ctrl-C
will cancel the remote apply if it's still pending. If the apply started it
will stop streaming the logs, but will not stop the apply running remotely.

Preparing the remote apply...

To view this run in a browser, visit:
https://8080-azbuilder-terrakube-klg638fzzv3.ws-us114.gitpod.io/app/simple/auto-approve/runs/2

Waiting for the plan to start...

***************************************
Running Terraform PLAN
***************************************
module.time_module.random_integer.time: Refreshing state... [id=1]
null_resource.previous: Refreshing state... [id=8638434822281483174]
time_sleep.wait_30_seconds: Refreshing state... [id=2024-05-20T23:34:11Z]
null_resource.next4: Refreshing state... [id=7893297334135631849]
null_resource.next2: Refreshing state... [id=1936697189527663205]
null_resource.next3: Refreshing state... [id=7370101385154037294]
null_resource.next: Refreshing state... [id=6850444880568818972]

No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration
and found no differences, so no changes are needed.
user@pop-os:~/git/simple-terraform$ terraform destroy

Running apply in Terraform Cloud. Output will stream here. Pressing Ctrl-C
will cancel the remote apply if it's still pending. If the apply started it
will stop streaming the logs, but will not stop the apply running remotely.

Preparing the remote apply...

To view this run in a browser, visit:
https://8080-azbuilder-terrakube-klg638fzzv3.ws-us114.gitpod.io/app/simple/auto-approve/runs/3

Waiting for the plan to start...

***************************************
Running Terraform PLAN
***************************************
module.time_module.random_integer.time: Refreshing state... [id=1]
null_resource.previous: Refreshing state... [id=8638434822281483174]
time_sleep.wait_30_seconds: Refreshing state... [id=2024-05-20T23:34:11Z]
null_resource.next3: Refreshing state... [id=7370101385154037294]
null_resource.next: Refreshing state... [id=6850444880568818972]
null_resource.next2: Refreshing state... [id=1936697189527663205]
null_resource.next4: Refreshing state... [id=7893297334135631849]

Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  # null_resource.next will be destroyed
  - resource "null_resource" "next" {
      - id = "6850444880568818972" -> null
    }

  # null_resource.next2 will be destroyed
  - resource "null_resource" "next2" {
      - id = "1936697189527663205" -> null
    }

  # null_resource.next3 will be destroyed
  - resource "null_resource" "next3" {
      - id = "7370101385154037294" -> null
    }

  # null_resource.next4 will be destroyed
  - resource "null_resource" "next4" {
      - id = "7893297334135631849" -> null
    }

  # null_resource.previous will be destroyed
  - resource "null_resource" "previous" {
      - id = "8638434822281483174" -> null
    }

  # time_sleep.wait_30_seconds will be destroyed
  - resource "time_sleep" "wait_30_seconds" {
      - create_duration = "1s" -> null
      - id              = "2024-05-20T23:34:11Z" -> null
    }

  # module.time_module.random_integer.time will be destroyed
  - resource "random_integer" "time" {
      - id     = "1" -> null
      - max    = 5 -> null
      - min    = 1 -> null
      - result = 1 -> null
    }

Plan: 0 to add, 0 to change, 7 to destroy.

Changes to Outputs:
  - creation_time = "1s" -> null
  - fake_data     = {
      - data     = "Hello World"
      - resource = {
          - resource1 = "fake"
        }
    } -> null

Do you really want to destroy all resources in workspace "auto-approve"?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

null_resource.next3: Destroying... [id=7370101385154037294]
null_resource.next4: Destroying... [id=7893297334135631849]
null_resource.next2: Destroying... [id=1936697189527663205]
null_resource.next: Destroying... [id=6850444880568818972]
null_resource.next2: Destruction complete after 0s
null_resource.next4: Destruction complete after 0s
null_resource.next3: Destruction complete after 0s
null_resource.next: Destruction complete after 0s
time_sleep.wait_30_seconds: Destroying... [id=2024-05-20T23:34:11Z]
time_sleep.wait_30_seconds: Destruction complete after 0s
null_resource.previous: Destroying... [id=8638434822281483174]
module.time_module.random_integer.time: Destroying... [id=1]
null_resource.previous: Destruction complete after 0s
module.time_module.random_integer.time: Destruction complete after 0s

Apply complete! Resources: 0 added, 0 changed, 7 destroyed.

user@pop-os:~/git/simple-terraform$ terraform destroy

Running apply in Terraform Cloud. Output will stream here. Pressing Ctrl-C
will cancel the remote apply if it's still pending. If the apply started it
will stop streaming the logs, but will not stop the apply running remotely.

Preparing the remote apply...

To view this run in a browser, visit:
https://8080-azbuilder-terrakube-klg638fzzv3.ws-us114.gitpod.io/app/simple/auto-approve/runs/4

Waiting for the plan to start...

^[***************************************
Running Terraform PLAN
***************************************

No changes. No objects need to be destroyed.

Either you have not created any objects yet or the existing objects were
already deleted outside of Terraform.
user@pop-os:~/git/simple-terraform$

Adding a new filter status named "noChanges"

image

image

image

image

Copy link

@alfespa17 alfespa17 linked an issue May 20, 2024 that may be closed by this pull request
@alfespa17 alfespa17 merged commit 362f24d into main May 20, 2024
4 checks passed
@alfespa17 alfespa17 deleted the plan-no-changes branch May 20, 2024 23:43
@jcanizalez
Copy link
Member

@alfespa17
As a suggestion I think is better to update the status of the step only and not all the job. Because finally the job ran successfully. This is an example in Terraform Cloud. The run mantains as Successfully, and the step appears as not run. This will simplify the filters to avoid adding other state

image

@alfespa17
Copy link
Member Author

The step status is "notexecuted" you can see the screenshot above and the final job state is "nochanges" do you think it should use another status?

@jcanizalez
Copy link
Member

I think at step level it's fine to use that status, and maybe the Job should continue using "Completed" to avoid to have a lot of states. And because finally the job finished successfully

@alfespa17
Copy link
Member Author

Maybe you are right, I will change the logic a little bit tomorrow, I will add some field to know if a job has "no changes" because right now I am using the job status here

runsModel.getAttributes().put("has-changes", job.getStatus().equals(JobStatus.noChanges) ? false: true);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Runs with no changes do not need confirmation
2 participants