Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple container management #310

Closed
wants to merge 4 commits into from
Closed
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
25 changes: 6 additions & 19 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
module github.com/sdslabs/beastv4

require (
cloud.google.com/go v0.28.0 // indirect
github.com/BurntSushi/toml v0.3.1
github.com/Microsoft/go-winio v0.4.11 // indirect
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 // indirect
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 // indirect
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de
github.com/cpuguy83/go-md2man v1.0.10 // indirect
github.com/containerd/containerd v1.5.8
github.com/denisenkom/go-mssqldb v0.0.0-20180901172138-1eb28afdf9b6 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/docker/distribution v2.6.2+incompatible // indirect
github.com/docker/docker v1.13.1
github.com/docker/go-connections v0.4.0
github.com/docker/go-units v0.3.3 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 // indirect
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect
Expand All @@ -24,9 +21,7 @@ require (
github.com/gliderlabs/ssh v0.1.1 // indirect
github.com/go-openapi/spec v0.20.3 // indirect
github.com/go-sql-driver/mysql v1.4.0
github.com/golang/protobuf v1.3.3
github.com/google/go-cmp v0.2.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/golang/protobuf v1.5.0
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jinzhu/gorm v1.9.1
github.com/kevinburke/ssh_config v0.0.0-20180830205328-81db2a75821e // indirect
Expand All @@ -37,26 +32,18 @@ require (
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/mohae/struct2csv v0.0.0-20151122200941-e72239694eae
github.com/olekukonko/tablewriter v0.0.5
github.com/onsi/gomega v1.4.2 // indirect
github.com/pelletier/go-buffruneio v0.2.0 // indirect
github.com/pkg/errors v0.8.0 // indirect
github.com/sergi/go-diff v1.0.0 // indirect
github.com/sirupsen/logrus v1.0.6
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.2 // indirect
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.0.0
github.com/src-d/gcfg v1.3.0 // indirect
github.com/stevvooe/resumable v0.0.0-20180830230917-22b14a53ba50 // indirect
github.com/swaggo/gin-swagger v1.0.0
github.com/swaggo/swag v1.7.0
github.com/xanzy/ssh-agent v0.2.0 // indirect
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110
golang.org/x/sys v0.0.0-20210227040730-b0d1d43c014d // indirect
golang.org/x/tools v0.1.0 // indirect
google.golang.org/appengine v1.2.0 // indirect
google.golang.org/grpc v1.19.0
gopkg.in/airbrake/gobrake.v2 v2.0.9 // indirect
gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2 // indirect
google.golang.org/grpc v1.33.2
gopkg.in/src-d/go-billy.v4 v4.3.0 // indirect
gopkg.in/src-d/go-git-fixtures.v3 v3.3.0 // indirect
gopkg.in/src-d/go-git.v4 v4.7.0
Expand Down
902 changes: 864 additions & 38 deletions go.sum

Large diffs are not rendered by default.

194 changes: 194 additions & 0 deletions pkg/cr/containerd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package cr

import (
"bytes"
"context"
"io"
"syscall"

"github.com/containerd/containerd"
"github.com/containerd/containerd/cio"
"github.com/containerd/containerd/images"
)

var LogBuffers map[string]*cio.Creator
var TaskMaps map[string]*containerd.Task

type ContainerdClient struct {
client *containerd.Client
}

func NewContainerdClient() (Runtime, error) {
client, err := containerd.New("/run/containerd/containerd.sock")
if err != nil {
return nil, err
}
defer client.Close()

return &ContainerdClient{
client: client,
}, nil
}

func (self *ContainerdClient) ContainerList(ctx context.Context, options ContainerListOptions) ([]Container, error) {
var filters string
// TODO: handle filters
list, err := self.client.Containers(ctx, filters)
if err != nil {
return []Container{}, err
}

containers := make([]Container, len(list))
for index, container := range list {
info, _ := container.Info(ctx)
// labels, _ := container.Labels(ctx)
containers[index] = Container{
ID: info.ID,
Image: info.Image,
Created: info.CreatedAt.Local().Unix(),
}
}
return containers, err
}

func (self *ContainerdClient) ContainerStop(ctx context.Context, containerId string) error {
task := *TaskMaps[containerId]

if err := task.Kill(ctx, syscall.SIGTERM); err != nil {
return err
}

_, err := task.Delete(ctx)
if err != nil {
return err
}
return nil
}

func (self *ContainerdClient) ContainerRemove(ctx context.Context, containerId string, options ContainerRemoveOptions) error {
container, err := self.client.LoadContainer(ctx, containerId)
if err != nil {
return err
}

err = container.Delete(ctx, containerd.WithSnapshotCleanup)
return err
}

func (self *ContainerdClient) ContainerCreate(ctx context.Context, config *CreateContainerConfig, containerName string) (string, error) {
// TODO: add config options
container, err := self.client.NewContainer(ctx, containerName)
if err != nil {
return "", nil
}

return container.ID(), nil
}

func (self *ContainerdClient) ContainerStart(ctx context.Context, containerId string) error {
container, err := self.client.LoadContainer(ctx, containerId)
if err != nil {
return err
}

cio := cio.NewCreator(cio.WithStdio)
task, err := container.NewTask(ctx, cio)
if err != nil {
return err
}

TaskMaps[containerId] = &task
LogBuffers[containerId] = &cio

_, err = task.Wait(ctx)
if err != nil {
return err
}

if err := task.Start(ctx); err != nil {
return err
}
return nil
}

func (self *ContainerdClient) ContainerLogs(ctx context.Context, containerId string, options ContainerLogsOptions) (string, error) {
cio := *LogBuffers[containerId]
io, err := cio(containerId)
if err != nil {
return "", nil
}

_ = io.Config()
// TODO: persist IO from container task and output logs here
return "", nil
}

func (self *ContainerdClient) ContainerCommit(ctx context.Context, containerId string) (string, error) {
container, err := self.client.LoadContainer(ctx, containerId)
if err != nil {
return "", err
}

if err = container.Update(ctx); err != nil {
return "", nil
}
return container.ID(), nil

}

// TODO: add buildkit and handle image related operations
func (self *ContainerdClient) ImageBuild(ctx context.Context, builderContext io.Reader, options ImageBuildOptions) (*bytes.Buffer, error) {
// var image images.Image

// builder, err := client.New(ctx, "//TODO: buildkit daemon address", client.WithFailFast())
// if err != nil {
// return nil, err
// }

// ch := make(chan *client.SolveStatus)
// _, err = builder.Build(ctx, *solveOpt, "", dockerfile.Build, ch)

// imageStore := self.client.ImageService()
// image, err = imageStore.Create(ctx, image)
// return nil, err
return nil, nil
}

func (self *ContainerdClient) ImageRemove(ctx context.Context, imageId string, options ImageRemoveOptions) error {
imageStore := self.client.ImageService()
err := imageStore.Delete(ctx, imageId, images.SynchronousDelete())
return err
}

func (self *ContainerdClient) ImageInspect(ctx context.Context, imageId string) (Image, error) {
imageStore := self.client.ImageService()
inspectVal, err := imageStore.Get(ctx, imageId)
if err != nil {
return Image{}, err
}

image := Image{
ID: inspectVal.Name,
}
return image, nil
}

func (self *ContainerdClient) ImageList(ctx context.Context, options ImageListOptions) ([]Image, error) {
var filters string
imageStore := self.client.ImageService()
list, err := imageStore.List(ctx, filters)
if err != nil {
return []Image{}, err
}

images := make([]Image, len(list))
for index, image := range list {
images[index] = Image{
Created: image.CreatedAt.Unix(),
ID: image.Name,
Labels: image.Labels,
Size: image.Target.Size,
}
}
return images, err
}
Loading