-
Notifications
You must be signed in to change notification settings - Fork 190
/
helpers.go
186 lines (154 loc) · 5.07 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package main
import (
"fmt"
"net"
"time"
"github.com/firecracker-microvm/firecracker-go-sdk"
models "github.com/firecracker-microvm/firecracker-go-sdk/client/models"
"github.com/firecracker-microvm/firecracker-containerd/config"
"github.com/firecracker-microvm/firecracker-containerd/proto"
)
const (
defaultMemSizeMb = 128
defaultCPUCount = 1
)
func machineConfigurationFromProto(cfg *config.Config, req *proto.FirecrackerMachineConfiguration) models.MachineConfiguration {
config := models.MachineConfiguration{
CPUTemplate: models.CPUTemplate(cfg.CPUTemplate),
VcpuCount: firecracker.Int64(defaultCPUCount),
MemSizeMib: firecracker.Int64(defaultMemSizeMb),
Smt: firecracker.Bool(cfg.SmtEnabled),
}
if req == nil {
return config
}
if name := req.CPUTemplate; name != "" {
config.CPUTemplate = models.CPUTemplate(name)
}
if count := req.VcpuCount; count > 0 {
config.VcpuCount = firecracker.Int64(int64(count))
}
if size := req.MemSizeMib; size > 0 {
config.MemSizeMib = firecracker.Int64(int64(size))
}
config.Smt = firecracker.Bool(req.HtEnabled)
return config
}
// networkConfigFromProto creates a firecracker NetworkInterface object from
// the protobuf FirecrackerNetworkInterface message.
func networkConfigFromProto(nwIface *proto.FirecrackerNetworkInterface, vmID string) (*firecracker.NetworkInterface, error) {
result := &firecracker.NetworkInterface{
AllowMMDS: nwIface.AllowMMDS,
}
if nwIface.InRateLimiter != nil {
result.InRateLimiter = rateLimiterFromProto(nwIface.InRateLimiter)
}
if nwIface.OutRateLimiter != nil {
result.OutRateLimiter = rateLimiterFromProto(nwIface.OutRateLimiter)
}
if staticConf := nwIface.StaticConfig; staticConf != nil {
result.StaticConfiguration = &firecracker.StaticNetworkConfiguration{
HostDevName: staticConf.HostDevName,
MacAddress: staticConf.MacAddress,
}
if ipConf := staticConf.IPConfig; ipConf != nil {
ip, ipNet, err := net.ParseCIDR(ipConf.PrimaryAddr)
if err != nil {
return nil, fmt.Errorf("failed to parse CIDR from %q: %w", ipConf.PrimaryAddr, err)
}
result.StaticConfiguration.IPConfiguration = &firecracker.IPConfiguration{
IPAddr: net.IPNet{
IP: ip,
Mask: ipNet.Mask,
},
Gateway: net.ParseIP(ipConf.GatewayAddr),
Nameservers: ipConf.Nameservers,
}
}
}
if cniConf := nwIface.CNIConfig; cniConf != nil {
result.CNIConfiguration = &firecracker.CNIConfiguration{
NetworkName: cniConf.NetworkName,
IfName: cniConf.InterfaceName,
BinPath: cniConf.BinPath,
ConfDir: cniConf.ConfDir,
CacheDir: cniConf.CacheDir,
}
for _, cniArg := range cniConf.Args {
var kv [2]string
kv[0] = cniArg.Key
kv[1] = cniArg.Value
result.CNIConfiguration.Args = append(result.CNIConfiguration.Args, kv)
}
}
return result, nil
}
// rateLimiterFromProto creates a firecracker RateLimiter object from the
// protobuf message.
func rateLimiterFromProto(rl *proto.FirecrackerRateLimiter) *models.RateLimiter {
if rl == nil {
return nil
}
result := models.RateLimiter{}
if rl.Bandwidth != nil {
result.Bandwidth = tokenBucketFromProto(rl.Bandwidth)
}
if rl.Ops != nil {
result.Ops = tokenBucketFromProto(rl.Ops)
}
return &result
}
func withRateLimiterFromProto(rl *proto.FirecrackerRateLimiter) firecracker.DriveOpt {
if rl == nil {
return func(d *models.Drive) {
// no-op
}
}
return firecracker.WithRateLimiter(*rateLimiterFromProto(rl))
}
// tokenBucketFromProto creates a firecracker TokenBucket object from the
// protobuf message.
func tokenBucketFromProto(bucket *proto.FirecrackerTokenBucket) *models.TokenBucket {
builder := firecracker.TokenBucketBuilder{}
if bucket.OneTimeBurst > 0 {
builder = builder.WithInitialSize(bucket.OneTimeBurst)
}
if bucket.RefillTime > 0 {
builder = builder.WithRefillDuration(time.Duration(bucket.RefillTime) * time.Millisecond)
}
if bucket.Capacity > 0 {
builder = builder.WithBucketSize(bucket.Capacity)
}
res := builder.Build()
return &res
}
func cacheTypeFromProto(cacheType string) *string {
// protobuf 'string' type default to empty string if the encoded message
// does not contain a value for that field.
if cacheType == "" {
return nil
}
return firecracker.String(cacheType)
}
func withCacheTypeFromProto(cacheType string) firecracker.DriveOpt {
// protobuf 'string' type default to empty string if the encoded message
// does not contain a value for that field.
if cacheType == "" {
return func(d *models.Drive) {
// no-op
}
}
return firecracker.WithCacheType(cacheType)
}