Skip to content

Commit bb94ea0

Browse files
committed
add support of depends_on.required attribute
Signed-off-by: Guillaume Lours <[email protected]>
1 parent 0938c7e commit bb94ea0

File tree

13 files changed

+58
-12
lines changed

13 files changed

+58
-12
lines changed

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/AlecAivazis/survey/v2 v2.3.7
77
github.com/Microsoft/go-winio v0.6.1
88
github.com/buger/goterm v1.0.4
9-
github.com/compose-spec/compose-go v1.16.0
9+
github.com/compose-spec/compose-go v1.17.0
1010
github.com/containerd/console v1.0.3
1111
github.com/containerd/containerd v1.7.2
1212
github.com/cucumber/godog v0.0.0-00010101000000-000000000000 // replaced; see replace for the actual version used
@@ -162,7 +162,8 @@ require (
162162
go.opentelemetry.io/otel/metric v0.37.0 // indirect
163163
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
164164
golang.org/x/crypto v0.7.0 // indirect
165-
golang.org/x/mod v0.9.0 // indirect
165+
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 // indirect
166+
golang.org/x/mod v0.11.0 // indirect
166167
golang.org/x/net v0.9.0 // indirect
167168
golang.org/x/oauth2 v0.7.0 // indirect
168169
golang.org/x/sys v0.7.0 // indirect

go.sum

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWH
131131
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
132132
github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4 h1:/inchEIKaYC1Akx+H+gqO04wryn5h75LSazbRlnya1k=
133133
github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb h1:EDmT6Q9Zs+SbUoc7Ik9EfrFqcylYqgPZ9ANSbTAntnE=
134-
github.com/compose-spec/compose-go v1.16.0 h1:HYk4uYWXgArHh6NG+WE4yGYayOXw+hjqJ+eJxpjWWjk=
135-
github.com/compose-spec/compose-go v1.16.0/go.mod h1:3yngGBGfls6FHGQsg4B1z6gz8ej9SOvmAJtxCwgbcnc=
134+
github.com/compose-spec/compose-go v1.17.0 h1:cvje90CU94dQyTnJoHJYjx9yE4Iggse1XmGcO3Qi5ts=
135+
github.com/compose-spec/compose-go v1.17.0/go.mod h1:zR2tP1+kZHi5vJz7PjpW6oMoDji/Js3GHjP+hfjf70Q=
136136
github.com/containerd/cgroups v1.1.0 h1:v8rEWFl6EoqHB+swVNjVoCJE8o3jX7e8nqBGPLaDFBM=
137137
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
138138
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
@@ -713,6 +713,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
713713
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
714714
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
715715
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
716+
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 h1:MGwJjxBy0HJshjDNfLsYO8xppfqWlA5ZT9OhtUUhTNw=
717+
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
716718
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
717719
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
718720
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
@@ -738,8 +740,8 @@ golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
738740
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
739741
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
740742
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
741-
golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs=
742-
golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
743+
golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU=
744+
golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
743745
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
744746
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
745747
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=

pkg/compose/compose.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ func (s *composeService) projectFromName(containers Containers, projectName stri
203203
condition := ServiceConditionRunningOrHealthy
204204
// Let's restart the dependency by default if we don't have the info stored in the label
205205
restart := true
206+
required := true
206207
dependency := dcArr[0]
207208

208209
// backward compatibility
@@ -212,7 +213,7 @@ func (s *composeService) projectFromName(containers Containers, projectName stri
212213
restart, _ = strconv.ParseBool(dcArr[2])
213214
}
214215
}
215-
service.DependsOn[dependency] = types.ServiceDependency{Condition: condition, Restart: restart}
216+
service.DependsOn[dependency] = types.ServiceDependency{Condition: condition, Restart: restart, Required: required}
216217
}
217218
}
218219
project.Services = append(project.Services, *service)

pkg/compose/convergence.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,9 @@ func shouldWaitForDependency(serviceName string, dependencyConfig types.ServiceD
362362
return false, nil
363363
}
364364
}
365+
if !dependencyConfig.Required {
366+
return false, nil
367+
}
365368
return false, err
366369
} else if service.Scale == 0 {
367370
// don't wait for the dependency which configured to have 0 containers running

pkg/compose/convergence_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ func TestWaitDependencies(t *testing.T) {
236236
redisService := types.ServiceConfig{Name: "redis", Scale: 1}
237237
project := types.Project{Name: strings.ToLower(testProject), Services: []types.ServiceConfig{dbService, redisService}}
238238
dependencies := types.DependsOnConfig{
239-
"db": {Condition: types.ServiceConditionStarted},
240-
"redis": {Condition: types.ServiceConditionStarted},
239+
"db": {Condition: types.ServiceConditionStarted, Required: true},
240+
"redis": {Condition: types.ServiceConditionStarted, Required: true},
241241
}
242242
assert.NilError(t, tested.waitDependencies(context.Background(), &project, dependencies, nil))
243243
})

pkg/compose/create.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ func prepareVolumes(p *types.Project) error {
139139
p.Services[i].DependsOn[service.Name].Condition == "" {
140140
p.Services[i].DependsOn[service.Name] = types.ServiceDependency{
141141
Condition: types.ServiceConditionStarted,
142+
Required: true,
142143
}
143144
}
144145
}

pkg/compose/create_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func TestPrepareVolumes(t *testing.T) {
124124
Name: "aService",
125125
VolumesFrom: []string{"anotherService"},
126126
DependsOn: map[string]composetypes.ServiceDependency{
127-
"anotherService": {Condition: composetypes.ServiceConditionHealthy},
127+
"anotherService": {Condition: composetypes.ServiceConditionHealthy, Required: true},
128128
},
129129
},
130130
{

pkg/compose/dependencies.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,15 @@ func NewGraph(project *types.Project, initialStatus ServiceStatus) (*Graph, erro
268268
graph.AddVertex(s.Name, s.Name, initialStatus)
269269
}
270270

271-
for _, s := range project.Services {
271+
for index, s := range project.Services {
272272
for _, name := range s.GetDependencies() {
273273
err := graph.AddEdge(s.Name, name)
274274
if err != nil {
275+
if !s.DependsOn[name].Required {
276+
delete(s.DependsOn, name)
277+
project.Services[index] = s
278+
continue
279+
}
275280
if api.IsNotFoundError(err) {
276281
ds, err := project.GetDisabledService(name)
277282
if err == nil {

pkg/compose/restart.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (s *composeService) restart(ctx context.Context, projectName string, option
4848
}
4949
}
5050

51-
// ignore depends_on relations which are not impacted by restarting service
51+
// ignore depends_on relations which are not impacted by restarting service or not required
5252
for i, service := range project.Services {
5353
for name, r := range service.DependsOn {
5454
if !r.Restart {

pkg/compose/start.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ func (s *composeService) start(ctx context.Context, projectName string, options
108108
for _, s := range project.Services {
109109
depends[s.Name] = types.ServiceDependency{
110110
Condition: getDependencyCondition(s, project),
111+
Required: true,
111112
}
112113
}
113114
if options.WaitTimeout > 0 {

0 commit comments

Comments
 (0)