-
Notifications
You must be signed in to change notification settings - Fork 5.8k
config: add --filter to select services by profile or label #14046
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
ndeloof
wants to merge
1
commit into
docker:main
Choose a base branch
from
ndeloof:config-services-filter
base: main
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 all commits
Commits
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
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
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,20 @@ | ||
| services: | ||
| core: | ||
| image: alpine | ||
| labels: | ||
| tier: backend | ||
|
|
||
| worker: | ||
| image: alpine | ||
| profiles: | ||
| - workers | ||
| labels: | ||
| tier: backend | ||
|
|
||
| monitor: | ||
| image: alpine | ||
| profiles: | ||
| - monitoring | ||
| - workers | ||
| labels: | ||
| tier: ops |
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,132 @@ | ||
| /* | ||
| Copyright 2026 Docker Compose CLI authors | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| // Package filter implements criteria=value expressions selecting a subset of | ||
| // a Compose application's services, so that the selection logic can be | ||
| // shared by every command operating on a subset of services. | ||
| package filter | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "slices" | ||
| "strings" | ||
|
|
||
| "github.com/compose-spec/compose-go/v2/types" | ||
| ) | ||
|
|
||
| const ( | ||
| // CriteriaProfile selects services declaring the given profile in their | ||
| // `profiles` attribute. Filtering on a profile implies activating it: | ||
| // matching services are searched among all the services of the model, | ||
| // whether or not their profile is otherwise active. | ||
| CriteriaProfile = "profile" | ||
| // CriteriaLabel selects services carrying the given label, expressed | ||
| // either as a bare KEY (any value) or as KEY=VALUE. | ||
| CriteriaLabel = "label" | ||
| ) | ||
|
|
||
| // Expression is a single parsed criteria=value selection expression. | ||
| type Expression struct { | ||
| Criteria string | ||
| Value string | ||
| } | ||
|
|
||
| // Filter is a set of selection expressions. Expressions with the same | ||
| // criteria are alternatives (OR); distinct criteria must all be satisfied | ||
| // (AND), following the `docker --filter` conventions. | ||
| type Filter []Expression | ||
|
|
||
| // Parse parses raw criteria=value expressions into a Filter. | ||
| func Parse(expressions []string) (Filter, error) { | ||
| var f Filter | ||
| for _, raw := range expressions { | ||
| criteria, value, ok := strings.Cut(raw, "=") | ||
| if !ok || value == "" { | ||
| return nil, fmt.Errorf("invalid filter %q: must be a criteria=value expression", raw) | ||
| } | ||
| switch criteria { | ||
| case CriteriaProfile: | ||
| if value == "*" { | ||
| return nil, fmt.Errorf("invalid filter %q: profiles must be selected explicitly", raw) | ||
| } | ||
| f = append(f, Expression{Criteria: criteria, Value: value}) | ||
| case CriteriaLabel: | ||
| f = append(f, Expression{Criteria: criteria, Value: value}) | ||
| default: | ||
| return nil, fmt.Errorf("invalid filter %q: unknown criteria %q (supported: %s, %s)", raw, criteria, CriteriaProfile, CriteriaLabel) | ||
| } | ||
| } | ||
| return f, nil | ||
| } | ||
|
|
||
| // Profiles returns the profiles named by profile= expressions, so that | ||
| // callers can activate them before matching: filtering on a profile implies | ||
| // activating it. | ||
| func (f Filter) Profiles() []string { | ||
| var profiles []string | ||
| for _, e := range f { | ||
| if e.Criteria == CriteriaProfile && !slices.Contains(profiles, e.Value) { | ||
| profiles = append(profiles, e.Value) | ||
| } | ||
| } | ||
| return profiles | ||
| } | ||
|
|
||
| // Match reports whether service satisfies every criteria of the filter, any | ||
| // expression of a criteria being sufficient for that criteria. | ||
| func (f Filter) Match(service types.ServiceConfig) bool { | ||
| byCriteria := map[string]bool{} | ||
| for _, e := range f { | ||
| byCriteria[e.Criteria] = byCriteria[e.Criteria] || e.match(service) | ||
| } | ||
| for _, matched := range byCriteria { | ||
| if !matched { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| func (e Expression) match(service types.ServiceConfig) bool { | ||
| switch e.Criteria { | ||
| case CriteriaProfile: | ||
| return slices.Contains(service.Profiles, e.Value) | ||
| case CriteriaLabel: | ||
| key, value, hasValue := strings.Cut(e.Value, "=") | ||
| label, ok := service.Labels[key] | ||
| if !ok { | ||
| return false | ||
| } | ||
| return !hasValue || label == value | ||
| default: | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| // SelectNames returns the sorted names of the project's enabled services | ||
| // satisfying the filter. Callers are expected to have activated the profiles | ||
| // returned by [Filter.Profiles] beforehand, e.g. with | ||
| // [types.Project.WithProfiles]. | ||
| func (f Filter) SelectNames(project *types.Project) []string { | ||
| var names []string | ||
| for name, service := range project.Services { | ||
| if f.Match(service) { | ||
| names = append(names, name) | ||
| } | ||
| } | ||
| slices.Sort(names) | ||
| return names | ||
| } |
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.
[medium]
profile=*filter silently produces incomplete results — profiled services are never activatedThe PR description states that
profile=*"selects every service declaring at least one profile" with no need for a separate--profileflag, but the implementation contradicts this.Filter.Profiles()explicitly excludes the wildcard withe.Value != "*", soWithProfilesis never called for theprofile=*case. SinceSelectNamesonly iterates overproject.Services(active services), any profiled service that was not already activated at load time silently stays inDisabledServicesand is invisible to the filter.The e2e test (
pkg/e2e/config_test.goline 134) actually confirms the workaround: it passes--profile "*"as a separate CLI flag alongside--filter profile=*to activate all profiles first. Without that extra flag, the filter returns an empty or incomplete list — the opposite of "no separate--profileis needed."Trigger path: A user runs
without any
--profileflag and expects all profiled services to appear. Instead they get only those whose profile happens to already be active (typically none beyond the default set).Fix: detect the wildcard in
runServices(or inFilter.Profiles()) and callproject.WithProfileswith all profiles the project declares before filtering:Or, simpler, change
Profiles()to return the sentinel"*"and let the caller pass it toWithProfiles(ifWithProfileshandles"*"already via the existing--profile "*"mechanism).