faasd/pkg/supervisor.go
Alex Ellis (OpenFaaS Ltd) 5c45242b3d Initial
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
2019-12-20 12:56:19 +00:00

68 lines
1.2 KiB
Go

package pkg
import (
"context"
"fmt"
"github.com/containerd/containerd"
"github.com/containerd/containerd/namespaces"
)
type Supervisor struct {
client *containerd.Client
}
func NewSupervisor(sock string) (*Supervisor, error) {
client, err := containerd.New(sock)
if err != nil {
panic(err)
}
return &Supervisor{
client: client,
}, nil
}
func (s *Supervisor) Close() {
defer s.client.Close()
}
func (s *Supervisor) Start(svcs []Service) error {
for _, svc := range svcs {
fmt.Printf("Preparing: %s", svc.Name)
fmt.Printf("Pulling: %s\n", svc.Image)
bytesOut, err := pullImage(s.client, svc.Image)
if err != nil {
return err
}
fmt.Printf("Pull done for: %s, %d bytes\n", svc.Image, bytesOut)
}
return nil
}
func pullImage(client *containerd.Client, image string) (int64, error) {
ctx := namespaces.WithNamespace(context.Background(), "default")
pulled, err := client.Pull(ctx, image, containerd.WithPullUnpack)
if err != nil {
return 0, err
}
bytesOut, _ := pulled.Size(ctx)
return bytesOut, nil
}
type Service struct {
Image string
Env []string
Name string
Mounts []Mount
}
type Mount struct {
Src string
Dest string
}