Skip to content

Commit

Permalink
Implement lambda related functions (#500)
Browse files Browse the repository at this point in the history
* implement wait

* implement log

* implement attach

* modify create

* refactor log
  • Loading branch information
Aceralon authored Nov 17, 2021
1 parent 63cb525 commit 8c94291
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
55 changes: 39 additions & 16 deletions engine/virt/virt.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import (
"strings"
"time"

virtapi "github.com/projecteru2/libyavirt/client"
virttypes "github.com/projecteru2/libyavirt/types"

"github.com/projecteru2/core/cluster"
"github.com/projecteru2/core/engine"
enginetypes "github.com/projecteru2/core/engine/types"
"github.com/projecteru2/core/log"
coresource "github.com/projecteru2/core/source"
"github.com/projecteru2/core/types"
coretypes "github.com/projecteru2/core/types"
virtapi "github.com/projecteru2/libyavirt/client"
virttypes "github.com/projecteru2/libyavirt/types"
)

const (
Expand Down Expand Up @@ -153,7 +153,7 @@ func (v *Virt) NetworkList(ctx context.Context, drivers []string) (nets []*engin

// BuildRefs builds references, it's not necessary for virt. presently.
func (v *Virt) BuildRefs(ctx context.Context, name string, tags []string) (refs []string) {
log.Warnf(ctx, "BuildRefs does not implement")
log.Warn(ctx, "BuildRefs does not implement")
return
}

Expand All @@ -178,6 +178,8 @@ func (v *Virt) VirtualizationCreate(ctx context.Context, opts *enginetypes.Virtu
Labels: opts.Labels,
AncestorID: opts.AncestorWorkloadID,
DmiUUID: opts.Labels[DmiUUIDKey],
Cmd: opts.Cmd,
Lambda: opts.Lambda,
}

var resp virttypes.Guest
Expand All @@ -189,9 +191,12 @@ func (v *Virt) VirtualizationCreate(ctx context.Context, opts *enginetypes.Virtu
}

// VirtualizationResourceRemap .
func (v *Virt) VirtualizationResourceRemap(ctx context.Context, opts *enginetypes.VirtualizationRemapOptions) (ch <-chan enginetypes.VirtualizationRemapMessage, err error) {
err = types.ErrEngineNotImplemented
return
func (v *Virt) VirtualizationResourceRemap(ctx context.Context, opts *enginetypes.VirtualizationRemapOptions) (<-chan enginetypes.VirtualizationRemapMessage, error) {
// VM does not support binding cores.
log.Warn(ctx, "virtualizationResourceRemap is not supported by vm")
ch := make(chan enginetypes.VirtualizationRemapMessage)
defer close(ch)
return ch, nil
}

// VirtualizationCopyTo copies one.
Expand Down Expand Up @@ -240,12 +245,25 @@ func (v *Virt) VirtualizationInspect(ctx context.Context, ID string) (*enginetyp

// VirtualizationLogs streams a specific guest's log.
func (v *Virt) VirtualizationLogs(ctx context.Context, opts *enginetypes.VirtualizationLogStreamOptions) (stdout io.ReadCloser, stderr io.ReadCloser, err error) {
return nil, nil, fmt.Errorf("VirtualizationLogs does not implement")
n := -1
if opts.Tail != "all" && opts.Tail != "" {
if n, err = strconv.Atoi(opts.Tail); err != nil {
return nil, nil, err
}
}

stream, err := v.client.Log(ctx, n, opts.ID)
return stream, nil, err
}

// VirtualizationAttach attaches something to a guest.
func (v *Virt) VirtualizationAttach(ctx context.Context, ID string, stream, stdin bool) (io.ReadCloser, io.ReadCloser, io.WriteCloser, error) {
return nil, nil, nil, fmt.Errorf("VirtualizationAttach does not implement")
func (v *Virt) VirtualizationAttach(ctx context.Context, ID string, stream, openStdin bool) (stdout, stderr io.ReadCloser, stdin io.WriteCloser, err error) {
flags := virttypes.AttachGuestFlags{Safe: true, Force: true}
attachGuest, err := v.client.AttachGuest(ctx, ID, []string{}, flags)
if err != nil {
return nil, nil, nil, err
}
return ioutil.NopCloser(attachGuest), nil, attachGuest, nil
}

// VirtualizationResize resized window size
Expand All @@ -255,7 +273,17 @@ func (v *Virt) VirtualizationResize(ctx context.Context, ID string, height, widt

// VirtualizationWait is waiting for a shut-off
func (v *Virt) VirtualizationWait(ctx context.Context, ID, state string) (*enginetypes.VirtualizationWaitResult, error) {
return nil, fmt.Errorf("VirtualizationWait does not implement")
r := &enginetypes.VirtualizationWaitResult{}
msg, err := v.client.WaitGuest(ctx, ID, true)
if err != nil {
r.Message = err.Error()
r.Code = -1
return r, err
}

r.Message = msg.Msg
r.Code = msg.Code
return r, nil
}

// VirtualizationUpdateResource updates resource.
Expand Down Expand Up @@ -287,11 +315,6 @@ func (v *Virt) VirtualizationCopyFrom(ctx context.Context, ID, path string) (con
return
}

// VirtualizationExecute executes commands in running virtual unit
func (v *Virt) VirtualizationExecute(ctx context.Context, ID string, commands, env []string, workdir string) (io.WriteCloser, io.ReadCloser, error) {
return nil, nil, fmt.Errorf("VirtualizationExecute not implemented")
}

// ResourceValidate validate resource usage
func (v *Virt) ResourceValidate(ctx context.Context, cpu float64, cpumap map[string]int64, memory, storage int64) error {
// TODO list all workloads, calculate resource
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ require (
github.com/opencontainers/runc v1.0.0-rc95 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pkg/errors v0.9.1
github.com/projecteru2/libyavirt v0.0.0-20211019072514-d14bb80e15f6
github.com/projecteru2/libyavirt v0.0.0-20211112023512-fbd788e688d5
github.com/prometheus/client_golang v1.11.0
github.com/sanity-io/litter v1.5.1
github.com/sirupsen/logrus v1.7.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,8 @@ github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/projecteru2/libyavirt v0.0.0-20211019072514-d14bb80e15f6 h1:QYRC6wecWPSIU49lhrGSCFkpEq+XgJSrAdmB11luxRY=
github.com/projecteru2/libyavirt v0.0.0-20211019072514-d14bb80e15f6/go.mod h1:FOc+hWBMLsMrmx5p3/moizKeSomedZPNwB6LhS+kEnE=
github.com/projecteru2/libyavirt v0.0.0-20211112023512-fbd788e688d5 h1:fZIc0V2WgI3X5g3S2gjZUTjIfDdYI5/714UvdgAVlNo=
github.com/projecteru2/libyavirt v0.0.0-20211112023512-fbd788e688d5/go.mod h1:FOc+hWBMLsMrmx5p3/moizKeSomedZPNwB6LhS+kEnE=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso=
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
Expand Down

0 comments on commit 8c94291

Please sign in to comment.