forked from juju/juju
/
cloudcontainer.go
160 lines (138 loc) · 3.91 KB
/
cloudcontainer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright 2018 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"github.com/juju/errors"
"github.com/juju/mgo/v2"
"github.com/juju/mgo/v2/bson"
"github.com/juju/mgo/v2/txn"
"github.com/DavinZhang/juju/core/network"
)
// CloudContainer represents the state of a CAAS container, eg pod.
type CloudContainer interface {
// Unit returns the name of the unit for this container.
Unit() string
// ProviderId returns the id assigned to the container/pod
// by the cloud.
ProviderId() string
// Address returns the container address.
Address() *network.SpaceAddress
// Ports returns the open container ports.
Ports() []string
}
// cloudContainer is an implementation of CloudContainer.
type cloudContainer struct {
doc cloudContainerDoc
unitName string
}
type cloudContainerDoc struct {
// Id holds cloud container document key.
// It is the global key of the unit represented
// by this container.
Id string `bson:"_id"`
ProviderId string `bson:"provider-id"`
Address *address `bson:"address"`
Ports []string `bson:"ports"`
}
// Id implements CloudContainer.
func (c *cloudContainer) Id() string {
return c.doc.Id
}
// Unit implements CloudContainer.
func (c *cloudContainer) Unit() string {
return c.unitName
}
// ProviderId implements CloudContainer.
func (c *cloudContainer) ProviderId() string {
return c.doc.ProviderId
}
// Address implements CloudContainer.
func (c *cloudContainer) Address() *network.SpaceAddress {
if c.doc.Address == nil {
return nil
}
addr := c.doc.Address.networkAddress()
return &addr
}
// Ports implements CloudContainer.
func (c *cloudContainer) Ports() []string {
return c.doc.Ports
}
// globalCloudContainerKey returns the global database key for the
// cloud container status key for this unit.
func globalCloudContainerKey(name string) string {
return unitGlobalKey(name) + "#container"
}
func (u *Unit) cloudContainer() (*cloudContainerDoc, error) {
coll, closer := u.st.db().GetCollection(cloudContainersC)
defer closer()
var doc cloudContainerDoc
err := coll.FindId(u.globalKey()).One(&doc)
if err == mgo.ErrNotFound {
return nil, errors.NotFoundf("cloud container for unit %v", u.Name())
}
if err != nil {
return nil, errors.Trace(err)
}
return &doc, nil
}
func (u *Unit) saveContainerOps(doc cloudContainerDoc) ([]txn.Op, error) {
existing, err := u.cloudContainer()
if err != nil && !errors.IsNotFound(err) {
return nil, errors.Trace(err)
}
if err != nil {
return []txn.Op{{
C: cloudContainersC,
Id: doc.Id,
Assert: txn.DocMissing,
Insert: doc,
}}, nil
}
var asserts bson.D
providerValueAssert := bson.DocElem{"provider-id", existing.ProviderId}
if existing.ProviderId != "" {
asserts = bson.D{providerValueAssert}
} else {
asserts = bson.D{{"$or",
[]bson.D{{providerValueAssert}, {{"provider-id", bson.D{{"$exists", false}}}}}}}
}
return []txn.Op{{
C: cloudContainersC,
Id: existing.Id,
Assert: asserts,
Update: bson.D{
{"$set",
bson.D{{"provider-id", doc.ProviderId},
{"ports", doc.Ports},
{"address", doc.Address}},
},
},
}}, nil
}
func (u *Unit) removeCloudContainerOps() []txn.Op {
ops := []txn.Op{{
C: cloudContainersC,
Id: u.globalKey(),
Remove: true,
}}
return ops
}
// Containers returns the containers for the specified provider ids.
func (m *CAASModel) Containers(providerIds ...string) ([]CloudContainer, error) {
coll, closer := m.st.db().GetCollection(cloudContainersC)
defer closer()
var all []cloudContainerDoc
err := coll.Find(bson.D{{"provider-id", bson.D{{"$in", providerIds}}}}).All(&all)
if err != nil {
return nil, errors.Trace(err)
}
var result []CloudContainer
for _, doc := range all {
unitKey := m.localID(doc.Id)
// key is "u#<unitname>#charm"
idx := len(unitKey) - len("#charm")
result = append(result, &cloudContainer{doc: doc, unitName: unitKey[2:idx]})
}
return result, nil
}