Fix placement parser panic (#1121)

This commit is contained in:
Hang Yan
2019-04-04 15:31:06 -04:00
committed by Charlie Drage
parent 06468b2f63
commit fb7a92ca5e
2 changed files with 10 additions and 2 deletions
+4 -1
View File
@@ -386,7 +386,10 @@ func TestCheckLabelsPorts(t *testing.T) {
func TestCheckPlacementCustomLabels(t *testing.T) {
placement := types.Placement{
Constraints: []string{"node.labels.something == anything"},
Constraints: []string{
"node.labels.something == anything",
"node.labels.monitor != xxx",
},
}
output := loadV3Placement(placement.Constraints)
+6 -1
View File
@@ -134,8 +134,13 @@ func parseV3(files []string) (kobject.KomposeObject, error) {
func loadV3Placement(constraints []string) map[string]string {
placement := make(map[string]string)
errMsg := " constraints in placement is not supported, only 'node.hostname', 'engine.labels.operatingsystem' and 'node.labels.xxx' (ex: node.labels.something == anything) is supported as a constraint "
for _, j := range constraints {
p := strings.Split(j, " == ")
if len(p) < 2 {
log.Warn(p[0], errMsg)
continue
}
if p[0] == "node.hostname" {
placement["kubernetes.io/hostname"] = p[1]
} else if p[0] == "engine.labels.operatingsystem" {
@@ -144,7 +149,7 @@ func loadV3Placement(constraints []string) map[string]string {
label := strings.TrimPrefix(p[0], "node.labels.")
placement[label] = p[1]
} else {
log.Warn(p[0], " constraints in placement is not supported, only 'node.hostname', 'engine.labels.operatingsystem' and 'node.labels.xxx' (ex: node.labels.something == anything) is supported as a constraint ")
log.Warn(p[0], errMsg)
}
}
return placement