本文整理匯總了Golang中github.com/NetSys/quilt/db.Database.SelectFromContainer方法的典型用法代碼示例。如果您正苦於以下問題:Golang Database.SelectFromContainer方法的具體用法?Golang Database.SelectFromContainer怎麽用?Golang Database.SelectFromContainer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/NetSys/quilt/db.Database
的用法示例。
在下文中一共展示了Database.SelectFromContainer方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: readContainerTransact
func readContainerTransact(view db.Database, dir directory) {
minion, err := view.MinionSelf()
worker := err == nil && minion.Role == db.Worker
for _, container := range view.SelectFromContainer(nil) {
container.IP = ""
var labels []string
if children, ok := dir[container.DockerID]; ok {
json.Unmarshal([]byte(children["Labels"]), &labels)
container.IP = children["IP"]
ip := net.ParseIP(container.IP).To4()
if ip != nil {
container.Mac = fmt.Sprintf("02:00:%02x:%02x:%02x:%02x",
ip[0], ip[1], ip[2], ip[3])
}
}
if worker {
// Masters get their labels from the policy, workers from the
// etcd store.
container.Labels = labels
}
view.Commit(container)
}
}
示例2: syncDB
func syncDB(view db.Database, dkcsArg []docker.Container) ([]string, []db.Container) {
score := func(left, right interface{}) int {
dbc := left.(db.Container)
dkc := right.(docker.Container)
// Depending on the container, the command in the database could be
// either The command plus it's arguments, or just it's arguments. To
// handle that case, we check both.
cmd1 := dkc.Args
cmd2 := append([]string{dkc.Path}, dkc.Args...)
dbcCmd := dbc.Command
for key, value := range dbc.Env {
if dkc.Env[key] != value {
return -1
}
}
var dkcLabels []string
for label, value := range dkc.Labels {
if !docker.IsUserLabel(label) || value != docker.LabelTrueValue {
continue
}
dkcLabels = append(dkcLabels, docker.ParseUserLabel(label))
}
switch {
case dkc.Image != dbc.Image:
return -1
case len(dbcCmd) != 0 && !strEq(dbcCmd, cmd1) && !strEq(dbcCmd, cmd2):
return -1
case dkc.ID == dbc.DockerID:
return 0
default:
return util.EditDistance(dbc.Labels, dkcLabels)
}
}
pairs, dbcs, dkcs := join.Join(view.SelectFromContainer(nil), dkcsArg, score)
for _, pair := range pairs {
dbc := pair.L.(db.Container)
dbc.DockerID = pair.R.(docker.Container).ID
view.Commit(dbc)
}
var term []string
for _, dkc := range dkcs {
term = append(term, dkc.(docker.Container).ID)
}
var boot []db.Container
for _, dbc := range dbcs {
boot = append(boot, dbc.(db.Container))
}
return term, boot
}
示例3: updateWorker
func updateWorker(view db.Database, self db.Minion, store Store,
etcdData storeData) {
var containers []storeContainer
for _, etcdc := range etcdData.containers {
if etcdc.Minion == self.PrivateIP {
containers = append(containers, etcdc)
}
}
pairs, dbcs, etcdcs := join.Join(view.SelectFromContainer(nil), containers,
func(left, right interface{}) int {
dbc := left.(db.Container)
l := storeContainer{
StitchID: dbc.StitchID,
Minion: dbc.Minion,
Image: dbc.Image,
Command: dbc.Command,
Env: dbc.Env,
Labels: dbc.Labels,
}
return containerJoinScore(l, right.(storeContainer))
})
for _, i := range dbcs {
dbc := i.(db.Container)
view.Remove(dbc)
}
for _, etcdc := range etcdcs {
pairs = append(pairs, join.Pair{
L: view.InsertContainer(),
R: etcdc,
})
}
for _, pair := range pairs {
dbc := pair.L.(db.Container)
etcdc := pair.R.(storeContainer)
dbc.StitchID = etcdc.StitchID
dbc.Minion = etcdc.Minion
dbc.Image = etcdc.Image
dbc.Command = etcdc.Command
dbc.Env = etcdc.Env
dbc.Labels = etcdc.Labels
view.Commit(dbc)
}
updateContainerIP(view.SelectFromContainer(nil), self.PrivateIP, store)
}
示例4: placeContainers
func placeContainers(view db.Database) {
constraints := view.SelectFromPlacement(nil)
containers := view.SelectFromContainer(nil)
minions := view.SelectFromMinion(nil)
ctx := makeContext(minions, constraints, containers)
cleanupPlacements(ctx)
placeUnassigned(ctx)
for _, change := range ctx.changed {
view.Commit(*change)
}
}
示例5: testReadContainerTransact
func testReadContainerTransact(t *testing.T, view db.Database) {
minion := view.InsertMinion()
minion.Role = db.Worker
minion.Self = true
view.Commit(minion)
for _, id := range []string{"a", "b"} {
container := view.InsertContainer()
container.DockerID = id
view.Commit(container)
}
container := view.InsertContainer()
container.DockerID = "c"
container.IP = "junk"
view.Commit(container)
dir := directory(map[string]map[string]string{
"a": {"IP": "1.0.0.0", "Labels": `["e"]`},
"b": {"IP": "2.0.0.0", "Labels": `["e", "f"]`},
})
readContainerTransact(view, dir)
ipMap := map[string]string{}
labelMap := map[string][]string{}
for _, c := range view.SelectFromContainer(nil) {
ipMap[c.DockerID] = c.IP
labelMap[c.DockerID] = c.Labels
}
expIPMap := map[string]string{
"a": "1.0.0.0",
"b": "2.0.0.0",
"c": "",
}
if !eq(ipMap, expIPMap) {
t.Error(spew.Sprintf("Found %s, Expected: %s", ipMap, expIPMap))
}
expLabelMap := map[string][]string{
"a": {"e"},
"b": {"e", "f"},
"c": nil,
}
if !eq(labelMap, expLabelMap) {
t.Error(spew.Sprintf("Found %s, Expected: %s", ipMap, expIPMap))
}
}
示例6: updateContainers
func updateContainers(view db.Database, spec stitch.Stitch) {
score := func(l, r interface{}) int {
left := l.(db.Container)
right := r.(db.Container)
if left.Image != right.Image ||
!util.StrSliceEqual(left.Command, right.Command) ||
!util.StrStrMapEqual(left.Env, right.Env) {
return -1
}
score := util.EditDistance(left.Labels, right.Labels)
if left.StitchID != right.StitchID {
score++
}
return score
}
pairs, news, dbcs := join.Join(queryContainers(spec),
view.SelectFromContainer(nil), score)
for _, dbc := range dbcs {
view.Remove(dbc.(db.Container))
}
for _, new := range news {
pairs = append(pairs, join.Pair{L: new, R: view.InsertContainer()})
}
for _, pair := range pairs {
newc := pair.L.(db.Container)
dbc := pair.R.(db.Container)
// By sorting the labels we prevent the database from getting confused
// when their order is non deterministic.
dbc.Labels = newc.Labels
sort.Sort(sort.StringSlice(dbc.Labels))
dbc.Command = newc.Command
dbc.Image = newc.Image
dbc.Env = newc.Env
dbc.StitchID = newc.StitchID
view.Commit(dbc)
}
}
示例7: runAppTransact
func (sv *supervisor) runAppTransact(view db.Database,
dkcsArgs []docker.Container) []string {
var tearDowns []string
dbKey := func(val interface{}) interface{} {
return val.(db.Container).DockerID
}
dkKey := func(val interface{}) interface{} {
return val.(docker.Container).ID
}
pairs, dbcs, dkcs := join.HashJoin(db.ContainerSlice(
view.SelectFromContainer(nil)),
docker.ContainerSlice(dkcsArgs), dbKey, dkKey)
for _, iface := range dbcs {
dbc := iface.(db.Container)
tearDowns = append(tearDowns, dbc.DockerID)
view.Remove(dbc)
}
for _, dkc := range dkcs {
pairs = append(pairs, join.Pair{L: view.InsertContainer(), R: dkc})
}
for _, pair := range pairs {
dbc := pair.L.(db.Container)
dkc := pair.R.(docker.Container)
dbc.DockerID = dkc.ID
dbc.Pid = dkc.Pid
dbc.Image = dkc.Image
dbc.Command = append([]string{dkc.Path}, dkc.Args...)
view.Commit(dbc)
}
return tearDowns
}
示例8: testUpdateWorkerDBC
func testUpdateWorkerDBC(t *testing.T, view db.Database) {
minion := view.InsertMinion()
minion.Role = db.Worker
minion.Self = true
view.Commit(minion)
for id := 1; id < 3; id++ {
container := view.InsertContainer()
container.StitchID = id
container.IP = fmt.Sprintf("10.1.0.%d", id-1)
container.Minion = "1.2.3.4"
container.Command = []string{"echo", "hi"}
container.Env = map[string]string{"GOPATH": "~"}
view.Commit(container)
}
cs := storeContainerSlice{
{
StitchID: 1,
Command: []string{"echo", "hi"},
Labels: []string{"red", "blue"},
Env: map[string]string{"GOPATH": "~"},
Minion: "1.2.3.4",
}, {
StitchID: 2,
Command: []string{"echo", "hi"},
Labels: []string{"blue", "green"},
Env: map[string]string{"GOPATH": "~"},
Minion: "1.2.3.4",
}, {
StitchID: 3,
Command: []string{"echo", "bye"},
Labels: []string{"blue", "green"},
Env: map[string]string{"GOPATH": "~"},
Minion: "1.2.3.5",
},
}
store := newTestMock()
jsonCS, _ := json.Marshal(cs)
err := store.Set(containerStore, string(jsonCS), 0)
assert.Nil(t, err)
jsonNull, _ := json.Marshal(map[string]string{})
minionDirKey := path.Join(nodeStore, "1.2.3.4")
err = store.Set(path.Join(minionDirKey, minionIPStore), string(jsonNull), 0)
assert.Nil(t, err)
updateWorker(view, db.Minion{PrivateIP: "1.2.3.4",
Subnet: "10.1.0.0"}, store, storeData{containers: cs})
ipMap := map[int]string{}
labelMap := map[int][]string{}
commandMap := map[string][]string{}
envMap := map[string]map[string]string{}
for _, c := range view.SelectFromContainer(nil) {
ipMap[c.StitchID] = c.IP
labelMap[c.StitchID] = c.Labels
commandMap[c.DockerID] = c.Command
envMap[c.DockerID] = c.Env
}
expIPMap := map[int]string{
1: "10.1.0.0",
2: "10.1.0.1",
}
assert.Equal(t, expIPMap, ipMap)
resultMap := map[string]string{}
storeIPs, _ := store.Get(path.Join(minionDirKey, minionIPStore))
json.Unmarshal([]byte(storeIPs), &resultMap)
for id, ip := range resultMap {
sid, _ := strconv.Atoi(id)
if otherIP, ok := ipMap[sid]; !ok || ip != otherIP {
t.Fatalf("IPs did not match: %s vs %s", ip, otherIP)
}
}
expLabelMap := map[int][]string{
1: {"red", "blue"},
2: {"blue", "green"},
}
assert.Equal(t, expLabelMap, labelMap)
}