Skip to content

Commit

Permalink
Move {Actual|Desired}ContainerSpec to own package
Browse files Browse the repository at this point in the history
[#155638517]

Signed-off-by: Tom Godkin <[email protected]>
  • Loading branch information
yulianedyalkova authored and Tom Godkin committed Mar 6, 2018
1 parent ad77efc commit 8745192
Show file tree
Hide file tree
Showing 33 changed files with 310 additions and 299 deletions.
55 changes: 55 additions & 0 deletions gardener/container-spec/spec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package spec

import (
"code.cloudfoundry.org/garden"
specs "github.com/opencontainers/runtime-spec/specs-go"
)

type ActualContainerSpec struct {
// The PID of the container's init process
Pid int

// The path to the container's bundle directory
BundlePath string

// The path to the container's rootfs
RootFSPath string

// Whether the container is stopped
Stopped bool

// Process IDs (not PIDs) of processes in the container
ProcessIDs []string

// Events (e.g. OOM) which have occured in the container
Events []string

// Applied limits
Limits garden.Limits

// Whether the container is privileged
Privileged bool
}

type DesiredContainerSpec struct {
Handle string

CgroupPath string

Namespaces map[string]string

// Container hostname
Hostname string

// Bind mounts
BindMounts []garden.BindMount

Env []string

// Container is privileged
Privileged bool

Limits garden.Limits

BaseConfig specs.Spec
}
102 changes: 27 additions & 75 deletions gardener/gardener.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
specs "github.com/opencontainers/runtime-spec/specs-go"

"code.cloudfoundry.org/garden"
spec "code.cloudfoundry.org/guardian/gardener/container-spec"
"code.cloudfoundry.org/lager"
)

Expand Down Expand Up @@ -38,19 +39,19 @@ type SysInfoProvider interface {
}

type Containerizer interface {
Create(log lager.Logger, spec DesiredContainerSpec) error
Create(log lager.Logger, desiredContainerSpec spec.DesiredContainerSpec) error
Handles() ([]string, error)

StreamIn(log lager.Logger, handle string, spec garden.StreamInSpec) error
StreamOut(log lager.Logger, handle string, spec garden.StreamOutSpec) (io.ReadCloser, error)
StreamIn(log lager.Logger, handle string, streamInSpec garden.StreamInSpec) error
StreamOut(log lager.Logger, handle string, streamOutSpec garden.StreamOutSpec) (io.ReadCloser, error)

Run(log lager.Logger, handle string, spec garden.ProcessSpec, io garden.ProcessIO) (garden.Process, error)
Run(log lager.Logger, handle string, processSpec garden.ProcessSpec, io garden.ProcessIO) (garden.Process, error)
Attach(log lager.Logger, handle string, processGUID string, io garden.ProcessIO) (garden.Process, error)
Stop(log lager.Logger, handle string, kill bool) error
Destroy(log lager.Logger, handle string) error
RemoveBundle(log lager.Logger, handle string) error

Info(log lager.Logger, handle string) (ActualContainerSpec, error)
Info(log lager.Logger, handle string) (spec.ActualContainerSpec, error)
Metrics(log lager.Logger, handle string) (ActualContainerMetrics, error)
}

Expand Down Expand Up @@ -106,55 +107,6 @@ func (fn UidGeneratorFunc) Generate() string {
return fn()
}

type DesiredContainerSpec struct {
Handle string

CgroupPath string

Namespaces map[string]string

// Container hostname
Hostname string

// Bind mounts
BindMounts []garden.BindMount

Env []string

// Container is privileged
Privileged bool

Limits garden.Limits

BaseConfig specs.Spec
}

type ActualContainerSpec struct {
// The PID of the container's init process
Pid int

// The path to the container's bundle directory
BundlePath string

// The path to the container's rootfs
RootFSPath string

// Whether the container is stopped
Stopped bool

// Process IDs (not PIDs) of processes in the container
ProcessIDs []string

// Events (e.g. OOM) which have occured in the container
Events []string

// Applied limits
Limits garden.Limits

// Whether the container is privileged
Privileged bool
}

