forked from LaconicNetwork/kompose
Add support for negated placement constraint (#1419)
This commit is contained in:
@@ -500,11 +500,29 @@ func TestCheckPlacementCustomLabels(t *testing.T) {
|
||||
"node.labels.monitor != xxx",
|
||||
},
|
||||
}
|
||||
output := loadV3Placement(placement.Constraints)
|
||||
output := loadV3Placement(placement)
|
||||
|
||||
expected := map[string]string{"something": "anything"}
|
||||
expected := kobject.Placement{
|
||||
PositiveConstraints: map[string]string{
|
||||
"something": "anything",
|
||||
},
|
||||
NegativeConstraints: map[string]string{
|
||||
"monitor": "xxx",
|
||||
},
|
||||
}
|
||||
|
||||
if output["something"] != expected["something"] {
|
||||
t.Errorf("Expected %s, got %s", expected, output)
|
||||
checkConstraints(t, "positive", output.PositiveConstraints, expected.PositiveConstraints)
|
||||
checkConstraints(t, "negative", output.NegativeConstraints, expected.NegativeConstraints)
|
||||
}
|
||||
|
||||
func checkConstraints(t *testing.T, caseName string, output, expected map[string]string) {
|
||||
t.Log("Test case:", caseName)
|
||||
if len(output) != len(expected) {
|
||||
t.Errorf("constraints len is not equal, expected %d, got %d", len(expected), len(output))
|
||||
}
|
||||
for key := range output {
|
||||
if output[key] != expected[key] {
|
||||
t.Errorf("%s constraint is not equal, expected %s, got %s", key, expected[key], output[key])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+27
-11
@@ -33,7 +33,7 @@ import (
|
||||
"github.com/docker/cli/cli/compose/loader"
|
||||
"github.com/docker/cli/cli/compose/types"
|
||||
|
||||
shlex "github.com/google/shlex"
|
||||
"github.com/google/shlex"
|
||||
"github.com/kubernetes/kompose/pkg/kobject"
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
@@ -133,27 +133,43 @@ func parseV3(files []string) (kobject.KomposeObject, error) {
|
||||
return komposeObject, nil
|
||||
}
|
||||
|
||||
func loadV3Placement(constraints []string) map[string]string {
|
||||
placement := make(map[string]string)
|
||||
func loadV3Placement(placement types.Placement) kobject.Placement {
|
||||
komposePlacement := kobject.Placement{
|
||||
PositiveConstraints: make(map[string]string),
|
||||
NegativeConstraints: make(map[string]string),
|
||||
}
|
||||
equal, notEqual := " == ", " != "
|
||||
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, " == ")
|
||||
for _, j := range placement.Constraints {
|
||||
operator := equal
|
||||
if strings.Contains(j, notEqual) {
|
||||
operator = notEqual
|
||||
}
|
||||
p := strings.Split(j, operator)
|
||||
if len(p) < 2 {
|
||||
log.Warn(p[0], errMsg)
|
||||
continue
|
||||
}
|
||||
|
||||
var key string
|
||||
if p[0] == "node.hostname" {
|
||||
placement["kubernetes.io/hostname"] = p[1]
|
||||
key = "kubernetes.io/hostname"
|
||||
} else if p[0] == "engine.labels.operatingsystem" {
|
||||
placement["beta.kubernetes.io/os"] = p[1]
|
||||
key = "beta.kubernetes.io/os"
|
||||
} else if strings.HasPrefix(p[0], "node.labels.") {
|
||||
label := strings.TrimPrefix(p[0], "node.labels.")
|
||||
placement[label] = p[1]
|
||||
key = strings.TrimPrefix(p[0], "node.labels.")
|
||||
} else {
|
||||
log.Warn(p[0], errMsg)
|
||||
continue
|
||||
}
|
||||
|
||||
if operator == equal {
|
||||
komposePlacement.PositiveConstraints[key] = p[1]
|
||||
} else if operator == notEqual {
|
||||
komposePlacement.NegativeConstraints[key] = p[1]
|
||||
}
|
||||
}
|
||||
return placement
|
||||
return komposePlacement
|
||||
}
|
||||
|
||||
// Convert the Docker Compose v3 volumes to []string (the old way)
|
||||
@@ -434,7 +450,7 @@ func dockerComposeToKomposeMapping(composeObject *types.Config) (kobject.Kompose
|
||||
}
|
||||
|
||||
// placement:
|
||||
serviceConfig.Placement = loadV3Placement(composeServiceConfig.Deploy.Placement.Constraints)
|
||||
serviceConfig.Placement = loadV3Placement(composeServiceConfig.Deploy.Placement)
|
||||
|
||||
if composeServiceConfig.Deploy.UpdateConfig != nil {
|
||||
serviceConfig.DeployUpdateConfig = *composeServiceConfig.Deploy.UpdateConfig
|
||||
|
||||
Reference in New Issue
Block a user