-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support alternative depends_on configuration
- Loading branch information
Showing
4 changed files
with
158 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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,84 @@ | ||
package template | ||
|
||
import ( | ||
"fmt" | ||
"github.com/docker/cli/cli/compose/loader" | ||
) | ||
|
||
// Remove and return the Compose v2 compatible depends_on configurations. | ||
// These won't parse for the v3 service type, so we'll just have to add them | ||
// back after the conversion is done. | ||
func extractDependsOnConfig(configuration map[string]interface{}) map[string]interface{} { | ||
servicesWithDependsOn := map[string]interface{}{} | ||
|
||
for name, config := range configuration { | ||
if mConfig, ok := config.(map[string]interface{}); !ok { | ||
panic(fmt.Sprintf("unexpected service definition type: %T", config)) | ||
|
||
} else if dependsOn, ok := mConfig["depends_on"]; ok { | ||
validateDependsOn(dependsOn) | ||
|
||
servicesWithDependsOn[name] = map[string]interface{}{ | ||
"depends_on": dependsOn, | ||
} | ||
|
||
delete(mConfig, "depends_on") | ||
} | ||
} | ||
|
||
return servicesWithDependsOn | ||
} | ||
|
||
func validateDependsOn(v interface{}) { | ||
switch v.(type) { | ||
case []string: | ||
// ok | ||
|
||
case []interface{}: | ||
for _, item := range v.([]interface{}) { | ||
if _, ok := item.(string); !ok { | ||
panic(fmt.Sprintf("invalid depends_on list item: %+v %T", item, item)) | ||
} | ||
} | ||
|
||
case map[string]interface{}: | ||
for svc, config := range v.(map[string]interface{}) { | ||
if mConfig, ok := config.(map[string]interface{}); !ok { | ||
panic(fmt.Sprintf("invalid depends_on defined for %s (type %T)", svc, config)) | ||
} else if condition, ok := mConfig["condition"]; !ok { | ||
panic(fmt.Sprintf("condition not found for %s dependency", svc)) | ||
} else if condition != "service_started" && condition != "service_healthy" { | ||
panic(fmt.Sprintf("invalid condition defined for %s : %s", svc, condition)) | ||
} | ||
} | ||
|
||
default: | ||
panic(fmt.Sprintf("unexpected depends_on config type: %T", v)) | ||
} | ||
} | ||
|
||
// Merge in the previously removed depends_on configurations to the rendered YAML string. | ||
func insertDependsOnConfig(target string, source map[string]interface{}, service string) string { | ||
var config interface{} | ||
|
||
if svcConfig, ok := source[service]; !ok { | ||
return target | ||
} else if mConfig, ok := svcConfig.(map[string]interface{}); !ok { | ||
panic(fmt.Sprintf("somehow lost the depends_on settings for %s", service)) | ||
} else { | ||
config = mConfig["depends_on"] | ||
} | ||
|
||
if config == nil { | ||
panic(fmt.Sprintf("somehow lost the depends_on settings for %s", service)) | ||
} | ||
|
||
parsed, err := loader.ParseYAML([]byte(target)) | ||
if err != nil { | ||
panic(fmt.Sprintf("failed to parse the output YAML for %s", service)) | ||
} | ||
|
||
parsed["depends_on"] = config | ||
|
||
return convertToYaml(parsed) | ||
} |
This file contains 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,25 @@ | ||
version: '3.5' | ||
services: | ||
|
||
dep: | ||
image: sample/inline | ||
x-podlike: | ||
templates: | ||
|
||
- inline: | ||
first: | ||
image: sample/first | ||
|
||
- inline: | ||
second: | ||
image: sample/second | ||
depends_on: | ||
first: | ||
condition: service_healthy | ||
|
||
- inline: | ||
third: | ||
image: sample/third | ||
depends_on: | ||
- first | ||
- second |
This file contains 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 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