Skip to content

Commit

Permalink
dockercompose: Add ability to suppress automatic links in UI (#6270) (#…
Browse files Browse the repository at this point in the history
…6328)

* dockercompose: Add ability to suppress automatic links in UI (#6270)

Signed-off-by: Kinland <16787581+kinland@users.noreply.github.com>

* linting: resolve linting issue leftover from legacy code
The linting spacing rules must have changed since the last time pkg/model/docker_compose_target.go was touched.

Signed-off-by: Kinland <16787581+kinland@users.noreply.github.com>

* dockercompose: Fix default value of infer_links, which should have been true (#6270)

Signed-off-by: Kinland <16787581+kinland@users.noreply.github.com>

* dockercompose: add test for inferLinks=false, fix ignored links on DC ports test (#6270)

Signed-off-by: Kinland <16787581+kinland@users.noreply.github.com>

---------

Signed-off-by: Kinland <16787581+kinland@users.noreply.github.com>
  • Loading branch information
kinland authored Mar 6, 2024
1 parent 10dd412 commit b096000
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 10 deletions.
9 changes: 7 additions & 2 deletions internal/store/engine_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -860,12 +860,15 @@ func ManifestTargetEndpoints(mt *ManifestTarget) (endpoints []model.Link) {
if mt.Manifest.IsDC() {
hostPorts := make(map[int32]bool)
publishedPorts := mt.Manifest.DockerComposeTarget().PublishedPorts()
inferLinks := mt.Manifest.DockerComposeTarget().InferLinks()
for _, p := range publishedPorts {
if p == 0 || hostPorts[int32(p)] {
continue
}
hostPorts[int32(p)] = true
endpoints = append(endpoints, model.MustNewLink(fmt.Sprintf("http://localhost:%d/", p), ""))
if inferLinks {
endpoints = append(endpoints, model.MustNewLink(fmt.Sprintf("http://localhost:%d/", p), ""))
}
}

for _, binding := range mt.State.DCRuntimeState().Ports {
Expand All @@ -876,7 +879,9 @@ func ManifestTargetEndpoints(mt *ManifestTarget) (endpoints []model.Link) {
continue
}
hostPorts[p] = true
endpoints = append(endpoints, model.MustNewLink(fmt.Sprintf("http://localhost:%d/", p), ""))
if inferLinks {
endpoints = append(endpoints, model.MustNewLink(fmt.Sprintf("http://localhost:%d/", p), ""))
}
}

endpoints = append(endpoints, mt.Manifest.DockerComposeTarget().Links...)
Expand Down
59 changes: 52 additions & 7 deletions internal/store/engine_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ type endpointsCase struct {
dcPublishedPorts []int
dcPortBindings []v1alpha1.DockerPortBinding

k8sResLinks []model.Link
localResLinks []model.Link
dcResLinks []model.Link
k8sResLinks []model.Link
localResLinks []model.Link
dcResLinks []model.Link
dcDoNotInferLinks bool
}

func (c endpointsCase) validate() {
Expand Down Expand Up @@ -163,10 +164,38 @@ func TestManifestTargetEndpoints(t *testing.T) {
model.MustNewLink("http://localhost:7000/", ""),
},
dcPublishedPorts: []int{8000, 7000},
},
{
name: "docker compose ports and links",
expected: []model.Link{
model.MustNewLink("http://localhost:8000/", ""),
model.MustNewLink("http://localhost:7000/", ""),
model.MustNewLink("www.apple.edu", "apple"),
model.MustNewLink("www.zombo.com", "zombo"),
},
dcPublishedPorts: []int{8000, 7000},
dcResLinks: []model.Link{
model.MustNewLink("www.apple.edu", "apple"),
model.MustNewLink("www.zombo.com", "zombo"),
},
},
{
name: "docker compose ports with inferLinks=false",
dcPublishedPorts: []int{8000, 7000},
dcDoNotInferLinks: true,
},
{
name: "docker compose ports and links with inferLinks=false",
expected: []model.Link{
model.MustNewLink("www.apple.edu", "apple"),
model.MustNewLink("www.zombo.com", "zombo"),
},
dcPublishedPorts: []int{8000, 7000},
dcResLinks: []model.Link{
model.MustNewLink("www.apple.edu", "apple"),
model.MustNewLink("www.zombo.com", "zombo"),
},
dcDoNotInferLinks: true,
},
{
name: "docker compose dynamic ports",
Expand Down Expand Up @@ -246,10 +275,26 @@ func TestManifestTargetEndpoints(t *testing.T) {
})
} else if len(c.localResLinks) > 0 {
m = m.WithDeployTarget(model.LocalTarget{Links: c.localResLinks})
} else if len(c.dcPublishedPorts) > 0 {
m = m.WithDeployTarget(model.DockerComposeTarget{}.WithPublishedPorts(c.dcPublishedPorts))
} else if len(c.dcResLinks) > 0 {
m = m.WithDeployTarget(model.DockerComposeTarget{Links: c.dcResLinks})
}

isDC := len(c.dcPublishedPorts) > 0 || len(c.dcResLinks) > 0

if isDC {
dockerDeployTarget := model.DockerComposeTarget{}

if len(c.dcPublishedPorts) > 0 {
dockerDeployTarget = dockerDeployTarget.WithPublishedPorts(c.dcPublishedPorts)
}

if len(c.dcResLinks) > 0 {
dockerDeployTarget.Links = c.dcResLinks
}

if c.dcDoNotInferLinks {
dockerDeployTarget = dockerDeployTarget.WithInferLinks(false)
}

m = m.WithDeployTarget(dockerDeployTarget)
}

if len(c.dcPortBindings) > 0 && !m.IsDC() {
Expand Down
4 changes: 3 additions & 1 deletion internal/tiltfile/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,8 @@ def dc_resource(name: str,
labels: Union[str, List[str]] = [],
auto_init: bool = True,
project_name: str = "",
new_name: str = "") -> None:
new_name: str = "",
infer_links: bool = True) -> None:
"""Configures the Docker Compose resource of the given name. Note: Tilt does an amount of resource configuration
for you(for more info, see `Tiltfile Concepts: Resources <tiltfile_concepts.html#resources>`_); you only need
to invoke this function if you want to configure your resource beyond what Tilt does automatically.
Expand All @@ -454,6 +455,7 @@ def dc_resource(name: str,
project_name: The Docker Compose project name to match the corresponding project loaded by
``docker_compose``, if necessary for disambiguation.
new_name: If non-empty, will be used as the new name for this resource.
infer_links: whether to include the default localhost links. Defaults to ``True``. If ``False``, only links explicitly provided via the links argument will be displayed.
"""

pass
Expand Down
11 changes: 11 additions & 0 deletions internal/tiltfile/docker_compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ func (s *tiltfileState) dcResource(thread *starlark.Thread, fn *starlark.Builtin
var imageVal starlark.Value
var triggerMode triggerMode
var resourceDepsVal starlark.Sequence
var inferLinks = value.Optional[starlark.Bool]{Value: true}
var links links.LinkList
var labels value.LabelSet
var autoInit = value.Optional[starlark.Bool]{Value: true}
Expand All @@ -223,6 +224,7 @@ func (s *tiltfileState) dcResource(thread *starlark.Thread, fn *starlark.Builtin
"image?", &imageVal,
"trigger_mode?", &triggerMode,
"resource_deps?", &resourceDepsVal,
"infer_links?", &inferLinks,
"links?", &links,
"labels?", &labels,
"auto_init?", &autoInit,
Expand Down Expand Up @@ -267,6 +269,10 @@ func (s *tiltfileState) dcResource(thread *starlark.Thread, fn *starlark.Builtin
options.TriggerMode = triggerMode
}

if inferLinks.IsSet {
options.InferLinks = inferLinks
}

options.Links = append(options.Links, links.Links...)

for key, val := range labels.Values {
Expand Down Expand Up @@ -383,6 +389,7 @@ type dcService struct {
type dcResourceOptions struct {
imageRefFromUser reference.Named
TriggerMode triggerMode
InferLinks value.Optional[starlark.Bool]
Links []model.Link
AutoInit value.Optional[starlark.Bool]

Expand Down Expand Up @@ -501,6 +508,10 @@ func (s *tiltfileState) dcServiceToManifest(service *dcService, dcSet *dcResourc
}.WithImageMapDeps(model.FilterLiveUpdateOnly(service.ImageMapDeps, iTargets)).
WithPublishedPorts(service.PublishedPorts)

if options.InferLinks.IsSet {
dcInfo = dcInfo.WithInferLinks(bool(options.InferLinks.Value))
}

autoInit := true
if options.AutoInit.IsSet {
autoInit = bool(options.AutoInit.Value)
Expand Down
19 changes: 19 additions & 0 deletions pkg/model/docker_compose_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ type DockerComposeTarget struct {

publishedPorts []int

inferLinks struct {
IsSet bool
Value bool
}

Links []Link
}

Expand Down Expand Up @@ -49,6 +54,20 @@ func (t DockerComposeTarget) PublishedPorts() []int {
return append([]int{}, t.publishedPorts...)
}

func (t DockerComposeTarget) InferLinks() bool {
if t.inferLinks.IsSet {
return t.inferLinks.Value
} else {
return true
}
}

func (t DockerComposeTarget) WithInferLinks(inferLinks bool) DockerComposeTarget {
t.inferLinks.IsSet = true
t.inferLinks.Value = inferLinks
return t
}

func (t DockerComposeTarget) WithLinks(links []Link) DockerComposeTarget {
t.Links = links
return t
Expand Down

0 comments on commit b096000

Please sign in to comment.