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

Refactor bundle init #2074

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open

Conversation

shreyas-goenka
Copy link
Contributor

@shreyas-goenka shreyas-goenka commented Jan 3, 2025

Summary of changes

This PR introduces three new abstractions:

  1. Resolver: Resolves which reader and writer to use for a template.
  2. Writer: Writes a template project to disk. Prompts the user if necessary.
  3. Reader: Reads a template specification from disk, built into the CLI or from GitHub.

Introducing these abstractions helps decouple reading a template from writing it. When I tried adding telemetry for the bundle init command, I noticed that the code in cmd/init.go was getting convoluted and hard to test. A future change could have accidentally logged PII when a user initialised a custom template.

Hedging against that risk is important here because we use a generic untyped map<string, string> representation in the backend to log telemetry for the databricks bundle init. Otherwise, we risk accidentally breaking our compliance with our centralization requirements.

Details

After this PR there are two classes of templates that can be initialized:

  1. A databricks template: This could be a builtin template or a template outside the CLI like mlops-stacks, which is still owned and managed by Databricks. These templates log their telemetry arguments and template name.
  2. A custom template: These are templates created by and managed by the end user. In these templates we do not log the template name and args. Instead a generic placeholder string of "custom" is logged in our telemetry system.

NOTE: The functionality of the databricks bundle init command remains the same after this PR. Only the internal abstractions used are changed.

Tests

New unit tests. Existing golden and unit tests. Also a fair bit of manual testing.

Copy link

github-actions bot commented Jan 3, 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: 2074
  • Commit SHA: ed24f2880b101b8a671f681d9e87ea332aa33ebd

Checks will be approved automatically on success.

}

// Return template descriptions for command-line help
func nativeTemplateHelpDescriptions() string {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved totemplate.HelpDescriptions()

}

// Return template options for an interactive prompt
func nativeTemplateOptions() []cmdio.Tuple {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved to template.options()

return names
}

func getNativeTemplateByDescription(description string) string {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved inline to template. SelectTemplate()

return ""
}

func getUrlForNativeTemplate(name string) string {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replaced by template.Get()

return ""
}

func getFsForNativeTemplate(name string) (fs.FS, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replaced by template.Reader.FS() method.

return parts[len(parts)-1]
}

func constructOutputFiler(ctx context.Context, outputDir string) (filer.Filer, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved to template.constructOutputFiler. Also added unit tests for it in TestDefaultWriterConfigure and TestDefaultWriterConfigureOnDBR

@@ -198,92 +38,32 @@ See https://docs.databricks.com/en/dev-tools/bundles/templates.html for more inf

cmd.PreRunE = root.MustWorkspaceClient
cmd.RunE = func(cmd *cobra.Command, args []string) error {
if tag != "" && branch != "" {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved over to template.Resolver.Resolve. This allows us to easily write unit tests for the function.

@@ -16,7 +16,7 @@ import (
"github.com/databricks/databricks-sdk-go/service/workspace"
)

type workspaceFilesExtensionsClient struct {
type WorkspaceFilesExtensionsClient struct {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Made public for assertion in TestDefaultWriterConfigureOnDBR

// configFilePath: file path containing user defined config values
// templateFS: root of the template definition
// outputFiler: filer to use for writing the initialized template
func Materialize(ctx context.Context, configFilePath string, templateFS fs.FS, outputFiler filer.Filer) error {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

logic Moved to defaultWriter{} struct in this library

type Writer interface {
Configure(ctx context.Context, configPath, outputDir string) error
Materialize(ctx context.Context, r Reader) error
LogTelemetry(ctx context.Context) error
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This function does nothing right now. Telemetry functionality will be added in a followup PR.

@shreyas-goenka shreyas-goenka marked this pull request as ready for review January 6, 2025 13:42
@shreyas-goenka
Copy link
Contributor Author

Integration tests were green, and after a while, I made a few linting commits. Triggered another run of the integration tests.

Copy link
Contributor

@denik denik left a comment

Choose a reason for hiding this comment

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

It's not completely clear what parts are just moved and which are new, could you summarize that in PR description?

libs/template/reader.go Outdated Show resolved Hide resolved
libs/template/reader_test.go Outdated Show resolved Hide resolved
libs/template/resolver_test.go Outdated Show resolved Hide resolved
@shreyas-goenka shreyas-goenka requested a review from denik January 9, 2025 11:17
@shreyas-goenka
Copy link
Contributor Author

@denik Thanks! I redid the PR summary. Hopefully that makes it more legible what exactly changed.

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.

2 participants