type ActualContainerMetrics struct {
CPU garden.ContainerCPUStat
Memory garden.ContainerMemoryStat
Expand Down Expand Up @@ -195,19 +147,19 @@ type Gardener struct {

// Create creates a container by combining the results of networker.Network,
// volumizer.Create and containzer.Create.
func (g *Gardener) Create(spec garden.ContainerSpec) (ctr garden.Container, err error) {
if spec.Handle == "" {
spec.Handle = g.UidGenerator.Generate()
func (g *Gardener) Create(containerSpec garden.ContainerSpec) (ctr garden.Container, err error) {
if containerSpec.Handle == "" {
containerSpec.Handle = g.UidGenerator.Generate()
}

log := g.Logger.Session("create", lager.Data{"handle": spec.Handle})
log := g.Logger.Session("create", lager.Data{"handle": containerSpec.Handle})
log.Info("start")

defer func(startedAt time.Time) {
_ = metrics.SendValue("ContainerCreationDuration", float64(time.Since(startedAt).Nanoseconds()), "nanos")
}(time.Now())

if !g.AllowPrivilgedContainers && spec.Privileged {
if !g.AllowPrivilgedContainers && containerSpec.Privileged {
return nil, errors.New("privileged container creation is disabled")
}

Expand All @@ -216,7 +168,7 @@ func (g *Gardener) Create(spec garden.ContainerSpec) (ctr garden.Container, err
return nil, err
}

if err := g.checkDuplicateHandle(knownHandles, spec.Handle); err != nil {
if err := g.checkDuplicateHandle(knownHandles, containerSpec.Handle); err != nil {
return nil, err
}

Expand All @@ -232,7 +184,7 @@ func (g *Gardener) Create(spec garden.ContainerSpec) (ctr garden.Container, err

log.Info("start")

err := g.destroy(log, spec.Handle)
err := g.destroy(log, containerSpec.Handle)
if err != nil {
log.Error("destroy-failed", err)
}
Expand All @@ -247,25 +199,25 @@ func (g *Gardener) Create(spec garden.ContainerSpec) (ctr garden.Container, err
log.Error("graph-cleanup-failed", err)
}

runtimeSpec, err := g.Volumizer.Create(log, spec)
runtimeSpec, err := g.Volumizer.Create(log, containerSpec)
if err != nil {
return nil, err
}

desiredSpec := DesiredContainerSpec{
Handle: spec.Handle,
Hostname: spec.Handle,
Privileged: spec.Privileged,
Env: spec.Env,
BindMounts: spec.BindMounts,
Limits: spec.Limits,
desiredSpec := spec.DesiredContainerSpec{
Handle: containerSpec.Handle,
Hostname: containerSpec.Handle,
Privileged: containerSpec.Privileged,
Env: containerSpec.Env,
BindMounts: containerSpec.BindMounts,
Limits: containerSpec.Limits,
BaseConfig: runtimeSpec,
}
if err := g.Containerizer.Create(log, desiredSpec); err != nil {
return nil, err
}

actualSpec, err := g.Containerizer.Info(log, spec.Handle)
actualSpec, err := g.Containerizer.Info(log, containerSpec.Handle)
if err != nil {
return nil, err
}
Expand All @@ -276,22 +228,22 @@ func (g *Gardener) Create(spec garden.ContainerSpec) (ctr garden.Container, err
return nil, err
}

if err = g.Networker.Network(log, spec, actualSpec.Pid); err != nil {
if err = g.Networker.Network(log, containerSpec, actualSpec.Pid); err != nil {
return nil, err
}

container, err := g.Lookup(spec.Handle)
container, err := g.Lookup(containerSpec.Handle)
if err != nil {
return nil, err
}

if spec.GraceTime != 0 {
if err := container.SetGraceTime(spec.GraceTime); err != nil {
if containerSpec.GraceTime != 0 {
if err := container.SetGraceTime(containerSpec.GraceTime); err != nil {
return nil, err
}
}

for name, value := range spec.Properties {
for name, value := range containerSpec.Properties {
if err := container.SetProperty(name, value); err != nil {
return nil, err
}
Expand Down
25 changes: 13 additions & 12 deletions gardener/gardener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"code.cloudfoundry.org/garden"
"code.cloudfoundry.org/guardian/gardener"
spec "code.cloudfoundry.org/guardian/gardener/container-spec"
fakes "code.cloudfoundry.org/guardian/gardener/gardenerfakes"
"code.cloudfoundry.org/lager"
"code.cloudfoundry.org/lager/lagertest"
Expand Down Expand Up @@ -46,7 +47,7 @@ var _ = Describe("Gardener", func() {

propertyManager.GetReturns("", true)
containerizer.HandlesReturns([]string{"some-handle"}, nil)
containerizer.InfoReturns(gardener.ActualContainerSpec{Pid: 470, RootFSPath: "rootfs"}, nil)
containerizer.InfoReturns(spec.ActualContainerSpec{Pid: 470, RootFSPath: "rootfs"}, nil)

gdnr = &gardener.Gardener{
SysInfoProvider: sysinfoProvider,
Expand Down Expand Up @@ -252,7 +253,7 @@ var _ = Describe("Gardener", func() {
})

It("should ask the networker to configure the network", func() {
containerizer.InfoReturns(gardener.ActualContainerSpec{
containerizer.InfoReturns(spec.ActualContainerSpec{
Pid: 42,
BundlePath: "bndl",
}, nil)
Expand All @@ -269,7 +270,7 @@ var _ = Describe("Gardener", func() {

Context("when container info cannot be retrieved", func() {
It("errors", func() {
containerizer.InfoReturns(gardener.ActualContainerSpec{}, errors.New("boom"))
containerizer.InfoReturns(spec.ActualContainerSpec{}, errors.New("boom"))

_, err := gdnr.Create(garden.ContainerSpec{Handle: "bob"})
Expect(err).To(MatchError("boom"))
Expand All @@ -279,7 +280,7 @@ var _ = Describe("Gardener", func() {

Context("when container info returns pid = 0", func() {
BeforeEach(func() {
containerizer.InfoReturns(gardener.ActualContainerSpec{Pid: 0}, nil)
containerizer.InfoReturns(spec.ActualContainerSpec{Pid: 0}, nil)
})

It("errors", func() {
Expand Down Expand Up @@ -1049,7 +1050,7 @@ var _ = Describe("Gardener", func() {
Expect(err).NotTo(HaveOccurred())
Expect(info.State).To(Equal("active"))

containerizer.InfoReturns(gardener.ActualContainerSpec{
containerizer.InfoReturns(spec.ActualContainerSpec{
Stopped: true,
}, nil)

Expand Down Expand Up @@ -1077,7 +1078,7 @@ var _ = Describe("Gardener", func() {
})

It("returns the container path based on the info returned by the containerizer", func() {
containerizer.InfoReturns(gardener.ActualContainerSpec{
containerizer.InfoReturns(spec.ActualContainerSpec{
BundlePath: "/foo/bar/baz",
}, nil)

Expand All @@ -1089,7 +1090,7 @@ var _ = Describe("Gardener", func() {

Context("when getting the ActualContainerSpec fails", func() {
It("return the error", func() {
containerizer.InfoReturns(gardener.ActualContainerSpec{}, errors.New("info-error"))
containerizer.InfoReturns(spec.ActualContainerSpec{}, errors.New("info-error"))

_, err := container.Info()
Expect(err).To(MatchError("info-error"))
Expand Down Expand Up @@ -1151,7 +1152,7 @@ var _ = Describe("Gardener", func() {
})

It("returns the events reported by the containerizer", func() {
containerizer.InfoReturns(gardener.ActualContainerSpec{
containerizer.InfoReturns(spec.ActualContainerSpec{
Events: []string{"some", "things", "happened"},
}, nil)

Expand Down Expand Up @@ -1200,7 +1201,7 @@ var _ = Describe("Gardener", func() {

Context("when info errors", func() {
It("returns the error", func() {
containerizer.InfoReturns(gardener.ActualContainerSpec{}, errors.New("info-error"))
containerizer.InfoReturns(spec.ActualContainerSpec{}, errors.New("info-error"))

infos, err := gdnr.BulkInfo([]string{"some-handle-1"})
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -1356,7 +1357,7 @@ var _ = Describe("Gardener", func() {
})

It("gets the set CPU limits", func() {
containerizer.InfoReturns(gardener.ActualContainerSpec{
containerizer.InfoReturns(spec.ActualContainerSpec{
Limits: garden.Limits{
CPU: garden.CPULimits{
LimitInShares: 10,
Expand All @@ -1370,7 +1371,7 @@ var _ = Describe("Gardener", func() {
})

It("gets the set memory limits", func() {
containerizer.InfoReturns(gardener.ActualContainerSpec{
containerizer.InfoReturns(spec.ActualContainerSpec{
Limits: garden.Limits{
Memory: garden.MemoryLimits{
LimitInBytes: 20,
Expand All @@ -1385,7 +1386,7 @@ var _ = Describe("Gardener", func() {

Context("when Info fails", func() {
It("forwards the error", func() {
containerizer.InfoReturns(gardener.ActualContainerSpec{}, errors.New("some-error"))
containerizer.InfoReturns(spec.ActualContainerSpec{}, errors.New("some-error"))

_, err := container.CurrentCPULimits()
Expect(err).To(MatchError("some-error"))
Expand Down
Loading

0 comments on commit 8745192

Please sign in to comment.