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

Handle ${workspace.file_path} references in source-linked deployments #2046

Merged
merged 10 commits into from
Jan 8, 2025

Conversation

ilyakuz-db
Copy link
Contributor

@ilyakuz-db ilyakuz-db commented Dec 23, 2024

Changes

  1. Updates workspace.file_path during variable resolution phase in source-linked deployment to address cases like this https://github.com/databricks/bundle-examples/blob/main/default_python/resources/default_python_pipeline.yml#L13
  2. Updates workspace.file_path in metadata.json
  3. Prints warning for users when workspace.file_path is explicitly set but deploy is running in source-linked mode

Tests

Unit test

@ilyakuz-db
Copy link
Contributor Author

I moved source-linked deployment preset handler in separate mutator and now it runs before variable resolution step

One caveat is that this mutator uses development mode setting but it runs before ProcessTargetMode so it checks b.Config.Bundle.Mode == config.Development without any additional validations

Copy link

github-actions bot commented Jan 2, 2025

If integration tests don't run automatically, an authorized user can run them manually by following the instructions below:

Trigger:
go/deco-tests-run/cli

Inputs:

  • PR number: 2046
  • Commit SHA: a65dc982a955cde543eb7503de86f683dd30c499

Checks will be approved automatically on success.

@@ -41,6 +41,10 @@ func Initialize() bundle.Mutator {
mutator.PopulateCurrentUser(),
mutator.LoadGitDetails(),

// This mutator needs to be run before variable interpolation and defining default workspace paths
// because it affects how workspace variables are resolved.
mutator.ApplySourceLinkedDeploymentPreset(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved all source-linked initialization logic to separate mutator which will is running before workspace paths is defined and variables are resolved

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is concerning -- it decentralizes how we deal with mode: development and I'd like to keep that in one place. I understand why you need to do this here, and we can merge the PR to unblock the issue in the workspace, but we should revert to the original place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pietern what do you think would be the best approach here?

We have several constraint that are not super compatible

  1. we need to enable/disable source-linked before variable resolution phase (to properly compute file_path)
  2. source-linked deployment needs to be enabled in dev mode
  3. dev mode validation relies on workspace paths, which may include variables (see findNonUserPath)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of intercepting interpolation of ${workspace.file_path} we could perform a modification pass after variable resolution where we replace instances of the value of this variable. Would that work?

cc @denik

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep on hold until @denik starts work on variable resolution v2 to see what would be the better approach

b.Config.Presets.SourceLinkedDeployment = &enabled
}

if b.Config.Workspace.FilePath != "" && config.IsExplicitlyEnabled(b.Config.Presets.SourceLinkedDeployment) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added warning for cases when workspace.file_path is defined by user but deploy is running in source-linked mode

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please include a comment saying that this mutator runs before these fields are defaulted. I was puzzled by this but then found it is run before the mutators that populate its default value.

Copy link
Contributor

@andrewnester andrewnester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM @pietern @denik can you take a look as well?

)

disabled := false
b.Config.Presets.SourceLinkedDeployment = &disabled
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SInce you're mutating the config here, you'd have to call b.Config.Mutate and do all the mutation inside the callback there

Copy link
Contributor Author

@ilyakuz-db ilyakuz-db Jan 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I will update it! Could you elaborate what is the difference between this mutation and normal assignment to struct. Seems that it works good in both cases but I may miss some context

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed with Pieter that it's better to skip dynamic assignment here in favor of simplicity, but I can update it in another PR if it doesn't feel right

@pietern pietern changed the title fix: Handle ${workspace.file_path references} in source-linked deploy… Handle ${workspace.file_path} references in source-linked deployments Jan 8, 2025
@pietern pietern changed the title Handle ${workspace.file_path} references in source-linked deployments Handle ${workspace.file_path} references in source-linked deployments Jan 8, 2025

