From 8c94291651aed1381390f235d2e8352c96ae1d49 Mon Sep 17 00:00:00 2001 From: Joseph Gu Date: Wed, 17 Nov 2021 15:35:14 +0800 Subject: [PATCH] Implement lambda related functions (#500) * implement wait * implement log * implement attach * modify create * refactor log --- engine/virt/virt.go | 55 ++++++++++++++++++++++++++++++++------------- go.mod | 2 +- go.sum | 4 ++-- 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/engine/virt/virt.go b/engine/virt/virt.go index b4f3731d4..f6dbad6e5 100644 --- a/engine/virt/virt.go +++ b/engine/virt/virt.go @@ -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 ( @@ -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 } @@ -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 @@ -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. @@ -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 @@ -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. @@ -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 diff --git a/go.mod b/go.mod index b7547fc4e..069255cf8 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 33ee86eb2..e6c01a3c5 100644 --- a/go.sum +++ b/go.sum @@ -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=