Repository navigation
[WSLC Compose 1/5] Establish project and resource identity #42086
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
Open
David Bennett (dkbennett)
wants to merge
4
commits into
feature/compose
Choose a base branch
from
user/dkbennett/compose-identity
base: feature/compose
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // Copyright (C) Microsoft Corporation. All rights reserved. | ||
|
|
||
| #pragma once | ||
|
|
||
| namespace wsl::windows::service::wslc::compose::label { | ||
|
|
||
| inline constexpr char c_project[] = "com.docker.compose.project"; | ||
| inline constexpr char c_service[] = "com.docker.compose.service"; | ||
| inline constexpr char c_containerNumber[] = "com.docker.compose.container-number"; | ||
| inline constexpr char c_oneoff[] = "com.docker.compose.oneoff"; | ||
| inline constexpr char c_network[] = "com.docker.compose.network"; | ||
| inline constexpr char c_managed[] = "com.microsoft.wslc.compose.managed"; | ||
| inline constexpr char c_managedValue[] = "true"; | ||
| inline constexpr char c_metadataVersion[] = "com.microsoft.wslc.compose.metadata-version"; | ||
| inline constexpr char c_metadataVersionValue[] = "1"; | ||
|
|
||
| } // namespace wsl::windows::service::wslc::compose::label |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| // Copyright (C) Microsoft Corporation. All rights reserved. | ||
|
|
||
| #include "precomp.h" | ||
| #include "ComposeNormalizer.h" | ||
| #include "ResourceName.h" | ||
|
|
||
| namespace wsl::windows::service::wslc { | ||
|
|
||
| namespace { | ||
|
|
||
| std::string NormalizeProjectName(std::string_view name) | ||
| { | ||
| std::string result{name}; | ||
| std::ranges::transform(result, result.begin(), [](unsigned char value) { return static_cast<char>(std::tolower(value)); }); | ||
| std::erase_if(result, [](unsigned char value) { return !std::isalnum(value) && value != '-' && value != '_'; }); | ||
| while (!result.empty() && !std::isalnum(static_cast<unsigned char>(result.front()))) | ||
| { | ||
| result.erase(result.begin()); | ||
| } | ||
|
|
||
| THROW_HR_IF(E_INVALIDARG, result.empty()); | ||
| THROW_HR_IF(E_INVALIDARG, result.size() > WSLC_MAX_COMPOSE_PROJECT_NAME_LENGTH); | ||
| return result; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| ComposeSpec ComposeNormalizer::Normalize(const ComposeDocuments& documents, const ComposeProjectSelection& selection) | ||
| { | ||
| THROW_HR_IF(E_INVALIDARG, documents.SchemaVersion != WSLC_COMPOSE_SCHEMA_VERSION); | ||
| THROW_HR_IF(E_INVALIDARG, documents.WorkingDirectory.empty()); | ||
| THROW_HR_IF(E_INVALIDARG, documents.ProjectDirectory.empty()); | ||
| THROW_HR_IF(E_INVALIDARG, documents.Documents.size() != 1); | ||
| ValidateSelection(selection); | ||
|
|
||
| const auto& document = documents.Documents.front(); | ||
| THROW_HR_IF(E_INVALIDARG, document.SourcePath.empty()); | ||
| THROW_HR_IF(E_INVALIDARG, document.BaseDirectory.empty()); | ||
| THROW_HR_IF(E_INVALIDARG, document.Content.empty()); | ||
|
|
||
| std::string content(reinterpret_cast<const char*>(document.Content.data()), document.Content.size()); | ||
| THROW_HR_IF(E_INVALIDARG, content.find('\0') != std::string::npos); | ||
|
|
||
| auto spec = ComposeSpec::Parse(document.SourcePath, content); | ||
|
|
||
| std::string projectName; | ||
| if (documents.ExplicitProjectName.has_value()) | ||
| { | ||
| projectName = NormalizeProjectName(*documents.ExplicitProjectName); | ||
| } | ||
| else | ||
| { | ||
| auto projectDirectory = documents.ProjectDirectory.lexically_normal(); | ||
| if (projectDirectory.filename().empty()) | ||
| { | ||
| projectDirectory = projectDirectory.parent_path(); | ||
| } | ||
|
|
||
| projectName = NormalizeProjectName(wsl::shared::string::WideToMultiByte(projectDirectory.filename().wstring())); | ||
| } | ||
|
|
||
| spec.ProjectName = std::move(projectName); | ||
| ValidateName(DefaultNetworkName(spec.ProjectName).c_str(), WSLC_MAX_NETWORK_NAME_LENGTH); | ||
| for (auto& container : spec.Containers) | ||
| { | ||
| if (container.Name.empty()) | ||
|
Copilot marked this conversation as resolved.
Resolved
|
||
| { | ||
| container.Name = std::format("{}-{}-1", spec.ProjectName, container.ServiceName); | ||
| } | ||
|
|
||
| ValidateName(container.Name.c_str(), WSLC_MAX_CONTAINER_NAME_LENGTH); | ||
|
dkbennett marked this conversation as resolved.
Resolved
|
||
| } | ||
|
dkbennett marked this conversation as resolved.
Resolved
|
||
|
|
||
| return spec; | ||
| } | ||
|
|
||
| std::string ComposeNormalizer::DefaultNetworkName(std::string_view projectName) | ||
| { | ||
| return std::format("{}_default", projectName); | ||
| } | ||
|
|
||
| void ComposeNormalizer::ValidateSelection(const ComposeProjectSelection& selection) | ||
| { | ||
| THROW_HR_IF(E_NOTIMPL, !selection.Profiles.empty() || !selection.Services.empty() || selection.IncludeDependencies); | ||
| } | ||
|
|
||
| std::string ComposeNormalizer::ValidateProjectKey(std::string_view projectKey) | ||
| { | ||
| THROW_HR_IF(E_INVALIDARG, !IsValidProjectKey(projectKey)); | ||
| return std::string{projectKey}; | ||
| } | ||
|
|
||
| bool ComposeNormalizer::IsValidProjectKey(std::string_view projectKey) noexcept | ||
| { | ||
| if (projectKey.empty() || projectKey.size() > WSLC_MAX_COMPOSE_PROJECT_NAME_LENGTH || | ||
| !std::isalnum(static_cast<unsigned char>(projectKey.front()))) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return std::ranges::all_of(projectKey, [](unsigned char value) { | ||
| return std::isdigit(value) || std::islower(value) || value == '-' || value == '_'; | ||
| }); | ||
| } | ||
|
|
||
| } // namespace wsl::windows::service::wslc | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // Copyright (C) Microsoft Corporation. All rights reserved. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "ComposeSpec.h" | ||
| #include "wslc.h" | ||
| #include <optional> | ||
|
|
||
| namespace wsl::windows::service::wslc { | ||
|
|
||
| struct ComposeDocument | ||
| { | ||
| std::filesystem::path SourcePath; | ||
| std::filesystem::path BaseDirectory; | ||
| std::vector<std::byte> Content; | ||
| }; | ||
|
|
||
| struct ComposeDocuments | ||
| { | ||
| ULONG SchemaVersion{}; | ||
| std::filesystem::path WorkingDirectory; | ||
| std::filesystem::path ProjectDirectory; | ||
| std::optional<std::string> ExplicitProjectName; | ||
| std::vector<ComposeDocument> Documents; | ||
| }; | ||
|
|
||
| struct ComposeProjectSelection | ||
| { | ||
| std::vector<std::string> Profiles; | ||
| std::vector<std::string> Services; | ||
| bool IncludeDependencies{}; | ||
| }; | ||
|
|
||
| struct ComposeNormalizer | ||
| { | ||
| static ComposeSpec Normalize(const ComposeDocuments& documents, const ComposeProjectSelection& selection); | ||
| static std::string DefaultNetworkName(std::string_view projectName); | ||
| static bool IsValidProjectKey(std::string_view projectKey) noexcept; | ||
| static std::string ValidateProjectKey(std::string_view projectKey); | ||
| static void ValidateSelection(const ComposeProjectSelection& selection); | ||
| }; | ||
|
|
||
| } // namespace wsl::windows::service::wslc |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sold on using the docker scheme for these. I think it would be safer to use "com.microsoft.wslc.compose.*" prefixes for everything, but this is a constant easily changed.