Skip to content

Commit 2dfefb7

Browse files
Diego Meyerustiugov
authored andcommitted
Environment variables
Signed-off-by: Diego Meyer <[email protected]>
1 parent cc70f39 commit 2dfefb7

File tree

6 files changed

+50
-17
lines changed

6 files changed

+50
-17
lines changed

cri/firecracker/coordinator.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ import (
3030
"sync/atomic"
3131
"time"
3232

33-
"github.com/vhive-serverless/vhive/ctriface"
3433
log "github.com/sirupsen/logrus"
34+
"github.com/vhive-serverless/vhive/ctriface"
3535
)
3636

3737
type coordinator struct {
@@ -99,12 +99,16 @@ func (c *coordinator) setIdleInstance(fi *funcInstance) {
9999
}
100100

101101
func (c *coordinator) startVM(ctx context.Context, image string) (*funcInstance, error) {
102+
return c.startVMWithEnvironment(ctx, image, []string{})
103+
}
104+
105+
func (c *coordinator) startVMWithEnvironment(ctx context.Context, image string, environment []string) (*funcInstance, error) {
102106
if fi := c.getIdleInstance(image); c.orch != nil && c.orch.GetSnapshotsEnabled() && fi != nil {
103107
err := c.orchLoadInstance(ctx, fi)
104108
return fi, err
105109
}
106110

107-
return c.orchStartVM(ctx, image)
111+
return c.orchStartVM(ctx, image, environment)
108112
}
109113

110114
func (c *coordinator) stopVM(ctx context.Context, containerID string) error {
@@ -150,7 +154,7 @@ func (c *coordinator) insertActive(containerID string, fi *funcInstance) error {
150154
return nil
151155
}
152156

153-
func (c *coordinator) orchStartVM(ctx context.Context, image string) (*funcInstance, error) {
157+
func (c *coordinator) orchStartVM(ctx context.Context, image string, envVariables []string) (*funcInstance, error) {
154158
vmID := strconv.Itoa(int(atomic.AddUint64(&c.nextID, 1)))
155159
logger := log.WithFields(
156160
log.Fields{
@@ -170,7 +174,7 @@ func (c *coordinator) orchStartVM(ctx context.Context, image string) (*funcInsta
170174
defer cancel()
171175

172176
if !c.withoutOrchestrator {
173-
resp, _, err = c.orch.StartVM(ctxTimeout, vmID, image)
177+
resp, _, err = c.orch.StartVMWithEnvironment(ctxTimeout, vmID, image, envVariables)
174178
if err != nil {
175179
logger.WithError(err).Error("coordinator failed to start VM")
176180
}

cri/firecracker/service.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ import (
2727
"errors"
2828
"sync"
2929

30+
log "github.com/sirupsen/logrus"
3031
"github.com/vhive-serverless/vhive/cri"
3132
"github.com/vhive-serverless/vhive/ctriface"
32-
log "github.com/sirupsen/logrus"
3333
criapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
3434
)
3535

@@ -110,7 +110,8 @@ func (fs *FirecrackerService) createUserContainer(ctx context.Context, r *criapi
110110
return nil, err
111111
}
112112

113-
funcInst, err := fs.coordinator.startVM(context.Background(), guestImage)
113+
environment := cri.ToStringArray(config.GetEnvs())
114+
funcInst, err := fs.coordinator.startVMWithEnvironment(context.Background(), guestImage, environment)
114115
if err != nil {
115116
log.WithError(err).Error("failed to start VM")
116117
return nil, err
@@ -127,12 +128,12 @@ func (fs *FirecrackerService) createUserContainer(ctx context.Context, r *criapi
127128

128129
// Wait for placeholder UC to be created
129130
<-stockDone
130-
131+
131132
// Check for error from container creation
132-
if stockErr != nil {
133-
log.WithError(stockErr).Error("failed to create container")
134-
return nil, stockErr
135-
}
133+
if stockErr != nil {
134+
log.WithError(stockErr).Error("failed to create container")
135+
return nil, stockErr
136+
}
136137

137138
containerdID := stockResp.ContainerId
138139
err = fs.coordinator.insertActive(containerdID, funcInst)
@@ -216,5 +217,4 @@ func getEnvVal(key string, config *criapi.ContainerConfig) (string, error) {
216217
}
217218

218219
return "", errors.New("failed to provide non empty guest image in user container config")
219-
220220
}

cri/gvisor/coordinator.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func newCoordinator() (*coordinator, error) {
8383
return c, nil
8484
}
8585

86-
func (c *coordinator) startContainer(ctx context.Context, imageName string) (_ *gvContainer, retErr error) {
86+
func (c *coordinator) startContainer(ctx context.Context, imageName string, environment []string) (_ *gvContainer, retErr error) {
8787
ctx = namespaces.WithNamespace(ctx, namespaceName)
8888
ctrID := strconv.Itoa(int(atomic.AddUint64(&c.nextID, 1)))
8989
image, err := c.getImage(ctx, imageName)
@@ -95,7 +95,10 @@ func (c *coordinator) startContainer(ctx context.Context, imageName string) (_ *
9595
ctrID,
9696
containerd.WithImage(*image),
9797
containerd.WithNewSnapshot(ctrID, *image),
98-
containerd.WithNewSpec(oci.WithImageConfig(*image)),
98+
containerd.WithNewSpec(
99+
oci.WithImageConfig(*image),
100+
oci.WithEnv(environment),
101+
),
99102
)
100103
if err != nil {
101104
return nil, err

cri/gvisor/service.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ import (
2828
"fmt"
2929
"sync"
3030

31-
"github.com/vhive-serverless/vhive/cri"
3231
log "github.com/sirupsen/logrus"
32+
"github.com/vhive-serverless/vhive/cri"
3333
criapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
3434
)
3535

@@ -108,7 +108,8 @@ func (gs *GVisorService) createUserContainer(ctx context.Context, r *criapi.Crea
108108
return nil, err
109109
}
110110

111-
ctr, err := gs.coor.startContainer(ctx, guestImage)
111+
environment := cri.ToStringArray(config.GetEnvs())
112+
ctr, err := gs.coor.startContainer(ctx, guestImage, environment)
112113
if err != nil {
113114
log.WithError(err).Error("failed to start container")
114115
return nil, err

cri/util.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cri
2+
3+
import (
4+
"fmt"
5+
log "github.com/sirupsen/logrus"
6+
criapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
7+
)
8+
9+
// (Key, Value) pair is mapped to a 'Key=Value' entry.
10+
func ToStringArray(envVariables []*criapi.KeyValue) []string {
11+
result := make([]string, len(envVariables))
12+
13+
for _, kv := range envVariables {
14+
env := fmt.Sprintf("%s=%s", kv.GetKey(), kv.GetValue())
15+
result = append(result, env)
16+
}
17+
18+
log.Debugf("Converted '%v' to '%v'\n", envVariables, result)
19+
return result
20+
}

ctriface/iface.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ import (
5050
_ "google.golang.org/grpc/codes" //tmp
5151
_ "google.golang.org/grpc/status" //tmp
5252

53+
"github.com/go-multierror/multierror"
5354
"github.com/vhive-serverless/vhive/memory/manager"
5455
"github.com/vhive-serverless/vhive/metrics"
5556
"github.com/vhive-serverless/vhive/misc"
56-
"github.com/go-multierror/multierror"
5757

5858
_ "github.com/davecgh/go-spew/spew" //tmp
5959
)
@@ -71,6 +71,10 @@ const (
7171

7272
// StartVM Boots a VM if it does not exist
7373
func (o *Orchestrator) StartVM(ctx context.Context, vmID, imageName string) (_ *StartVMResponse, _ *metrics.Metric, retErr error) {
74+
return o.StartVMWithEnvironment(ctx, vmID, imageName, []string{})
75+
}
76+
77+
func (o *Orchestrator) StartVMWithEnvironment(ctx context.Context, vmID, imageName string, environmentVariables []string) (_ *StartVMResponse, _ *metrics.Metric, retErr error) {
7478
var (
7579
startVMMetric *metrics.Metric = metrics.NewMetric()
7680
tStart time.Time
@@ -128,6 +132,7 @@ func (o *Orchestrator) StartVM(ctx context.Context, vmID, imageName string) (_ *
128132
oci.WithImageConfig(*vm.Image),
129133
firecrackeroci.WithVMID(vmID),
130134
firecrackeroci.WithVMNetwork,
135+
oci.WithEnv(environmentVariables),
131136
),
132137
containerd.WithRuntime("aws.firecracker", nil),
133138
)

0 commit comments

Comments
 (0)