if config.IsExplicitlyEnabled((b.Config.Presets.SourceLinkedDeployment)) {
if !isDatabricksWorkspace {
path := dyn.NewPath(dyn.Key("targets"), dyn.Key(target), dyn.Key("presets"), dyn.Key("source_linked_deployment"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work -- the targets field is cleared after selecting it.

But because the target is merged into the main configuration, you can use presets.source_linked_deployment directly and it will return the original location.

b.Config.Presets.SourceLinkedDeployment = &enabled
}

if b.Config.Workspace.FilePath != "" && config.IsExplicitlyEnabled(b.Config.Presets.SourceLinkedDeployment) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please include a comment saying that this mutator runs before these fields are defaulted. I was puzzled by this but then found it is run before the mutators that populate its default value.

diags = diags.Append(
diag.Diagnostic{
Severity: diag.Warning,
Summary: "workspace.file_path setting will be ignored in source-linked deployment mode",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This warning should have a Detail section explaining the issue further.

}

if b.Config.Workspace.FilePath != "" && config.IsExplicitlyEnabled(b.Config.Presets.SourceLinkedDeployment) {
path := dyn.NewPath(dyn.Key("targets"), dyn.Key(target), dyn.Key("workspace"), dyn.Key("file_path"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue as above w.r.t. the path not existing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like it works fine, or do we expect other behavior here?

image image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I was confused by the path including targets.<target>.

This call site and the one above index it with path[2:].

It looks like it is not used in full, so the first two elements can be removed from the assignment.

@@ -41,6 +41,10 @@ func Initialize() bundle.Mutator {
mutator.PopulateCurrentUser(),
mutator.LoadGitDetails(),

// This mutator needs to be run before variable interpolation and defining default workspace paths
// because it affects how workspace variables are resolved.
mutator.ApplySourceLinkedDeploymentPreset(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is concerning -- it decentralizes how we deal with mode: development and I'd like to keep that in one place. I understand why you need to do this here, and we can merge the PR to unblock the issue in the workspace, but we should revert to the original place.

@@ -54,5 +54,8 @@ func (m *compute) Apply(_ context.Context, b *bundle.Bundle) diag.Diagnostics {

// Set file upload destination of the bundle in metadata
b.Metadata.Config.Workspace.FilePath = b.Config.Workspace.FilePath
if config.IsExplicitlyEnabled(b.Config.Presets.SourceLinkedDeployment) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing comment why this is done.

@pietern pietern added this pull request to the merge queue Jan 8, 2025
Merged via the queue into main with commit 0289bec Jan 8, 2025
9 checks passed
@pietern pietern deleted the feat/source-linked-deployment-file-path branch January 8, 2025 12:50
pietern added a commit that referenced this pull request Jan 8, 2025
Bundles:
 * Fix finding Python within virtualenv on Windows ([#2034](#2034)).
 * Include missing field descriptions in JSON schema ([#2045](#2045)).
 * Add validation for volume referenced from `artifact_path` ([#2050](#2050)).
 * Handle `${workspace.file_path}` references in source-linked deployments ([#2046](#2046)).
 * Set the write bit for files written during template initialization ([#2068](#2068)).
github-merge-queue bot pushed a commit that referenced this pull request Jan 8, 2025
Bundles:
* Fix finding Python within virtualenv on Windows
([#2034](#2034)).
* Include missing field descriptions in JSON schema
([#2045](#2045)).
* Add validation for volume referenced from `artifact_path`
([#2050](#2050)).
* Handle `${workspace.file_path}` references in source-linked
deployments ([#2046](#2046)).
* Set the write bit for files written during template initialization
([#2068](#2068)).
func lookup(v dyn.Value, path dyn.Path, b *bundle.Bundle) (dyn.Value, error) {
if config.IsExplicitlyEnabled(b.Config.Presets.SourceLinkedDeployment) {
if path.String() == "workspace.file_path" {
return dyn.V(b.SyncRootPath), nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why could not we just set bundle.workspace.file_path to b.SyncRootPath and let it be resolved naturally?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I explained it here, tldr: overriding file path is nice but with new changes there is a risk to affect source files accidentally

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but we can revisit that

github-merge-queue bot pushed a commit that referenced this pull request Jan 20, 2025
…th specified (#2119)

## Changes

Resolves remaining comments from here
#2046


[This](#2046 (comment))
and
[this](#2046 (comment))
are on hold until Pieter's response

## Tests
<!-- How is this tested? -->
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.

4 participants