Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Documentation/unit-files-and-scheduling.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ app.service 282f949f.../10.10.20.1 active running
app.service fd1d3e94.../10.0.0.1 active running
```

`MachineMetadata` also support relational operators, including `<=`, `>=`, `<`, `>`, `==` and `!=`:

```
[X-Fleet]
MachineMetadata=ram<1024
```
This requires an eligible machine to have the `ram` less than 1024. The value must be numeral when using `<=`, `>=`, `<` or `>`.

A machine is not automatically configured with metadata.
A deployer may define machine metadata using the `metadata` [config option][config-option].

Expand Down
15 changes: 14 additions & 1 deletion fleetctl/list_machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ func formatMetadata(metadata map[string]string) string {
sorted.Sort()
for _, key := range sorted {
value := metadata[key]
pairs[idx] = fmt.Sprintf("%s=%s", key, value)
if hasMetadataOperator(value) {
pairs[idx] = fmt.Sprintf("%s%s", key, value)
} else {
pairs[idx] = fmt.Sprintf("%s=%s", key, value)
}
idx++
}
return strings.Join(pairs, ",")
Expand All @@ -135,3 +139,12 @@ func machineToFieldKeys(m map[string]machineToField) (keys []string) {
}
return
}

func hasMetadataOperator(instr string) bool {
for _, op := range []string{"<=", ">=", "!=", "==", "<", ">"} {
if strings.HasPrefix(instr, op) {
return true
}
}
return false
}
5 changes: 5 additions & 0 deletions functional/fixtures/units/metadata-op.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[Service]
ExecStart=/bin/bash -c "while true; do echo Hello, World!; sleep 1; done"

[X-Fleet]
MachineMetadata="ram>=1024"
85 changes: 85 additions & 0 deletions functional/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package functional

import (
"fmt"
"path"
"regexp"
"strings"
"testing"
Expand Down Expand Up @@ -90,3 +91,87 @@ func TestTemplatesWithSpecifiersInMetadata(t *testing.T) {
t.Fatalf("metadata@invalid unit should not be scheduled: \nstdout: %s\nstderr: %s", stdout, stderr)
}
}

// TestMetadataOperator ensures that metadata operators work also for
// extended operators such as ">=", "<=", "<", ">", "!=", or "==".
func TestMetadataOperator(t *testing.T) {
cluster, err := platform.NewNspawnCluster("smoke")
if err != nil {
t.Fatal(err)
}
defer cluster.Destroy(t)

members, err := platform.CreateNClusterMembers(cluster, 1)
if err != nil {
t.Fatal(err)
}
m0 := members[0]
_, err = cluster.WaitForNMachines(m0, 1)
if err != nil {
t.Fatal(err)
}

stdout, stderr, err := cluster.Fleetctl(m0, "list-machines", "--fields", "machine,metadata")
if err != nil {
t.Fatalf("Unable to get machine metadata\nstdout: %s\nstderr: %s\nerr: %v", stdout, stderr, err)
}

runMetaOp := func(ramEq string, expectSuccess bool) {
tmpMdOpService := "/tmp/metadata-op.service"
MdOpService := "fixtures/units/metadata-op.service"
MdOpBaseName := path.Base(MdOpService)
var nUnits int

if expectSuccess {
t.Logf("Testing %s expecting success...", ramEq)
nUnits = 1
} else {
t.Logf("Testing %s expecting failure...", ramEq)
nUnits = 0
}

err = util.GenNewFleetService(tmpMdOpService, MdOpService, ramEq, "ram>=1024")
if err != nil {
t.Fatalf("Failed to generate a temp fleet service: %v", err)
}

stdout, stderr, err = cluster.Fleetctl(m0, "start", "--no-block", tmpMdOpService)
if err != nil {
t.Fatalf("starting unit %s returned error:\nstdout: %s\nstderr: %s\nerr: %v",
tmpMdOpService, stdout, stderr, err)
}

_, err = cluster.WaitForNActiveUnits(m0, nUnits)
if err != nil {
t.Fatal(err)
}

stdout, stderr, err = cluster.Fleetctl(m0, "destroy", MdOpBaseName)
if err != nil {
t.Fatalf("unit %s cannot be stopped: \nstdout: %s\nstderr: %s\nerr: %v",
MdOpBaseName, stdout, stderr, err)
}

_, err = cluster.WaitForNUnitFiles(m0, 0)
if err != nil {
t.Fatal(err)
}

}

// run tests for success cases
runMetaOp("ram>=1024", true)
runMetaOp("ram<=1024", true)
runMetaOp("ram>1023", true)
runMetaOp("ram<1025", true)
runMetaOp("ram!=1025", true)
runMetaOp("ram==1024", true)

// run tests for failure cases
runMetaOp("ram>=1025", false)
runMetaOp("ram<=1023", false)
runMetaOp("ram>1024", false)
runMetaOp("ram<1024", false)
runMetaOp("ram!=1024", false)
runMetaOp("ram==1025", false)
}
5 changes: 4 additions & 1 deletion functional/util/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

const (
fleetAPIPort = 54728
fleetRAM = 1024 // in MiB
FleetTTL = "3s"
cloudConfig = `#cloud-config

