Commit Graph

90 Commits

Author SHA1 Message Date
Christian Mesh
5e7397b8a3 Parallelize provider installation (#2729)
Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
2025-12-01 11:55:53 -05:00
Martin Atkins
0503163e28 tracing: Centralize our OpenTelemetry package imports
OpenTelemetry has various Go packages split across several Go modules that
often need to be carefully upgraded together. And in particular, we are
using the "semconv" package in conjunction with the OpenTelemetry SDK's
"resource" package in a way that requires that they both agree on which
version of the OpenTelemetry Semantic Conventions are being followed.

To help avoid "dependency hell" situations when upgrading, this centralizes
all of our direct calls into the OpenTelemetry SDK and tracing API into
packages under internal/tracing, by exposing a few thin wrapper functions
that other packages can use to access the same functionality indirectly.

We only use a relatively small subset of the OpenTelemetry library surface
area, so we don't need too many of these reexports and they should not
represent a significant additional maintenance burden.

For the semconv and resource interaction in particular this also factors
that out into a separate helper function with a unit test, so we should
notice quickly whenever they become misaligned. This complements the
end-to-end test previously added in opentofu/opentofu#3447 to give us
faster feedback about this particular problem, while the end-to-end test
has the broader scope of making sure there aren't any errors at all when
initializing OpenTelemetry tracing.

Finally, this also replaces the constants we previously had in package
traceaddrs with functions that return attribute.KeyValue values directly.
This matches the API style used by the OpenTelemetry semconv packages, and
makes the calls to these helpers from elsewhere in the system a little
more concise.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2025-10-30 13:27:10 -07:00
Martin Atkins
5fa35c5601 backend+command: Alias names for backend types
This introduces the concept of "backend aliases", which are alternative
names that can be used to refer to a given backend.

Each backend type has one canonical name and zero or more alias names. The
"backend" block in the root module can specify either a canonical backend
type or an alias, but internally OpenTofu will always track the backend
type using its canonical name.

In particular, the following are all true when the configuration specifies
an alias instead of a canonical backend type:
- The "tofu init" output includes a brief extra message saying which
  backend type OpenTofu actually used, because that is the name that we'd
  prioritize in our documentation and so an operator can use the canonical
  type to find the relevant docs when needed.
- The .terraform/terraform.tfstate file that tracks the working directory's
  currently-initialized backend settings always uses the canonical backend
  type, and so it's possible to freely switch between aliases and canonical
  without "tofu init" thinking that a state migration might be needed.
- Plan files similarly use the canonical backend type to track which
  backend was active when the plan was created, which doesn't have any
  significant user-facing purpose, but is consistent with the previous
  point since the settings in the plan file effectively substitute for
  the .terraform/terraform.tfstate file when applying a saved plan.
- The terraform_remote_state data source in the provider
  terraform.io/builtin/terraform accepts both canonical and alias in its
  backend type argument, treating both as equivalent for the purpose of
  fetching the state snapshot for the configured workspace.

The primary motivation for this new facility is to allow the planned
"oracle_oci" backend to have an alias "oci" to allow writing configurations
that are cross-compatible with HashiCorp Terraform, since that software
has chosen to have unqualified OCI mean Oracle's system, whereas OpenTofu
has previously established that unqualified OCI means "Open Container
Initiative" in our ecosystem.

In particular, this design makes it possible in principle to bring an
existing Terraform configuration specifying backend "oci" over to OpenTofu
without modifications, and then to optionally switch it to specifying
backend "oracle-oci" at a later time without a spurious prompt to migrate
state snapshots to the same physical location where they are already
stored.

This commit doesn't actually introduce any aliases and therefore doesn't
have any tests for the new mechanism because our backend system uses a
global table that isn't friendly to mocking for testing purposes. I've
tested this manually using a placeholder alias to have confidence that it
works, and I expect that a subsequent commit introducing the new
"oracle_oci" backend will also introduce its "oci" alias and will include
tests that cover use of the alias and migration from the alias to the
canonical name and vice-versa.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2025-09-11 11:33:53 -07:00
Christian Mesh
1d38fd69d8 Switch from polling to native blocking locking for the provider cache (#3090)
Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
Co-authored-by: Martin Atkins <mart@degeneration.co.uk>
2025-08-01 10:17:33 -04:00
Martin Atkins
67a5cd0911 statemgr+remote: context.Context parameters
This extends statemgr.Persistent, statemgr.Locker and remote.Client to
all expect context.Context parameters, and then updates all of the existing
implementations of those interfaces to support them.

All of the calls to statemgr.Persistent and statemgr.Locker methods outside
of tests are consistently context.TODO() for now, because the caller
landscape of these interfaces has some complications:

1. statemgr.Locker is also used by the clistate package for its state
   implementation that was derived from statemgr.Filesystem's predecessor,
   even though what clistate manages is not actually "state" in the sense
   of package statemgr. The callers of that are not yet ready to provide
   real contexts.

   In a future commit we'll either need to plumb context through to all of
   the clistate callers, or continue the effort to separate statemgr from
   clistate by introducing a clistate-specific "locker" API for it
   to use instead.

2. We call statemgr.Persistent and statemgr.Locker methods in situations
   where the active context might have already been cancelled, and so we'll
   need to make sure to ignore cancellation when calling those.

   This is mainly limited to PersistState and Unlock, since both need to
   be able to complete after a cancellation, but there are various
   codepaths that perform a Lock, Refresh, Persist, Unlock sequence and so
   it isn't yet clear where is the best place to enforce the invariant that
   Persist and Unlock must not be called with a cancelable context. We'll
   deal with that more in subsequent commits.

Within the various state manager and remote client implementations the
contexts _are_ wired together as best as possible with how these subsystems
are already laid out, and so once we deal with the problems above and make
callers provide suitable contexts they should be able to reach all of the
leaf API clients that might want to generate OpenTelemetry traces.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2025-07-10 08:11:39 -07:00
Martin Atkins
b7e0a93708 command: Only shim dependency lock file for installation actions
Recently we added a call to Locks.UpgradeFromPredecessorProject to try to
preserve dependency selections made for providers under
registry.terraform.io/hashicorp/* when switching to OpenTofu for the first
time.

However, this behavior did not properly cater for the situation where the
configuration intentionally specifies registry.terraform.io explicitly in
a source address: that would then cause OpenTofu to incorrectly try to make
a factory function for the shimmed provider version when working in
command.Meta.providerFactories, which would then fail because no such
provider appears in the cache directory.

Instead then, we'll limit the shimming only to installation-related actions
while only using the dependency locks exactly as written when preparing to
actually _run_ the provider plugins.

This is bothersome to test because our tests are not allowed to directly
access registry.terraform.io; the test case here mimicks one case in which
it could be valid for an OpenTofu user to explicitly use
registry.terraform.io: if they've used the CLI configuration to arrange for
that hostname to be handled only via a mirror rather than by direct access
to the origin registry. The terms of service for registry.terraform.io
currently prohibit using it for anything other than Terraform, so we ensure
that this test cannot make requests to any real services at that hostname.

Note that telling OpenTofu to use registry.terraform.io is not officially
supported and may cause other problems beyond what was addressed by this
PR, since OpenTofu tends to assume that this hostname would appear only
during the process of migrating from Terraform and might make unexpected
decisions based on that assumption. Despite us making this fix, those who
are explicitly specifying registry.terraform.io in their configuration
should make plans to stop doing that and to set things up some other way
instead.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2025-07-01 07:44:47 -07:00
Martin Atkins
a1ba3e24aa tofu: EvalContext expression evaluation takes context.Context (#2937)
Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2025-06-19 10:46:31 +01:00
Martin Atkins
00dc728aea getproviders: context.Context for source constructor functions
This completes some of the missing connections for contexts in the provider
source codepaths by introducing context.Context parameters and wiring them
through so we can eliminate a few more context.TODO() placeholders.

For consistency's sake this adds context.Context to all four of the
getproviders.Source implementations that directly interact with stuff
outside of OpenTofu (network services or filesystem), even though not
all of them currently make use of it, just because interactions with
outside stuff tends to encourage cross-cutting concerns like logging and
tracing and so this ensures we have contexts propagated in there for such
future uses.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2025-06-13 08:22:47 -07:00
Martin Atkins
d2bef1fd47 Adopt OpenTofu's own "svchost" module
Previously we were using a third-party library, but that doesn't have any
support for passing context.Context through its API and so isn't suitable
for our goals of adding OpenTelemetry tracing for all outgoing network
requests.

We now have our own fork that is updated to use context.Context. It also
has a slightly reduced scope no longer including various details that
are tightly-coupled to our cliconfig mechanism and so better placed in the
main OpenTofu codebase so we can evolve it in future without making
lockstep library releases.

The "registry-address" library also uses svchost and uses some of its types
in its public API, so this also incorporates v2 of that library that is
updated to use our own svchost module.

Unfortunately this commit is a mix of mechanical updates to the new
libraries and some new code dealing with the functionality that is removed
in our fork of svchost. The new code is primarily in the "svcauthconfig"
package, which is similar in purpose "ociauthconfig" but for OpenTofu's
own auth mechanism instead of the OCI Distribution protocol's auth
mechanism.

This includes some additional plumbing of context.Context where it was
possible to do so without broad changes to files that would not otherwise
have been included in this commit, but there are a few leftover spots that
are context.TODO() which we'll address separately in later commits.

This removes the temporary workaround from d079da6e9e, since we are now
able to plumb the OpenTelemetry span tree all the way to the service
discovery requests.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2025-06-12 09:37:59 -07:00
Christian Mesh
11694a6ac0 Alternate approach to linking and locking the global cache (#2708)
Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
Co-authored-by: Martin Atkins <mart@degeneration.co.uk>
2025-05-08 15:26:46 -04:00
Martin Atkins
601e84ee71 backend: Backend.StateMgr takes context.Context
This adds a new context.Context argument to the Backend.StateMgr method,
updates all of the implementations to match, and then updates all of the
callers to pass in a context.

A small number of callers don't yet have context plumbed to them so those
use context.TODO() as a placeholder for now, so we can more easily find
and fix them in later commits once we have contexts more thoroughly
plumbed.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2025-05-07 14:14:34 -07:00
James Humphries
6d3aed0e8f [OpenTelemetry] Add module init tracing (#2711)
Signed-off-by: James Humphries <james@james-humphries.co.uk>
2025-05-01 14:15:03 +01:00
James Humphries
fca652c667 Add context propagation to Command Meta entrypoint methods (#2735)
Signed-off-by: James Humphries <james@james-humphries.co.uk>
2025-04-30 16:28:19 +01:00
James Humphries
d92d4f9c11 [OpenTelemetry] Add traces to init command (#2665)
Signed-off-by: James Humphries <james@james-humphries.co.uk>
Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
Co-authored-by: Christian Mesh <christianmesh1@gmail.com>
2025-04-25 12:40:48 +01:00
Christian Mesh
0178912104 Refactor the provider installer loop (#2695)
Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
2025-04-24 05:57:07 -04:00
Martin Atkins
55855fca70 getproviders: Unify package authentication with hash lock selection
As discussed in opentofu/opentofu#2656, this consolidates the two concerns
of the PackageAuthentication interface into a single function that deals
both with package authentication _and_ with reporting all of the package
hashes that were used to make the authentication decision.

This means that any .zip archive that OpenTofu directly verifies during
installation can now have its hash recorded in the dependency lock file
even if that package didn't come from the provider's origin registry, which
is beneficial when the first installation of a provider comes from a
secondary ("mirror") source because it creates an additional hook by which
that dependency lock file entry can be "upgraded" to be complete in a
future "tofu init" run against the origin registry, or by the
"tofu providers lock" command.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2025-04-14 08:31:40 -07:00
Christian Mesh
6f04489f23 Typo: consolodation -> consolidation (#2587)
Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
2025-03-12 11:47:47 -04:00
Andrei Ciobanu
dfe2876931 Add a new warning when a provider cannot be downloaded and it was requested by an implicit usage (#2479)
Signed-off-by: yottta <andrei.ciobanu@opentofu.org>
Co-authored-by: AbstractionFactory <179820029+abstractionfactory@users.noreply.github.com>
2025-02-12 18:40:54 +02:00
Sekiranda Hamza
211ec55a30 Skip reading encryption keys on tofu init with -backend=false flag set (#2293)
Signed-off-by: Sekiranda <sekirandahamza@gmail.com>
2024-12-20 10:45:12 +02:00
Christian Mesh
12ed264597 Warning and Error consolidation CLI options (#1894)
Signed-off-by: Hefeweizen <jmales@gmail.com>
Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
Signed-off-by: Syasusu <syasusu@163.com>
Co-authored-by: Hefeweizen <jmales@gmail.com>
Co-authored-by: Syasusu <syasusu@163.com>
2024-09-23 07:31:06 -04:00
Nathan Baulch
9b7bec31b4 Another batch of minor typos (#1953)
Signed-off-by: Nathan Baulch <nathan.baulch@gmail.com>
2024-09-09 07:51:39 -04:00
Burak Şen
7a02fad996 Fix repetitive diagnosis output in init response (#1890)
Signed-off-by: buraksenn <buraksenb@gmail.com>
2024-09-05 07:36:18 -04:00
Nathan Baulch
ea558d9d4b Fix typos (#1905)
Signed-off-by: Nathan Baulch <nathan.baulch@gmail.com>
Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
Co-authored-by: Christian Mesh <christianmesh1@gmail.com>
2024-08-29 13:20:33 -04:00
Christian Mesh
ee75b40d14 Update help text for static variable support (#1804)
Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
2024-07-11 11:00:18 -04:00
Arel Rabinowitz
5667df4969 Change link for missing module or missing provider (#1724)
Signed-off-by: RLRabinowitz <rlrabinowitz2@gmail.com>
2024-06-16 11:46:37 -04:00
Syasusu
bdab86962f feat: init and get command support json format output (#1453)
Signed-off-by: Syasusu <syasusu@163.com>
2024-04-02 12:32:37 -04:00
Christian Mesh
586c45fe5a Refactor encryption configuration (#1387)
Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
2024-03-13 10:58:52 -04:00
Christian Mesh
5ab6167bbf Initial wiring of encryption through the command package (#1316)
Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
2024-03-07 08:55:57 -05:00
Christian Mesh
2f5dcd5c0a Integrate Encryption into State Backends (#1288)
Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
2024-03-04 09:25:14 -05:00
namgyalangmo
cb2e9119aa Update copyright notice (#1232)
Signed-off-by: namgyalangmo <75657887+namgyalangmo@users.noreply.github.com>
2024-02-08 09:48:59 +00:00
Christian Mesh
78464f251a Add a warning when multiple likely forks of a provider are detected (#1009)
Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
2023-12-14 12:21:16 -05:00
Christian Mesh
f1a57344bd Improve unclear provider resolver error messages (#1003)
Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
2023-12-12 13:10:16 -05:00
Christian Mesh
47071c2696 904 missing provider module message (#981)
Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
2023-12-07 10:34:50 -05:00
HaojueWang
084c5dfc8b keep init wording consistent in internal/command/init.go (#855)
Signed-off-by: haojue <acewhj@gmail.com>
2023-11-13 10:03:25 -05:00
James Humphries
9c24b6183a Revert PRs that introduced propogating contexts (#835)
Co-authored-by: Dmitry Kisler <admin@dkisler.com>
2023-11-08 21:09:14 +00:00
Kuba Martin
9c789368dc Stop reading providers from the HashiCorp registry based on the statefile if not specified in code. (#773)
Signed-off-by: Jakub Martin <kubam@spacelift.io>
2023-10-27 16:23:41 +02:00
Marcin Wyszynski
275dd116f9 Pass context to all remote.Client operations (#786)
Signed-off-by: Marcin Wyszynski <marcin.pixie@gmail.com>
2023-10-25 12:37:58 +02:00
Marcin Wyszynski
a6a54c3777 Make backend.StateMgr accept a context (#780)
Signed-off-by: Marcin Wyszynski <marcin.pixie@gmail.com>
2023-10-24 15:47:46 +02:00
Marcin Wyszynski
bda32938e4 Make backend.ConfigSchema accept a context (#776)
Signed-off-by: Marcin Wyszynski <marcin.pixie@gmail.com>
2023-10-24 13:14:01 +02:00
RLRabinowitz
8967cdcee7 Remove all Terraform Cloud mentions in STDOUTs in the codebase (#703) 2023-10-12 12:07:16 +01:00
Elbaz
01cf4cda3f Update placeholderplaceholderplaceholder occurrences to our doc site (#684) 2023-10-10 12:54:03 +03:00
Mikhail Sliusarev
60a9db0a43 Alter the help message for test command to print the expected behaviour (#622) 2023-10-03 11:17:26 +03:00
Lars Lehtonen
6cd1c00e1d internal/command: demote modulePath() export (#617)
Signed-off-by: Lars Lehtonen <lars.lehtonen@gmail.com>
2023-09-29 11:23:50 +02:00
Dmitry Kisler
a127607a85 Rename terraform to tofu in GoString method and docstrings (#576)
Signed-off-by: Dmitry Kisler <admin@dkisler.com>
2023-09-26 19:09:27 +02:00
Yaron Yarimi
487d9bc6a4 Rename multiple packages to OpenTofu (addrs, backend, command) (#506) 2023-09-21 15:38:46 +03:00
Yaron Yarimi
794e3413bb Rename opentf package to tofu (#466) 2023-09-20 15:16:53 +03:00
Yaron Yarimi
c8acedd885 Rename github.com/placeholderplaceholderplaceholder/opentf to github.com/opentofu/opentofu (#461) 2023-09-20 14:35:35 +03:00
Lars Lehtonen
c2fd0562c1 internal/builtin internal/cloud internal/command Wrap Errors (#413) 2023-09-18 15:16:17 +03:00
James Humphries
0654ffd0dd [RFC Implementation] Bypass gpg validation of packages if the registry does not provide any keys (#309) 2023-09-14 13:55:01 +02:00
RLRabinowitz
1b36b9262a Merge pull request #200 from opentffoundation/internal-opentf
Change `internal/terraform` to `internal/opentf`
2023-08-28 15:48:46 +03:00