engine: resources: docker: Add AutoEdges between container and image

This commit is contained in:
Jonathan Gold
2019-02-07 19:05:56 -05:00
committed by James Shubin
parent bdfb1cf33e
commit 3233973748
3 changed files with 75 additions and 4 deletions

View File

@@ -400,16 +400,63 @@ func (obj *DockerContainerRes) Cmp(r engine.Res) error {
return nil return nil
} }
// DockerUID is the UID struct for DockerContainerRes. // DockerContainerUID is the UID struct for DockerContainerRes.
type DockerUID struct { type DockerContainerUID struct {
engine.BaseUID engine.BaseUID
name string name string
} }
// DockerContainerResAutoEdges holds the state of the auto edge generator.
type DockerContainerResAutoEdges struct {
UIDs []engine.ResUID
pointer int
}
// AutoEdges returns edges to any docker:image resource that matches the image
// specified in the docker:container resource definition.
func (obj *DockerContainerRes) AutoEdges() (engine.AutoEdge, error) {
var result []engine.ResUID
var reversed bool
if obj.State != "removed" {
reversed = true
}
result = append(result, &DockerImageUID{
BaseUID: engine.BaseUID{
Reversed: &reversed,
},
image: dockerImageNameTag(obj.Image),
})
return &DockerContainerResAutoEdges{
UIDs: result,
pointer: 0,
}, nil
}
// Next returnes the next automatic edge.
func (obj *DockerContainerResAutoEdges) Next() []engine.ResUID {
if len(obj.UIDs) == 0 {
return nil
}
value := obj.UIDs[obj.pointer]
obj.pointer++
return []engine.ResUID{value}
}
// Test gets results of the earlier Next() call, & returns if we should continue.
func (obj *DockerContainerResAutoEdges) Test(input []bool) bool {
if len(obj.UIDs) <= obj.pointer {
return false
}
if len(input) != 1 { // in case we get given bad data
panic(fmt.Sprintf("Expecting a single value!"))
}
return true // keep going
}
// UIDs includes all params to make a unique identification of this object. // UIDs includes all params to make a unique identification of this object.
// Most resources only return one, although some resources can return multiple. // Most resources only return one, although some resources can return multiple.
func (obj *DockerContainerRes) UIDs() []engine.ResUID { func (obj *DockerContainerRes) UIDs() []engine.ResUID {
x := &DockerUID{ x := &DockerContainerUID{
BaseUID: engine.BaseUID{Name: obj.Name(), Kind: obj.Kind()}, BaseUID: engine.BaseUID{Name: obj.Name(), Kind: obj.Kind()},
name: obj.Name(), name: obj.Name(),
} }

View File

@@ -239,11 +239,25 @@ type DockerImageUID struct {
func (obj *DockerImageRes) UIDs() []engine.ResUID { func (obj *DockerImageRes) UIDs() []engine.ResUID {
x := &DockerImageUID{ x := &DockerImageUID{
BaseUID: engine.BaseUID{Name: obj.Name(), Kind: obj.Kind()}, BaseUID: engine.BaseUID{Name: obj.Name(), Kind: obj.Kind()},
name: obj.Name(), image: dockerImageNameTag(obj.Name()),
} }
return []engine.ResUID{x} return []engine.ResUID{x}
} }
// AutoEdges returns the AutoEdge interface.
func (obj *DockerImageRes) AutoEdges() (engine.AutoEdge, error) {
return nil, nil
}
// IFF aka if and only if they are equivalent, return true. If not, false.
func (obj *DockerImageUID) IFF(uid engine.ResUID) bool {
res, ok := uid.(*DockerImageUID)
if !ok {
return false
}
return obj.image == res.image
}
// UnmarshalYAML is the custom unmarshal handler for this struct. // UnmarshalYAML is the custom unmarshal handler for this struct.
// It is primarily useful for setting the defaults. // It is primarily useful for setting the defaults.
func (obj *DockerImageRes) UnmarshalYAML(unmarshal func(interface{}) error) error { func (obj *DockerImageRes) UnmarshalYAML(unmarshal func(interface{}) error) error {

10
examples/lang/docker0.mcl Normal file
View File

@@ -0,0 +1,10 @@
docker:container "mgmt-nginx" {
state => "running",
image => "nginx",
cmd => ["nginx", "-g", "daemon off;",],
ports => {"tcp" => {80 => 8080,},},
}
docker:image "nginx" {
state => "exists",
}