Expand Down Expand Up @@ -62,7 +63,7 @@ coreos:
command: start
content: |
[Service]
Environment=FLEET_METADATA=hostname=%H
Environment=FLEET_METADATA=hostname=%H,ram={{printf "%d" .FleetRAM}}
ExecStart=/opt/fleet/fleetd -config /opt/fleet/fleet.conf
`
)
Expand All @@ -80,6 +81,7 @@ type configValues struct {
EtcdEndpoint string
EtcdKeyPrefix string
FleetAPIPort int
FleetRAM int
FleetAgentTTL string
FleetExtra string
}
Expand Down Expand Up @@ -118,6 +120,7 @@ func BuildCloudConfig(dst io.Writer, ip, etcdEndpoint, etcdKeyPrefix string) err
EtcdEndpoint: etcdEndpoint,
EtcdKeyPrefix: etcdKeyPrefix,
FleetAPIPort: fleetAPIPort,
FleetRAM: fleetRAM,
FleetAgentTTL: FleetTTL,
FleetExtra: os.Getenv("FLEETD_TEST_ENV"),
}
Expand Down
12 changes: 11 additions & 1 deletion job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,17 @@ func (j *Job) RequiredTargetMetadata() map[string]pkg.Set {
fleetMachineMetadata,
} {
for _, valuePair := range j.requirements()[key] {
s := strings.Split(valuePair, "=")
var s []string
for _, sep := range []string{"==", "<=", ">=", "!=", "<", ">"} {
index := strings.Index(valuePair, sep)
if index != -1 {
s = []string{valuePair[0:index], valuePair[index:]}
break
}
}
if s == nil {
s = strings.Split(valuePair, "=")
}

if len(s) != 2 {
continue
Expand Down
88 changes: 86 additions & 2 deletions machine/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
package machine

import (
"strconv"
"strings"

"github.com/coreos/fleet/log"
"github.com/coreos/fleet/pkg"
)
Expand All @@ -38,8 +41,89 @@ func HasMetadata(state *MachineState, metadata map[string]pkg.Set) bool {
if values.Contains(local) {
log.Debugf("Local Metadata(%s) meets requirement", key)
} else {
log.Debugf("Local Metadata(%s) does not match requirement", key)
return false
vs := values.Values()
for _, v := range vs {
if index := strings.Index(v, "<="); strings.Contains(v, "<=") && (index == 0) {
need, err1 := strconv.Atoi(v[2:])
have, err2 := strconv.Atoi(local)
if err1 == nil && err2 == nil {
if have <= need {
log.Debugf("Local Metadata(%s) meets requirement", key)
continue
} else {
log.Debugf("Local Metadata(%s) does not match requirement", key)
return false
}
} else {
log.Debugf("Local Metadata(%s) does not match requirement", key)
return false
}
} else if index := strings.Index(v, ">="); strings.Contains(v, ">=") && (index == 0) {
need, err1 := strconv.Atoi(v[2:])
have, err2 := strconv.Atoi(local)
if err1 == nil && err2 == nil {
if have >= need {
log.Debugf("Local Metadata(%s) meets requirement", key)
continue
} else {
log.Debugf("Local Metadata(%s) does not match requirement", key)
return false
}
} else {
log.Debugf("Local Metadata(%s) does not match requirement", key)
return false
}
} else if index := strings.Index(v, ">"); strings.Contains(v, ">") && (index == 0) {
need, err1 := strconv.Atoi(v[1:])
have, err2 := strconv.Atoi(local)
if err1 == nil && err2 == nil {
if have > need {
log.Debugf("Local Metadata(%s) meets requirement", key)
continue
} else {
log.Debugf("Local Metadata(%s) does not match requirement", key)
return false
}
} else {
log.Debugf("Local Metadata(%s) does not match requirement", key)
return false
}
} else if index := strings.Index(v, "<"); strings.Contains(v, "<") && (index == 0) {
need, err1 := strconv.Atoi(v[1:])
have, err2 := strconv.Atoi(local)
if err1 == nil && err2 == nil {
if have < need {
log.Debugf("Local Metadata(%s) meets requirement", key)
continue
} else {
log.Debugf("Local Metadata(%s) does not match requirement", key)
return false
}
} else {
log.Debugf("Local Metadata(%s) does not match requirement", key)
return false
}
} else if index := strings.Index(v, "!="); strings.Contains(v, "!=") && (index == 0) {
if v[2:] != local {
log.Debugf("Local Metadata(%s) meets requirement", key)
continue
} else {
log.Debugf("Local Metadata(%s) does not match requirement", key)
return false
}
} else if index := strings.Index(v, "=="); strings.Contains(v, "==") && (index == 0) {
if v[2:] == local {
log.Debugf("Local Metadata(%s) meets requirement", key)
continue
} else {
log.Debugf("Local Metadata(%s) does not match requirement", key)
return false
}
} else {
log.Debugf("Local Metadata(%s) does not match requirement", key)
return false
}
}
}
}

Expand Down
Loading