Skip to content

Commit c852cf7

Browse files
authored
chore: adds data structures for container dependency (#2393)
Incorporates data structures for container dependencies to address #2175. By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent 3714eb5 commit c852cf7

File tree

5 files changed

+77
-3
lines changed

5 files changed

+77
-3
lines changed

internal/pkg/manifest/workload.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ type Workload struct {
6060

6161
// Image represents the workload's container image.
6262
type Image struct {
63-
Build BuildArgsOrString `yaml:"build"` // Build an image from a Dockerfile.
64-
Location *string `yaml:"location"` // Use an existing image instead.
65-
DockerLabels map[string]string `yaml:"labels,flow"` // Apply Docker labels to the container at runtime.
63+
Build BuildArgsOrString `yaml:"build"` // Build an image from a Dockerfile.
64+
Location *string `yaml:"location"` // Use an existing image instead.
65+
DockerLabels map[string]string `yaml:"labels,flow"` // Apply Docker labels to the container at runtime.
66+
DependsOn map[string]string `yaml:"depends_on,flow"` // Add any sidecar dependencies.
6667
}
6768

6869
// ImageWithPort represents a container image with an exposed port.
@@ -375,6 +376,7 @@ type SidecarConfig struct {
375376
Secrets map[string]string `yaml:"secrets"`
376377
MountPoints []SidecarMountPoint `yaml:"mount_points"`
377378
DockerLabels map[string]string `yaml:"labels"`
379+
DependsOn map[string]string `yaml:"depends_on"`
378380
}
379381

380382
// TaskConfig represents the resource boundaries and environment variables for the containers in the task.

internal/pkg/manifest/workload_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,3 +579,59 @@ network:
579579
})
580580
}
581581
}
582+
583+
func TestDependency_UnmarshalYAML(t *testing.T) {
584+
testCases := map[string]struct {
585+
inContent []byte
586+
587+
wantedStruct Image
588+
wantedError error
589+
}{
590+
"Unspecified optional dependencies don't appear in image": {
591+
inContent: []byte(``),
592+
wantedStruct: Image{},
593+
},
594+
"Empty dependencies don't appear in image": {
595+
inContent: []byte(`depends_on:`),
596+
wantedStruct: Image{},
597+
},
598+
"Error when unmarshallable": {
599+
inContent: []byte(`depends_on:
600+
frontend: coolwebsite
601+
sidecar2: wheels`),
602+
wantedStruct: Image{
603+
DependsOn: map[string]string{
604+
"frontend": "coolwebsite",
605+
"sidecar2": "wheels",
606+
},
607+
},
608+
wantedError: errors.New("yaml: line 2: did not find expected key"),
609+
},
610+
"Valid yaml specified": {
611+
inContent: []byte(`depends_on:
612+
frontend: coolwebsite
613+
sidecar2: wheels`),
614+
wantedStruct: Image{
615+
DependsOn: map[string]string{
616+
"frontend": "coolwebsite",
617+
"sidecar2": "wheels",
618+
},
619+
},
620+
},
621+
}
622+
623+
for name, tc := range testCases {
624+
t.Run(name, func(t *testing.T) {
625+
i := Image{}
626+
627+
err := yaml.Unmarshal(tc.inContent, &i)
628+
629+
if tc.wantedError != nil {
630+
require.EqualError(t, err, tc.wantedError.Error())
631+
} else {
632+
require.NoError(t, err)
633+
require.Equal(t, tc.wantedStruct.DependsOn, i.DependsOn)
634+
}
635+
})
636+
}
637+
}

internal/pkg/template/workload.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ type SidecarOpts struct {
9090
Secrets map[string]string
9191
MountPoints []*MountPoint
9292
DockerLabels map[string]string
93+
DependsOn map[string]string
9394
}
9495

9596
// StorageOpts holds data structures for rendering Volumes and Mount Points
@@ -235,6 +236,7 @@ type WorkloadOpts struct {
235236
Command []string
236237
DomainAlias string
237238
DockerLabels map[string]string
239+
DependsOn map[string]string
238240

239241
// Additional options for service templates.
240242
WorkloadType string

templates/workloads/partials/cf/sidecars.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@
4949
DockerLabels:{{range $name, $value := $sidecar.DockerLabels}}
5050
{{$name | printf "%q"}}: {{$value | printf "%q"}}{{end}}
5151
{{- end -}}
52+
{{- if $sidecar.DependsOn}}
53+
DependsOn:
54+
{{- range $name, $conditionFrom := $sidecar.DependsOn}}
55+
- Condition: {{$conditionFrom}}
56+
ContainerName: {{$name}}
57+
{{- end}}
58+
{{- end}}
5259
{{- if $sidecar.CredsParam}}
5360
RepositoryCredentials:
5461
CredentialsParameter: {{$sidecar.CredsParam}}

templates/workloads/partials/cf/workload-container.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
DockerLabels:{{range $name, $value := .DockerLabels}}
1212
{{$name | printf "%q"}}: {{$value | printf "%q"}}{{end}}
1313
{{- end}}
14+
{{- if .DependsOn}}
15+
DependsOn:
16+
{{- range $name, $conditionFrom := .DependsOn}}
17+
- Condition: {{$conditionFrom}}
18+
ContainerName: {{$name}}
19+
{{- end}}
20+
{{- end}}
1421
{{- if eq .WorkloadType "Load Balanced Web Service"}}
1522
PortMappings:
1623
- ContainerPort: !Ref ContainerPort

0 commit comments

Comments
 (0)